mor tests
This commit is contained in:
parent
dbeff4b885
commit
dcb4c7bf51
1 changed files with 85 additions and 2 deletions
87
src/main.zig
87
src/main.zig
|
@ -1,3 +1,4 @@
|
|||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
const pos = @import("pos");
|
||||
|
||||
|
@ -12,7 +13,7 @@ const DeviceAction = enum {
|
|||
toggle,
|
||||
};
|
||||
|
||||
fn sendWemoCommand(device_entry: std.hash_map.StringHashMap([]const u8).Entry, action: DeviceAction, allocator: std.mem.Allocator) !void {
|
||||
fn sendWemoCommand(allocator: std.mem.Allocator, device_entry: std.hash_map.StringHashMap([]const u8).Entry, action: DeviceAction) !void {
|
||||
const state = switch (action) {
|
||||
.on => "1",
|
||||
.off => "0",
|
||||
|
@ -353,7 +354,10 @@ fn processCommand(allocator: std.mem.Allocator, sentence: [:0]const u8, parser:
|
|||
|
||||
if (parseAction(action_words)) |action| {
|
||||
if (try extractDevice(allocator, object_words, devices)) |entry| {
|
||||
try sendWemoCommand(entry, action, allocator);
|
||||
if (builtin.is_test)
|
||||
testSendCommand(entry, action)
|
||||
else
|
||||
try sendWemoCommand(allocator, entry, action);
|
||||
success = true;
|
||||
break; // Success, exit
|
||||
}
|
||||
|
@ -362,6 +366,12 @@ fn processCommand(allocator: std.mem.Allocator, sentence: [:0]const u8, parser:
|
|||
if (!success) return error.UnrecognizedSentence;
|
||||
}
|
||||
|
||||
var test_device_entry: std.hash_map.StringHashMap([]const u8).Entry = undefined;
|
||||
var test_action: DeviceAction = undefined;
|
||||
fn testSendCommand(device_entry: std.hash_map.StringHashMap([]const u8).Entry, action: DeviceAction) void {
|
||||
test_device_entry = device_entry;
|
||||
test_action = action;
|
||||
}
|
||||
test "extractDevice single word exact match" {
|
||||
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
|
||||
defer devices.deinit();
|
||||
|
@ -467,3 +477,76 @@ test "extractDevice word replacement like to light" {
|
|||
|
||||
try std.testing.expectEqualStrings("192.168.1.101", result.?.value_ptr.*);
|
||||
}
|
||||
test "processCommand successful match" {
|
||||
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
|
||||
defer devices.deinit();
|
||||
|
||||
try devices.put("kitchen", "192.168.1.100");
|
||||
|
||||
var parser = try pos.Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
const sentence = "turn on the kitchen";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
try processCommand(std.testing.allocator, sentence_z, &parser, &devices);
|
||||
|
||||
try std.testing.expectEqualStrings("192.168.1.100", test_device_entry.value_ptr.*);
|
||||
try std.testing.expectEqual(DeviceAction.on, test_action);
|
||||
}
|
||||
|
||||
test "processCommand with word replacement lake to light" {
|
||||
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
|
||||
defer devices.deinit();
|
||||
|
||||
try devices.put("kitchen light", "192.168.1.100");
|
||||
|
||||
var parser = try pos.Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
const sentence = "turn on the kitchen lake";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
try processCommand(std.testing.allocator, sentence_z, &parser, &devices);
|
||||
|
||||
try std.testing.expectEqualStrings("192.168.1.100", test_device_entry.value_ptr.*);
|
||||
try std.testing.expectEqual(DeviceAction.on, test_action);
|
||||
}
|
||||
|
||||
test "processCommand with word replacement like to light" {
|
||||
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
|
||||
defer devices.deinit();
|
||||
|
||||
try devices.put("bedroom light", "192.168.1.101");
|
||||
|
||||
var parser = try pos.Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
const sentence = "turn off the bedroom like";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
try processCommand(std.testing.allocator, sentence_z, &parser, &devices);
|
||||
|
||||
try std.testing.expectEqualStrings("192.168.1.101", test_device_entry.value_ptr.*);
|
||||
try std.testing.expectEqual(DeviceAction.off, test_action);
|
||||
}
|
||||
|
||||
test "processCommand no match found" {
|
||||
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
|
||||
defer devices.deinit();
|
||||
|
||||
try devices.put("kitchen", "192.168.1.100");
|
||||
|
||||
var parser = try pos.Parser.init(std.testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
const sentence = "turn on the bathroom";
|
||||
const sentence_z = try std.testing.allocator.dupeZ(u8, sentence);
|
||||
defer std.testing.allocator.free(sentence_z);
|
||||
|
||||
// Should return UnrecognizedSentence error
|
||||
try std.testing.expectError(error.UnrecognizedSentence, processCommand(std.testing.allocator, sentence_z, &parser, &devices));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue