Skip to content

Commit

Permalink
Merge 504ebef into 94421ad
Browse files Browse the repository at this point in the history
  • Loading branch information
jtenner committed May 20, 2020
2 parents 94421ad + 504ebef commit f81e67b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
4 changes: 0 additions & 4 deletions packages/assembly/assembly/__tests__/lessThan.spec.wat
Expand Up @@ -4741,8 +4741,6 @@
i32.ne
i32.const 3472
call $assembly/internal/assert/assert
i32.const 0
drop
local.get $4
local.get $3
local.get $1
Expand Down Expand Up @@ -5039,8 +5037,6 @@
i32.ne
i32.const 3472
call $assembly/internal/assert/assert
i32.const 0
drop
local.get $4
local.get $3
local.get $1
Expand Down
25 changes: 20 additions & 5 deletions packages/assembly/assembly/__tests__/toBeNull.spec.wat
Expand Up @@ -4255,8 +4255,6 @@
drop
local.get $3
call $assembly/internal/Actual/Actual.report<assembly/__tests__/setup/Vec3/Vec3 | null>
i32.const 1
drop
i32.const 0
local.get $2
call $assembly/internal/Expected/Expected.report<assembly/__tests__/setup/Vec3/Vec3 | null>
Expand Down Expand Up @@ -4545,6 +4543,25 @@
local.get $0
call $~lib/rt/pure/__release
)
(func $assembly/internal/Expected/Expected.report<assembly/__tests__/setup/Vec3/Vec3> (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $0
call $~lib/rt/pure/__retain
local.set $0
local.get $0
i32.const 1
global.set $~argumentsLength
i32.const 0
call $assembly/internal/Reflect/Reflect.toReflectedValue<assembly/__tests__/setup/Vec3/Vec3>@varargs
local.set $2
local.get $2
call $assembly/internal/Reflect/Reflect.attachStackTrace
local.get $2
local.get $1
call $assembly/internal/Expected/reportExpectedReflectedValue
local.get $0
call $~lib/rt/pure/__release
)
(func $assembly/internal/Expectation/Expectation<assembly/__tests__/setup/Vec3/Vec3>#toBeNull (param $0 i32) (param $1 i32)
(local $2 i32)
(local $3 i32)
Expand All @@ -4565,10 +4582,8 @@
local.get $3
call $assembly/internal/Actual/Actual.report<assembly/__tests__/setup/Vec3/Vec3>
i32.const 0
drop
i32.const 0
local.get $2
call $assembly/internal/Expected/Expected.report<assembly/__tests__/setup/Vec3/Vec3 | null>
call $assembly/internal/Expected/Expected.report<assembly/__tests__/setup/Vec3/Vec3>
local.get $2
local.get $3
i32.const 0
Expand Down
2 changes: 0 additions & 2 deletions packages/assembly/assembly/__tests__/toBeTruthy.spec.wat
Expand Up @@ -4836,8 +4836,6 @@
call $assembly/internal/Expected/Expected.reportTruthy
i32.const 1
drop
i32.const 1
drop
i32.const 0
drop
local.get $2
Expand Down
58 changes: 21 additions & 37 deletions packages/assembly/assembly/internal/Expectation.ts
Expand Up @@ -84,23 +84,15 @@ export class Expectation<T> {
Expected.reportTruthy(negated);

if (isReference(actual)) {
if (isNullable(actual)) {
// strings require an extra length check
if (actual instanceof String) {
let truthy = i32(changetype<usize>(actual) != 0 && actual.length > 0);
assert(truthy ^ negated, message);
} else {
let truthy = i32(changetype<usize>(actual) != 0);
assert(truthy ^ negated, message);
}
if (actual instanceof String) {
let truthy = i32(
changetype<usize>(actual) != 0 &&
changetype<string>(actual).length > 0,
);
assert(truthy ^ negated, message);
} else {
// strings require an extra length check
if (actual instanceof String) {
let truthy = i32(actual.length > 0);
assert(truthy ^ negated, message);
} else {
assert(i32(!negated), message);
}
let truthy = i32(changetype<usize>(actual) != 0);
assert(truthy ^ negated, message);
}
} else {
if (isFloat(actual)) {
Expand All @@ -125,7 +117,7 @@ export class Expectation<T> {
if (isReference(actual)) {
// strings require an extra length check
if (actual instanceof String) {
let falsy = i32(changetype<usize>(actual) == 0 || actual.length == 0);
let falsy = i32(changetype<usize>(actual) == 0 || changetype<string>(actual).length == 0);
assert(falsy ^ negated, message);
} else {
let falsy = i32(changetype<usize>(actual) == 0);
Expand Down Expand Up @@ -179,7 +171,7 @@ export class Expectation<T> {
"Invalid call to toBeGreaterThan. Generic type T must have an operator implemented for the greaterThan (>) operation.",
);

if (isReference<T>()) {
if (isReference(actual)) {
// Perform reference type null checks
assert(
i32(changetype<usize>(expected) != 0),
Expand Down Expand Up @@ -234,13 +226,13 @@ export class Expectation<T> {
}

// Compare float types
if (isFloat<T>()) {
if (isFloat(actual)) {
assert(
i32(!isNaN<T>(expected)),
i32(!isNaN(expected)),
"Value comparison fails, expected value is NaN.",
);
assert(
i32(!isNaN<T>(actual)),
i32(!isNaN(actual)),
"Value comparison fails, actual value is NaN.",
);
}
Expand Down Expand Up @@ -272,9 +264,7 @@ export class Expectation<T> {
i32(changetype<usize>(actual) != 0),
"Value comparison fails, actual value is null.",
);
}

if (isFloat(actual)) {
} else if (isFloat(actual)) {
assert(
i32(!isNaN(expected)),
"Value comparison fails, expected value is NaN.",
Expand Down Expand Up @@ -345,18 +335,12 @@ export class Expectation<T> {
} else if (isReference(actual)) {
Actual.report(actual);

// this call to isNullable is to make type checking consistent
if (isNullable(actual)) {
// @ts-ignore T is nullable
Expected.report<T>(null, negated);
} else {
Expected.report<T | null>(null, negated);
}
Expected.report(changetype<T>(0), negated);
assert(negated ^ i32(changetype<usize>(actual) == 0), message);
Actual.clear();
Expected.clear();
} else {
ERROR("toBeNull assertion must be called with a reference type T.");
ERROR("toBeNull assertion must be called with a reference type T or usize.");
}
}

Expand All @@ -369,7 +353,7 @@ export class Expectation<T> {
let negated = this._not;

// must be called on a float T
if (!isFloat<T>())
if (!isFloat(actual))
ERROR("toBeCloseTo must be called with a Float value type T.");
Actual.report(actual);
Expected.report(expected, negated);
Expand All @@ -386,7 +370,7 @@ export class Expectation<T> {

// calculated: `|expected - actual| < 1 / numberOfDigits`.
// @ts-ignore tooling errors because T does not extend a numeric value type. This compiles just fine.
let isClose = i32(abs<T>(expected - actual) < Math.pow(0.1, decimalPlaces));
let isClose = i32(abs(expected - actual) < Math.pow(0.1, decimalPlaces));
assert(negated ^ isClose, message);
Actual.clear();
Expected.clear();
Expand Down Expand Up @@ -429,7 +413,7 @@ export class Expectation<T> {
public toHaveLength(expected: i32, message: string = ""): void {
let actual = this.actual;
let negated = this._not;
let length = <i32>0;
let length = 0;
if (actual instanceof ArrayBuffer) {
length = actual.byteLength;
} else {
Expand Down Expand Up @@ -469,7 +453,7 @@ export class Expectation<T> {
}

public toContainEqual<U>(expected: U, message: string = ""): void {
this.toIncludeEqual<U>(expected, message);
this.toIncludeEqual(expected, message);
}

public toMatchSnapshot(name: string | null = null): void {
Expand All @@ -481,7 +465,7 @@ export class Expectation<T> {
// @ts-ignore: decorators *are* valid here
@global
export function expect<T>(actual: T): Expectation<T> {
return new Expectation<T>(actual);
return new Expectation(actual);
}

// @ts-ignore: decorators *are* valid here
Expand Down

0 comments on commit f81e67b

Please sign in to comment.