/
hamlcoffee_spec.js.coffee
150 lines (118 loc) · 5.14 KB
/
hamlcoffee_spec.js.coffee
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
describe 'HAML', ->
describe '.escape', ->
it 'escapes an ampersand', ->
expect(HAML.escape('house & garden')).toEqual 'house & garden'
it 'escapes the less than sign', ->
expect(HAML.escape('money < fun')).toEqual 'money < fun'
it 'escapes the greater than sign', ->
expect(HAML.escape('love > hate')).toEqual 'love > hate'
it 'escapes a double quote', ->
expect(HAML.escape('Michael "netzpirat" Kessler')).toEqual 'Michael "netzpirat" Kessler'
it 'escapes a single quote', ->
expect(HAML.escape("Michael 'netzpirat' Kessler")).toEqual 'Michael 'netzpirat' Kessler'
describe '.cleanValue', ->
it 'returns an empty string for null', ->
expect(HAML.cleanValue(null)).toEqual ''
it 'returns an empty string for undefined', ->
expect(HAML.cleanValue(undefined)).toEqual ''
it 'marks a boolean true with unicode character u0093', ->
expect(HAML.cleanValue(true)).toEqual '\u0093true'
it 'marks a boolean true with unicode character u0093', ->
expect(HAML.cleanValue(false)).toEqual '\u0093false'
it 'passes everything else unchanged', ->
expect(HAML.cleanValue('Still the same')).toEqual 'Still the same'
describe '.extend', ->
it 'extends the given object from a source object', ->
object = HAML.extend {}, { a: 1, b: 2 }
expect(object.a).toEqual 1
expect(object.b).toEqual 2
it 'extends the given object from multipe source objects', ->
object = HAML.extend {}, { a: 1 }, { b: 2 }, { c: 3, d: 4 }
expect(object.a).toEqual 1
expect(object.b).toEqual 2
expect(object.c).toEqual 3
expect(object.d).toEqual 4
it 'overwrites existing properties', ->
object = HAML.extend {}, { a: 1 }, { b: 2 }, { a: 2, b: 4 }
expect(object.a).toEqual 2
expect(object.b).toEqual 4
describe '.globals', ->
it 'retuns the global object', ->
expect(HAML.globals).toEqual Object(HAML.globals)
it 'returns an empty object', ->
expect(Object.keys(HAML.globals).length).toEqual 0
describe '.context', ->
it 'merges the locals into the globals', ->
spyOn(HAML, 'globals').andCallFake -> { b: 2, d: 4 }
context = HAML.context({ a: 1, c: 3 })
expect(context.a).toEqual 1
expect(context.b).toEqual 2
expect(context.c).toEqual 3
expect(context.d).toEqual 4
describe '.preserve', ->
it 'preserves all newlines', ->
expect(HAML.preserve("Newlines\nall\nthe\nway\n")).toEqual "Newlines
all
the
way
"
describe '.findAndPreserve', ->
it 'replaces newlines within the preserved tags', ->
expect(HAML.findAndPreserve("""
<pre>
This will
be preserved
</pre>
<p>
This will not
be preserved
</p>
<textarea>
This will
be preserved
</textarea>
""")).toEqual """
<pre>
 This will
 be preserved
</pre>
<p>
This will not
be preserved
</p>
<textarea>
 This will
 be preserved
</textarea>
"""
describe '.surround', ->
it 'surrounds the text to the function result', ->
expect(HAML.surround('Prefix', 'Suffix', -> '<p>text</p>')).toEqual 'Prefix<p>text</p>Suffix'
describe '.succeed', ->
it 'appends the text to the function result', ->
expect(HAML.succeed('Suffix', -> '<p>text</p>')).toEqual '<p>text</p>Suffix'
describe '.precede', ->
it 'prepends the text to the function result', ->
expect(HAML.precede('Prefix', -> '<p>text</p>')).toEqual 'Prefix<p>text</p>'
describe '.reference', ->
describe 'class generation', ->
it 'uses the constructor name as name', ->
class MyUser
id: 42
expect(HAML.reference(new MyUser())).toEqual "class='my_user' id='my_user_42'"
it 'uses the custom name from #hamlObjectRef', ->
class MyUser
id: 23
hamlObjectRef: -> 'custom_name'
expect(HAML.reference(new MyUser())).toEqual "class='custom_name' id='custom_name_23'"
it 'defaults to `object` without consturctor name', ->
expect(HAML.reference({ id: 666 })).toEqual "class='object' id='object_666'"
it 'prepends a given prefix', ->
class MyUser
id: 42
expect(HAML.reference(new MyUser(), 'prefix')).toEqual "class='prefix_my_user' id='prefix_my_user_42'"
describe 'id generation', ->
it 'uses the object #id property', ->
class FromIdProp
id: 42
expect(HAML.reference(new FromIdProp())).toEqual "class='from_id_prop' id='from_id_prop_42'"
it 'uses the object #to_key function', ->
class FromToKeyFunc
to_key: -> 23
expect(HAML.reference(new FromToKeyFunc())).toEqual "class='from_to_key_func' id='from_to_key_func_23'"
it 'uses the object #id function', ->
class FromIdFunc
id: -> 123
expect(HAML.reference(new FromIdFunc())).toEqual "class='from_id_func' id='from_id_func_123'"
it 'uses the object itself', ->
expect(HAML.reference(123)).toEqual "class='number' id='number_123'"