diff --git a/resources/multiple_elements.yml b/resources/multiple_elements.yml new file mode 100644 index 0000000..da81ab4 --- /dev/null +++ b/resources/multiple_elements.yml @@ -0,0 +1,9 @@ +elements: + - name: Example Name + bool_val: true + int_val: 0 + float_val: 3.14 + - name: Example Name 2 + bool_val: false + int_val: 120 + float_val: 56.123 \ No newline at end of file diff --git a/resources/yaml-test-suite/98YD-mixed.yml b/resources/yaml-test-suite/98YD-mixed.yml new file mode 100644 index 0000000..7986aa0 --- /dev/null +++ b/resources/yaml-test-suite/98YD-mixed.yml @@ -0,0 +1,14 @@ +elemennts: + - name: Spec Example 5.5. Comment Indicator + from: http://www.yaml.org/spec/1.2/spec.html#id2773032 + tags: spec comment empty + yaml: | + # Comment only. + tree: | + +STR + -STR + json: "" + dump: "" + bool_val: true + bool_val_b2: false + bool_val_with_spaces: true \ No newline at end of file diff --git a/resources/yaml-test-suite/98YD.yml b/resources/yaml-test-suite/98YD.yml index 94a7c85..9d5fed6 100644 --- a/resources/yaml-test-suite/98YD.yml +++ b/resources/yaml-test-suite/98YD.yml @@ -8,7 +8,4 @@ elemennts: +STR -STR json: "" - dump: "" - bool_val: true - bool_val_b2: false - bool_val_with_spaces: true + dump: "" \ No newline at end of file diff --git a/resources/yaml-test-suite/multiple_elements.yml b/resources/yaml-test-suite/multiple_elements.yml deleted file mode 100644 index ad954e1..0000000 --- a/resources/yaml-test-suite/multiple_elements.yml +++ /dev/null @@ -1,5 +0,0 @@ -elemennts: - - name: Example Name - bool_val: true - - name: Example Name 2 - bool_val: false \ No newline at end of file diff --git a/src/root.zig b/src/root.zig index 85b3c96..8eb4e16 100644 --- a/src/root.zig +++ b/src/root.zig @@ -454,7 +454,9 @@ pub fn Ymlz(comptime Destination: type) type { fn parseNumericExpression(self: *Self, comptime T: type, raw_line: []const u8, depth: usize) !T { const expression = try self.parseSimpleExpression(raw_line, depth, false); - const value = self.getExpressionValue(expression); + var value = self.getExpressionValue(expression); + + value = std.mem.trim(u8, value, " "); switch (@typeInfo(T)) { .Int => { diff --git a/src/tests.zig b/src/tests.zig index c5d3425..710d6d1 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -8,13 +8,15 @@ test "Multiple elements in yaml file" { const MultiElement = struct { name: []const u8, bool_val: bool, + int_val: u8, + float_val: f64, }; const Elements = struct { elements: []MultiElement }; const yml_file_location = try std.fs.cwd().realpathAlloc( std.testing.allocator, - "./resources/yaml-test-suite/multiple_elements.yml", + "./resources/multiple_elements.yml", ); defer std.testing.allocator.free(yml_file_location); @@ -32,9 +34,17 @@ test "Multiple elements in yaml file" { // Test 2nd element try expect(result.elements[1].bool_val == false); try expect(std.mem.eql(u8, result.elements[1].name, "Example Name 2")); + + // Test Ints + try expect(result.elements[0].int_val == 0); + try expect(result.elements[1].int_val == 120); + + // Test floats + try expect(result.elements[0].float_val == 3.14); + try expect(result.elements[1].float_val == 56.123); } -test "98YD" { +test "98YD with bools" { const Element = struct { name: []const u8, from: []const u8, @@ -54,7 +64,7 @@ test "98YD" { const yml_file_location = try std.fs.cwd().realpathAlloc( std.testing.allocator, - "./resources/yaml-test-suite/98YD.yml", + "./resources/yaml-test-suite/98YD-mixed.yml", ); defer std.testing.allocator.free(yml_file_location); @@ -69,6 +79,37 @@ test "98YD" { try expect(element.bool_val_2 == false); try expect(element.bool_val_with_spaces == true); + try expect(std.mem.eql(u8, element.name, "Spec Example 5.5. Comment Indicator")); + try expect(element.dump.len == 0); +} + +test "98YD" { + const Element = struct { + name: []const u8, + from: []const u8, + tags: []const u8, + yaml: []const u8, + tree: []const u8, + json: []const u8, + dump: []const u8, + }; + + const Experiment = struct { + elements: []Element, + }; + + const yml_file_location = try std.fs.cwd().realpathAlloc( + std.testing.allocator, + "./resources/yaml-test-suite/98YD.yml", + ); + defer std.testing.allocator.free(yml_file_location); + + var ymlz = try Ymlz(Experiment).init(std.testing.allocator); + const result = try ymlz.loadFile(yml_file_location); + defer ymlz.deinit(result); + + const element = result.elements[0]; + try expect(std.mem.eql(u8, element.name, "Spec Example 5.5. Comment Indicator")); // dump: "" try expect(element.dump.len == 0);