Skip to content

Commit 6d3df96

Browse files
authored
feat: support instance of matcher (#99)
1 parent b48c5f7 commit 6d3df96

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

assembly/expect.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { equal, isNull } from "./comparison";
22
import { assertResult } from "./env";
33
import { toJson } from "./formatPrint";
44

5-
5+
// @ts-ignore
66
@inline
77
const EXPECT_MAX_INDEX = 2147483647;
88

99
export class Value<T> {
10+
reversed: bool = false;
1011
data: T;
1112
constructor(_data: T) {
1213
this.data = _data;
@@ -110,4 +111,38 @@ export class Value<T> {
110111
}
111112
return this;
112113
}
114+
115+
isa<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
116+
assertResult.collectCheckResult(
117+
// @ts-ignore
118+
this.data instanceof ExpectType,
119+
codeInfoIndex,
120+
// TODO: need extend chain information
121+
`RTID<${load<u32>(changetype<usize>(this.data) - 8)}>`,
122+
`RTID<${idof<ExpectType>()}>`,
123+
);
124+
return this;
125+
}
126+
127+
isExactly<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
128+
if (isNullable<T>()) {
129+
if (this.data == null) {
130+
assertResult.collectCheckResult(
131+
false,
132+
codeInfoIndex,
133+
`<<null>>`,
134+
`RTID<${idof<ExpectType>()}>`,
135+
);
136+
return this;
137+
}
138+
}
139+
const rtid = load<u32>(changetype<usize>(this.data) - 8);
140+
assertResult.collectCheckResult(
141+
rtid == idof<ExpectType>(),
142+
codeInfoIndex,
143+
`RTID<${rtid}>`,
144+
`RTID<${idof<ExpectType>()}>`,
145+
);
146+
return this;
147+
}
113148
}

docs/api-documents/matchers.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,14 @@ Specially, for float type, use `closeTo` instead of `equal` to avoid rounding er
2626

2727
## Nullable
2828

29-
`isNull` and `notNull` matcher can be used to a nullable object.
30-
Of cource, you can also use `equal` and `notEqual` to do same thing with explicit generic declartion `expect<T | null>()`
29+
`isNull` and `notNull` matchers can be used to a nullable object.
30+
Of course, you can also use `equal` and `notEqual` to do same thing with explicit generic declaration `expect<T | null>()`
31+
32+
## Typing
33+
34+
`isa` and `isExactly` matchers can be used to compare typing.
35+
36+
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.
37+
38+
- `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`.
39+
- `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`.

tests/as/expect.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { describe, expect, test } from "../../assembly";
22

3+
class Base {}
4+
class Ext_0 extends Base {}
5+
class Ext_0_0 extends Ext_0 {}
6+
class Ext_1 extends Base {}
7+
class Ext_1_1 extends Ext_1 {}
8+
39
describe("expect", () => {
410
test("< = >", () => {
511
expect(1).greaterThan(0);
@@ -13,4 +19,11 @@ describe("expect", () => {
1319
expect<string | null>(null).isNull();
1420
expect<string | null>("test").notNull();
1521
});
22+
test("isa", () => {
23+
let ext: Base = new Ext_0_0();
24+
expect(ext).isa<Base>();
25+
expect(ext).isa<Ext_0>();
26+
expect(ext).isa<Ext_0_0>();
27+
expect(ext).isExactly<Ext_0_0>();
28+
});
1629
});

0 commit comments

Comments
 (0)