UNPKG

11.3 kBJavaScriptView Raw
1var expect = require("chai").expect;
2var lib = require("../argumints.js");
3var ArguMints = lib.ArguMints;
4var myMints = lib.myArguMints;
5
6// we're running from a script.
7ArguMints.nodeCLI = false;
8
9
10
11var dArgs = [];//["--minty-dump", "--minty-verbose"];
12describe("ArguMints Test Suite - Vanilla -->", function() {
13 describe("Test Empty Retort", function(){
14 it("Retorts to no input arguments by the user", function() {
15 var calledRetortExpandCb = false;
16 var calledRetortCompleteCb = false;
17
18 myMints.retort([],
19 function(args){
20 calledRetortExpandCb = true;
21 },
22 function(arg, exp, idx, cnt){
23 calledRetortCompleteCb = true;
24 }
25 );
26
27 expect(calledRetortExpandCb).to.equal(true);
28 expect(calledRetortCompleteCb).to.equal(true);
29 });
30 });
31
32 describe("Test Args -->", function(){
33 it("Basic Retort Inputs, argv, opt, flag, keyValue -->", function() {
34 myMints.reset();
35
36
37 // NOTE: in this test script, 'spec' in an argument that is passed into
38 // the testing harness, so all retorts will be appended to that argv value 'spec'.
39 // thus tests below (when checking for indices) will be 1 based to skip the first 'spec'
40 // arguments for the test.
41 myMints.retort(["a", "b", "c"].concat(dArgs));
42
43 expect(myMints.argv(1)).to.equal("a");
44 expect(myMints.argv(2)).to.equal("b");
45 expect(myMints.argv(3)).to.equal("c");
46
47
48 myMints.retort(["--op1", "--op2", "--op3"].concat(dArgs));
49
50 expect(myMints.opt('op1')).to.equal(true);
51 expect(myMints.opt('op2')).to.equal(true);
52 expect(myMints.opt('op3')).to.equal(true);
53
54 // single test to insure 'flag()' returns the copied object
55 // with equivalent properties.
56 expect(myMints.opt().op1).to.equal(true);
57
58 // test that we're always copying rather than returning the internal instance
59 expect(myMints.opt() !== myMints.opt()).to.equal(true);
60
61 myMints.retort(["-xzvf", "-dywa"]);
62
63 expect(myMints.flag('x')).to.equal(true);
64 expect(myMints.flag('z')).to.equal(true);
65 expect(myMints.flag('v')).to.equal(true);
66 expect(myMints.flag('f')).to.equal(true);
67 expect(myMints.flag('d')).to.equal(true);
68 expect(myMints.flag('y')).to.equal(true);
69 expect(myMints.flag('w')).to.equal(true);
70 expect(myMints.flag('a')).to.equal(true);
71
72 // single test to insure 'flag()' returns the copied object
73 // with equivalent properties.
74 expect(myMints.flag().x).to.equal(true);
75
76 // test that we're always copying rather than returning the internal instance
77 expect(myMints.flag() !== myMints.flag()).to.equal(true);
78
79 myMints.retort(["vacationDays=14", "changeInMyPocket=.27", "checkFor=null","meaningOfLife=undefined","zed=dead", "isIt=true", "theDude=abides"].concat(dArgs));
80
81 expect(myMints.keyValue('isIt')).to.equal(true);
82 expect(myMints.keyValue('checkFor')).to.equal(null);
83 expect(myMints.keyValue('vacationDays')).to.equal(14);
84 expect(myMints.keyValue('changeInMyPocket')).to.equal(.27);
85 expect(myMints.keyValue('meaningOfLife')).to.equal(undefined);
86 expect(myMints.keyValue('zed')).to.equal("dead");
87 expect(myMints.keyValue('theDude')).to.equal("abides");
88
89 expect(typeof myMints.keyValue()).to.equal('object');
90 });
91 });
92
93 describe("Test Key Value Pairs using JSON Expansion-->", function(){
94 it("Retorts to JSON formatted input, and tests the keyValue settings-->.", function() {
95 myMints.reset();
96
97
98 // NOTE: in this test script, 'spec' in an argument that is passed into
99 // the testing harness, so all retorts will be appended to that argv value 'spec'.
100 // thus tests below (when checking for indices) will be 1 based to skip the first 'spec'
101 // arguments for the test.
102 myMints.retort(['{"keyValStr":"str", "keyValInt":1, "keyValDouble":1.00000001, "keyValBool":true, "keyValArr":[1,2,3], "keyValObj":{"name":"anObject"}, "keyValNul":null }'].concat(dArgs));
103
104 expect(myMints.keyValue('keyValStr')).to.equal("str");
105 expect(myMints.keyValue('keyValInt')).to.equal(1);
106 expect(myMints.keyValue('keyValDouble')).to.equal(1.00000001);
107 expect(myMints.keyValue('keyValBool')).to.equal(true);
108 expect(myMints.keyValue('keyValArr')[2]).to.equal(3);
109 expect(myMints.keyValue('keyValObj').name).to.equal("anObject");
110 expect(myMints.keyValue('keyValNul')).to.equal(null);
111 });
112 });
113
114 describe("Test Regular Expressions-->", function(){
115 it("Test a Regular Expression directly input-->", function() {
116 myMints.reset();
117 myMints.retort(["--minty-match-argv", "anotherEmail@gmail.com", "barcher4620@gmail.com", '`/^(([^<>()[\\]\\\\.,;:\\s@"]+(\\.[^<>()[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/g`'].concat(dArgs));
118 expect(myMints.matches().length).to.equal(2);
119 });
120 });
121
122
123
124 describe("Test JSON File Expansion-->", function(){
125 it("Retorts a file expansion input pointing to json formatted data-->", function() {
126 myMints.reset();
127 myMints.retort(["@test/testargs1.json"].concat(dArgs));
128
129 expect(myMints.keyValue("aNumber")).to.equal(1);
130 expect(myMints.keyValue("aBool")).to.equal(true);
131 expect(myMints.keyValue("anArray")[2]).to.equal("buckle");
132 expect(myMints.keyValue("anObject").prop).to.equal("aProperty");
133 expect(myMints.keyValue("nullValue")).to.equal(null);
134 });
135 });
136
137 describe("Test Key Duplication-->", function(){
138 it("Retorts to keyValue pair duplicates using --minty-append-dup-keys--> ", function() {
139 myMints.reset();
140 myMints.retort(["--minty-append-dup-keys","key1=2", "key1=3", "key2=5"].concat(dArgs));
141
142 expect(myMints.keyValue("key1")).to.equal(myMints.keyValue("key2"));
143
144 myMints.reset();
145 myMints.retort(["--minty-append-dup-keys", "key1=@test/test.txt", "key1=@test/test2.txt", "key2=@test/test.txt", "key3=@test/test2.txt"].concat(dArgs));
146
147 // check string concatenation
148 expect(myMints.keyValue("key1")).to.equal(myMints.keyValue("key2") + myMints.keyValue("key3"));
149
150
151 myMints.reset();
152 //check ops
153
154 myMints.retort(["--minty-append-dup-keys", "--minty-op-div", "div=10", "div=2", "ans=5"].concat(dArgs));
155 expect(myMints.keyValue("div")).to.equal(myMints.keyValue("ans"));
156 myMints.reset();
157 myMints.retort(["--minty-append-dup-keys", "--minty-op-sub", "div=10", "div=2", "-12"].concat(dArgs));
158 expect(myMints.keyValue("div")).to.equal(myMints.argv(1));
159 myMints.reset();
160 myMints.retort(["--minty-append-dup-keys", "--minty-op-mul", "div=10", "div=2", "ans=20"].concat(dArgs));
161 expect(myMints.keyValue("div")).to.equal(myMints.keyValue("ans"));
162 myMints.reset();
163 myMints.retort(["--minty-append-dup-keys", "--minty-op-sqrt", "div=4", "2"].concat(dArgs));
164 expect(myMints.keyValue("div")).to.equal(myMints.argv(1));
165 myMints.reset();
166 myMints.retort(["--minty-append-dup-keys", "--minty-op-sqr", "div=10", "100"].concat(dArgs));
167 expect(myMints.keyValue("div")).to.equal(myMints.argv(1));
168 myMints.reset();
169 myMints.retort(["--minty-append-dup-keys", "--minty-op-ln", "ln=5"].concat(dArgs));
170 expect(myMints.keyValue("ln")).to.equal(Math.log(5));
171 myMints.reset();
172 myMints.retort(["--minty-append-dup-keys", "--minty-op-tan", "tan=.6"].concat(dArgs));
173 expect(myMints.keyValue("tan")).to.equal(Math.tan(.6));
174 myMints.reset();
175 myMints.retort(["--minty-append-dup-keys", "--minty-op-cos", "cos=.6"].concat(dArgs));
176 expect(myMints.keyValue("cos")).to.equal(Math.cos(.6));
177 myMints.reset();
178 myMints.retort(["--minty-append-dup-keys", "--minty-op-sin", "sin=.5"].concat(dArgs));
179 expect(myMints.keyValue("sin")).to.equal(Math.sin(.5));
180 myMints.reset();
181 myMints.retort(["--minty-append-dup-keys", "--minty-op-atan", "atan=.5"].concat(dArgs));
182 expect(myMints.keyValue("atan")).to.equal(Math.atan(.5));
183 myMints.reset();
184 myMints.retort(["--minty-append-dup-keys", "--minty-op-exp", "exp=2"].concat(dArgs));
185 expect(myMints.keyValue("exp")).to.equal(Math.exp(2));
186 });
187 });
188
189
190 describe("Test Argument Manipulation-->", function(){
191 it("Retorts and uses the retort expansion callback to manipulate arguments to modify the behavior of retort.--> ", function() {
192 myMints.reset();
193
194 // the factorial function, ArguMints style!
195 var fact = 1;
196 myMints.retort( [4].concat(dArgs),null, function(arg, exp, idx, cnt){
197 if( (typeof exp) == 'number' && exp > 0){
198 fact *= exp;
199 myMints.insertArg(arg-1, 0);
200 }
201 });
202
203 expect(fact).to.equal(24);
204 myMints.reset();
205 fact = 1;
206 myMints.retort( [0].concat(dArgs),null, function(arg, exp, idx, cnt){
207 if( (typeof exp) == 'number' && exp > 0){
208 fact *= exp;
209 myMints.insertArg(arg-1, 0);
210 }
211 });
212
213 expect(fact).to.equal(1);
214
215 myMints.reset();
216
217 // the summation function, ArguMints style!
218 var sum = 0;
219 var t = 4; // first x numbers
220 myMints.retort( [1].concat(dArgs),null, function(arg, exp, idx, cnt){
221 if( (typeof exp) == 'number' ){
222 sum += exp;
223
224 t--;
225 if(t > 0){
226 myMints.pushArg(exp+1);
227 }
228 }
229 });
230
231 expect(sum).to.equal(10);
232 });
233 });
234});
\No newline at end of file