-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.rb
126 lines (110 loc) · 3.76 KB
/
tests.rb
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
require 'rubygems'
require 'johnson'
require 'test/unit'
class BaconlTest < Test::Unit::TestCase
def setup
@context = Johnson::Runtime.new
@context.evaluate( open("./baconl.js").read )
end
def eval( val )
@context.evaluate( val )
end
def baconl( val )
eval( "baconl(\"#{val}\");" )
end
def node( definition )
eval( "baconl.node(\"#{definition}\"); ")
end
def test_simple_strings
assert_equal( "welcome mr. pope" , baconl('welcome mr. pope') )
assert_equal( "welcome\nmr. pope" , baconl('welcome\nmr. pope') )
assert_equal( "welcome\nmr.\npope" , baconl('welcome\nmr.\npope'))
end
def test_markup
assert_equal("<h1>The cat in the hat</h1>" , baconl('%h1 The cat in the hat') )
assert_equal("<h1>The cat in the hat</h1>\n<h2>by <span>Dr. Seuss</span></h2>" , baconl('%h1 The cat in the hat\n%h2 by \n %span Dr. Seuss') )
end
def test_attributes
assert_equal( "<h1 class='header'>The itinerant earthworm</h1>" , baconl("%h1.header The itinerant earthworm") )
assert_equal( "<h1 id='best-header' class='header'>The itinerant earthworm</h1>" , baconl("%h1#best-header.header The itinerant earthworm") )
assert_equal( "<h1 id='best-header' class='header'>The itinerant earthworm</h1>\n<h2 id='second-best-header'>I wish i was best</h2>" , baconl("%h1#best-header.header The itinerant earthworm\\n%h2#second-best-header I wish i was best") )
end
def test_construct_node
assert_equal( "<div></div>" , eval('baconl.node( "%div" ).html();') )
assert_equal( "<div></div>" , eval('baconl.node(baconl.node( "%div" )).html();') )
assert_equal( "<div>body</div>" , eval('baconl.node( "%div body" ).html();') )
assert_equal( "<div class='home'>body</div>" , eval('baconl.node( "%div.home body" ).html();') )
end
def test_prepend_node
doc = eval(%Q[
baconl.node( "%div" )
.prepend( baconl.node("%h1 Welcome!") )
.html();])
assert_equal( "<div><h1>Welcome!</h1></div>" , doc )
doc = eval(%Q[
baconl.node( "%div" )
.prepend( baconl.node("%h1 Second") )
.prepend( baconl.node("%h2 first") )
.html();])
assert_equal( "<div><h2>first</h2>\n<h1>Second</h1></div>" , doc )
end
def test_append_node
doc = eval(%Q[
baconl.node( "%div" )
.append( baconl.node("%h1 Welcome!") )
.html();])
assert_equal( "<div><h1>Welcome!</h1></div>" , doc )
end
def test_append_template
doc = eval(%Q[
baconl.node( "%div" ).append( "%h1 Welcome!" ).html();
])
assert_equal( "<div><h1>Welcome!</h1></div>" , doc )
end
def test_set_html
doc = eval(%Q[
var list = baconl.node("%ul");
list.html(
baconl.node("%li 1"),
baconl.node("%li 2"),
baconl.node("%li 3")
);
list.html();
])
assert_equal( "<ul><li>1</li>\n<li>2</li>\n<li>3</li></ul>" , doc )
end
def test_manipulate_classes
doc = eval(%Q[
var node = baconl.node("%div.cute");
node.addClass("left right").removeClass("right center").html();
])
assert_equal("<div class='cute left'></div>" , doc )
end
def test_remove_node
doc = eval(%Q[
var item = baconl.node("%li 1");
var list = baconl.node("%ul");
list.html(
item,
baconl.node("%li 2"),
baconl.node("%li 3")
);
item.remove();
list.html();
])
assert_equal( "<ul><li>2</li>\n<li>3</li></ul>" , doc )
end
def test_empty
doc = eval(%Q[
var list = baconl.node("%ul");
list.html(
baconl.node("%li 1"),
baconl.node("%li 2")
);
list.html();
])
assert_equal( "<ul><li>1</li>\n<li>2</li></ul>" , doc )
doc = eval("list.empty().html();")
assert_equal( "<ul></ul>" , doc )
end
end