-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathhelpers.coffee
57 lines (49 loc) · 1.48 KB
/
helpers.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
yaml = require('js-yaml')
fs = require('fs')
lang = require('../src/lang')
assert = require('assert')
exports.loadJson = (pth)->
JSON.parse(fs.readFileSync(pth, "utf8"))
exports.loadYaml = (pth)->
yaml.safeLoad(fs.readFileSync(pth, "utf8"))
edn = require("jsedn")
to_js = (obj)->
if obj.constructor == edn.Map
res = {}
for k,idx in obj.keys
res[k.name.replace(/^:/,'')] = to_js(obj.vals[idx])
res
else if obj.constructor == edn.Keyword
obj.name
else if obj.constructor == edn.Vector
obj.val.map(to_js)
else if obj.constructor == edn.Set
obj.val.map(to_js)
else if obj.constructor == edn.List
["$#{obj.val[0].name}"].concat(obj.val[1..].map(to_js))
else if obj.constructor == edn.Symbol
obj.name
else
obj
exports.loadEdn = (pth)->
str = fs.readFileSync(pth, "utf8")
to_js(edn.parse(str))
match_recur = (obj, sample, path)->
if lang.isArray(sample)
for v,i in sample
next = obj[i]
new_path = path.concat(i)
if not next?
assert(false, "No object on #{new_path.join('.')}, but #{JSON.stringify(obj)}")
match_recur(next, v, new_path)
else if lang.isObject(sample)
for k,v of sample
next = obj[k]
new_path = path.concat(k)
if not next?
assert(false, "No object on #{new_path.join('.')}, but #{JSON.stringify(obj)}")
match_recur(next, v, new_path)
else
assert.equal(obj, sample, "Path #{path.join('.')}")
exports.match = (obj, sample)->
match_recur(obj, sample, [])