Merge lp:~jjacobs/methanal/util-tests into lp:methanal

Proposed by Jonathan Jacobs
Status: Merged
Approved by: Tristan Seligmann
Approved revision: 112
Merged at revision: not available
Proposed branch: lp:~jjacobs/methanal/util-tests
Merge into: lp:methanal
Diff against target: 454 lines
5 files modified
methanal/js/Methanal/Tests/DOMUtil.js (+0/-75)
methanal/js/Methanal/Tests/MockBrowser.js (+102/-0)
methanal/js/Methanal/Tests/TestUtil.js (+178/-5)
methanal/js/Methanal/Tests/TestView.js (+2/-2)
methanal/js/Methanal/Util.js (+16/-7)
To merge this branch: bzr merge lp:~jjacobs/methanal/util-tests
Reviewer Review Type Date Requested Status
Tristan Seligmann Approve
Review via email: mp+13234@code.launchpad.net

This proposal supersedes a proposal from 2009-10-12.

To post a comment you must log in.
Revision history for this message
Tristan Seligmann (mithrandi) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== removed file 'methanal/js/Methanal/Tests/DOMUtil.js'
--- methanal/js/Methanal/Tests/DOMUtil.js 2009-10-10 10:28:01 +0000
+++ methanal/js/Methanal/Tests/DOMUtil.js 1970-01-01 00:00:00 +0000
@@ -1,75 +0,0 @@
1// import Divmod.MockBrowser
2
3
4
5/**
6 * HTMLSelectElement mock implementation.
7 */
8Divmod.MockBrowser.Element.subclass(
9 Methanal.Tests.DOMUtil, 'MockHTMLSelectElement').methods(
10 function __init__(self) {
11 Methanal.Tests.DOMUtil.MockHTMLSelectElement.upcall(
12 self, '__init__', 'select');
13 self.options = [];
14 },
15
16
17 /**
18 * Add a new element to the collection of OPTION elements for this SELECT.
19 */
20 function add(self, element, before) {
21 var index = Methanal.Util.arrayIndexOf(self.options, before);
22 if (index == -1) {
23 index = self.options.length;
24 self.options.push(element);
25 } else {
26 self.options.splice(index, 0, element);
27 }
28 element.index = index;
29 });
30
31
32
33/**
34 * HTMLDocument mock implementation.
35 */
36Divmod.MockBrowser.Document.subclass(
37 Methanal.Tests.DOMUtil, 'Document').methods(
38 function __init__(self) {
39 self._elementTags = {};
40 Methanal.Tests.DOMUtil.Document.upcall(self, '__init__');
41 },
42
43
44 /**
45 * Register a new type for a tag name.
46 */
47 function registerElementTag(self, tagName, type) {
48 var old = self._elementTags[tagName];
49 self._elementTags[tagName] = type;
50 return old;
51 },
52
53
54 function createElement(self, tagName) {
55 var el;
56 if (self._elementTags[tagName]) {
57 el = self._elementTags[tagName]();
58 } else {
59 el = Divmod.MockBrowser.Element(tagName);
60 }
61 el._setOwnerDocument(self);
62 self._allElements.push(el);
63 return el;
64 });
65
66
67
68// Only override Divmod's mock document.
69if (document instanceof Divmod.MockBrowser.Document) {
70 if (!(document instanceof Methanal.Tests.DOMUtil.Document)) {
71 document = Methanal.Tests.DOMUtil.Document();
72 document.registerElementTag(
73 'select', Methanal.Tests.DOMUtil.MockHTMLSelectElement);
74 }
75}
760
=== added file 'methanal/js/Methanal/Tests/MockBrowser.js'
--- methanal/js/Methanal/Tests/MockBrowser.js 1970-01-01 00:00:00 +0000
+++ methanal/js/Methanal/Tests/MockBrowser.js 2009-10-12 17:10:19 +0000
@@ -0,0 +1,102 @@
1// import Divmod.MockBrowser
2
3
4
5Divmod.MockBrowser.Element.subclass(
6 Methanal.Tests.MockBrowser, 'Element').methods(
7 function __init__(self, tagName) {
8 Methanal.Tests.MockBrowser.Element.upcall(self, '__init__', tagName);
9 self._updateChildProperties();
10 },
11
12
13 function _updateChildProperties(self) {
14 self.firstChild = self.childNodes[0] || null;
15 self.lastChild = self.childNodes[self.childNodes.length - 1] || null;
16 },
17
18
19 function appendChild(self, child) {
20 Methanal.Tests.MockBrowser.Element.upcall(self, 'appendChild', child);
21 self._updateChildProperties();
22 },
23
24
25 function removeChild(self, child) {
26 Methanal.Tests.MockBrowser.Element.upcall(self, 'removeChild', child);
27 self._updateChildProperties();
28 });
29
30
31
32/**
33 * HTMLSelectElement mock implementation.
34 */
35Methanal.Tests.MockBrowser.Element.subclass(
36 Methanal.Tests.MockBrowser, 'MockHTMLSelectElement').methods(
37 function __init__(self) {
38 Methanal.Tests.MockBrowser.MockHTMLSelectElement.upcall(
39 self, '__init__', 'select');
40 self.options = [];
41 },
42
43
44 /**
45 * Add a new element to the collection of OPTION elements for this SELECT.
46 */
47 function add(self, element, before) {
48 var index = Methanal.Util.arrayIndexOf(self.options, before);
49 if (index == -1) {
50 index = self.options.length;
51 self.options.push(element);
52 } else {
53 self.options.splice(index, 0, element);
54 }
55 element.index = index;
56 });
57
58
59
60/**
61 * HTMLDocument mock implementation.
62 */
63Divmod.MockBrowser.Document.subclass(
64 Methanal.Tests.MockBrowser, 'Document').methods(
65 function __init__(self) {
66 self._elementTags = {};
67 Methanal.Tests.MockBrowser.Document.upcall(self, '__init__');
68 },
69
70
71 /**
72 * Register a new type for a tag name.
73 */
74 function registerElementTag(self, tagName, type) {
75 var old = self._elementTags[tagName];
76 self._elementTags[tagName] = type;
77 return old;
78 },
79
80
81 function createElement(self, tagName) {
82 var el;
83 if (self._elementTags[tagName]) {
84 el = self._elementTags[tagName]();
85 } else {
86 el = Methanal.Tests.MockBrowser.Element(tagName);
87 }
88 el._setOwnerDocument(self);
89 self._allElements.push(el);
90 return el;
91 });
92
93
94
95// Only override Divmod's mock document.
96if (document instanceof Divmod.MockBrowser.Document) {
97 if (!(document instanceof Methanal.Tests.MockBrowser.Document)) {
98 document = Methanal.Tests.MockBrowser.Document();
99 document.registerElementTag(
100 'select', Methanal.Tests.MockBrowser.MockHTMLSelectElement);
101 }
102}
0103
=== modified file 'methanal/js/Methanal/Tests/TestUtil.js'
--- methanal/js/Methanal/Tests/TestUtil.js 2009-10-10 10:28:01 +0000
+++ methanal/js/Methanal/Tests/TestUtil.js 2009-10-12 17:10:19 +0000
@@ -1,11 +1,184 @@
1// import Divmod.UnitTest1// import Divmod.UnitTest
2// import Divmod.MockBrowser
3// import Methanal.Util2// import Methanal.Util
4// import Methanal.Tests.Util3// import Methanal.Tests.Util
54// import Methanal.Tests.MockBrowser
65
76
8Divmod.UnitTest.TestCase.subclass(Methanal.Tests.TestUtil, 'TestUtil').methods(7
8Methanal.Tests.Util.TestCase.subclass(
9 Methanal.Tests.TestUtil, 'TestUtil').methods(
10 /**
11 * L{Methanal.Util.addElementClass} and L{Methanal.Util.removeElementClass}
12 * add and remove values to an element's C{className} attribute.
13 */
14 function test_addRemoveElementClass(self) {
15 var node = document.createElement('div');
16 var addElementClass = Methanal.Util.addElementClass;
17 var removeElementClass = Methanal.Util.removeElementClass;
18
19 addElementClass(node, 'foo');
20 self.assertIdentical(node.className, 'foo');
21 addElementClass(node, 'foo');
22 self.assertIdentical(node.className, 'foo');
23
24 addElementClass(node, 'bar');
25 self.assertIdentical(node.className, 'foo bar');
26
27 removeElementClass(node, 'foo');
28 self.assertIdentical(node.className, 'bar');
29
30 removeElementClass(node, 'bar');
31 self.assertIdentical(node.className, '');
32
33 removeElementClass(node, 'bar');
34 self.assertIdentical(node.className, '');
35 },
36
37
38 /**
39 * Create a DOM node with some children.
40 */
41 function _makeNodeWithChildren(self) {
42 var T = Methanal.Util.DOMBuilder(document);
43 var node = T('div', {}, [
44 T('span', {}, ['hello ',
45 T('strong', {}, 'world')]),
46 T('em', {}, ['!'])]);
47 self.assertIdentical(node.childNodes.length, 2);
48 return node;
49 },
50
51
52 /**
53 * L{Methanal.Util.removeNodeContent} removes all of a node's children.
54 */
55 function test_removeNodeContent(self) {
56 var node = self._makeNodeWithChildren();
57 Methanal.Util.removeNodeContent(node);
58 self.assertIdentical(node.childNodes.length, 0);
59 },
60
61
62 /**
63 * L{Methanal.Util.replaceNodeContent} replaces a node's children with
64 * some other children.
65 */
66 function test_replaceNodeContent(self) {
67 var T = Methanal.Util.DOMBuilder(document);
68 var node = self._makeNodeWithChildren();
69 var children = [T('strong', {}, ['yay'])];
70 Methanal.Util.replaceNodeContent(node, children);
71 self.assertArraysEqual(node.childNodes, children);
72 },
73
74
75 /**
76 * L{Methanal.Util.replaceNodeText} replaces a node's content with a text
77 * node.
78 */
79 function test_replaceNodeText(self) {
80 var node = self._makeNodeWithChildren();
81 Methanal.Util.replaceNodeText(node, 'hey');
82 self.assertIdentical(node.childNodes.length, 1);
83 self.assertIsInstanceOf(node.firstChild, Divmod.MockBrowser.TextNode);
84 },
85
86
87 /**
88 * L{Methanal.Util.formatDecimal} pretty-prints a number with thousand
89 * separators.
90 */
91 function test_formatDecimal(self) {
92 var formatDecimal = Methanal.Util.formatDecimal;
93 self.assertIdentical(formatDecimal(1), '1');
94 self.assertIdentical(formatDecimal(100), '100');
95 self.assertIdentical(formatDecimal(1000), '1,000');
96 self.assertIdentical(formatDecimal(10000), '10,000');
97 self.assertIdentical(formatDecimal(1000000), '1,000,000');
98
99 self.assertIdentical(formatDecimal(1000.25), '1,000.25');
100 self.assertIdentical(formatDecimal(1000000.66), '1,000,000.66');
101 },
102
103
104 /**
105 * L{Methanal.Util.cycle} produces a callable that iterates through
106 * the original arguments indefinitely.
107 */
108 function test_cycle(self) {
109 var cycler = Methanal.Util.cycle(42, 5144, 'a');
110 self.assertIdentical(cycler(), 42);
111 self.assertIdentical(cycler(), 5144);
112 self.assertIdentical(cycler(), 'a');
113 self.assertIdentical(cycler(), 42);
114 },
115
116
117 /**
118 * L{Methanal.Util.arrayIndexOf} finds the first index of an element in an
119 * array, or C{-1} if no such element exists.
120 */
121 function test_arrayIndexOf(self) {
122 var arrayIndexOf = Methanal.Util.arrayIndexOf;
123 var a = ['x', 'y', 'z', 'x'];
124 self.assertIdentical(arrayIndexOf(a, 'x'), 0);
125 self.assertIdentical(arrayIndexOf(a, 'y'), 1);
126 self.assertIdentical(arrayIndexOf(a, 'z'), 2);
127 self.assertIdentical(arrayIndexOf(a, 'a'), -1);
128 },
129
130
131 /**
132 * L{Methanal.Util.nodeInserted} calls a widget's C{nodeInserted} method
133 * and the C{nodeInserted} method of each child widget.
134 */
135 function test_nodeInserted(self) {
136 function makeWidget(childWidgets) {
137 return {
138 'childWidgets': childWidgets,
139 'nodeInserted': function () {
140 this.nodeInserted = true;
141 }};
142 };
143
144 var childWidgets = [];
145 for (var i = 0; i < 5; ++i) {
146 childWidgets.push(makeWidget([]));
147 }
148 var widget = makeWidget(childWidgets);
149
150 Methanal.Util.nodeInserted(widget);
151 self.assertIdentical(widget.nodeInserted, true);
152 for (var i = 0; i < childWidgets.length; ++i) {
153 self.assertIdentical(childWidgets[i].nodeInserted, true);
154 }
155 },
156
157
158 /**
159 * L{Methanal.Util.repr} converts an object to a human-readable string.
160 */
161 function test_repr(self) {
162 var repr = Methanal.Util.repr;
163 self.assertIdentical(repr('a'), '"a"');
164 self.assertIdentical(repr('a"b"c'), '"a\\"b\\"c"');
165 self.assertIdentical(repr(1), '1');
166 self.assertIdentical(repr(null), 'null');
167 self.assertIdentical(repr(undefined), 'undefined');
168 self.assertIdentical(repr(repr), '<function repr>');
169 var a = [1, 2, 3, 'a', ['b', '"c"']];
170 self.assertIdentical(repr(a), '[1, 2, 3, "a", ["b", "\\"c\\""]]');
171 var o = {a: 1};
172 self.assertIdentical(repr(o), '{"a": 1}');
173 var o2 = {a: 1, b: 2};
174 self.assertIdentical(repr(o2), '{"a": 1, "b": 2}');
175
176 var type = Divmod.Class.subclass('Foo');
177 var i = type();
178 self.assertIdentical(repr(i), i.toString());
179 },
180
181
9 /**182 /**
10 * L{Methanal.Util.strToInt} converts a base-10 integer value, represented183 * L{Methanal.Util.strToInt} converts a base-10 integer value, represented
11 * as a C{String}, to an C{Integer}.184 * as a C{String}, to an C{Integer}.
12185
=== modified file 'methanal/js/Methanal/Tests/TestView.js'
--- methanal/js/Methanal/Tests/TestView.js 2009-10-10 10:28:01 +0000
+++ methanal/js/Methanal/Tests/TestView.js 2009-10-12 17:10:19 +0000
@@ -3,7 +3,7 @@
3// import Methanal.View3// import Methanal.View
4// import Methanal.Util4// import Methanal.Util
5// import Methanal.Tests.Util5// import Methanal.Tests.Util
6// import Methanal.Tests.DOMUtil6// import Methanal.Tests.MockBrowser
77
88
99
@@ -374,7 +374,7 @@
374/**374/**
375 * Mimic Internet Explorer's broken C{HTMLSelectElement.add} behaviour.375 * Mimic Internet Explorer's broken C{HTMLSelectElement.add} behaviour.
376 */376 */
377Methanal.Tests.DOMUtil.MockHTMLSelectElement.subclass(377Methanal.Tests.MockBrowser.MockHTMLSelectElement.subclass(
378 Methanal.Tests.TestView, 'MockIEHTMLSelectElement').methods(378 Methanal.Tests.TestView, 'MockIEHTMLSelectElement').methods(
379 function add(self, element, before) {379 function add(self, element, before) {
380 if (before === null) {380 if (before === null) {
381381
=== modified file 'methanal/js/Methanal/Util.js'
--- methanal/js/Methanal/Util.js 2009-10-10 10:28:01 +0000
+++ methanal/js/Methanal/Util.js 2009-10-12 17:10:19 +0000
@@ -1,7 +1,3 @@
1// import Nevow.Athena
2
3
4
5/**1/**
6 * Add a class to an element's "className" attribute.2 * Add a class to an element's "className" attribute.
7 *3 *
@@ -95,7 +91,7 @@
95 *91 *
96 * @type node: DOM node92 * @type node: DOM node
97 *93 *
98 * @type children: C{Array}94 * @type children: C{Array} of C{DOM nodes}
99 */95 */
100Methanal.Util.replaceNodeContent = function replaceNodeContent(node, children) {96Methanal.Util.replaceNodeContent = function replaceNodeContent(node, children) {
101 Methanal.Util.removeNodeContent(node);97 Methanal.Util.removeNodeContent(node);
@@ -107,7 +103,7 @@
107103
108104
109/**105/**
110 * Replace a node's text node with new text.106 * Replace all of a node's content with text.
111 *107 *
112 * @type node: DOM node108 * @type node: DOM node
113 *109 *
@@ -308,7 +304,20 @@
308 return '<function ' + o.name + '>';304 return '<function ' + o.name + '>';
309 }305 }
310 } else if (o instanceof Array) {306 } else if (o instanceof Array) {
311 return o.toSource();307 var names = Methanal.Util.map(Methanal.Util.repr, o);
308 return '[' + names.join(', ') + ']';
309 } else {
310 if (o.constructor !== undefined && o.constructor.name !== undefined) {
311 var typeName = o.constructor.name;
312 if (typeName == 'Object') {
313 var names = [];
314 var repr = Methanal.Util.repr;
315 for (var key in o) {
316 names.push(repr(key) + ': ' + repr(o[key]));
317 }
318 return '{' + names.join(', ') + '}';
319 }
320 }
312 }321 }
313 return o.toString();322 return o.toString();
314};323};

Subscribers

People subscribed via source and target branches