Initial import.
[pwl6.git] / test / spec / yuu / core.js
1 var JS = this.JS || require('jstest');
2 var yuu = require('yuu/core');
3
4 JS.Test.describe('yuu core', function () { with (this) {
5 it("knows signum", function () { with (this) {
6 assertEqual(-1, Math.sign(-10));
7 assertEqual(1, Math.sign(10));
8 assertEqual(0, Math.sign(0));
9 }});
10 it("knows signum for weird numbers", function () { with (this) {
11 assertEqual(-1, Math.sign(-Infinity));
12 assertEqual(1, Math.sign(Infinity));
13 assert(Number.isNaN(Math.sign(NaN)));
14 }});
15
16 it("String endsWith", function () { with (this) {
17 assert("12345".endsWith("12345"));
18 assert("12345".endsWith("45"));
19 assert("12345".endsWith("5"));
20 assert("12345".endsWith(""));
21 assertNot("12345".endsWith("0"));
22 assertNot("12345".endsWith("35"));
23 assertNot("12345".endsWith("34"));
24 }});
25
26 it("String startsWith", function () { with (this) {
27 assert("12345".startsWith("12345"));
28 assert("12345".startsWith("12"));
29 assert("12345".startsWith("1"));
30 assert("12345".startsWith(""));
31 assertNot("12345".startsWith("0"));
32 assertNot("12345".startsWith("13"));
33 assertNot("12345".startsWith("23"));
34 }});
35
36 it("splits path extensions", function () { with (this) {
37 assertEqual(["a", ".png"], yuu.splitPathExtension("a.png"));
38 assertEqual(["a.b", ".png"], yuu.splitPathExtension("a.b.png"));
39 assertEqual(["a", ""], yuu.splitPathExtension("a"));
40 assertEqual(["a.b/c", ".png"], yuu.splitPathExtension("a.b/c.png"));
41 assertEqual([".gitignore", ""], yuu.splitPathExtension(".gitignore"));
42 }});
43
44 JS.Test.describe("resourcePath", function () { with (this) {
45 var f = yuu.resourcePath;
46 it("leaves ordinary paths alone", function () { with (this) {
47 assertEqual("foo.png", f("foo.png", "error", "error"));
48 assertEqual("foo/bar.png", f("foo/bar.png", "error", "error"));
49 assertEqual("foo@2x.png", f("foo@2x.png", "error", "error"));
50 }});
51 it("expands @ paths", function () { with (this) {
52 assertEqual("data/images/foo.png", f("@foo", "images", "png"));
53 assertEqual("data/images/foo.png", f("@foo.png", "images", "png"));
54 assertEqual("data/images/foo.jpg", f("@foo.jpg", "images", "png"));
55 assertEqual("data/images/foo/bar.png", f("@foo/bar", "images", "png"));
56 assertEqual("foo/data/images/bar.png", f("foo/@bar", "images", "png"));
57 }});
58 it("expands yuu/@ paths", function () { with (this) {
59 assertEqual(yuu.PATH + "data/images/bar.png",
60 f("yuu/@bar", "images", "png"));
61 }});
62 }});
63
64 JS.Test.describe('lerping', function () { with (this) {
65 it("lerps numbers", function () { with (this) {
66 assertEqual(0.0, (0).lerp(1, 0.0));
67 assertEqual(0.5, (0).lerp(1, 0.5));
68 assertEqual(1.0, (0).lerp(1, 1.0));
69 }});
70
71 var arrayTypes = {
72 "untyped": Array,
73 "float32": Float32Array,
74 "float64": Float64Array,
75 "int8": Int8Array,
76 "uint8": Uint8Array,
77 "int16": Int16Array,
78 "uint16": Uint16Array,
79 "int32": Int32Array,
80 "uint32": Uint32Array
81 };
82
83 Object.keys(arrayTypes).forEach(function (name) {
84 var A = arrayTypes[name];
85 it("lerps " + name + " arrays element-wise", function () { with (this) {
86 var a = new A([0, 0, 0]);
87 var b = new A([0, 10, 20]);
88 assertEqual(a, a.lerp(b, 0));
89 assertEqual(b, a.lerp(b, 1));
90 assertEqual(new A([0, 5, 10]), a.lerp(b, 0.5));
91 }});
92 });
93
94 Object.keys(arrayTypes).forEach(function (name) {
95 var A = arrayTypes[name];
96 it("slices " + name + " arrays", function () { with (this) {
97 var a = new A([0, 1, 2, 3, 4, 5]);
98 var b = a.slice();
99 assertEqual(a, b);
100 assertKindOf(A, b);
101 assertNotSame(a, b);
102 }});
103 });
104 }});
105 }});