-
Notifications
You must be signed in to change notification settings - Fork 36
/
mustache_specs.jl
120 lines (97 loc) · 2.92 KB
/
mustache_specs.jl
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
## This downloads and populates the spec test files
using Mustache
using YAML
using Test
ghub = "https://raw.githubusercontent.com/mustache/spec/72233f3ffda9e33915fd3022d0a9ebbcce265acd/specs/{{:spec}}.yml"
specs = ["comments",
"delimiters",
"interpolation",
"inverted",
"partials",
"sections"#,
#"~lambdas"
]
D = Dict()
for spec in specs
nm = Mustache.render(ghub, spec=spec)
D[spec] = YAML.load_file(download(nm))
end
function write_spec_file(fname)
io = open("spec_$fname.jl","w")
println(io, """
using Mustache
using Test
""")
x = D[fname]
println(io, "@testset \" $fname \" begin\n")
for (i,t) in enumerate(x["tests"])
tpl = t["template"]
tpl = replace(tpl, "\\" => "\\\\")
tpl = replace(tpl, r"\"" => "\\\"")
data = t["data"]
desc = t["desc"]
if haskey(t, "partials")
for (k,v) in t["partials"]
data[k] = v
end
end
val = try
Mustache.render(t["template"], data)
catch err
"Failed"
end
expected = t["expected"]
expected_ = replace(expected, "\\" => "\\\\")
expected_ = replace(expected_, r"\"" => "\\\"")
expected_ = "\"\"\"" * expected_ * "\"\"\""
println(io, "\n\t## $desc")
print(io, "tpl = \"\"\"")
print(io, tpl)
println(io, "\"\"\"\n")
println(io, "\t@test Mustache.render(tpl, $data) == $expected_")
println("")
end
println(io, "end\n\n")
close(io)
end
for spec in specs
write_spec_file(spec)
end
# partials are different, as they refer to an external file
# this should clean up temp files, but we don't run as part of test suite
# test 7 fails, but I think that one is wrong
using Test
function test_partials()
for spec in specs
for (i,t) in enumerate(D[spec]["tests"])
if haskey(t, "partials")
println("""Test $spec / $i""")
d = t["data"]
partial = t["partials"]
for (k,v) in partial
io = open(k, "w")
write(io, v)
close(io)
end
tpl = t["template"]
expected = t["expected"]
out = Mustache.render(tpl, d)
val = out == expected
if val
@test val
else
val = replace(out, r"\n"=>"") == replace(expected, r"\n"=>"")
if val
println("""$(t["desc"]): newline issue ...""")
@test val
else
println("""$(t["desc"]): FAILED:\n $out != $expected""")
end
end
for (k,v) in partial
rm(k)
end
end
end
end
end