Skip to content

Commit 2a24ec6

Browse files
committed
Have test() method automatically create and remove a Document for reach test.
1 parent df1c5cd commit 2a24ec6

12 files changed

Lines changed: 38 additions & 59 deletions

File tree

test/lib/helpers.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Let's be strict
2-
1+
// Override equals to convert functions to message and execute them as tests()
32
function equals(actual, expected, message) {
43
if (typeof actual === 'function') {
54
if (!message) {
@@ -15,9 +14,18 @@ function equals(actual, expected, message) {
1514
}
1615
actual = actual();
1716
}
17+
// Let's be strict
1818
return strictEqual(actual, expected, message);
1919
}
2020

21+
function test(testName, expected) {
22+
return QUnit.test(testName, function() {
23+
var doc = new Document();
24+
expected();
25+
doc.remove();
26+
});
27+
}
28+
2129
function compareNumbers(number1, number2, message) {
2230
if (number1 !== 0)
2331
number1 = Math.round(number1 * 100) / 100;

test/tests/Color.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
module('Color');
22

33
test('Set named color', function() {
4-
var doc = new Document();
54
var path = new Path();
65
path.fillColor = 'red';
76
compareRGBColors(path.fillColor, new RGBColor(1, 0, 0));
87
equals(path.fillColor.toCssString(), 'rgba(255, 0, 0, 1)');
98
});
109

1110
test('Set color to hex', function() {
12-
var doc = new Document();
1311
var path = new Path();
1412
path.fillColor = '#ff0000';
1513
compareRGBColors(path.fillColor, new RGBColor(1, 0, 0));
@@ -22,7 +20,6 @@ test('Set color to hex', function() {
2220
});
2321

2422
test('Set color to object', function() {
25-
var doc = new Document();
2623
var path = new Path();
2724
path.fillColor = { red: 1, green: 0, blue: 1};
2825
compareRGBColors(path.fillColor, new RGBColor(1, 0, 1));
@@ -35,7 +32,6 @@ test('Set color to object', function() {
3532
});
3633

3734
test('Set color to array', function() {
38-
var doc = new Document();
3935
var path = new Path();
4036
path.fillColor = [1, 0, 0];
4137
compareRGBColors(path.fillColor, new RGBColor(1, 0, 0));

test/tests/Group.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
module('Group');
22

33
test('new Group()', function() {
4-
var doc = new Document();
54
var group = new Group();
65
equals(function() {
7-
return doc.activeLayer.children[0] == group;
6+
return paper.document.activeLayer.children[0] == group;
87
}, true);
98
});
109

1110
test('new Group([item])', function() {
12-
var doc = new Document();
1311
var path = new Path();
1412
var group = new Group([path]);
1513
equals(function() {
16-
return doc.activeLayer.children.length == 1;
17-
}, true);
14+
return paper.document.activeLayer.children.length;
15+
}, 1);
1816
equals(function() {
1917
return group.children[0] == path;
2018
}, true);
2119
});
2220

2321
test('Group bounds', function() {
24-
var doc = new Document();
25-
doc.currentStyle = {
22+
paper.document.currentStyle = {
2623
strokeWidth: 5,
2724
strokeColor: 'black'
2825
};

test/tests/Item.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module('Item');
22

33
test('copyTo(document)', function() {
4-
var doc = new Document();
4+
var doc = paper.document;
55
var path = new Path();
66
var secondDoc = new Document();
77
var copy = path.copyTo(secondDoc);
@@ -17,7 +17,7 @@ test('copyTo(document)', function() {
1717
});
1818

1919
test('copyTo(layer)', function() {
20-
var doc = new Document();
20+
var doc = paper.document;
2121
var path = new Path();
2222

2323
var layer = new Layer();
@@ -31,19 +31,19 @@ test('copyTo(layer)', function() {
3131
});
3232

3333
test('clone()', function() {
34-
var doc = new Document();
34+
var doc = paper.document;
3535
var path = new Path();
3636
var copy = path.clone();
3737
equals(function() {
38-
return doc.activeLayer.children.length == 2;
39-
}, true);
38+
return doc.activeLayer.children.length;
39+
}, 2);
4040
equals(function() {
4141
return path != copy;
4242
}, true);
4343
});
4444

4545
test('appendChild(item)', function() {
46-
var doc = new Document();
46+
var doc = paper.document;
4747
var path = new Path();
4848
doc.activeLayer.appendChild(path);
4949
equals(function() {
@@ -52,7 +52,7 @@ test('appendChild(item)', function() {
5252
});
5353

5454
test('item.parent / item.isChild / item.isParent', function() {
55-
var doc = new Document();
55+
var doc = paper.document;
5656
var secondDoc = new Document();
5757
var path = new Path();
5858
doc.activeLayer.appendChild(path);
@@ -81,7 +81,7 @@ test('item.parent / item.isChild / item.isParent', function() {
8181
});
8282

8383
test('item.lastChild / item.firstChild', function() {
84-
var doc = new Document();
84+
var doc = paper.document;
8585
var path = new Path();
8686
var secondPath = new Path();
8787
equals(function() {
@@ -93,7 +93,7 @@ test('item.lastChild / item.firstChild', function() {
9393
});
9494

9595
test('appendBottom(item)', function() {
96-
var doc = new Document();
96+
var doc = paper.document;
9797
var path = new Path();
9898
var secondPath = new Path();
9999
doc.activeLayer.appendBottom(secondPath);
@@ -103,7 +103,7 @@ test('appendBottom(item)', function() {
103103
});
104104

105105
test('moveAbove(item)', function() {
106-
var doc = new Document();
106+
var doc = paper.document;
107107
var path = new Path();
108108
var secondPath = new Path();
109109
path.moveAbove(secondPath);
@@ -113,7 +113,7 @@ test('moveAbove(item)', function() {
113113
});
114114

115115
test('moveBelow(item)', function() {
116-
var doc = new Document();
116+
var doc = paper.document;
117117
var firstPath = new Path();
118118
var secondPath = new Path();
119119
equals(function() {
@@ -126,7 +126,7 @@ test('moveBelow(item)', function() {
126126
});
127127

128128
test('isDescendant(item) / isAncestor(item)', function() {
129-
var doc = new Document();
129+
var doc = paper.document;
130130
var path = new Path();
131131
equals(function() {
132132
return path.isDescendant(doc.activeLayer);
@@ -152,7 +152,7 @@ test('isDescendant(item) / isAncestor(item)', function() {
152152
});
153153

154154
test('isGroupedWith', function() {
155-
var doc = new Document();
155+
var doc = paper.document;
156156
var path = new Path();
157157
var secondPath = new Path();
158158
var group = new Group([path]);
@@ -191,7 +191,6 @@ test('isGroupedWith', function() {
191191
});
192192

193193
test('getPreviousSibling() / getNextSibling()', function() {
194-
var doc = new Document();
195194
var firstPath = new Path();
196195
var secondPath = new Path();
197196
equals(function() {
@@ -206,7 +205,6 @@ test('getPreviousSibling() / getNextSibling()', function() {
206205
});
207206

208207
test('hidden', function() {
209-
var doc = new Document();
210208
var firstPath = new Path();
211209
firstPath.visible = false;
212210
equals(function() {
@@ -215,7 +213,7 @@ test('hidden', function() {
215213
});
216214

217215
test('reverseChildren()', function() {
218-
var doc = new Document();
216+
var doc = paper.document;
219217
var path = new Path();
220218
var secondPath = new Path();
221219
var thirdPath = new Path();
@@ -235,6 +233,7 @@ test('reverseChildren()', function() {
235233
});
236234

237235
test('Check item#document when moving items across documents', function() {
236+
var doc = paper.document;
238237
var doc1 = new Document();
239238
var path = new Path();
240239
var group = new Group();
@@ -256,7 +255,6 @@ test('Check item#document when moving items across documents', function() {
256255
});
257256

258257
test('group.selected', function() {
259-
var doc = new Document();
260258
var path = new Path([0, 0]);
261259
var path2 = new Path([0, 0]);
262260
var group = new Group([path, path2]);

test/tests/Layer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module('Layer');
22

33
test('previousSibling / nextSibling', function() {
4-
var doc = new Document();
4+
var doc = paper.document;
55
var firstLayer = doc.activeLayer;
66
var secondLayer = new Layer();
77
equals(function() {
@@ -40,7 +40,7 @@ test('previousSibling / nextSibling', function() {
4040
});
4141

4242
test('moveAbove / moveBelow', function() {
43-
var doc = new Document();
43+
var doc = paper.document;
4444
var firstLayer = doc.activeLayer;
4545
var secondLayer = new Layer();
4646
secondLayer.moveBelow(firstLayer);

test/tests/Path.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module('Path');
22

33
test('path.join(path)', function() {
4-
var doc = new Document();
54
var path = new Path();
65
path.add(0, 0);
76
path.add(10, 0);
@@ -13,7 +12,7 @@ test('path.join(path)', function() {
1312
path.join(path2);
1413
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }');
1514
equals(function() {
16-
return doc.activeLayer.children.length;
15+
return paper.document.activeLayer.children.length;
1716
}, 1);
1817

1918
var path = new Path();
@@ -55,7 +54,6 @@ test('path.join(path)', function() {
5554
});
5655

5756
test('path.remove()', function() {
58-
var doc = new Document();
5957
var path = new Path();
6058
path.add(0, 0);
6159
path.add(10, 0);
@@ -80,33 +78,31 @@ test('path.remove()', function() {
8078
path.remove();
8179

8280
equals(function() {
83-
return doc.activeLayer.children.length;
81+
return paper.document.activeLayer.children.length;
8482
}, 0);
8583
});
8684

8785

8886
test('Is the path deselected after setting a new list of segments?', function() {
89-
var doc = new Document();
9087
var path = new Path([0, 0]);
9188
path.selected = true;
9289
equals(function() {
9390
return path.selected;
9491
}, true);
9592
equals(function() {
96-
return doc.selectedItems.length;
93+
return paper.document.selectedItems.length;
9794
}, 1);
9895

9996
path.segments = [[0, 10]];
10097
equals(function() {
10198
return path.selected;
10299
}, false);
103100
equals(function() {
104-
return doc.selectedItems.length;
101+
return paper.document.selectedItems.length;
105102
}, 0);
106103
});
107104

108105
test('Path#reverse', function() {
109-
var doc = new Document();
110106
var path = new Path.Circle([100, 100], 30);
111107
path.reverse();
112108
equals(path.segments.toString(), '{ point: { x: 100, y: 130 }, handleIn: { x: -16.56854, y: 0 }, handleOut: { x: 16.56854, y: 0 } },{ point: { x: 130, y: 100 }, handleIn: { x: 0, y: 16.56854 }, handleOut: { x: 0, y: -16.56854 } },{ point: { x: 100, y: 70 }, handleIn: { x: 16.56854, y: 0 }, handleOut: { x: -16.56854, y: 0 } },{ point: { x: 70, y: 100 }, handleIn: { x: 0, y: -16.56854 }, handleOut: { x: 0, y: 16.56854 } }');

test/tests/PathStyle.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
module('Path Style');
22

33
test('currentStyle', function() {
4-
var doc = new Document();
5-
doc.currentStyle.fillColor = 'black';
4+
paper.document.currentStyle.fillColor = 'black';
65
var path = new Path();
76
compareRGBColors(path.fillColor, 'black', 'path.fillColor');
87

98
// When changing the current style of the document, the style of
109
// paths created using document.currentStyle should not change.
11-
doc.currentStyle.fillColor = 'red';
10+
paper.document.currentStyle.fillColor = 'red';
1211
compareRGBColors(path.fillColor, 'black', 'path.fillColor');
1312
});
1413

1514
test('setting currentStyle to an object', function() {
16-
var doc = new Document();
17-
doc.currentStyle = {
15+
paper.document.currentStyle = {
1816
fillColor: 'red',
1917
strokeColor: 'green'
2018
};
@@ -24,7 +22,6 @@ test('setting currentStyle to an object', function() {
2422
});
2523

2624
test('setting path styles to an object', function() {
27-
var doc = new Document();
2825
var path = new Path();
2926
path.style = {
3027
fillColor: 'red',
@@ -35,7 +32,6 @@ test('setting path styles to an object', function() {
3532
});
3633

3734
test('setting group styles to an object', function() {
38-
var doc = new Document();
3935
var group = new Group();
4036
var path = new Path();
4137
group.appendTop(path);
@@ -48,7 +44,6 @@ test('setting group styles to an object', function() {
4844
});
4945

5046
test('getting group styles', function() {
51-
var doc = new Document();
5247
var group = new Group();
5348
var path = new Path();
5449
path.fillColor = 'red';
@@ -72,7 +67,6 @@ test('getting group styles', function() {
7267
});
7368

7469
test('setting group styles', function() {
75-
var doc = new Document();
7670
var group = new Group();
7771
var path = new Path();
7872
path.fillColor = 'red';
@@ -96,7 +90,6 @@ test('setting group styles', function() {
9690
});
9791

9892
test('setting group styles 2', function() {
99-
var doc = new Document();
10093
var group = new Group();
10194
var path = new Path();
10295
path.fillColor = 'red';

test/tests/Path_Bounds.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module('Path Bounds');
22

33
test('path.bounds', function() {
4-
var doc = new Document();
54
var path = new Path([
65
new Segment(new Point(121, 334), new Point(-19, 38), new Point(30.7666015625, -61.53369140625)),
76
new Segment(new Point(248, 320), new Point(-42, -74), new Point(42, 74)),

test/tests/Path_Curves.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module('Path Curves');
22

33
test('path.curves Synchronisation', function() {
4-
5-
var doc = new Document();
64
var path = new Path();
75

86
path.add(new Point(0, 100));

0 commit comments

Comments
 (0)