UNPKG

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