diff --git a/assembly/expect.ts b/assembly/expect.ts index ac38205..fc1f326 100644 --- a/assembly/expect.ts +++ b/assembly/expect.ts @@ -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 { + reversed: bool = false; data: T; constructor(_data: T) { this.data = _data; @@ -110,4 +111,38 @@ export class Value { } return this; } + + isa(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value { + assertResult.collectCheckResult( + // @ts-ignore + this.data instanceof ExpectType, + codeInfoIndex, + // TODO: need extend chain information + `RTID<${load(changetype(this.data) - 8)}>`, + `RTID<${idof()}>`, + ); + return this; + } + + isExactly(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value { + if (isNullable()) { + if (this.data == null) { + assertResult.collectCheckResult( + false, + codeInfoIndex, + `<>`, + `RTID<${idof()}>`, + ); + return this; + } + } + const rtid = load(changetype(this.data) - 8); + assertResult.collectCheckResult( + rtid == idof(), + codeInfoIndex, + `RTID<${rtid}>`, + `RTID<${idof()}>`, + ); + return this; + } } diff --git a/docs/api-documents/matchers.md b/docs/api-documents/matchers.md index 4e6d895..212b064 100644 --- a/docs/api-documents/matchers.md +++ b/docs/api-documents/matchers.md @@ -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()` +`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()` + +## 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`. diff --git a/tests/as/expect.test.ts b/tests/as/expect.test.ts index bcd375d..020fc4d 100644 --- a/tests/as/expect.test.ts +++ b/tests/as/expect.test.ts @@ -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); @@ -13,4 +19,11 @@ describe("expect", () => { expect(null).isNull(); expect("test").notNull(); }); + test("isa", () => { + let ext: Base = new Ext_0_0(); + expect(ext).isa(); + expect(ext).isa(); + expect(ext).isa(); + expect(ext).isExactly(); + }); });