aboutsummaryrefslogtreecommitdiff
path: root/lib/std/json/hashmap_test.zig
blob: 49d8caffae70f6a263b09375ab4e83ed1f926929 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const std = @import("std");
const testing = std.testing;

const ArrayHashMap = @import("hashmap.zig").ArrayHashMap;

const parseFromSlice = @import("static.zig").parseFromSlice;
const parseFromSliceLeaky = @import("static.zig").parseFromSliceLeaky;
const parseFromTokenSource = @import("static.zig").parseFromTokenSource;
const parseFromValue = @import("static.zig").parseFromValue;
const stringifyAlloc = @import("stringify.zig").stringifyAlloc;
const Value = @import("dynamic.zig").Value;

const jsonReader = @import("./scanner.zig").reader;

const T = struct {
    i: i32,
    s: []const u8,
};

test "parse json hashmap" {
    const doc =
        \\{
        \\  "abc": {"i": 0, "s": "d"},
        \\  "xyz": {"i": 1, "s": "w"}
        \\}
    ;
    const parsed = try parseFromSlice(ArrayHashMap(T), testing.allocator, doc, .{});
    defer parsed.deinit();

    try testing.expectEqual(@as(usize, 2), parsed.value.map.count());
    try testing.expectEqualStrings("d", parsed.value.map.get("abc").?.s);
    try testing.expectEqual(@as(i32, 1), parsed.value.map.get("xyz").?.i);
}

test "parse json hashmap while streaming" {
    const doc =
        \\{
        \\  "abc": {"i": 0, "s": "d"},
        \\  "xyz": {"i": 1, "s": "w"}
        \\}
    ;
    var stream = std.io.fixedBufferStream(doc);
    var json_reader = jsonReader(testing.allocator, stream.reader());

    var parsed = try parseFromTokenSource(
        ArrayHashMap(T),
        testing.allocator,
        &json_reader,
        .{},
    );
    defer parsed.deinit();
    // Deinit our reader to invalidate its buffer
    json_reader.deinit();

    try testing.expectEqual(@as(usize, 2), parsed.value.map.count());
    try testing.expectEqualStrings("d", parsed.value.map.get("abc").?.s);
    try testing.expectEqual(@as(i32, 1), parsed.value.map.get("xyz").?.i);
}

test "parse json hashmap duplicate fields" {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();

    const doc =
        \\{
        \\  "abc": {"i": 0, "s": "d"},
        \\  "abc": {"i": 1, "s": "w"}
        \\}
    ;

    try testing.expectError(error.DuplicateField, parseFromSliceLeaky(ArrayHashMap(T), arena.allocator(), doc, .{
        .duplicate_field_behavior = .@"error",
    }));

    const first = try parseFromSliceLeaky(ArrayHashMap(T), arena.allocator(), doc, .{
        .duplicate_field_behavior = .use_first,
    });
    try testing.expectEqual(@as(usize, 1), first.map.count());
    try testing.expectEqual(@as(i32, 0), first.map.get("abc").?.i);

    const last = try parseFromSliceLeaky(ArrayHashMap(T), arena.allocator(), doc, .{
        .duplicate_field_behavior = .use_last,
    });
    try testing.expectEqual(@as(usize, 1), last.map.count());
    try testing.expectEqual(@as(i32, 1), last.map.get("abc").?.i);
}

test "stringify json hashmap" {
    var value = ArrayHashMap(T){};
    defer value.deinit(testing.allocator);
    {
        const doc = try stringifyAlloc(testing.allocator, value, .{});
        defer testing.allocator.free(doc);
        try testing.expectEqualStrings("{}", doc);
    }

    try value.map.put(testing.allocator, "abc", .{ .i = 0, .s = "d" });
    try value.map.put(testing.allocator, "xyz", .{ .i = 1, .s = "w" });

    {
        const doc = try stringifyAlloc(testing.allocator, value, .{});
        defer testing.allocator.free(doc);
        try testing.expectEqualStrings(
            \\{"abc":{"i":0,"s":"d"},"xyz":{"i":1,"s":"w"}}
        , doc);
    }

    try testing.expect(value.map.swapRemove("abc"));
    {
        const doc = try stringifyAlloc(testing.allocator, value, .{});
        defer testing.allocator.free(doc);
        try testing.expectEqualStrings(
            \\{"xyz":{"i":1,"s":"w"}}
        , doc);
    }

    try testing.expect(value.map.swapRemove("xyz"));
    {
        const doc = try stringifyAlloc(testing.allocator, value, .{});
        defer testing.allocator.free(doc);
        try testing.expectEqualStrings("{}", doc);
    }
}

test "stringify json hashmap whitespace" {
    var value = ArrayHashMap(T){};
    defer value.deinit(testing.allocator);
    try value.map.put(testing.allocator, "abc", .{ .i = 0, .s = "d" });
    try value.map.put(testing.allocator, "xyz", .{ .i = 1, .s = "w" });

    {
        const doc = try stringifyAlloc(testing.allocator, value, .{ .whitespace = .indent_2 });
        defer testing.allocator.free(doc);
        try testing.expectEqualStrings(
            \\{
            \\  "abc": {
            \\    "i": 0,
            \\    "s": "d"
            \\  },
            \\  "xyz": {
            \\    "i": 1,
            \\    "s": "w"
            \\  }
            \\}
        , doc);
    }
}

test "json parse from value hashmap" {
    const doc =
        \\{
        \\  "abc": {"i": 0, "s": "d"},
        \\  "xyz": {"i": 1, "s": "w"}
        \\}
    ;
    const parsed1 = try parseFromSlice(Value, testing.allocator, doc, .{});
    defer parsed1.deinit();

    const parsed2 = try parseFromValue(ArrayHashMap(T), testing.allocator, parsed1.value, .{});
    defer parsed2.deinit();

    try testing.expectEqualStrings("d", parsed2.value.map.get("abc").?.s);
}