Skip to content

Commit

Permalink
Test cases addition to find edge cases in ymlz (#16)
Browse files Browse the repository at this point in the history
* Start adding more tests and find more edge cases

* pass test casea

* fix more edge cases
  • Loading branch information
pwbh authored Sep 6, 2024
1 parent 7b0fcc4 commit 8df404a
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
11 changes: 11 additions & 0 deletions resources/yaml-test-suite/98YD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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: ""
18 changes: 18 additions & 0 deletions resources/yaml-test-suite/CC74.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
elements:
- name: Spec Example 6.20. Tag Handles
from: http://www.yaml.org/spec/1.2/spec.html#id2783195
tags: spec directive tag unknown-tag
yaml: |
%TAG !e! tag:example.com,2000:app/
---
!e!foo "bar"
tree: |
+STR
+DOC ---
=VAL <tag:example.com,2000:app/foo> "bar
-DOC
-STR
json: |
"bar"
dump: |
--- !<tag:example.com,2000:app/foo> "bar"
40 changes: 40 additions & 0 deletions resources/yaml-test-suite/F6MC.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
elements:
- name: More indented lines at the beginning of folded block scalars
from: "@perlpunk"
tags: folded indent
yaml: |
---
a: >2
more indented
regular
b: >2
more indented
regular
tree: |
+STR
+DOC ---
+MAP
=VAL :a
=VAL > more indented\nregular\n
=VAL :b
=VAL >\n\n more indented\nregular\n
-MAP
-DOC
-STR
json: |
{
"a": " more indented\nregular\n",
"b": "\n\n more indented\nregular\n"
}
emit: |
---
a: >2
more indented
regular
b: >2
more indented
regular
15 changes: 15 additions & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ pub fn Ymlz(comptime Destination: type) type {
if (raw_line) |line| {
try self.allocations.append(line);

if (line.len == 0) {
return "\n";
}

// TODO: Need to fix this comments can start not only from index 0.
if (line[0] == '#') {
// Skipping comments
Expand Down Expand Up @@ -362,6 +366,8 @@ pub fn Ymlz(comptime Destination: type) type {
const expression = try self.parseSimpleExpression(raw_line, depth);
const value = self.getExpressionValue(expression);

if (value.len == 0) return value;

switch (value[0]) {
'|' => {
return self.parseMultilineString(depth + 1, true);
Expand Down Expand Up @@ -461,6 +467,14 @@ pub fn Ymlz(comptime Destination: type) type {

fn parseSimpleExpression(self: *Self, raw_line: []const u8, depth: usize) !Expression {
const indent_depth = self.getIndentDepth(depth);

if (raw_line.len < indent_depth) {
return .{
.value = .{ .Simple = raw_line },
.raw = raw_line,
};
}

const line = raw_line[indent_depth..];

if (line[0] == '-') {
Expand Down Expand Up @@ -491,6 +505,7 @@ pub fn Ymlz(comptime Destination: type) type {

test {
_ = Suspense;
_ = @import("tests.zig");
}

test "should be able to parse simple types" {
Expand Down
89 changes: 89 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const std = @import("std");

const expect = std.testing.expect;

const Ymlz = @import("root.zig").Ymlz;

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);

try expect(std.mem.eql(u8, result.elements[0].name, "Spec Example 5.5. Comment Indicator"));
}

test "CC74" {
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/CC74.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);

try expect(std.mem.eql(u8, result.elements[0].name, "Spec Example 6.20. Tag Handles"));
}

test "F6MC" {
const Element = struct {
name: []const u8,
from: []const u8,
tags: []const u8,
yaml: []const u8,
tree: []const u8,
json: []const u8,
emit: []const u8,
};

const Experiment = struct {
elements: []Element,
};

const yml_file_location = try std.fs.cwd().realpathAlloc(
std.testing.allocator,
"./resources/yaml-test-suite/F6MC.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);

try expect(std.mem.eql(u8, result.elements[0].name, "More indented lines at the beginning of folded block scalars"));
}

0 comments on commit 8df404a

Please sign in to comment.