UNPKG

3.68 kBtext/coffeescriptView Raw
1fs = require 'fs'
2
3CoffeeScript = require 'coffee-script'
4HamlCoffee = require '../src/haml-coffee'
5HC = require '../src/hamlc'
6
7suites = [
8 'haml'
9 'haml_coffee'
10]
11
12beforeEach ->
13 @addMatchers
14 toBeCompiledTo: (expected) ->
15 @message = -> @actual.report
16 @actual.generated is expected
17
18for suite in suites
19 data = JSON.parse fs.readFileSync("spec/suites/#{ suite }_spec.json")
20
21 for group, specs of data
22 for desc, spec of specs
23 do (group, desc, spec) ->
24 describe group, ->
25 it "handles #{ desc }", ->
26
27 window = {}
28 window.HAML = {}
29
30 config = {
31 escapeHtml : if spec.config?.escape_html is 'true' then true else false
32 escapeAttributes : if spec.config?.escape_attributes is 'true' then true else false
33 extendScope : if spec.config?.extend_scope is 'true' then true else false
34 placement : spec.config?.placement || 'global'
35 format : spec.config?.format || 'xhtml'
36 }
37
38 compiler = new HamlCoffee(config)
39
40 report = "Generated output doesn't match the expected result.\n\n"
41 report += "-------------------- Compiler settings ------------------------\n"
42 report += JSON.stringify(config)
43 report += "\n-------------------- Haml template ----------------------------\n"
44
45 if spec.haml_template
46 spec.haml = fs.readFileSync("spec/suites/templates/#{ spec.haml_template }.haml").toString()
47
48 report += spec.haml
49
50 if spec.partials && config.placement is 'global'
51 report += "\n-------------------- Partial templates --------------------------\n"
52
53 for name, partial of spec.partials
54 try
55 source = fs.readFileSync("spec/suites/templates/#{ partial }.haml").toString()
56 window.HAML[name] = HC.compile(source)
57 report += "window.HAML['#{ name }'] compiled from source '#{ partial }'\n"
58 catch error
59 report += "Error compiling partial #{ name }: #{ error }"
60
61 if spec.locals
62 report += "\n-------------------- Local variables --------------------------\n"
63 report += JSON.stringify(spec.locals)
64
65 compiler.parse spec.haml
66 cst = compiler.render 'test'
67
68 report += "\n-------------------- Marked output nodes ----------------------\n"
69 for line in compiler.lines
70 report += JSON.stringify(line) + '\n'
71
72 report += "-------------------- Rendered CST template --------------------\n"
73 report += cst
74
75 try
76 template = CoffeeScript.compile cst
77
78 eval template
79
80 html = window.HAML.test(spec.locals)
81
82 catch error
83 report += "\n-------------- Error compiling JST -------------------------\n"
84 report += "#{ error }"
85
86 report += "\n-------------------- Rendered HTML ---------------------------\n"
87 report += html
88
89 spec.html = fs.readFileSync("spec/suites/templates/#{ spec.html_template }.html").toString() if spec.html_template
90 result = spec.html
91
92 report += "\n-------------------- Expected result--------------------------\n"
93 report += result
94 report += "\n---------------------------------------------------------------\n"
95
96 expect({
97 generated : html
98 report : report.split('\n').join('\n ')
99 }).toBeCompiledTo(result)