Skip to content

Commit

Permalink
fix test run in wasi mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenVoich committed Dec 6, 2023
1 parent b37df7e commit cdb6d71
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.2.0

- Fixed test run in `wasi` mode

### Breaking changes:
- `expect.call` now only available when imported from `mo:test/async`

## 1.1.0

- Added `expect` helper for easier testing of assertions
2 changes: 1 addition & 1 deletion mops.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "test"
version = "1.1.1"
version = "1.2.0"
description = "Motoko testing library to run tests with mops"
repository = "https://github.com/ZenVoich/test"
keywords = [ "test", "testing", "unit", "suite", "expect", "matchers", "mops" ]
Expand Down
6 changes: 5 additions & 1 deletion src/async.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Debug "mo:base/Debug";
import {expect = _expect; fail = _fail} "./expect";
import {expectAsync = _expectAsync} "./expect/async";

module {
public func test(name : Text, fn : () -> async ()) : async () {
Expand All @@ -16,6 +17,9 @@ module {
Debug.print("mops:1:skip " # name);
};

public let expect = _expect;
public let expect = {
_expect with
call = _expectAsync.call;
};
public let fail = _fail;
};
8 changes: 8 additions & 0 deletions src/expect/async.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {bindCompare; fail = _fail} "./utils";
import ExpectCall "./expect-call";

module {
public let expectAsync = {
call = ExpectCall.ExpectCall;
};
};
23 changes: 0 additions & 23 deletions src/expect/lib.mo
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
import Debug "mo:base/Debug";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Nat16 "mo:base/Nat16";
import Nat32 "mo:base/Nat32";
import Nat64 "mo:base/Nat64";
import Int "mo:base/Int";
import Int8 "mo:base/Int8";
import Int16 "mo:base/Int16";
import Int32 "mo:base/Int32";
import Int64 "mo:base/Int64";
import Float "mo:base/Float";
import Char "mo:base/Char";
import Text "mo:base/Text";
import TrieMap "mo:base/TrieMap";
import Hash "mo:base/Hash";
import Array "mo:base/Array";
import Option "mo:base/Option";
import Bool "mo:base/Bool";
import Iter "mo:base/Iter";

import {bindCompare; fail = _fail} "./utils";
import ExpectInt "./expect-int";
import ExpectInt8 "./expect-int8";
Expand All @@ -38,7 +17,6 @@ import ExpectBlob "./expect-blob";
import ExpectPrincipal "./expect-principal";
import ExpectOption "./expect-option";
import ExpectResult "./expect-result";
import ExpectCall "./expect-call";

module {
public let expect = {
Expand All @@ -60,7 +38,6 @@ module {
principal = ExpectPrincipal.ExpectPrincipal;
option = ExpectOption.ExpectOption;
result = ExpectResult.ExpectResult;
call = ExpectCall.ExpectCall;
};

public let fail = _fail;
Expand Down
181 changes: 181 additions & 0 deletions test/expect-async.test.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import Debug "mo:base/Debug";
import Nat "mo:base/Nat";
import Blob "mo:base/Blob";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Error "mo:base/Error";
import {test; suite; expect; fail} "../src/async";

await test("bool", func() : async () {
expect.bool(true).isTrue();
expect.bool(false).isFalse();
expect.bool(true).equal(true);
expect.bool(false).equal(false);
expect.bool(true).notEqual(false);
});

await test("option", func() : async () {
expect.option(null, Nat.toText, Nat.equal).isNull();
expect.option(?1, Nat.toText, Nat.equal).isSome();
expect.option(?2, Nat.toText, Nat.equal).equal(?2);
expect.option(?3, Nat.toText, Nat.equal).notEqual(?44);
expect.option(?3, Nat.toText, Nat.equal).notEqual(null);
expect.option(null, Nat.toText, Nat.equal).equal(null);
});

await test("option custom type", func() : async () {
type MyType = {
x : Nat;
y : Nat;
};
let v = ?{x = 1; y = 2};

func showMyType(a : MyType) : Text {
debug_show(a);
};

func equalMyType(a : MyType, b : MyType) : Bool {
a.x == b.x and a.y == b.y
};

expect.option(v, showMyType, equalMyType).notEqual(null);
expect.option(v, showMyType, equalMyType).equal(?{x = 1; y = 2});
});

await test("char", func() : async () {
expect.char('a').equal('a');
expect.char('a').notEqual('A');
});

await test("text", func() : async () {
expect.text("hello motoko").endsWith("motoko");
expect.text("hello motoko").contains("mot");
});

await test("nat", func() : async () {
let myNat = 33;
expect.nat(myNat).notEqual(22);
expect.nat(myNat).equal(33);
expect.nat(myNat).less(66);
});

await test("intX, natX", func() : async () {
let myNat : Nat = 22;
let myNat8 : Nat8 = 33;
let myInt : Int = -44;
let myInt8 : Int8 = -44;
let myFloat : Float = 1.313;
expect.int(myNat).equal(22);
expect.nat8(myNat8).equal(33);
expect.nat(myNat).equal(22);
expect.nat(myNat).less(66);

expect.int(myNat).notEqual(221);
expect.int8(myInt8).equal(myInt8);
expect.int64(123123123123).notEqual(1231231231232);
expect.nat8(myNat8).notEqual(32);
expect.nat(myNat).notEqual(221);
expect.nat8(myNat8).lessOrEqual(33);
});

await test("array contains", func() : async () {
expect.array([1,2,3,4,5,6,7,8,9,0], Nat.toText, Nat.equal).contains(6);
let exAr = expect.array([1,2,3,4,5,6,7,8,9,0], Nat.toText, Nat.equal);
exAr.contains(6);
exAr.contains(1);
exAr.contains(0);
exAr.notContains(88);
exAr.notContains(21);
exAr.size(10);
});

await test("array size", func() : async () {
expect.array([1,2,3,4,5,6,7,8,9,0], Nat.toText, Nat.equal).size(10);
});

await test("array equal", func() : async () {
expect.array([1,2,3,4], Nat.toText, Nat.equal).equal([1,2,3,4]);
expect.array([1,2,3,4], Nat.toText, Nat.equal).notEqual([1,2,2,4]);
expect.array([1,2,3,4], Nat.toText, Nat.equal).notEqual([1,2,3,4,5]);
expect.array([1,2,3,4], Nat.toText, Nat.equal).notEqual([1,2,3]);
});

await test("blob", func() : async () {
expect.blob(Blob.fromArray([1,2,3,4])).equal(Blob.fromArray([1,2,3,4]));
expect.blob(Blob.fromArray([1,2,3,4])).notEqual(Blob.fromArray([2,2,3,4]));
expect.blob(Blob.fromArray([1,2,3,4])).size(4);
});

await test("principal", func() : async () {
expect.principal(Principal.fromBlob(Blob.fromArray([1,2,3,4]))).equal(Principal.fromBlob(Blob.fromArray([1,2,3,4])));
expect.principal(Principal.fromBlob(Blob.fromArray([1,2,3,4]))).notEqual(Principal.fromBlob(Blob.fromArray([1,2,3,5])));
expect.principal(Principal.fromBlob("\04")).isAnonymous();
expect.principal(Principal.fromBlob(Blob.fromArray([4]))).isAnonymous();
});

await test("result", func() : async () {
type MyRes = Result.Result<Nat, Text>;
let ok : MyRes = #ok(22);
let err : MyRes = #err("error");

let expectOk = expect.result<Nat, Text>(ok, func(a) = debug_show(a), func(a, b) = a == b);
let expectErr = expect.result<Nat, Text>(err, func(a) = debug_show(a), func(a, b) = a == b);

expectOk.isOk();
expectOk.equal(#ok(22));

expectErr.isErr();
expectErr.equal(#err("error"));
expectErr.notEqual(#err("other error"));
});

await test("result opt ok", func() : async () {
type MyRes = Result.Result<?Nat, Text>;
let ok : MyRes = #ok(?22);
let err : MyRes = #err("error");

let expectOk = expect.result<?Nat, Text>(ok, func(a) = debug_show(a), func(a, b) = a == b);
let expectErr = expect.result<?Nat, Text>(err, func(a) = debug_show(a), func(a, b) = a == b);

expectOk.isOk();
expectOk.equal(#ok(?22));

expectErr.isErr();
expectErr.equal(#err("error"));
});

await test("expect custom", func() : async () {
type Custom = {
x : Nat;
y : Nat;
};

class expectCustom(a : Custom) {
func show(x : Custom) : Text = debug_show(x);

public func equal(b : Custom) {
if (a != b) {
fail(show(a), "to be ==", show(b));
};
};

public func greater(b : Custom) {
let ok = a.x > b.x and a.y > b.y;
if (not ok) {
fail(show(a), ">=", show(b));
};
};
};

expectCustom({x = 1; y = 3}).equal({x = 1; y = 3});
expectCustom({x = 2; y = 4}).greater({x = 1; y = 3});
});

await test("expectAsync.call", func() : async () {
// test throw error
func myFunc() : async () {
throw Error.reject("error");
};

await expect.call(myFunc).reject();
});
3 changes: 2 additions & 1 deletion test/expect.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Blob "mo:base/Blob";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Error "mo:base/Error";
import {test; suite; expect; fail} "../src";
import {test; suite; fail} "../src";
import {expect} "../src/async";

test("bool", func() {
expect.bool(true).isTrue();
Expand Down
12 changes: 12 additions & 0 deletions test/wasi.test.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @testmode wasi
import {suite; test; expect} "../src";

suite("simple suite", func() {
test("simple test", func() {
assert true;
});
test("expect.bool", func() {
expect.bool(true).isTrue();
expect.bool(false).isFalse();
});
});

0 comments on commit cdb6d71

Please sign in to comment.