/
test_element.js
107 lines (100 loc) · 3.33 KB
/
test_element.js
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
var vows = require('vows'),
assert = require('assert'),
ltx = require('./../lib/index');
vows.describe('ltx').addBatch({
'serialization': {
'serialize an element': function() {
var e = new ltx.Element('e');
assert.equal(e.toString(), '<e/>');
},
'serialize an element with attributes': function() {
var e = new ltx.Element('e',
{ a1: 'foo' });
assert.equal(e.toString(), '<e a1="foo"/>');
},
'serialize an element with attributes to entities': function() {
var e = new ltx.Element('e',
{ a1: '"well"' });
assert.equal(e.toString(), '<e a1=""well""/>');
},
'serialize an element with text': function() {
var e = new ltx.Element('e').t('bar').root();
assert.equal(e.toString(), '<e>bar</e>');
},
'serialize an element with text to entities': function() {
var e = new ltx.Element('e').t('1 < 2').root();
assert.equal(e.toString(), '<e>1 < 2</e>');
},
'serialize an element with a number attribute': function() {
var e = new ltx.Element('e', { a: 23 });
assert.equal(e.toString(), '<e a="23"/>');
},
'serialize an element with number contents': function() {
var e = new ltx.Element('e').t(23);
assert.equal(e.toString(), '<e>23</e>');
}
},
'remove': {
'by element': function() {
var el = new ltx.Element('e').
c('c').c('x').up().up().
c('c2').up().
c('c').up();
el.remove(el.getChild('c'));
assert.equal(el.getChildren('c').length, 1);
assert.equal(el.getChildren('c2').length, 1);
},
'by name': function() {
var el = new ltx.Element('e').
c('c').up().
c('c2').up().
c('c').up();
el.remove('c');
assert.equal(el.getChildren('c').length, 0);
assert.equal(el.getChildren('c2').length, 1);
}
},
'clone': {
'clones': function() {
var orig = new ltx.Element('msg', { type: 'get' }).
c('content').t('foo').root();
var clone = orig.clone();
assert.equal(clone.name, orig.name);
assert.equal(clone.attrs.type, orig.attrs.type);
assert.equal(clone.attrs.to, orig.attrs.to);
assert.equal(clone.children.length, orig.children.length);
assert.equal(clone.getChildText('content'), orig.getChildText('content'));
assert.equal(orig.getChild('content').up(), orig);
assert.equal(clone.getChild('content').up(), clone);
},
'mod attr': function() {
var orig = new ltx.Element('msg', { type: 'get' });
var clone = orig.clone();
clone.attrs.type += '-result';
assert.equal(orig.attrs.type, 'get');
assert.equal(clone.attrs.type, 'get-result');
},
'rm attr': function() {
var orig = new ltx.Element('msg', { from: 'me' });
var clone = orig.clone();
delete clone.attrs.from;
clone.attrs.to = 'you';
assert.equal(orig.attrs.from, 'me');
assert.equal(orig.attrs.to, undefined);
assert.equal(clone.attrs.from, undefined);
assert.equal(clone.attrs.to, 'you');
},
'mod child': function() {
var orig = new ltx.Element('msg', { type: 'get' }).
c('content').t('foo').root();
var clone = orig.clone();
clone.getChild('content').
t('bar').
name = 'description';
assert.equal(orig.children[0].name, 'content');
assert.equal(orig.getChildText('content'), 'foo');
assert.equal(clone.children[0].name, 'description');
assert.equal(clone.getChildText('description'), 'foobar');
}
}
}).run();