Skip to content

Commit 83c258b

Browse files
committed
runtime tests
1 parent 9963ac3 commit 83c258b

File tree

2 files changed

+121
-18
lines changed

2 files changed

+121
-18
lines changed

test/typescript/runtime.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,92 @@
11
module MxTests {
22

3+
// class without equality-comparer
4+
class SimpleClass {
5+
};
36

7+
8+
// class overriding '__hash__' and '__equals__' methods.
9+
class SimpleClassWithComparer {
10+
11+
constructor(val: number) {
12+
this.value = val;
13+
this.name = val.toString();
14+
}
15+
16+
public value: number;
17+
public name: string;
18+
19+
__hash__(): number {
20+
return mx.hash(this.value, this.name);
21+
}
22+
23+
__equals__(obj: any) {
24+
return obj instanceof SimpleClassWithComparer && obj.value === this.value && obj.name === this.name;
25+
}
26+
};
27+
28+
29+
30+
31+
QUnit.module("Runtime");
32+
33+
34+
QUnit.test("hash", function (assert) {
35+
36+
assert.ok(mx.hash(null) === 0, "hash null!");
37+
assert.ok(mx.hash(undefined) === 0, "hash undefined!");
38+
assert.ok(mx.hash(10) === mx.hash(10), "hash integer number!");
39+
assert.ok(mx.hash(10.5) === mx.hash(10.5), "hash float number!");
40+
assert.ok(mx.hash("string") === mx.hash("string"), "hash string!");
41+
assert.ok(mx.hash(true) === mx.hash(true), "hash boolean!");
42+
assert.ok(mx.hash(new Date(2015, 0, 1)) === mx.hash(new Date(2015, 0, 1)), "hash date!");
43+
assert.ok(mx.hash({ name: "A" }) === mx.hash({ name: "A" }), "hash object literal!");
44+
assert.ok(mx.hash(new SimpleClass()) !== mx.hash(new SimpleClass()), "hash class instance!");
45+
assert.ok(mx.hash(new SimpleClassWithComparer(10)) === mx.hash(new SimpleClassWithComparer(10)), "hash class instance overriding __hash__ method!");
46+
assert.ok(mx.hash(10, 10.5, "string", new Date(2015, 0, 1)) === mx.hash(10, 10.5, "string", new Date(2015, 0, 1)), "combine hash codes!");
47+
});
48+
49+
50+
QUnit.test("equals", function (assert) {
51+
52+
assert.ok(mx.equals(null, null) === true, "equals null!");
53+
assert.ok(mx.equals(undefined, undefined) === true, "equals undefined!");
54+
assert.ok(mx.equals(10, 10), "equals integer number!");
55+
assert.ok(mx.equals(10.5, 10.5), "equals float number!");
56+
assert.ok(mx.equals("string", "string"), "equals string!");
57+
assert.ok(mx.equals(true, true), "equals boolean!");
58+
assert.ok(mx.equals(new Date(2015, 0, 1), new Date(2015, 0, 1)), "equals date!");
59+
assert.ok(mx.equals({ name: "A" }, { name: "A" }), "equals object literal!");
60+
assert.ok(mx.equals(new SimpleClass(), new SimpleClass()) === false, "equals class instance!");
61+
assert.ok(mx.equals(new SimpleClass(), new SimpleClass(), EqualityComparer.create(
62+
function () { return 0; },
63+
function () { return true; }
64+
)), "equals class instance using comparer!");
65+
assert.ok(mx.equals(new SimpleClassWithComparer(10), new SimpleClassWithComparer(10)), "equals class instance overriding __equals__ method!");
66+
});
67+
68+
69+
QUnit.test("compare", function (assert) {
70+
71+
assert.ok(mx.runtime.compare(1, null) === 1 && mx.runtime.compare(null, 1) === -1 && mx.runtime.compare(null, null) === 0, "compare null!");
72+
assert.ok(mx.runtime.compare(1, 0) === 1 && mx.runtime.compare(0, 1) === -1 && mx.runtime.compare(1, 1) === 0, "compare numbers!");
73+
assert.ok(mx.runtime.compare("B", "A") === 1 && mx.runtime.compare("A", "B") === -1 && mx.runtime.compare("A", "A") === 0, "compare string!");
74+
assert.ok(mx.runtime.compare(true, false) === 1 && mx.runtime.compare(false, true) === -1 && mx.runtime.compare(true, true) === 0, "compare bolean!");
75+
assert.ok(mx.runtime.compare(new Date(2015, 0, 2), new Date(2015, 0, 1)) === 1 && mx.runtime.compare(new Date(2015, 0, 1), new Date(2015, 0, 2)) === -1 && mx.runtime.compare(new Date(2015, 0, 1), new Date(2015, 0, 1)) === 0, "compare date!");
76+
assert.ok(mx.runtime.compare({ name: "A" }, { name: "B" }) === 0, "compare objects!");
77+
});
78+
79+
80+
QUnit.test("lambda", function (assert) {
81+
82+
var _f1 = mx.runtime.lambda<number, number>("t => t * t"),
83+
_f2 = mx.runtime.lambda<number, number, number>("(t, u) => t + u"),
84+
_f3 = mx.runtime.lambda<number>("(t, u, r) => t + u + r"),
85+
_f4 = mx.runtime.lambda<number, string, { id: number; name: string }>("(t, u) => {id:t, name:u}");
86+
87+
assert.ok(_f1(2) === 4, "square root lambda!");
88+
assert.ok(_f2(1, 2) === 3, "sum of 2 numbers lambda!");
89+
assert.ok(_f3(1, 2, 3) === 6, "sum of 3 numbers lambda!");
90+
assert.ok(_f4(1, "A").id === 1 && _f4(1, "A").name === "A", "object literal lambda!");
91+
});
492
}

test/unit/runtime.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
/// <reference path="../data/_references.js" />
22

33

4-
(function (global) {
4+
(function () {
55

6-
var Foo = global.Foo,
7-
FooWithEqualityComparer = global.FooWithEqualityComparer,
8-
EqualityComparer = mx.EqualityComparer;
96

7+
// class without equality-comparer
8+
function SimpleClass() {
9+
}
10+
11+
12+
// class overriding '__hash__' and '__equals__' methods.
13+
function SimpleClassWithComparer(val) {
14+
this.value = val;
15+
this.name = val.toString();
16+
17+
this.__hash__ = function () {
18+
return mx.hash(this.value, this.name);
19+
};
20+
21+
this.__equals__ = function (obj) {
22+
return obj instanceof SimpleClassWithComparer && obj.value === this.value && obj.name === this.name;
23+
};
24+
}
1025

1126

1227
QUnit.module("Runtime");
@@ -22,8 +37,8 @@
2237
assert.ok(mx.hash(true) === mx.hash(true), "hash boolean!");
2338
assert.ok(mx.hash(new Date(2015, 0, 1)) === mx.hash(new Date(2015, 0, 1)), "hash date!");
2439
assert.ok(mx.hash({ name: "A" }) === mx.hash({ name: "A" }), "hash object literal!");
25-
assert.ok(mx.hash(new Foo()) !== mx.hash(new Foo()), "hash class instance!");
26-
assert.ok(mx.hash(new FooWithEqualityComparer(10)) === mx.hash(new FooWithEqualityComparer(10)), "hash class instance overriding __hash__ method!");
40+
assert.ok(mx.hash(new SimpleClass()) !== mx.hash(new SimpleClass()), "hash class instance!");
41+
assert.ok(mx.hash(new SimpleClassWithComparer(10)) === mx.hash(new SimpleClassWithComparer(10)), "hash class instance overriding __hash__ method!");
2742
assert.ok(mx.hash(10, 10.5, "string", new Date(2015, 0, 1)) === mx.hash(10, 10.5, "string", new Date(2015, 0, 1)), "combine hash codes!");
2843
});
2944

@@ -38,12 +53,12 @@
3853
assert.ok(mx.equals(true, true), "equals boolean!");
3954
assert.ok(mx.equals(new Date(2015, 0, 1), new Date(2015, 0, 1)), "equals date!");
4055
assert.ok(mx.equals({ name: "A" }, { name: "A" }), "equals object literal!");
41-
assert.ok(mx.equals(new Foo(), new Foo()) === false, "equals class instance!");
42-
assert.ok(mx.equals(new Foo(), new Foo(), EqualityComparer.create(
56+
assert.ok(mx.equals(new SimpleClass(), new SimpleClass()) === false, "equals class instance!");
57+
assert.ok(mx.equals(new SimpleClass(), new SimpleClass(), mx.EqualityComparer.create(
4358
function () { return 0; },
4459
function () { return true; }
4560
)), "equals class instance using comparer!");
46-
assert.ok(mx.equals(new FooWithEqualityComparer(10), new FooWithEqualityComparer(10)), "equals class instance overriding __equals__ method!");
61+
assert.ok(mx.equals(new SimpleClassWithComparer(10), new SimpleClassWithComparer(10)), "equals class instance overriding __equals__ method!");
4762
});
4863

4964

@@ -60,15 +75,15 @@
6075

6176
QUnit.test("lambda", function (assert) {
6277

63-
var f1 = mx.runtime.lambda("t => t * t"),
64-
f2 = mx.runtime.lambda("(t, u) => t + u"),
65-
f3 = mx.runtime.lambda("(t, u, r) => t + u + r"),
66-
f4 = mx.runtime.lambda("(t, u) => {id:t, name:u}");
78+
var _f1 = mx.runtime.lambda("t => t * t"),
79+
_f2 = mx.runtime.lambda("(t, u) => t + u"),
80+
_f3 = mx.runtime.lambda("(t, u, r) => t + u + r"),
81+
_f4 = mx.runtime.lambda("(t, u) => {id:t, name:u}");
6782

68-
assert.ok(f1(2) === 4, "square root lambda!");
69-
assert.ok(f2(1, 2) === 3, "sum of 2 numbers lambda!");
70-
assert.ok(f3(1, 2, 3) === 6, "sum of 3 numbers lambda!");
71-
assert.ok(f4(1, "A").id === 1 && f4(1, "A").name === "A", "object literal lambda!");
83+
assert.ok(_f1(2) === 4, "square root lambda!");
84+
assert.ok(_f2(1, 2) === 3, "sum of 2 numbers lambda!");
85+
assert.ok(_f3(1, 2, 3) === 6, "sum of 3 numbers lambda!");
86+
assert.ok(_f4(1, "A").id === 1 && _f4(1, "A").name === "A", "object literal lambda!");
7287
});
7388

74-
})(window);
89+
})();

0 commit comments

Comments
 (0)