Skip to content

Commit

Permalink
.inspect() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dionysusnu committed Jan 25, 2024
1 parent 7b8174b commit a1a3bd1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/src/tests/Option.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="@rbxts/testez/globals" />

import { Option, Result, Vec } from "@rbxts/rust-classes";
import { Option, Result, unit, Vec } from "@rbxts/rust-classes";

export = () => {
it("Option.__tostring", () => {
Expand Down Expand Up @@ -66,6 +66,19 @@ export = () => {
expect(Option.some(1).map((i) => ++i)).to.equal(Option.some(2));
expect(Option.none<number>().map((i) => ++i)).to.equal(Option.none());
});
it("Option.inspect", () => {
let calledSome = 0;
let calledNone = 0;
Option.some(unit()).inspect((v) => {
expect(v).to.equal(unit());
calledSome++;
});
Option.none().inspect(() => {
calledNone++;
});
expect(calledSome).to.equal(1);
expect(calledNone).to.equal(0);
});
it("Option.mapOr", () => {
expect(Option.some(1).mapOr(0, (i) => ++i)).to.equal(2);
expect(Option.none<number>().mapOr(0, (i) => ++i)).to.equal(0);
Expand Down
26 changes: 26 additions & 0 deletions tests/src/tests/Result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ export = () => {
expect(Result.err(1).mapErr((i) => ++i)).to.equal(Result.err(2));
expect(Result.ok<number, number>(1).mapErr(() => error("Should not run"))).to.equal(Result.ok(1));
});
it("Result.inspect", () => {
let calledOk = 0;
let calledErr = 0;
Result.ok(unit()).inspect((v) => {
expect(v).to.equal(unit());
calledOk++;
});
Result.err(unit()).inspect(() => {
calledErr++;
});
expect(calledOk).to.equal(1);
expect(calledErr).to.equal(0);
});
it("Result.inspectErr", () => {
let calledOk = 0;
let calledErr = 0;
Result.ok(unit()).inspectErr(() => {
calledOk++;
});
Result.err(unit()).inspectErr((v) => {
expect(v).to.equal(unit());
calledErr++;
});
expect(calledOk).to.equal(0);
expect(calledErr).to.equal(1);
});
it("Result.and", () => {
expect(Result.ok(1).and(Result.ok(2))).to.equal(Result.ok(2));
expect(Result.err(1).and(Result.ok(2))).to.equal(Result.err(1));
Expand Down

0 comments on commit a1a3bd1

Please sign in to comment.