diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-25 00:26:55 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-25 00:29:49 +0900 |
| commit | 0b51c7019d55995ba53f361521163007941c844b (patch) | |
| tree | 8beffbb50a7d1e85f53c5cdc532e62e5967c405a /src/root.zig | |
| parent | fa8b75121b915e1ae1eb3311fa8051e241e7ddef (diff) | |
| download | zgjq-0b51c7019d55995ba53f361521163007941c844b.tar.gz zgjq-0b51c7019d55995ba53f361521163007941c844b.tar.zst zgjq-0b51c7019d55995ba53f361521163007941c844b.zip | |
implement comma operator
Diffstat (limited to 'src/root.zig')
| -rw-r--r-- | src/root.zig | 92 |
1 files changed, 42 insertions, 50 deletions
diff --git a/src/root.zig b/src/root.zig index ccfd36d..0d2a3ce 100644 --- a/src/root.zig +++ b/src/root.zig @@ -16,7 +16,13 @@ pub fn run(allocator: std.mem.Allocator, input: []const u8, query: []const u8) ! return output; } -fn testRun(expected: []const u8, allocator: std.mem.Allocator, input: []const u8, query: []const u8) !void { +fn testRun(expected: []const u8, input: []const u8, query: []const u8) !void { + try testRunMultiple(&.{expected}, input, query); +} + +fn testRunMultiple(expected: []const []const u8, input: []const u8, query: []const u8) !void { + const allocator = std.testing.allocator; + const parsed = try jv.parse(allocator, input); defer parsed.deinit(); const json = parsed.value; @@ -26,40 +32,34 @@ fn testRun(expected: []const u8, allocator: std.mem.Allocator, input: []const u8 try runtime.compileFromSlice(query); try runtime.start(json); - const result_value = try runtime.next() orelse return error.NoResult; - const result = try jv.stringify(allocator, result_value); - defer allocator.free(result); - try std.testing.expectEqualStrings(expected, result); + for (expected) |ex| { + const result_value = try runtime.next() orelse return error.NoResult; + const result = try jv.stringify(allocator, result_value); + defer allocator.free(result); + try std.testing.expectEqualStrings(ex, result); + } try std.testing.expectEqual(null, try runtime.next()); } test "identity filter" { - var debug_allocator = std.heap.DebugAllocator(.{}).init; - defer std.debug.assert(debug_allocator.deinit() == .ok); - const allocator = debug_allocator.allocator(); - - try testRun("null", allocator, "null", "."); - try testRun("false", allocator, "false", "."); - try testRun("true", allocator, "true", "."); - try testRun("123", allocator, "123", "."); - try testRun("3.1415", allocator, "3.1415", "."); - try testRun("[]", allocator, "[]", "."); - try testRun("{}", allocator, "{}", "."); - try testRun("[1,2,3]", allocator, "[1,2,3]", "."); - try testRun("{\"a\":123}", allocator, "{\"a\":123}", "."); + try testRun("null", "null", "."); + try testRun("false", "false", "."); + try testRun("true", "true", "."); + try testRun("123", "123", "."); + try testRun("3.1415", "3.1415", "."); + try testRun("[]", "[]", "."); + try testRun("{}", "{}", "."); + try testRun("[1,2,3]", "[1,2,3]", "."); + try testRun("{\"a\":123}", "{\"a\":123}", "."); } test "array index filter" { - var debug_allocator = std.heap.DebugAllocator(.{}).init; - defer std.debug.assert(debug_allocator.deinit() == .ok); - const allocator = debug_allocator.allocator(); - - try testRun("null", allocator, "[]", ".[0]"); - try testRun("1", allocator, "[1,2,3]", ".[0]"); - try testRun("null", allocator, "[1,2,3]", ".[5]"); - try testRun("11", allocator, "[0,1,2,3,4,5,6,7,8,9,10,11,12]", ".[11]"); - try testRun("100", allocator, + try testRun("null", "[]", ".[0]"); + try testRun("1", "[1,2,3]", ".[0]"); + try testRun("null", "[1,2,3]", ".[5]"); + try testRun("11", "[0,1,2,3,4,5,6,7,8,9,10,11,12]", ".[11]"); + try testRun("100", \\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, \\ 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, \\ 41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60, @@ -69,34 +69,26 @@ test "array index filter" { } test "object key filter" { - var debug_allocator = std.heap.DebugAllocator(.{}).init; - defer std.debug.assert(debug_allocator.deinit() == .ok); - const allocator = debug_allocator.allocator(); - - try testRun("123", allocator, "{\"a\":123}", ".a"); - try testRun("null", allocator, "{\"a\":123}", ".b"); - try testRun("\"hello\"", allocator, "{\"foo\":\"hello\"}", ".foo"); - try testRun("[1,2,3]", allocator, "{\"arr\":[1,2,3]}", ".arr"); - try testRun("{\"bar\":true}", allocator, "{\"foo\":{\"bar\":true}}", ".foo"); + try testRun("123", "{\"a\":123}", ".a"); + try testRun("null", "{\"a\":123}", ".b"); + try testRun("\"hello\"", "{\"foo\":\"hello\"}", ".foo"); + try testRun("[1,2,3]", "{\"arr\":[1,2,3]}", ".arr"); + try testRun("{\"bar\":true}", "{\"foo\":{\"bar\":true}}", ".foo"); } test "addition" { - var debug_allocator = std.heap.DebugAllocator(.{}).init; - defer std.debug.assert(debug_allocator.deinit() == .ok); - const allocator = debug_allocator.allocator(); - - try testRun("579", allocator, "null", "123 + 456"); - try testRun("35", allocator, "{\"a\":12,\"b\":23}", ".a + .b"); - try testRun("12", allocator, "[1,2,3]", ".[1] + 10"); - try testRun("6", allocator, "null", "1 + 2 + 3"); + try testRun("579", "null", "123 + 456"); + try testRun("35", "{\"a\":12,\"b\":23}", ".a + .b"); + try testRun("12", "[1,2,3]", ".[1] + 10"); + try testRun("6", "null", "1 + 2 + 3"); } test "pipe operator" { - var debug_allocator = std.heap.DebugAllocator(.{}).init; - defer std.debug.assert(debug_allocator.deinit() == .ok); - const allocator = debug_allocator.allocator(); + try testRun("123", "{\"a\":{\"b\":123}}", ".a | .b"); + try testRun("584", "null", "123 + 456 | . + 5"); + try testRun("10", "null", "1 | . + 2 | . + 3 | . | 4 + ."); +} - try testRun("123", allocator, "{\"a\":{\"b\":123}}", ".a | .b"); - try testRun("584", allocator, "null", "123 + 456 | . + 5"); - try testRun("10", allocator, "null", "1 | . + 2 | . + 3 | . | 4 + ."); +test "comma operator" { + try testRunMultiple(&.{ "12", "34", "56" }, "{\"a\":12,\"b\":34,\"c\":56}", ".a,.b,.c"); } |
