Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML 1.2 Test Case Generator #19

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,48 @@ nested:
wick: john doe
finally: [ 8.17, 19.78, 17, 21 ]
```

## Testing against YAML 1.2 compatibility tests

To test against the YAML 1.2 compatibility tests, first obtain the tests from https://github.com/yaml/yaml-test-suite.
Follow the instructions to obtain the `data` directory which contains all of the test cases.
Copy this directory into the `test` folder where `generator.zig` is located.
Running `zig build test` will now generate test cases for the YAML 1.2 compatibility tests. They can be found in the zig-cache in a file named `yamlTest.zig`.

The test cases have the following format.

For a test that should parse correctly.

```
test "indent/SKE5" {
var yaml = loadFromFile("test/data/tags/indent/SKE5/in.yaml") catch return error.Failed;
defer yaml.deinit();
}
```

For a test that should fail to parse.

```
test "indent/EW3V" {
var yaml = loadFromFile("test/data/tags/indent/EW3V/in.yaml") catch return;
defer yaml.deinit();
return error.UnexpectedSuccess;
}
```

running zig build test with the following option

```
-DspecificYAML="string"
-DspecificYAML={"stringOne","stringTwo"}
```

will generate only tests that match those strings. This can be used to specify the particular test or test groups you want to generate tests for. For example running
`-DspecificYAML={"comment/6HB6","5T43"}` will produce the following tests.

```
test "comment/6HB6"
test "mapping/5T43"
test "scalar/5T43"
test "flow/5T43"
```
20 changes: 20 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const std = @import("std");
const GenerateStep = @import("test/generator.zig").GenerateStep;

pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();

const enable_logging = b.option(bool, "log", "Whether to enable logging") orelse false;
const create_only_yaml_tests = b.option([]const []const u8, "specificYAML", "generate only YAML tests matching this eg -DspecificYAML{\"comment/6HB6\",\"EW3V\"}") orelse &[_][]const u8{};

const enable_silent_yaml = b.option(bool, "silentYAML", "all YAML tests will pass, failures will be logged cleanly") orelse false;

const lib = b.addStaticLibrary("yaml", "src/yaml.zig");
lib.setBuildMode(mode);
Expand All @@ -19,6 +23,8 @@ pub fn build(b: *std.build.Builder) void {
const example_opts = b.addOptions();
example.addOptions("build_options", example_opts);
example_opts.addOption(bool, "enable_logging", enable_logging);
example_opts.addOption(bool, "enable_silent_yaml", enable_silent_yaml);
example_opts.addOption([]const []const u8, "gen_tests_only", create_only_yaml_tests);

example.install();

Expand All @@ -38,4 +44,18 @@ pub fn build(b: *std.build.Builder) void {
e2e_tests.setBuildMode(mode);
e2e_tests.addPackagePath("yaml", "src/yaml.zig");
test_step.dependOn(&e2e_tests.step);

const cwd = std.fs.cwd();
if(cwd.access("test/data",.{})) {
std.debug.print("Found 'data' directory with YAML tests. Attempting to generate test cases\n",.{});

const gen = GenerateStep.init(b,"yamlTest.zig",create_only_yaml_tests,enable_silent_yaml);
test_step.dependOn(&gen.step);
var full_yaml_tests = b.addTest("zig-cache/yamlTest.zig");
full_yaml_tests.addPackagePath("yaml", "src/yaml.zig");
test_step.dependOn(&full_yaml_tests.step);
} else |_| {
std.debug.print("No 'data' directory with YAML tests provided\n",.{});
}
}

28 changes: 17 additions & 11 deletions src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub const Tree = struct {
}

self.source = source;
self.tokens = tokens.toOwnedSlice();
self.tokens = try tokens.toOwnedSlice();

var it = TokenIterator{ .buffer = self.tokens };
var parser = Parser{
Expand Down Expand Up @@ -372,7 +372,7 @@ const Parser = struct {

// Parse header
const explicit_doc: bool = if (self.eatToken(.doc_start, &.{})) |doc_pos| explicit_doc: {
if (self.getCol(doc_pos) > 0) return error.MalformedYaml;
if (try self.getCol(doc_pos) > 0) return error.MalformedYaml;
if (self.eatToken(.tag, &.{ .new_line, .comment })) |_| {
node.directive = try self.expectToken(.literal, &.{ .new_line, .comment });
}
Expand All @@ -393,13 +393,13 @@ const Parser = struct {
footer: {
if (self.eatToken(.doc_end, &.{})) |pos| {
if (!explicit_doc) return error.UnexpectedToken;
if (self.getCol(pos) > 0) return error.MalformedYaml;
if (try self.getCol(pos) > 0) return error.MalformedYaml;
node.base.end = pos;
break :footer;
}
if (self.eatToken(.doc_start, &.{})) |pos| {
if (!explicit_doc) return error.UnexpectedToken;
if (self.getCol(pos) > 0) return error.MalformedYaml;
if (try self.getCol(pos) > 0) return error.MalformedYaml;
self.token_it.seekBy(-1);
node.base.end = pos - 1;
break :footer;
Expand Down Expand Up @@ -434,14 +434,14 @@ const Parser = struct {

log.debug("(map) begin {s}@{d}", .{ @tagName(self.tree.tokens[node.base.start].id), node.base.start });

const col = self.getCol(node.base.start);
const col = try self.getCol(node.base.start);

while (true) {
self.eatCommentsAndSpace(&.{});

// Parse key
const key_pos = self.token_it.pos;
if (self.getCol(key_pos) < col) {
if (try self.getCol(key_pos) < col) {
break;
}

Expand Down Expand Up @@ -471,11 +471,11 @@ const Parser = struct {
};

if (val) |v| {
if (self.getCol(v.start) < self.getCol(key_pos)) {
if (try self.getCol(v.start) < try self.getCol(key_pos)) {
return error.MalformedYaml;
}
if (v.cast(Node.Value)) |_| {
if (self.getCol(v.start) == self.getCol(key_pos)) {
if (try self.getCol(v.start) == try self.getCol(key_pos)) {
return error.MalformedYaml;
}
}
Expand Down Expand Up @@ -658,8 +658,12 @@ const Parser = struct {
return self.line_cols.get(index).?.line;
}

fn getCol(self: *Parser, index: TokenIndex) usize {
return self.line_cols.get(index).?.col;
fn getCol(self: *Parser, index: TokenIndex) ParseError!usize {
if (self.line_cols.get(index)) |index_actual| {
return index_actual.col;
} else {
return ParseError.UnexpectedEof;
}
}

fn parseSingleQuoted(self: *Parser, node: *Node.Value, raw: []const u8) ParseError!void {
Expand Down Expand Up @@ -697,7 +701,9 @@ const Parser = struct {
}

fn parseDoubleQuoted(self: *Parser, node: *Node.Value, raw: []const u8) ParseError!void {
assert(raw[0] == '"' and raw[raw.len - 1] == '"');
if ((raw[0] == '"' and raw[raw.len - 1] == '"') == false) {
return ParseError.Unhandled;
}

const raw_no_quotes = raw[1 .. raw.len - 1];
try node.string_value.ensureTotalCapacity(self.allocator, raw_no_quotes.len);
Expand Down
16 changes: 8 additions & 8 deletions src/yaml.zig
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub const Value = union(enum) {
out_list.appendAssumeCapacity(value);
}

return Value{ .list = out_list.toOwnedSlice() };
return Value{ .list = try out_list.toOwnedSlice() };
} else if (node.cast(Node.Value)) |value| {
const raw = tree.getRaw(node.start, node.end);

Expand Down Expand Up @@ -221,7 +221,7 @@ pub const Value = union(enum) {
}
}

return Value{ .list = list.toOwnedSlice() };
return Value{ .list = try list.toOwnedSlice() };
} else {
var map = Map.init(arena);
errdefer map.deinit();
Expand Down Expand Up @@ -275,7 +275,7 @@ pub const Value = union(enum) {
}
}

return Value{ .list = list.toOwnedSlice() };
return Value{ .list = try list.toOwnedSlice() };
},
else => {
@compileError("Unhandled type: {s}" ++ @typeName(@TypeOf(input)));
Expand Down Expand Up @@ -399,7 +399,7 @@ pub const Yaml = struct {

if (union_info.tag_type) |_| {
inline for (union_info.fields) |field| {
if (self.parseValue(field.field_type, value)) |u_value| {
if (self.parseValue(field.type, value)) |u_value| {
return @unionInit(T, field.name, u_value);
} else |err| {
if (@as(@TypeOf(err) || error{TypeMismatch}, err) != error.TypeMismatch) return err;
Expand All @@ -426,16 +426,16 @@ pub const Yaml = struct {
break :blk map.get(field_name);
};

if (@typeInfo(field.field_type) == .Optional) {
@field(parsed, field.name) = try self.parseOptional(field.field_type, value);
if (@typeInfo(field.type) == .Optional) {
@field(parsed, field.name) = try self.parseOptional(field.type, value);
continue;
}

const unwrapped = value orelse {
log.err("missing struct field: {s}: {s}", .{ field.name, @typeName(field.field_type) });
log.err("missing struct field: {s}: {s}", .{ field.name, @typeName(field.type) });
return error.StructFieldMissing;
};
@field(parsed, field.name) = try self.parseValue(field.field_type, unwrapped);
@field(parsed, field.name) = try self.parseValue(field.type, unwrapped);
}

return parsed;
Expand Down
Loading