~cricket/zckt

ref: 6f63ef3030a5a016af270c4a4b5b6efd85b418e6 zckt/zckt.zig -rw-r--r-- 1.7 KiB
6f63ef30 — c piapiac changes to fit ckt changes 3 months ago
                                                                                
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
61
62
63
64
65
66
67
68
// small, bodged together ckt parser
const std = @import("std");

pub const tokenizer = @import("tokenizer.zig");
pub const parser = @import("parser.zig");

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

test "key = [ table ]" {
    const zckt =
        \\table = [ this is a table :), this is the second value ]
    ;

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

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

test "duplicate keys overwrite" {
    const zckt =
        \\key = this is a value
        \\
        \\# finally. comments
        \\key = haha! overwritten
    ;

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

    testing.expectEqualStrings("haha! overwritten", table.getString("key").?);
}

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 table_parser.parse(zckt, alloc);
    defer table.deinit();

    testing.expectEqualStrings(table.getString("multiline string").?, multiline_string);
}

test "expect invalid" {
    const zckt =
        \\key;
        \\=some other value
    ;

    const table = table_parser.parse(zckt, alloc);
    testing.expectError(error.EmptyKey, table);
}