mirror of
https://github.com/ziglang/zig.git
synced 2026-01-12 10:25:13 +00:00
39 lines
759 B
Zig
39 lines
759 B
Zig
const builtin = @import("builtin");
|
|
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
|
|
test "decl literal" {
|
|
const S = struct {
|
|
x: u32,
|
|
const foo: @This() = .{ .x = 123 };
|
|
};
|
|
|
|
const val: S = .foo;
|
|
try expect(val.x == 123);
|
|
}
|
|
|
|
test "call decl literal" {
|
|
const S = struct {
|
|
x: u32,
|
|
fn init() @This() {
|
|
return .{ .x = 123 };
|
|
}
|
|
};
|
|
|
|
const val: S = .init();
|
|
try expect(val.x == 123);
|
|
}
|
|
|
|
test "call decl literal with error union" {
|
|
const S = struct {
|
|
x: u32,
|
|
fn init(err: bool) !@This() {
|
|
if (err) return error.Bad;
|
|
return .{ .x = 123 };
|
|
}
|
|
};
|
|
|
|
const val: S = try .init(false);
|
|
try expect(val.x == 123);
|
|
}
|