Skip to content

Commit

Permalink
Add test.throws.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Apr 15, 2024
1 parent 60d2546 commit 58906d3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
12 changes: 0 additions & 12 deletions src/builtins/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,7 @@ pub const Symbol = enum {
paused,
done,

@"error",
float,
object,
map,
list,
function,
fiber,
string,
array,
none,
symbol,
pointer,
metatype,

// Open modes.
read,
Expand Down
2 changes: 1 addition & 1 deletion src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ pub const Parser = struct {
.pound,
.void_k,
.type_k,
.none_k,
.error_k,
.ident => {
return try self.parseTermExpr(.{});
},
Expand Down
15 changes: 14 additions & 1 deletion src/std/test.cy
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@
--| Returns `true` if two numbers are near each other within epsilon 1e-5.
#host func eqNear(a any, b any) bool

#host func fail() any
func fail():
throw error.AssertError

func fail(msg String):
eprint(msg)
throw error.AssertError

--| Asserts that an error was thrown when invoking a function.
func throws(fn any, err error):
var res = try fn()
if typeof(res) != error:
fail('Expected error.')
if res != err:
fail("Expected `$(err)`, found `$(res)`.")
5 changes: 0 additions & 5 deletions src/std/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const funcs = [_]NameHostFunc{
.{"eq", eq},
.{"eqList", zErrFunc(eqList)},
.{"eqNear", zErrFunc(eqNear)},
.{"fail", fail},
};

pub fn onLoad(vm_: ?*cc.VM, mod: cc.Sym) callconv(.C) void {
Expand All @@ -46,10 +45,6 @@ pub fn onLoad(vm_: ?*cc.VM, mod: cc.Sym) callconv(.C) void {
b.declareFuncSig("erase", &.{bt.Any}, bt.Dynamic, erase) catch cy.fatal();
}

fn fail(vm: *cy.VM, _: [*]const Value, _: u8) Value {
return rt.prepThrowError(vm, .AssertError);
}

/// Simply returns the value so the caller get's an erased `any` type.
fn erase(vm: *cy.VM, args: [*]const Value, _: u8) Value {
vm.retain(args[0]);
Expand Down
2 changes: 1 addition & 1 deletion test/core/symbols.cy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use test

var n = .Tiger
test.eq(int(n), 63)
test.eq(int(n), 53)

--cytest: pass

0 comments on commit 58906d3

Please sign in to comment.