-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.html
195 lines (171 loc) · 7.81 KB
/
unit.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>OrgJS : unit tests</title>
<script type='text/javascript' src='script/jquery-1.5.1.js'></script>
<link rel="stylesheet" href="test/lib/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="test/lib/qunit.js"></script>
<script type='text/javascript' src='script/org.js'></script>
</head>
<body>
<h1 id="qunit-header">QUnit tests for OrgJS</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
</div>
<script type="text/javascript">
var _U = new Org().Utils;
$(function(){
test("UTILS - fillParagraph", function(){
var test = "After reviewing the comments, I finally made it without enclosing each character in span tag. I have made two spans. One for typed characters and other for current character. On each keyup event, I update the div content by replacing the current HTML with updated HTML. Below is the basic logic I am using:";
var width = 50;
var result = _U.fillParagraph(test, width);
var lines = _U.lines(result);
equal(7, lines.length, "Number of split lines");
equal("After reviewing the comments, I finally made it", lines[0], "First line");
equal("am using:", lines[6], "Last line");
_U.each(lines, function(l){
ok(l.length <= width, "Line less than "+width+" chars long: " + l.length);
});
});
test("UTILS - indent", function(){
var test = _U.fillParagraph("After reviewing the comments, I finally made it without enclosing each character in span tag. I have made two spans. One for typed characters and other for current character. On each keyup event, I update the div content by replacing the current HTML with updated HTML. Below is the basic logic I am using:", 50);
var result = _U.indent(test, 4, " + ");
console.log(result);
var lines = _U.lines(result);
equal(" + After reviewing the comments, I finally made it", lines[0], "First line");
equal(" am using:", lines[6], "Last line");
_U.each(lines, function(l, idx){
if(idx === 0){return;}
ok(/^ {4}/.exec(l), "Line is indented with 4 spaces");
});
});
test("UTILS - each", function(){
var arr = [1,2,3];
var resultArr = [];
_U.each(arr, function(v, idx){resultArr.push(v*v);});
deepEqual(resultArr, [1,4,9], "Did not square the array!");
var obj = {a: 1, b: 2, c: 3};
var resultObj = [];
_U.each(obj, function(v, key){resultObj.push(v*v);});
deepEqual(resultObj, [1,4,9], "Did not square the object!");
});
test("UTILS - map", function(){
var arr = [1,2,3];
var resultArr = _U.map(arr, function(val, idx){return "" + idx + val;});
deepEqual(resultArr, ["01","12","23"], "Error mapping array to array");
});
test("UTILS - lines", function(){
deepEqual(_U.lines(null), [], "null case");
deepEqual(_U.lines("false"), ["false"], "false case");
deepEqual(_U.lines("0"), ["0"], "0 case");
deepEqual(_U.lines(""), [""], "empty string has 1 line");
deepEqual(_U.lines("\n"), ["", ""], "2 empty lines");
deepEqual(_U.lines("a\nb"), ["a", "b"], "2 lines");
deepEqual(_U.lines("a\n\nb"), ["a", "", "b"], "3 lines");
});
test("UTILS - range", function(){
deepEqual(_U.range(3), [0, 1, 2], "1 argument positive");
deepEqual(_U.range(-3), [0, -1, -2], "1 argument negative");
deepEqual(_U.range(1, 4), [1, 2, 3], "2 args incr");
deepEqual(_U.range(0, 5, 2), [0, 2, 4], "3 args");
deepEqual(_U.range(2, 0), [2, 1], "2 args decr");
deepEqual(_U.range(0, 5, -2), [], "3 args empty");
});
test("UTILS - trim", function(){
equal(_U.trim(" \r \t \n "), "", "only whitespaces");
equal(_U.trim("a b\n "), "a b", "after");
equal(_U.trim(" \ra b"), "a b", "before");
equal(_U.trim(" \r a b \n "), "a b", "both");
equal(_U.trim(null), "", "null case");
});
test("UTILS - unquote", function(){
equal(_U.unquote("'try'"), "try", "single quotes");
equal(_U.unquote('"try"'), "try", "double quotes");
equal(_U.unquote("'try"), "'try", "no ending quote");
equal(_U.unquote("'tr'y"), "'tr'y", "ending quote not at the end");
equal(_U.unquote(null), "", "null case");
});
test("UTILS - empty", function(){
equal(_U.empty([]), true, "0-element array");
equal(_U.empty(""), true, "0-size string");
equal(_U.empty(" "), false, "blank string");
equal(_U.empty([1]), false, "1-element array");
});
test("UTILS - blank", function(){
equal(_U.blank(" \t \n "), true, "blank string");
equal(_U.blank([]), true, "0-element array");
equal(_U.blank("aze"), false, "not blank string");
equal(_U.blank([1]), false, "1-element array");
});
test("UTILS - repeat", function(){
equal(_U.repeat(" ", 3), " ", "repeat white space");
equal(_U.repeat("ab", 2), "abab", "repeat string");
});
test("UTILS - firstlLine", function(){
equal(_U.firstLine(" a\t\nb\bc"), " a\t", "first line");
equal(_U.firstLine("\r\nb\bc"), "", "empty first line - windows format");
equal(_U.firstLine("\nb\bc"), "", "empty first line - unix format");
});
test("UTILS - lines", function(){
deepEqual(_U.lines(" a\t\nb\nc"), [" a\t", "b", "c"] , "lines - unix format");
deepEqual(_U.lines(" a\t\nb\r\nc"), [" a\t", "b", "c"] , "lines - mixed formats");
deepEqual(_U.lines(" a\t\r\nb\r\nc"), [" a\t", "b", "c"] , "lines - windows formats");
});
test("UTILS - keys", function(){
var keys;
keys = _U.keys([1]);
equal(keys.length, 1);
equal(keys[0], 0);
keys = _U.keys({a:1, b:2});
equal(keys.length, 2);
equal(keys[0], "a");
equal(keys[1], "b");
});
test("UTILS - joinKeys", function(){
var keys = _U.joinKeys({a:1, b:2}, "|");
equal(keys, "a|b", "joining object properties");
});
test("UTILS - path.parent", function(){
equal(_U.path.parent("a/b/c.txt"), "a/b/", "parent of a file");
equal(_U.path.parent("a/b/c/"), "a/b/", "parent of a folder");
equal(_U.path.parent("a/b/c"), "a/b/", "parent");
});
test("UTILS - path.concat", function(){
equal(_U.path.concat("a","/b/","c.txt"), "a/b/c.txt");
equal(_U.path.concat("a/b", "c/"), "a/b/c/");
equal(_U.path.concat("a/", "//b/", "/c"), "a/b/c");
});
test("UTILS - TreeNode", function(){
// Reset incrementing for IDs
_U.incr = _U.incrementor();
// Getting the IDs of an array of nodes
var getIds = function(arr){
return _U.map(arr, function(v){return v.id;});
};
// Creating structure
var TreeNode = _U.TreeNode;
var root = new TreeNode();
var n1 = new TreeNode(root, {leaf:true});
var n2 = new TreeNode(root);
var n21 = new TreeNode(n2, {leaf:true}); n2.append(n21);
var n22 = new TreeNode(n2, {leaf:true}); n2.append(n22);
var n23 = new TreeNode(n2, {leaf:true}); n2.append(n23);
var n24 = new TreeNode(n2, {leaf:true}); n2.append(n24);
var n25 = new TreeNode(n2, {leaf:true}); n2.append(n25);
deepEqual([3, 1], getIds(n22.ancestors()) , "ancestors() not working");
deepEqual([4, 5, 6, 7, 8], getIds(n2.children), "children not working");
deepEqual([4, 5], getIds(n23.prevAll()) , "prevAll() not working");
deepEqual([5], getIds([n23.prev()]) , "prev() not working");
deepEqual([7, 8], getIds(n23.nextAll()) , "nextAll() not working");
deepEqual([7], getIds([n23.next()]) , "next() not working");
deepEqual([4, 5, 6, 7, 8], getIds(n23.siblingsAll()) , "siblingsAll() not working");
deepEqual([4, 5, 7, 8], getIds(n23.siblings()) , "siblings() not working");
});
});
</script>
</body>
</html>