Debian Codegraph @

tests/wplua/scripts/json-utils.lua

raw
1 rules_json_str = [[
2 [
3 {
4 matches = [
5 {
6 device.name = "~alsa_card.*"
7 }
8 ]
9 actions = {
10 update-props = {
11 api.alsa.use-acp = true
12 api.acp.auto-port = false
13 }
14 }
15 }
16 {
17 matches = [
18 {
19 node.name = "alsa_output.0.my-alsa-device"
20 }
21 ]
22 actions = {
23 update-props = {
24 audio.rate = 96000
25 node.description = "My ALSA Node"
26 media.class = null
27 }
28 }
29 }
30 ]
31 ]]
32
33 match_props = { ["device.name"] = "unmatched-device-name" }
34 ret_props, ret = JsonUtils.match_rules_update_properties (Json.Raw (rules_json_str), match_props)
35 assert (ret == 0)
36 assert (ret_props["device.name"] == match_props["device.name"])
37
38 match_props = { ["device.name"] = "alsa_card_0.my-alsa-device" }
39 ret_props, ret = JsonUtils.match_rules_update_properties (Json.Raw (rules_json_str), match_props)
40 assert (ret == 2)
41 assert (ret_props["device.name"] == "alsa_card_0.my-alsa-device")
42 assert (ret_props["api.alsa.use-acp"] == "true")
43 assert (ret_props["api.acp.auto-port"] == "false")
44
45 match_props = { ["node.name"] = "alsa_output.0.my-alsa-device" }
46 ret_props, ret = JsonUtils.match_rules_update_properties (Json.Raw (rules_json_str), match_props)
47 assert (ret == 2)
48 assert (ret_props["node.name"] == "alsa_output.0.my-alsa-device")
49 assert (ret_props["audio.rate"] == "96000")
50 assert (ret_props["node.description"] == "My ALSA Node")
51 assert (ret_props["media.class"] == nil)
52
53 match_props = {
54 ["node.name"] = "alsa_output.0.my-alsa-device",
55 ["media.class"] = "Audio/Sink",
56 ["audio.rate"] = "48000",
57 ["node.description"] = "Test",
58 }
59 ret_props, ret = JsonUtils.match_rules_update_properties (Json.Raw (rules_json_str), match_props)
60 assert (ret == 3)
61 assert (ret_props["node.name"] == "alsa_output.0.my-alsa-device")
62 assert (ret_props["audio.rate"] == "96000")
63 assert (ret_props["node.description"] == "My ALSA Node")
64 assert (ret_props["media.class"] == nil)
65
66 rules_json_str = [[
67 [
68 {
69 matches = [
70 {
71 device.name = "~alsa_card.*"
72 }
73 ]
74 actions = {
75 update-props = {
76 api.acp.auto-port = false
77 }
78 set-answer = 42
79 }
80 }
81 {
82 matches = [
83 {
84 test.error = true
85 }
86 ]
87 actions = {
88 generate-error = "test.error is true"
89 }
90 }
91 {
92 matches = [
93 {
94 device.name = "alsa_card.1"
95 }
96 ]
97 actions = {
98 set-description = "My ALSA Device"
99 }
100 }
101 ]
102 ]]
103
104 function match_rules_callback (action, value)
105 if action == "update-props" then
106 local updates = value:parse ()
107 for k,v in pairs (updates) do
108 match_props[k] = tostring (v)
109 end
110 elseif action == "set-answer" then
111 local v = value:parse ()
112 match_props["answer.universe"] = tostring (v)
113 elseif action == "generate-error" then
114 local err = value:parse ()
115 return false, tostring (err)
116 elseif action == "set-description" then
117 local str = value:parse ()
118 match_props["device.description"] = tostring (str)
119 end
120
121 return true
122 end
123
124 match_props = {
125 ["device.name"] = "alsa_card.1",
126 ["test.error"] = "false",
127 }
128 ret, err = JsonUtils.match_rules (Json.Raw (rules_json_str), match_props, match_rules_callback)
129 assert (ret == true)
130 assert (err == nil)
131 assert (match_props["device.name"] == "alsa_card.1")
132 assert (match_props["api.acp.auto-port"] == "false")
133 assert (match_props["answer.universe"] == "42")
134 assert (match_props["device.description"] == "My ALSA Device")
135
136 match_props = {
137 ["device.name"] = "alsa_card.1",
138 ["test.error"] = "true",
139 }
140 ret, err = JsonUtils.match_rules (Json.Raw (rules_json_str), match_props, match_rules_callback)
141 assert (ret == false)
142 assert (err == "test.error is true")
143 assert (match_props["device.name"] == "alsa_card.1")
144 assert (match_props["api.acp.auto-port"] == "false")
145 assert (match_props["answer.universe"] == "42")
146 assert (match_props["device.description"] == nil)