diff --git a/CHANGELOG.md b/CHANGELOG.md index 656742d..0367220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/mops.toml b/mops.toml index 4f5f1a2..f6cb54d 100644 --- a/mops.toml +++ b/mops.toml @@ -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" ] diff --git a/src/async.mo b/src/async.mo index 7aa56b3..f528ffd 100644 --- a/src/async.mo +++ b/src/async.mo @@ -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 () { @@ -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; }; \ No newline at end of file diff --git a/src/expect/async.mo b/src/expect/async.mo new file mode 100644 index 0000000..e37662e --- /dev/null +++ b/src/expect/async.mo @@ -0,0 +1,8 @@ +import {bindCompare; fail = _fail} "./utils"; +import ExpectCall "./expect-call"; + +module { + public let expectAsync = { + call = ExpectCall.ExpectCall; + }; +}; \ No newline at end of file diff --git a/src/expect/lib.mo b/src/expect/lib.mo index 9471544..952df5f 100644 --- a/src/expect/lib.mo +++ b/src/expect/lib.mo @@ -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"; @@ -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 = { @@ -60,7 +38,6 @@ module { principal = ExpectPrincipal.ExpectPrincipal; option = ExpectOption.ExpectOption; result = ExpectResult.ExpectResult; - call = ExpectCall.ExpectCall; }; public let fail = _fail; diff --git a/test/expect-async.test.mo b/test/expect-async.test.mo new file mode 100644 index 0000000..d0c9017 --- /dev/null +++ b/test/expect-async.test.mo @@ -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; + let ok : MyRes = #ok(22); + let err : MyRes = #err("error"); + + let expectOk = expect.result(ok, func(a) = debug_show(a), func(a, b) = a == b); + let expectErr = expect.result(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; + let ok : MyRes = #ok(?22); + let err : MyRes = #err("error"); + + let expectOk = expect.result(ok, func(a) = debug_show(a), func(a, b) = a == b); + let expectErr = expect.result(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(); +}); \ No newline at end of file diff --git a/test/expect.test.mo b/test/expect.test.mo index c3af535..47a63f9 100644 --- a/test/expect.test.mo +++ b/test/expect.test.mo @@ -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(); diff --git a/test/wasi.test.mo b/test/wasi.test.mo new file mode 100644 index 0000000..551a7d9 --- /dev/null +++ b/test/wasi.test.mo @@ -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(); + }); +}); \ No newline at end of file