Skip to content

Commit

Permalink
Moving bools out of 98YD to avoid bloat. Adding trim to int parsing. …
Browse files Browse the repository at this point in the history
…Moving yaml tests to more canonical locations on disk. Adding floats in test
  • Loading branch information
BitlyTwiser committed Sep 15, 2024
1 parent 1a46d70 commit 3aa6d78
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
9 changes: 9 additions & 0 deletions resources/multiple_elements.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions resources/yaml-test-suite/98YD-mixed.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 1 addition & 4 deletions resources/yaml-test-suite/98YD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ elemennts:
+STR
-STR
json: ""
dump: ""
bool_val: true
bool_val_b2: false
bool_val_with_spaces: true
dump: ""
5 changes: 0 additions & 5 deletions resources/yaml-test-suite/multiple_elements.yml

This file was deleted.

4 changes: 3 additions & 1 deletion src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
47 changes: 44 additions & 3 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 3aa6d78

Please sign in to comment.