Skip to content
Merged
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
44 changes: 32 additions & 12 deletions assembly/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,28 @@ export class Value<T> {
constructor(_data: T) {
this.data = _data;
}
isNull(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {

private collect(
result: bool,
codeInfoIndex: number,
actualValue: string,
expectValue: string,
): void {
assertResult.collectCheckResult(
this.reversed ? !result : result,
codeInfoIndex,
actualValue,
expectValue,
);
}

get not(): Value<T> {
this.reversed = !this.reversed;
return this;
}

isNull(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
this.collect(
isNull<T>(this.data),
codeInfoIndex,
toJson(this.data),
Expand All @@ -22,7 +42,7 @@ export class Value<T> {
return this;
}
notNull(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
this.collect(
!isNull<T>(this.data),
codeInfoIndex,
toJson(this.data),
Expand All @@ -32,7 +52,7 @@ export class Value<T> {
}

equal(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
this.collect(
equal<T>(this.data, checkValue),
codeInfoIndex,
toJson(this.data),
Expand All @@ -41,7 +61,7 @@ export class Value<T> {
return this;
}
notEqual(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
this.collect(
!equal<T>(this.data, checkValue),
codeInfoIndex,
toJson(this.data),
Expand All @@ -51,7 +71,7 @@ export class Value<T> {
}

greaterThan(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
this.collect(
this.data > checkValue,
codeInfoIndex,
toJson(this.data),
Expand All @@ -63,7 +83,7 @@ export class Value<T> {
checkValue: T,
codeInfoIndex: u32 = EXPECT_MAX_INDEX,
): Value<T> {
assertResult.collectCheckResult(
this.collect(
this.data >= checkValue,
codeInfoIndex,
toJson(this.data),
Expand All @@ -72,7 +92,7 @@ export class Value<T> {
return this;
}
lessThan(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
this.collect(
this.data < checkValue,
codeInfoIndex,
toJson(this.data),
Expand All @@ -84,7 +104,7 @@ export class Value<T> {
checkValue: T,
codeInfoIndex: u32 = EXPECT_MAX_INDEX,
): Value<T> {
assertResult.collectCheckResult(
this.collect(
this.data <= checkValue,
codeInfoIndex,
toJson(this.data),
Expand All @@ -100,7 +120,7 @@ export class Value<T> {
): Value<T> {
const data = this.data;
if (isFloat<T>(checkValue) && isFloat<T>(data)) {
assertResult.collectCheckResult(
this.collect(
abs(data - checkValue) < delta,
codeInfoIndex,
toJson(this.data),
Expand All @@ -113,7 +133,7 @@ export class Value<T> {
}

isa<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
this.collect(
// @ts-ignore
this.data instanceof ExpectType,
codeInfoIndex,
Expand All @@ -127,7 +147,7 @@ export class Value<T> {
isExactly<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
if (isNullable<T>()) {
if (this.data == null) {
assertResult.collectCheckResult(
this.collect(
false,
codeInfoIndex,
`<<null>>`,
Expand All @@ -137,7 +157,7 @@ export class Value<T> {
}
}
const rtid = load<u32>(changetype<usize>(this.data) - 8);
assertResult.collectCheckResult(
this.collect(
rtid == idof<ExpectType>(),
codeInfoIndex,
`RTID<${rtid}>`,
Expand Down
13 changes: 11 additions & 2 deletions docs/api-documents/matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ test('two plus two is four', () => {

In this code, `expect(2+2)` returns an "Value" object. You typically won't do much with these objects except call matchers on them. In this code, `.equal(4)` is the matcher. When Jest runs, it tracks all the failing matchers so that it can print out nice error messages for you.

### Not

`not` matcher can reverse the matching results of all subsequent `matcher` statements.

```ts
expect(1).not.equal(2); // success
expect(1).not.equal(1); // fail
```

### Equal

In the most condition, `equal` is similar as `==`, you can use this matcher to compare `i32 | i64 | u32 | u64 | f32 | f64 | string` just like `==`. What's more, it can also be used to compare some inner type, such as `Array | Map | Set`.
Expand All @@ -24,12 +33,12 @@ Most ways of comparing numbers have matcher equivalents, like `equal`, `greaterT

Specially, for float type, use `closeTo` instead of `equal` to avoid rounding error.

## Nullable
### Nullable

`isNull` and `notNull` matchers can be used to a nullable object.
Of course, you can also use `equal` and `notEqual` to do same thing with explicit generic declaration `expect<T | null>()`

## Typing
### Typing

`isa` and `isExactly` matchers can be used to compare typing.

Expand Down
19 changes: 19 additions & 0 deletions tests/as/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from "../../assembly";
class Base {}
class Ext_0 extends Base {}
class Ext_0_0 extends Ext_0 {}
class Ext_0_0_0 extends Ext_0 {}
class Ext_1 extends Base {}
class Ext_1_1 extends Ext_1 {}

Expand All @@ -19,11 +20,29 @@ describe("expect", () => {
expect<string | null>(null).isNull();
expect<string | null>("test").notNull();
});
test("not", () => {
expect(1).not.equal(2);
});
test("isa", () => {
let ext: Base = new Ext_0_0();
expect(ext).isa<Base>();
expect(ext).isa<Ext_0>();
expect(ext).isa<Ext_0_0>();
expect(ext).not.isa<Ext_0_0_0>();
expect(ext).not.isa<Ext_1>();
expect(ext).not.isa<Ext_1_1>();
});
test("isExactly", () => {
let ext: Base = new Ext_0_0();
expect(ext).isExactly<Ext_0_0>();
expect(ext).not.isExactly<Base>();
expect(ext).not.isExactly<Ext_0>();
expect(ext).not.isExactly<Ext_0_0_0>();
});
test("nullable isExactly", () => {
let extNull: Base | null = null;
let extNonNull: Base | null = new Base();
expect(extNull).not.isExactly<Base>();
expect(extNonNull).isExactly<Base>();
});
});