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
37 changes: 36 additions & 1 deletion assembly/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { equal, isNull } from "./comparison";
import { assertResult } from "./env";
import { toJson } from "./formatPrint";


// @ts-ignore
@inline
const EXPECT_MAX_INDEX = 2147483647;

export class Value<T> {
reversed: bool = false;
data: T;
constructor(_data: T) {
this.data = _data;
Expand Down Expand Up @@ -110,4 +111,38 @@ export class Value<T> {
}
return this;
}

isa<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
assertResult.collectCheckResult(
// @ts-ignore
this.data instanceof ExpectType,
codeInfoIndex,
// TODO: need extend chain information
`RTID<${load<u32>(changetype<usize>(this.data) - 8)}>`,
`RTID<${idof<ExpectType>()}>`,
);
return this;
}

isExactly<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
if (isNullable<T>()) {
if (this.data == null) {
assertResult.collectCheckResult(
false,
codeInfoIndex,
`<<null>>`,
`RTID<${idof<ExpectType>()}>`,
);
return this;
}
}
const rtid = load<u32>(changetype<usize>(this.data) - 8);
assertResult.collectCheckResult(
rtid == idof<ExpectType>(),
codeInfoIndex,
`RTID<${rtid}>`,
`RTID<${idof<ExpectType>()}>`,
);
return this;
}
}
13 changes: 11 additions & 2 deletions docs/api-documents/matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ Specially, for float type, use `closeTo` instead of `equal` to avoid rounding er

## Nullable

`isNull` and `notNull` matcher can be used to a nullable object.
Of cource, you can also use `equal` and `notEqual` to do same thing with explicit generic declartion `expect<T | null>()`
`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

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

In Assemblyscript, a variable has 2 kinds of types: the defined type and the runtime type. For example, when `Ext` extends `Base`, a variable declared as type `Base` may actually be of type `Ext` at runtime.

- `isa` will check whether runtime type of given value is instance of expected type. In previous example, the runtime type of variable is instance of both `Base` and `Ext`.
- `isExactly` will check whether runtime type of give value is exactly same as expected type. In previous example, the runtime type of variable is exactly `Ext` but not `Base`.
13 changes: 13 additions & 0 deletions tests/as/expect.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, test } from "../../assembly";

class Base {}
class Ext_0 extends Base {}
class Ext_0_0 extends Ext_0 {}
class Ext_1 extends Base {}
class Ext_1_1 extends Ext_1 {}

describe("expect", () => {
test("< = >", () => {
expect(1).greaterThan(0);
Expand All @@ -13,4 +19,11 @@ describe("expect", () => {
expect<string | null>(null).isNull();
expect<string | null>("test").notNull();
});
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).isExactly<Ext_0_0>();
});
});