~cricket/zckt

263472288023d6db9a56c9a865aa2166709a3790 — c piapiac 5 months ago f768d1a
add some helper functions + a couple of tests
1 files changed, 63 insertions(+), 9 deletions(-)

M zckt.zig
M zckt.zig => zckt.zig +63 -9
@@ 14,8 14,22 @@ const Value = union(enum) {

    pub fn deinit(self: *Value, a: *mem.Allocator) void {
        switch (self.*) {
            .string => |s| a.free(s),
            .table => |*t| t.deinit(),
            .string => |str| a.free(str),
            .table => |*table| table.deinit(),
        }
    }

    pub fn asString(self: Value) error{NotAString}![]const u8 {
        switch (self) {
            .string => |str| return str,
            .table => return error.NotAString,
        }
    }

    pub fn asTable(self: Value) error{NotATable}!Table {
        switch (self) {
            .string => return error.NotATable,
            .table => |table| return table,
        }
    }



@@ 51,6 65,16 @@ const Table = struct {
        return self.map.get(key);
    }

    pub fn getString(self: *Table, key: []const u8) ?[]const u8 {
        const value = self.map.get(key) orelse return null;
        return value.asString() catch return null;
    }

    pub fn getTable(self: *Table, key: []const u8) ?Table {
        const value = self.map.get(key) orelse return null;
        return value.asTable() catch return null;
    }

    pub fn put(self: *Table, key: []const u8, value: Value) !void {
        var result = try self.map.fetchPut(key, value);



@@ 518,21 542,51 @@ pub const Parser = struct {
    }
};


const testing = std.testing;
const alloc = testing.allocator;

// TODO: more tests
test "small test" {
    const alloc = testing.allocator;
test "key = [ table ]" {
    const zckt =
        \\table = [ this is a table :), this is the second value ]
    ;

    var table = try Parser.parse(zckt, alloc);
    defer table.deinit();

    testing.expectEqualStrings(table.getTable("table").?.getString("1").?, "this is the second value");
}

test "duplicate keys overwrite" {
    const zckt =
        \\key = value
        \\key = another god damned value
        \\key = [ this is a table :), this is the second value ]
        \\key = this is a value
        \\key = haha! overwritten
    ;

    var table = try Parser.parse(zckt, alloc);
    defer table.deinit();

    testing.expectEqualStrings(table.get("key").?.table.get("1").?.string, "this is the second value");
    testing.expectEqualStrings(table.getString("key").?, "haha! overwritten");
}

test "multiline string" {
    const zckt =
        \\multiline string =
        \\  |this is a multiline string
        \\  |that means it can span multiple lines
        \\  |however, i can also make \
        \\  |part of it span one line!
    ;

    const multiline_string =
        \\this is a multiline string
        \\that means it can span multiple lines
        \\however, i can also make part of it span one line!
    ;

    var table = try Parser.parse(zckt, alloc);
    defer table.deinit();

    std.debug.print("\n{s}\n", .{table});
    testing.expectEqualStrings(table.getString("multiline string").?, multiline_string);
}