add more tests

This commit is contained in:
Emil Lerch 2025-09-20 14:51:49 -07:00
parent b5d3fbeb7b
commit 47a6ffb1e3
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -283,3 +283,85 @@ pub fn main() !void {
try stdout.print("Unrecognized sentence: {s}\n", .{args[1]});
}
}
test "extractDevice single word exact match" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
try devices.put("kitchen", "192.168.1.100");
try devices.put("bedroom", "192.168.1.101");
var object_words = [_][]const u8{"kitchen"};
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expectEqualStrings("192.168.1.100", result.?.value_ptr.*);
}
test "extractDevice single word case insensitive" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
try devices.put("kitchen", "192.168.1.100");
var object_words = [_][]const u8{"Kitchen"};
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expectEqualStrings("192.168.1.100", result.?.value_ptr.*);
}
test "extractDevice two words exact match" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
try devices.put("bedroom light", "192.168.1.100");
var object_words = [_][]const u8{ "bedroom", "light" };
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expectEqualStrings("192.168.1.100", result.?.value_ptr.*);
}
test "extractDevice two words case insensitive" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
try devices.put("bedroom light", "192.168.1.100");
var object_words = [_][]const u8{ "Bedroom", "Light" };
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expectEqualStrings("192.168.1.100", result.?.value_ptr.*);
}
test "extractDevice not found" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
try devices.put("kitchen", "192.168.1.100");
var object_words = [_][]const u8{"bathroom"};
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expect(result == null);
}
test "extractDevice empty words" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
var object_words = [_][]const u8{};
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expect(result == null);
}
test "extractDevice fallback to shorter match" {
var devices = std.StringHashMap([]const u8).init(std.testing.allocator);
defer devices.deinit();
try devices.put("kitchen", "192.168.1.100");
var object_words = [_][]const u8{ "kitchen", "ceiling", "light" };
const result = try extractDevice(std.testing.allocator, object_words[0..], &devices);
try std.testing.expectEqualStrings("192.168.1.100", result.?.value_ptr.*);
}