Skip to content

Commit 4ae5f48

Browse files
authored
feat: support not matcher (#100)
1 parent 6d3df96 commit 4ae5f48

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

assembly/expect.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,28 @@ export class Value<T> {
1212
constructor(_data: T) {
1313
this.data = _data;
1414
}
15-
isNull(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
15+
16+
private collect(
17+
result: bool,
18+
codeInfoIndex: number,
19+
actualValue: string,
20+
expectValue: string,
21+
): void {
1622
assertResult.collectCheckResult(
23+
this.reversed ? !result : result,
24+
codeInfoIndex,
25+
actualValue,
26+
expectValue,
27+
);
28+
}
29+
30+
get not(): Value<T> {
31+
this.reversed = !this.reversed;
32+
return this;
33+
}
34+
35+
isNull(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
36+
this.collect(
1737
isNull<T>(this.data),
1838
codeInfoIndex,
1939
toJson(this.data),
@@ -22,7 +42,7 @@ export class Value<T> {
2242
return this;
2343
}
2444
notNull(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
25-
assertResult.collectCheckResult(
45+
this.collect(
2646
!isNull<T>(this.data),
2747
codeInfoIndex,
2848
toJson(this.data),
@@ -32,7 +52,7 @@ export class Value<T> {
3252
}
3353

3454
equal(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
35-
assertResult.collectCheckResult(
55+
this.collect(
3656
equal<T>(this.data, checkValue),
3757
codeInfoIndex,
3858
toJson(this.data),
@@ -41,7 +61,7 @@ export class Value<T> {
4161
return this;
4262
}
4363
notEqual(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
44-
assertResult.collectCheckResult(
64+
this.collect(
4565
!equal<T>(this.data, checkValue),
4666
codeInfoIndex,
4767
toJson(this.data),
@@ -51,7 +71,7 @@ export class Value<T> {
5171
}
5272

5373
greaterThan(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
54-
assertResult.collectCheckResult(
74+
this.collect(
5575
this.data > checkValue,
5676
codeInfoIndex,
5777
toJson(this.data),
@@ -63,7 +83,7 @@ export class Value<T> {
6383
checkValue: T,
6484
codeInfoIndex: u32 = EXPECT_MAX_INDEX,
6585
): Value<T> {
66-
assertResult.collectCheckResult(
86+
this.collect(
6787
this.data >= checkValue,
6888
codeInfoIndex,
6989
toJson(this.data),
@@ -72,7 +92,7 @@ export class Value<T> {
7292
return this;
7393
}
7494
lessThan(checkValue: T, codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
75-
assertResult.collectCheckResult(
95+
this.collect(
7696
this.data < checkValue,
7797
codeInfoIndex,
7898
toJson(this.data),
@@ -84,7 +104,7 @@ export class Value<T> {
84104
checkValue: T,
85105
codeInfoIndex: u32 = EXPECT_MAX_INDEX,
86106
): Value<T> {
87-
assertResult.collectCheckResult(
107+
this.collect(
88108
this.data <= checkValue,
89109
codeInfoIndex,
90110
toJson(this.data),
@@ -100,7 +120,7 @@ export class Value<T> {
100120
): Value<T> {
101121
const data = this.data;
102122
if (isFloat<T>(checkValue) && isFloat<T>(data)) {
103-
assertResult.collectCheckResult(
123+
this.collect(
104124
abs(data - checkValue) < delta,
105125
codeInfoIndex,
106126
toJson(this.data),
@@ -113,7 +133,7 @@ export class Value<T> {
113133
}
114134

115135
isa<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
116-
assertResult.collectCheckResult(
136+
this.collect(
117137
// @ts-ignore
118138
this.data instanceof ExpectType,
119139
codeInfoIndex,
@@ -127,7 +147,7 @@ export class Value<T> {
127147
isExactly<ExpectType>(codeInfoIndex: u32 = EXPECT_MAX_INDEX): Value<T> {
128148
if (isNullable<T>()) {
129149
if (this.data == null) {
130-
assertResult.collectCheckResult(
150+
this.collect(
131151
false,
132152
codeInfoIndex,
133153
`<<null>>`,
@@ -137,7 +157,7 @@ export class Value<T> {
137157
}
138158
}
139159
const rtid = load<u32>(changetype<usize>(this.data) - 8);
140-
assertResult.collectCheckResult(
160+
this.collect(
141161
rtid == idof<ExpectType>(),
142162
codeInfoIndex,
143163
`RTID<${rtid}>`,

docs/api-documents/matchers.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ test('two plus two is four', () => {
1010

1111
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.
1212

13+
### Not
14+
15+
`not` matcher can reverse the matching results of all subsequent `matcher` statements.
16+
17+
```ts
18+
expect(1).not.equal(2); // success
19+
expect(1).not.equal(1); // fail
20+
```
21+
1322
### Equal
1423

1524
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`.
@@ -24,12 +33,12 @@ Most ways of comparing numbers have matcher equivalents, like `equal`, `greaterT
2433

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

27-
## Nullable
36+
### Nullable
2837

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

32-
## Typing
41+
### Typing
3342

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

tests/as/expect.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, test } from "../../assembly";
33
class Base {}
44
class Ext_0 extends Base {}
55
class Ext_0_0 extends Ext_0 {}
6+
class Ext_0_0_0 extends Ext_0 {}
67
class Ext_1 extends Base {}
78
class Ext_1_1 extends Ext_1 {}
89

@@ -19,11 +20,29 @@ describe("expect", () => {
1920
expect<string | null>(null).isNull();
2021
expect<string | null>("test").notNull();
2122
});
23+
test("not", () => {
24+
expect(1).not.equal(2);
25+
});
2226
test("isa", () => {
2327
let ext: Base = new Ext_0_0();
2428
expect(ext).isa<Base>();
2529
expect(ext).isa<Ext_0>();
2630
expect(ext).isa<Ext_0_0>();
31+
expect(ext).not.isa<Ext_0_0_0>();
32+
expect(ext).not.isa<Ext_1>();
33+
expect(ext).not.isa<Ext_1_1>();
34+
});
35+
test("isExactly", () => {
36+
let ext: Base = new Ext_0_0();
2737
expect(ext).isExactly<Ext_0_0>();
38+
expect(ext).not.isExactly<Base>();
39+
expect(ext).not.isExactly<Ext_0>();
40+
expect(ext).not.isExactly<Ext_0_0_0>();
41+
});
42+
test("nullable isExactly", () => {
43+
let extNull: Base | null = null;
44+
let extNonNull: Base | null = new Base();
45+
expect(extNull).not.isExactly<Base>();
46+
expect(extNonNull).isExactly<Base>();
2847
});
2948
});

0 commit comments

Comments
 (0)