-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-ref.js
46 lines (35 loc) · 1.2 KB
/
test-ref.js
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
const test = require('tape')
const { mapper } = require('.')
class Person { constructor (state) { Object.assign(this, state) } }
test(t => {
const m = mapper()
m.add(Person, 'Person [name] (id [id]) has mom [ref mom].')
const a = new Person({ name: 'Avery' })
const b = new Person({ name: 'Brook' })
a.mom = b
const str = m.stringify([a])
t.equal(str, 'Person Avery (id a0) has mom a1.\n\nPerson Brook (id a1) has mom (ValueUnknown).\n\n')
const out = m.parse(str)
t.deepEqual(out, [a, b])
// do we want to somehow flag b as subordinate, by reference only?
t.end()
})
test(t => {
const m = mapper()
m.add(Person, '[name] (person-[id]) knows person-[ref knows].')
const a = new Person({ name: 'Avery' })
const b = new Person({ name: 'Brook' })
const c = new Person({ name: 'Casey' })
a.knows = b
b.knows = c
c.knows = a
const str = m.stringify([a])
t.equal(str, 'Avery (person-a0) knows person-a1.\n\nBrook (person-a1) knows person-a2.\n\nCasey (person-a2) knows person-a0.\n\n')
const out = m.parse(str)
const [x, y, z] = out
t.equal(x.knows, y)
t.equal(y.knows, z)
t.equal(z.knows, x)
// t.deepEqual(out, [a,b,c]) can't handle cyclical structures
t.end()
})