UNPKG

4.23 kBJavaScriptView Raw
1var sys = require("sys"),
2 assert = require("assert"),
3 jsonCommand = require("../lib/jsonCommand");
4
5var testObj = {
6 id : 19375093,
7 text : "who knows",
8 user : {
9 id : 1310571,
10 name : "foo"
11 },
12 arr1 : [
13 'a', 'b', 'c'
14 ],
15 obj1 : {
16 arr2 : [
17 'd', 'e', 'f'
18 ]
19 },
20 created_at : 127817599,
21 zero : 0
22};
23
24function printTestName(testName) {
25 sys.puts("\nRunning " + testName + ":");
26 sys.puts("-----------------------------------------");
27}
28
29(function testSimpleSetup() {
30
31 (function testProcessArgs() {
32 printTestName("testProcessArgs");
33
34 var jsonC = new JSON.Command();
35 var conditions = [ "(name == 'foo')", "(text == 'boo')" ];
36 jsonC.processArgs([ "-c", conditions[0], "-c", conditions[1] ]);
37 assert.equal(jsonC.conditionals[0], conditions[0],
38 "conditionals contains specified conditional [0]");
39 assert.equal(jsonC.conditionals[1], conditions[1],
40 "conditionals contains specified conditional [1]");
41
42 })();
43
44 (function testCreateRequestedKeys() {
45 printTestName("testCreateRequestedKeys");
46
47 var jsonC = new JSON.Command();
48 jsonC.processArgs([ "newKey" ]);
49 jsonC.createRequestedKeys(testObj);
50
51 assert.equal(testObj.newKey, null,
52 "createRequestedKeys adds requested key to object");
53
54 jsonC = new JSON.Command();
55 jsonC.processArgs([ "zero" ]);
56 jsonC.createRequestedKeys(testObj);
57
58 assert.equal(testObj.zero, 0,
59 "createRequestedKeys does not add null for 0 to object");
60 })();
61
62 (function testCheckConditionals() {
63 printTestName("testCheckConditionals");
64
65 var jsonC = new JSON.Command();
66 jsonC.processArgs([ "-c", "(name == 'foo')"]);
67
68 assert.equal(jsonC.checkConditionals(testObj), false,
69 "checkConditionals (name=='foo') is false");
70
71 var jsonC = new JSON.Command();
72 jsonC.processArgs([ "-c", "(user.name == 'foo')"]);
73
74 assert.equal(jsonC.checkConditionals(testObj), true,
75 "checkConditionals (user.name=='foo') is true");
76 })();
77
78 (function testProcessKeyTransforms() {
79 printTestName("testProcessKeyTransforms");
80
81 var tmpTestObj = {
82 id : 19375093, text : "who knows", created_at : 127817599,
83 user : {
84 id : 1310571, name : "foo"
85 }
86 };
87
88 var jsonC = new JSON.Command();
89 jsonC.processArgs([ "user.new_name=user.name", "-d"]);
90 jsonC.createRequestedKeys(tmpTestObj);
91 jsonC.processKeyTransforms(tmpTestObj);
92
93 assert.equal(tmpTestObj.user.new_name, testObj.user.name,
94 "processKeyTransforms user.new_name = user.name is true");
95
96 })();
97
98 (function testProcessExecutables() {
99 printTestName("testProcessExecutables");
100
101 var tmpTestObj = {
102 id : 19375093, text : "who knows", created_at : 127817599,
103 user : {
104 id : 1310571, name : "foo"
105 }
106 };
107
108 var jsonC = new JSON.Command();
109 jsonC.processArgs([ "-e", "user.name = 'boo';"]);
110 jsonC.processExecutables(tmpTestObj);
111
112 assert.equal(tmpTestObj.user.name, "boo",
113 "processExecutables user.name = 'boo' is true");
114 })();
115
116 (function testProcessKeys() {
117 printTestName("testProcessKeys");
118
119 var jsonC = new JSON.Command();
120 jsonC.processArgs([ "user.name", "text", "arr1[1]", "obj1.arr2[2]" ]);
121 assert.equal(jsonC.keys.length, 4,
122 "processedKeys keys length == 4");
123
124 var resObj = jsonC.processKeys(testObj);
125
126 assert.equal(resObj.user.name, testObj.user.name,
127 "processKeys result object user.name = testObj.user.name is true");
128 assert.equal(resObj.text, testObj.text,
129 "processKeys result object user.name = testObj.text is true");
130 assert.equal(resObj.id, undefined,
131 "processKeys result object id is undefined is true");
132 assert.equal(resObj.created_at, undefined,
133 "processKeys result object created_at is undefined is true");
134 assert.equal(resObj.user.id, undefined,
135 "processKeys result object user.id is undefined is true");
136 assert.equal(resObj.arr1[0], testObj.arr1[1],
137 "processKeys result object arr1[0] = testObj.arr1[0] is true");
138 assert.equal(resObj.obj1.arr2[0], testObj.obj1.arr2[2],
139 "processKeys result object obj1.arr2[0] = testObj.obj1.arr2[2] is true");
140
141 })();
142
143})();
144
145sys.puts("\nAll tests passed!\n");