Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add assertionWithLazyOperator #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions bin/retest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ if (globsOrNames.some((item) => item.includes("*"))) {
globsOrNames.map(
(globOrName) =>
new Promise((resolve, reject) => {
glob(globOrName, (err, files) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this callback variant does not exist anymore in the version 10.3.10 (cf: https://github.com/isaacs/node-glob?tab=readme-ov-file#globpattern-string--string-options-globoptions--promisestring--path ). Only promise and sync options exist.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just posted this in the issues: #19 maybe should have checked PR first

if (err) {
reject(err);
} else {
resolve(files);
}
});
resolve(glob.globSync(globOrName));
})
)
).then((arrays) => [...new Set([].concat(...arrays))]);
Expand Down
18 changes: 18 additions & 0 deletions src/Test.res
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ let assertion = (~message=?, ~operator=?, compare, a, b) => {
}
}

let assertionWithLazyOperator = (~message=?, ~operator=?, compare, a, b) => {
if compare(a, b) {
incr(passCounter)
Js.Console.log(` ${passText}${formatMessage(message)}`)
} else {
incr(failCounter)
Js.Console.log(` ${failText}${formatMessage(message)}`)
Js.Console.log(` ---`)
switch operator {
| Some(operator) => Js.Console.log(` ${pink("operator")}: ${Lazy.force(operator)}`)
| None => ()
}
Js.Console.log2(` ${pink("left")}: `, a)
Js.Console.log2(` ${pink("right")}:`, b)
Js.Console.log(` ...`)
}
}

let doesNotThrow = (~message=?, func: unit => unit) => {
try {
func()
Expand Down
7 changes: 7 additions & 0 deletions src/Test.resi
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
let assertion: (~message: string=?, ~operator: string=?, ('a, 'b) => bool, 'a, 'b) => unit
let assertionWithLazyOperator: (
~message: string=?,
~operator: Lazy.t<string>=?,
('a, 'b) => bool,
'a,
'b,
) => unit
let pass: (~message: string=?, unit) => unit
let fail: (~message: string=?, unit) => unit
let todo: string => unit
Expand Down
22 changes: 22 additions & 0 deletions test/BasicExample.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ test("Custom comparator", () => {
assertion(~message="Char code should match", ~operator="isCharCode", isCharCode, a, 98.0)
})

test("Lazy operator", () => {
let a = 1
let b = 2
assertionWithLazyOperator(
~operator=lazy {
"Heavily computed operator"
},
(a, b) => a != b,
a,
b,
)
assertionWithLazyOperator(
~operator=lazy {
Console.log("Should not show unless fails")
"Heavily computed operator"
},
(a, b) => a == b,
a,
b,
)
})

type user = {username: string, id: string}

let userEq = (a, b) => a.id === b.id
Expand Down