treewide: forward compatibility changes
[project/firewall4.git] / tests / lib / mocklib / ubus.uc
1 let mocklib = global.mocklib;
2
3 return {
4 connect: function() {
5 let self = this;
6
7 return {
8 call: (object, method, args) => {
9 let signature = [ object + "~" + method ];
10
11 if (type(args) == "object") {
12 for (let i, k in sort(keys(args))) {
13 switch (type(args[k])) {
14 case "string":
15 case "double":
16 case "bool":
17 case "int":
18 push(signature, k + "-" + replace(args[k], /[^A-Za-z0-9_-]+/g, "_"));
19 break;
20
21 default:
22 push(signature, type(args[k]));
23 }
24 }
25 }
26
27 let candidates = [];
28
29 for (let i = length(signature); i > 0; i--) {
30 let path = sprintf("ubus/%s.json", join("~", signature)),
31 mock = mocklib.read_json_file(path);
32
33 if (mock != mock) {
34 self._error = "Invalid argument";
35
36 return null;
37 }
38 else if (mock) {
39 mocklib.trace_call("ctx", "call", { object, method, args });
40
41 return mock;
42 }
43
44 push(candidates, path);
45 pop(signature);
46 }
47
48 mocklib.I("No response fixture defined for ubus call %s/%s with arguments %s.", object, method, args);
49 mocklib.I("Provide a mock response through one of the following JSON files:\n%s\n", join("\n", candidates));
50
51 self._error = "Method not found";
52
53 return null;
54 },
55
56 disconnect: () => null,
57
58 error: () => self.error()
59 };
60 },
61
62 error: function() {
63 let e = this._error;
64 delete this._error;
65
66 return e;
67 }
68 };