From dcb4c7bf51ebfe89bdcd249726d5c06dc6be06d6 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sat, 20 Sep 2025 15:57:23 -0700 Subject: [PATCH] mor tests --- src/main.zig | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 1380d8d..973d8c3 100644 --- a/src/main.zig +++ b/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)); +}