Skip to content

Commit

Permalink
Merge pull request #9 from brettcannon/no-description-required
Browse files Browse the repository at this point in the history
Make the test message an optional labelled argument called `~msg`
  • Loading branch information
dusty-phillips authored May 6, 2024
2 parents 5e24864 + b411077 commit 5e40865
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 81 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ for CI:
This library models all the default assertions provided by Zora except for
those dealing with raising exceptions, which don't map neatly to ReScript
exceptions. There are additional bindings for checking if a ReScript `option`
is `Some()` or `None` or if a `Belt.Result` is `Ok()` or `Error()` and asserting
is `Some()` or `None` or if a `Result` is `Ok()` or `Error()` and asserting
on the value therein.

In the interest of avoiding bloat, I do not intend to add a lot of other
Expand All @@ -344,8 +344,8 @@ zora("Test assertions", t => {
t->notOk(false, "boolean is not ok")
t->optionNone(None, "None is None")
t->optionSome(Some(x), (t, n) => t->equal(n["hello"], "world", "option should be hello world"))
t->resultError(Belt.Result.Error(x), "Is Error Result")
t->resultOk(Belt.Result.Ok(x), (t, n) => t->equal(n["hello"], "world", "Is Ok Result"))
t->resultError(Error(x), "Is Error Result")
t->resultOk(Ok(x), (t, n) => t->equal(n["hello"], "world", "Is Ok Result"))
})
```

Expand Down
4 changes: 3 additions & 1 deletion rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
"package-specs": {
"module": "esmodule",
"in-source": true
}
},
"bs-dependencies": ["@rescript/core"],
"bsc-flags": ["-open RescriptCore"]
}
22 changes: 15 additions & 7 deletions src/Zora.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
import * as Belt_Result from "rescript/lib/es6/belt_Result.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";
import * as Core__Option from "@rescript/core/src/Core__Option.mjs";
import * as Core__Result from "@rescript/core/src/Core__Result.mjs";

function optionNone(zora, actual, message) {
zora.ok(Belt_Option.isNone(actual), message);
function optionNone(zora, actual, msg) {
if (msg !== undefined) {
zora.ok(Core__Option.isNone(actual), msg);
} else {
zora.ok(Core__Option.isNone(actual));
}
}

function optionSome(zora, actual, check) {
Expand All @@ -17,15 +21,19 @@ function optionSome(zora, actual, check) {
}
}

function resultError(zora, actual, message) {
zora.ok(Belt_Result.isError(actual), message);
function resultError(zora, actual, msg) {
if (msg !== undefined) {
zora.ok(Core__Result.isError(actual), msg);
} else {
zora.ok(Core__Result.isError(actual));
}
}

function resultOk(zora, actual, check) {
if (actual.TAG === "Ok") {
return check(zora, actual._0);
}
zora.fail("Expected ok value, got error");
zora.fail("Expected Ok value, got Error");
}

export {
Expand Down
36 changes: 21 additions & 15 deletions src/Zora.res
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,36 @@ type testMessage = string
@send external blockSkip: (t, testTitle, zoraTestBlock) => unit = "skip"
@send external blockOnly: (t, testTitle, zoraTestBlock) => unit = "only"

@send external equal: (t, 't, 't, testMessage) => unit = "equal"
@send external notEqual: (t, 't, 't, testMessage) => unit = "notEqual"
@send external is: (t, 't, 't, testMessage) => unit = "is"
@send external isNot: (t, 't, 't, testMessage) => unit = "isNot"
@send external ok: (t, bool, testMessage) => unit = "ok"
@send external notOk: (t, bool, testMessage) => unit = "notOk"
@send external fail: (t, testMessage) => unit = "fail"
@send external equal: (t, 't, 't, ~msg: testMessage=?) => unit = "equal"
@send external notEqual: (t, 't, 't, ~msg: testMessage=?) => unit = "notEqual"
@send external is: (t, 't, 't, ~msg: testMessage=?) => unit = "is"
@send external isNot: (t, 't, 't, ~msg: testMessage=?) => unit = "isNot"
@send external ok: (t, bool, ~msg: testMessage=?) => unit = "ok"
@send external notOk: (t, bool, ~msg: testMessage=?) => unit = "notOk"
@send external fail: (t, ~msg: testMessage=?) => unit = "fail"

let optionNone = (zora: t, actual: option<'a>, message: testMessage) => {
zora->ok(actual->Belt.Option.isNone, message)
let optionNone = (zora: t, actual: option<'a>, ~msg: option<testMessage>=?) => {
switch msg {
| Some(description) => zora->ok(actual->Option.isNone, ~msg=description)
| None => zora->ok(actual->Option.isNone)
}
}
let optionSome = (zora: t, actual: option<'a>, check: (t, 'a) => unit) => {
switch actual {
| None => zora->fail("Expected Some value, got None")
| None => zora->fail(~msg="Expected Some value, got None")
| Some(value) => zora->check(value)
}
}

let resultError = (zora: t, actual: Belt.Result.t<'a, 'b>, message: testMessage) => {
zora->ok(actual->Belt.Result.isError, message)
let resultError = (zora: t, actual: result<'a, 'b>, ~msg: option<testMessage>=?) => {
switch msg {
| Some(description) => zora->ok(actual->Result.isError, ~msg=description)
| None => zora->ok(actual->Result.isError)
}
}
let resultOk = (zora: t, actual: Belt.Result.t<'a, 'b>, check: (t, 'a) => unit) => {
let resultOk = (zora: t, actual: result<'a, 'b>, check: (t, 'a) => unit) => {
switch actual {
| Belt.Result.Error(_) => zora->fail("Expected ok value, got error")
| Belt.Result.Ok(value) => zora->check(value)
| Error(_) => zora->fail(~msg="Expected Ok value, got Error")
| Ok(value) => zora->check(value)
}
}
75 changes: 48 additions & 27 deletions tests/assertions.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,55 @@ import * as Zora$1 from "zora";
import * as Caml_option from "rescript/lib/es6/caml_option.js";

Zora$1.test("Test assertions", (async function (t) {
t.equal(42, 42, "Numbers are equal");
t.notEqual(42, 43, "Numbers are not equal");
var x = {
hello: "world"
};
var z = {
hello: "world"
};
t.is(x, x, "object is object");
t.is(x, x, "object is object");
t.isNot(x, z, "object is not object with same values");
t.equal(x, z, "Object is deep equal");
t.ok(true, "boolean is ok");
t.notOk(false, "boolean is not ok");
Zora.optionNone(t, undefined, "None is None");
Zora.optionSome(t, Caml_option.some(x), (function (t, n) {
t.equal(n.hello, "world", "option should be hello world");
t.test("With descriptions", (async function (t) {
t.equal(42, 42, "Numbers are equal");
t.notEqual(42, 43, "Numbers are not equal");
var x = {
hello: "world"
};
var z = {
hello: "world"
};
t.is(x, x, "object is object");
t.is(x, x, "object is object");
t.isNot(x, z, "object is not object with same values");
t.equal(x, z, "Object is deep equal");
t.ok(true, "boolean is ok");
t.notOk(false, "boolean is not ok");
Zora.optionNone(t, undefined, "None is None");
Zora.optionSome(t, Caml_option.some(x), (function (t, n) {
t.equal(n.hello, "world", "option should be hello world");
}));
Zora.resultError(t, {
TAG: "Error",
_0: x
}, "Is Error Result");
return Zora.resultOk(t, {
TAG: "Ok",
_0: x
}, (function (t, n) {
t.equal(n.hello, "world", "Is Ok Result");
}));
}));
t.test("Without descriptions", (async function (t) {
t.equal(42, 42);
t.notEqual(42, 43);
var x = {
hello: "world"
};
var z = {
hello: "world"
};
t.is(x, x);
t.isNot(x, z);
t.ok(true);
t.notOk(false);
Zora.optionNone(t, undefined, undefined);
return Zora.resultError(t, {
TAG: "Error",
_0: x
}, undefined);
}));
Zora.resultError(t, {
TAG: "Error",
_0: x
}, "Is Error Result");
return Zora.resultOk(t, {
TAG: "Ok",
_0: x
}, (function (t, n) {
t.equal(n.hello, "world", "Is Ok Result");
}));
}));

export {
Expand Down
50 changes: 35 additions & 15 deletions tests/assertions.test.res
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
open Zora

zora("Test assertions", async t => {
t->equal(42, 42, "Numbers are equal")
t->notEqual(42, 43, "Numbers are not equal")
let x = {"hello": "world"}
let y = x
let z = {"hello": "world"}
t->is(x, x, "object is object")
t->is(x, y, "object is object")
t->isNot(x, z, "object is not object with same values")
t->equal(x, z, "Object is deep equal")
t->ok(true, "boolean is ok")
t->notOk(false, "boolean is not ok")
t->optionNone(None, "None is None")
t->optionSome(Some(x), (t, n) => t->equal(n["hello"], "world", "option should be hello world"))
t->resultError(Belt.Result.Error(x), "Is Error Result")
t->resultOk(Belt.Result.Ok(x), (t, n) => t->equal(n["hello"], "world", "Is Ok Result"))
t->test("With descriptions", async t => {
t->equal(42, 42, ~msg="Numbers are equal")
t->notEqual(42, 43, ~msg="Numbers are not equal")
let x = {"hello": "world"}
let y = x
let z = {"hello": "world"}
t->is(x, x, ~msg="object is object")
t->is(x, y, ~msg="object is object")
t->isNot(x, z, ~msg="object is not object with same values")
t->equal(x, z, ~msg="Object is deep equal")
t->ok(true, ~msg="boolean is ok")
t->notOk(false, ~msg="boolean is not ok")
t->optionNone(None, ~msg="None is None")
t->optionSome(
Some(x),
(t, n) => t->equal(n["hello"], "world", ~msg="option should be hello world"),
)
t->resultError(Error(x), ~msg="Is Error Result")
t->resultOk(Ok(x), (t, n) => t->equal(n["hello"], "world", ~msg="Is Ok Result"))
})

t->test("Without descriptions", async t => {
t->equal(42, 42)
t->notEqual(42, 43)
let x = {"hello": "world"}
let z = {"hello": "world"}
t->is(x, x)
t->isNot(x, z)
t->ok(true)
t->notOk(false)
t->optionNone(None)
// optionSome has no ~msg.
t->resultError(Error(x))
// resultOk has no ~msg.
})
})
4 changes: 2 additions & 2 deletions tests/multiple_blocking.test.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ open Zora

zoraBlock("Should run some simple blocking tests", t => {
t->block("should greet", t => {
t->ok(true, "hello world")
t->ok(true, ~msg="hello world")
})

t->block("should answer question", t => {
let answer = 42
t->equal(answer, 42, "should be 42")
t->equal(answer, 42, ~msg="should be 42")
})
})
12 changes: 6 additions & 6 deletions tests/parallel.test.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ zora("Some Parallel Tests", async t => {

t->test("parallel 1", async t => {
{await wait(10)}->ignore
t->equal(state.contents, 1, "parallel 2 should have incremented by now")
t->equal(state.contents, 1, ~msg="parallel 2 should have incremented by now")
state.contents = state.contents + 1
t->equal(state.contents, 2, "parallel 1 should increment")
t->equal(state.contents, 2, ~msg="parallel 1 should increment")
})

t->test("parallel 2", async t => {
t->equal(state.contents, 0, "parallel 2 should be the first to increment")
t->equal(state.contents, 0, ~msg="parallel 2 should be the first to increment")
state.contents = state.contents + 1
t->equal(state.contents, 1, "parallel 2 should increment")
t->equal(state.contents, 1, ~msg="parallel 2 should increment")
})

t->test("parallel 3", async t => {
{await wait(20)}->ignore
t->equal(state.contents, 2, "parallel 1 and 2 should have incremented by now")
t->equal(state.contents, 2, ~msg="parallel 1 and 2 should have incremented by now")
state.contents = state.contents + 1
t->equal(state.contents, 3, "parallel 3 should increment last")
t->equal(state.contents, 3, ~msg="parallel 3 should increment last")
})
})
2 changes: 1 addition & 1 deletion tests/simple.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ open Zora

zoraBlock("should run a test synchronously", t => {
let answer = 3.14
t->equal(answer, 3.14, "Should be a tasty dessert")
t->equal(answer, 3.14, ~msg="Should be a tasty dessert")
})
4 changes: 2 additions & 2 deletions tests/skip.test.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ open Zora

zora("should skip some tests", async t => {
t->skip("broken test", async t => {
t->fail("Test is broken")
t->fail(~msg="Test is broken")
})

t->blockSkip("also broken", t => {
t->fail("Test is broken, too")
t->fail(~msg="Test is broken, too")
})
})
4 changes: 2 additions & 2 deletions tests/standaloneParallel.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ open Zora

zora("should run a test asynchronously", async t => {
let answer = 42
t->equal(answer, 42, "Should answer the question")
t->equal(answer, 42, ~msg="Should answer the question")
})

zora("should run a second test at the same time", async t => {
let answer = 3.14
t->equal(answer, 3.14, "Should be a tasty dessert")
t->equal(answer, 3.14, ~msg="Should be a tasty dessert")
})

0 comments on commit 5e40865

Please # to comment.