~alva/zig-bare

70cf6ebc330be020ae216613b1261dc7a2efb028 — owl 6 months ago 1d3dc73
remove zig compiler tests

They have become such a nuisance, and it's weird to test Zig compiler
behavior anyway.
1 files changed, 0 insertions(+), 352 deletions(-)

M src/test.zig
M src/test.zig => src/test.zig +0 -352
@@ 147,19 147,6 @@ test "read struct 6" {
    try expectEqual(res.c, true);
}

test "read struct with void members" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  const Foo = struct { a: u8, b: void };
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(Foo);
        \\}
    , "unsupported type void");
}

test "read enum" {
    const Foo = enum(u8) {
        a = 2,


@@ 283,19 270,6 @@ test "read map overflow" {
    try expectError(error.Overflow, map);
}

test "read map[u8]void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  const HM = std.AutoHashMap(u8, void);
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(HM);
        \\}
    , "unsupported type void");
}

test "read tagged union" {
    const Foo = union(enum) { a: i64, b: bool, c: u8 };
    var fbs = io.fixedBufferStream("\x02\x2a");


@@ 306,67 280,6 @@ test "read tagged union" {
    try expectEqual(Foo{ .c = 42 }, res);
}

test "read untagged union" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = union { a: u8, b: void };
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(Foo);
        \\}
    , "only tagged unions are supported");
}

test "read void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(void);
        \\}
    , "unsupported type void");
}

test "read unsupported integer type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(u7);
        \\}
    , "unsupported integer type u7");
}

test "read unsupported float type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(f16);
        \\}
    , "unsupported float type f16");
}

test "read unsupported pointer type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(*u8);
        \\}
    , "slices are the only supported pointer type");
}

test "write u8" {
    var buf: [1]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);


@@ 404,19 317,6 @@ test "write struct" {
    try expect(mem.eql(u8, fbs.getWritten(), expected));
}

test "write struct with void members" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = struct { x: u8, y: void };
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(Foo{ .x = 1, .y = {} });
        \\}
    , "unsupported type");
}

test "write enum" {
    const Foo = enum(u8) {
        a,


@@ 455,19 355,6 @@ test "write optional u8 null" {
    try expectEqual(fbs.getWritten()[0], @intFromBool(false));
}

test "write optional void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  const foo: ?void = null;
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "unsupported type");
}

test "write u8 array" {
    var buf: [4]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);


@@ 480,19 367,6 @@ test "write u8 array" {
    try expectEqualSlices(u8, fbs.getWritten(), &expected);
}

test "write array of void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  const foo: [4]void = .{ {}, {}, {}, {} };
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "unsupported type");
}

test "write slice" {
    var buf: [5]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);


@@ 508,19 382,6 @@ test "write slice" {
    try expectEqualSlices(u8, fbs.getWritten()[1..], &expected);
}

test "write slice of void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  const foo: []const void = &[_]void{ {}, {} };
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "unsupported type void");
}

test "write map[u8]u8" {
    var buf: [5]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);


@@ 557,19 418,6 @@ test "write map[string]u8" {
    try expectEqualSlices(u8, fbs.getWritten(), expected);
}

test "write map[u8]void" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  const foo = std.AutoHashMap(u8, void);
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "unsupported type");
}

test "write tagged union" {
    var buf: [10]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);


@@ 752,203 600,3 @@ test "bare.Decoder frees its memory" {
    _ = try d.decode([]const u8);
    // testing.allocator will yell if memory is leaked
}

test "invariant: fixed-length array must have length > 0 (read)" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  try d.decode([0]u8);
        \\}
    , "array length must be at least 1");
}

test "invariant: fixed-length array must have length > 0 (write)" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var foo = [0]u8{};
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(foo);
        \\}
    , "array length must be at least 1");
}

test "invariant: structs must have at least 1 field (read)" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = struct {};
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  try d.decode(Foo);
        \\}
    , "structs must have 1 or more fields");
}

test "invariant: structs must have at least 1 field (write)" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = struct {};
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  try e.encode(Foo{});
        \\}
    , "structs must have 1 or more fields");
}

test "invariant: unions must have at least 1 field (read)" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = union(enum) {};
        \\  var fbs = io.fixedBufferStream("lol");
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(Foo{});
        \\}
    , "union initializer must initialize one field");
}

test "invariant: unions must have at least 1 field (write)" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = union(enum) {};
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  const foo = Foo{};
        \\  _ = try e.encode(foo);
        \\}
    , "union initializer must initialize one field");
}

test "invariant: hashmap keys must be of primitive type" {
    try testCompileError(
        \\pub fn main() !void {
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  const hm = std.AutoHashMap([64]u8, void).init(gpa.allocator());
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var writer = fbs.writer();
        \\  var e = encoder(writer);
        \\  _ = try e.encode(hm);
        \\}
    , "unsupported hashmap key type [64]u8");
}

test "invariant: enum values must be unique" {
    try testCompileError(
        \\pub fn main() !void {
        \\  const Foo = enum(u8) {
        \\    x = 1,
        \\    y = 1,
        \\  };
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  var reader = fbs.reader();
        \\  var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        \\  var d = decoder(gpa.allocator(), reader);
        \\  _ = try d.decode(Foo);
        \\}
    , "enum tag value 1 already taken");
}

const fs = std.fs;

const boilerplate =
    \\const std = @import("std");
    \\const io = std.io;
    \\const math = std.math;
    \\const mem = std.mem;
    \\const testing = std.testing;
    \\
    \\const bare = @import("zig-bare.zig");
    \\const Decoder = bare.Decoder;
    \\const Encoder = bare.Encoder;
    \\const decoder = bare.decoder;
    \\const encoder = bare.encoder;
;

var test_file_seq: usize = 1;

fn testCompileError(comptime code: []const u8, comptime expected_error: []const u8) !void {
    var buf: [1024]u8 = undefined;
    const fname = try std.fmt.bufPrint(&buf, "/tmp/zig-bare-test-{d}.zig", .{test_file_seq});
    test_file_seq += 1;
    var cwd = fs.cwd();
    try cwd.copyFile("src/bare.zig", cwd, "/tmp/zig-bare.zig", .{});
    try cwd.copyFile("src/varint.zig", cwd, "/tmp/varint.zig", .{});
    var tmpfile = try cwd.createFile(fname, .{ .read = true, .truncate = true });
    defer tmpfile.close();
    _ = try tmpfile.write(boilerplate);
    _ = try tmpfile.write(code);
    const res = try std.ChildProcess.run(.{
        .allocator = testing.allocator,
        .argv = &[_][]const u8{ "zig", "build-exe", fname, "-fno-emit-bin", "--color", "off" },
    });
    defer {
        testing.allocator.free(res.stdout);
        testing.allocator.free(res.stderr);
    }
    try checkErrorMessage(res.stderr, expected_error);
    try expectEqual(@as(u8, 1), res.term.Exited);
}

fn runZigTestOnCodeString(comptime code: []const u8) !void {
    var buf: [1024]u8 = undefined;
    const fname = try std.fmt.bufPrint(&buf, "/tmp/zig-bare-test-{d}.zig", .{test_file_seq});
    test_file_seq += 1;
    var cwd = fs.cwd();
    try cwd.copyFile("src/bare.zig", cwd, "/tmp/zig-bare.zig", .{});
    try cwd.copyFile("src/varint.zig", cwd, "/tmp/varint.zig", .{});
    var tmpfile = try cwd.createFile(fname, .{ .read = true, .truncate = true });
    defer tmpfile.close();
    _ = try tmpfile.write(boilerplate);
    _ = try tmpfile.write(code);
    const res = try std.ChildProcess.run(.{
        .allocator = testing.allocator,
        .argv = &[_][]const u8{ "zig", "test", fname, "-fno-emit-bin", "--color", "off" },
    });
    defer {
        testing.allocator.free(res.stdout);
        testing.allocator.free(res.stderr);
    }
    try expectEqual(@as(u8, 0), res.term.Exited);
}

fn checkErrorMessage(haystack: []const u8, message: []const u8) !void {
    const errorstart = "error: ";
    const idx = mem.indexOf(u8, haystack, errorstart) orelse unreachable;
    const errorslice = haystack[idx + errorstart.len .. idx + errorstart.len + message.len];

    if (null != std.mem.indexOf(u8, errorslice, message))
        try expectEqualSlices(u8, message, errorslice);
}

test "README example" {
    @setEvalBranchQuota(1000000);
    const readme_text = @embedFile("README.md");
    const needle_start = "```zig";
    const needle_end = "```";
    const zig_code_start = comptime std.mem.indexOf(u8, readme_text, needle_start);

    if (zig_code_start) |start| {
        const rest = readme_text[start + needle_start.len ..];
        const zig_code_end = comptime std.mem.indexOf(u8, rest, needle_end);

        if (zig_code_end) |end| {
            const zig_code = rest[0..end];

            try runZigTestOnCodeString(zig_code);
        }
    }
}