-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.lua
93 lines (69 loc) · 1.59 KB
/
test.lua
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
local tracedoc = require "tracedoc"
local doc = tracedoc.new {
a = 1,
b = { 1,2,3 },
c = { d = 4 , e = 5 },
d = {},
}
local function dump(doc)
local changes = tracedoc.commit(doc, {})
print("Dump:")
for k,v in pairs(doc) do
print(k,v)
if type(v) == "table" then
for k, v in pairs(v) do
print("\t",k,v)
end
end
end
print("changes:")
for k,v in pairs(changes) do
print(k,v)
end
end
dump(doc)
print "-----------------------------"
doc.b[3] = nil
doc.b = { 1,3 } -- remove [3], change [2]
dump(doc)
tracedoc.opaque(doc.d, true)
doc.d.x = 1 -- d change ( d is opaque)
doc.d.y = 2 -- d change ( d is opaque)
assert(doc.b[1] == 1)
assert(doc.b[2] == 3)
assert(doc.c.d == 4)
doc.b[1] = 0 -- change
doc.b[2] = 2 -- not change
local tmp = doc.c
print(tracedoc.dump(doc.b))
dump(doc)
doc.a = nil -- clear doc.a
assert(doc.a == nil)
doc.b[3] = nil -- remove 3
table.remove(doc.b) -- remove 2
table.insert(doc.b , 5) -- doc.b[2] = 5
table.insert(doc.b , 6) -- doc.b[3] = 6
table.insert(doc.b , 7) -- doc.b[4] = 7
for k,v in ipairs(doc.b) do
print("doc.b[" .. k .. "]",v)
end
doc.e = "e" -- add doc.e
for k,v in pairs(doc) do
print("doc."..k , v)
end
dump(doc)
doc.a = 2 -- change
assert(doc.a == 2)
doc.b = 2 -- change and delete table
assert(doc.b == 2)
local doc_c = { e = 5 }
doc.c = doc_c -- change table
assert(doc.c.d == nil)
doc.b = nil
doc.d = setmetatable({}, { __tostring = function() return "userobject" end }) -- table with metatable is an userobject
doc.e = { x = 1, y = 2 }
assert(tmp == doc.c)
tmp = doc.c -- update c
assert(tmp ~= doc_c)
dump(doc)
assert(tmp.e == 5)