UNPKG

19.3 kBJavaScriptView Raw
1/*eslint new-cap: ["error", { "capIsNew": false }]*/
2'use strict';
3
4var assert = require('assert');
5var sass = require('../');
6
7describe('sass.types', function() {
8 describe('Boolean', function() {
9 it('exists', function() {
10 assert(sass.types.Boolean);
11 });
12
13 it('names the constructor correctly', function() {
14 assert.equal(sass.types.Boolean.name, 'SassBoolean');
15 });
16
17 it('supports call constructor', function() {
18 var t = sass.types.Boolean(true);
19 assert.equal(t.toString(), '[object SassBoolean]');
20
21 var f = sass.types.Boolean(false);
22 assert.equal(f.toString(), '[object SassBoolean]');
23 });
24
25 it('has true and false singletons', function() {
26 assert.equal(sass.types.Boolean(true), sass.types.Boolean(true));
27 assert.equal(sass.types.Boolean(false), sass.types.Boolean(false));
28 assert.notEqual(sass.types.Boolean(false), sass.types.Boolean(true));
29 assert.equal(sass.types.Boolean(true), sass.types.Boolean.TRUE);
30 assert.equal(sass.types.Boolean(false), sass.types.Boolean.FALSE);
31 });
32
33 it('supports DOES NOT support new constructor', function() {
34 assert.throws(function() {
35 new sass.types.Boolean(true);
36 }, function(error) {
37 assert.ok(error instanceof TypeError);
38 assert.equal(error.message, 'Cannot instantiate SassBoolean');
39 return true;
40 });
41 });
42
43 it('throws with incorrect constructor args', function() {
44 assert.throws(function() {
45 sass.types.Boolean();
46 }, function(error) {
47 assert.ok(error instanceof TypeError);
48 assert.equal(error.message, 'Expected one boolean argument');
49 return true;
50 });
51
52 [1, 2, '', 'hi', {}, []].forEach(function(arg) {
53 assert.throws(function() {
54 sass.types.Boolean(arg);
55 }, function(error) {
56 assert.ok(error instanceof TypeError);
57 assert.equal(error.message, 'Expected one boolean argument');
58 return true;
59 });
60 });
61
62 assert.throws(function() {
63 sass.types.Boolean(true, false);
64 }, function(error) {
65 assert.ok(error instanceof TypeError);
66 assert.equal(error.message, 'Expected one boolean argument');
67 return true;
68 });
69 });
70
71 it('implements getValue', function() {
72 var t = sass.types.Boolean(true);
73 assert.equal(typeof t.getValue, 'function');
74 assert.equal(t.getValue(), true);
75
76 var f = sass.types.Boolean(false);
77 assert.equal(typeof f.getValue, 'function');
78 assert.equal(f.getValue(), false);
79 });
80 });
81
82 describe('Color', function() {
83 it('exists', function() {
84 assert(sass.types.Color);
85 });
86
87 it('names the constructor correctly', function() {
88 assert.equal(sass.types.Color.name, 'SassColor');
89 });
90
91 it('supports call constructor', function() {
92 var t = sass.types.Color();
93 assert.equal(t.toString(), '[object SassColor]');
94 });
95
96 it('supports new constructor', function() {
97 var t = new sass.types.Color(1);
98 assert.equal(t.toString(), '[object SassColor]');
99 });
100
101 it('supports variadic constructor args', function() {
102 var a = new sass.types.Color();
103
104 assert.equal(a.getR(), 0);
105 assert.equal(a.getG(), 0);
106 assert.equal(a.getB(), 0);
107 assert.equal(a.getA(), 1);
108
109 var b = new sass.types.Color(1);
110
111 assert.equal(b.getR(), 0);
112 assert.equal(b.getG(), 0);
113 assert.equal(b.getB(), 1);
114 assert.equal(b.getA(), 0); // why ?
115
116 assert.throws(function() {
117 new sass.types.Color(1, 2);
118 }, function(error) {
119 // assert.ok(error instanceof TypeError);
120 assert.equal(error.message, 'Constructor should be invoked with either 0, 1, 3 or 4 arguments.');
121 return true;
122 });
123
124 var c = new sass.types.Color(1, 2, 3);
125
126 assert.equal(c.getR(), 1);
127 assert.equal(c.getG(), 2);
128 assert.equal(c.getB(), 3);
129 assert.equal(c.getA(), 1);
130
131 var d = new sass.types.Color(1, 2, 3, 4);
132
133 assert.equal(d.getR(), 1);
134 assert.equal(d.getG(), 2);
135 assert.equal(d.getB(), 3);
136 assert.equal(d.getA(), 4);
137
138 assert.throws(function() {
139 new sass.types.Color(1, 2, 3, 4, 5);
140 }, function(error) {
141 // assert.ok(error instanceof TypeError);
142 assert.equal(error.message, 'Constructor should be invoked with either 0, 1, 3 or 4 arguments.');
143 return true;
144 });
145 });
146
147 it('supports get{R,G,B,A} and set{R,G,B,A}', function() {
148 var c = new sass.types.Color();
149
150 assert.equal(c.getR(), 0);
151 assert.equal(c.getG(), 0);
152 assert.equal(c.getB(), 0);
153 assert.equal(c.getA(), 1);
154
155 assert.equal(c.setR(1), undefined);
156
157 assert.equal(c.getR(), 1);
158 assert.equal(c.getG(), 0);
159 assert.equal(c.getB(), 0);
160 assert.equal(c.getA(), 1);
161
162 assert.equal(c.setG(1), undefined);
163
164 assert.equal(c.getR(), 1);
165 assert.equal(c.getG(), 1);
166 assert.equal(c.getB(), 0);
167 assert.equal(c.getA(), 1);
168
169 assert.equal(c.setB(1), undefined);
170
171 assert.equal(c.getR(), 1);
172 assert.equal(c.getG(), 1);
173 assert.equal(c.getB(), 1);
174 assert.equal(c.getA(), 1);
175
176 assert.equal(c.setA(0), undefined);
177
178 assert.equal(c.getR(), 1);
179 assert.equal(c.getG(), 1);
180 assert.equal(c.getB(), 1);
181 assert.equal(c.getA(), 0);
182 });
183
184 it('throws with incorrect set{R,G,B,A} arguments', function() {
185 var c = new sass.types.Color();
186
187 function assertJustOneArgument(cb) {
188 assert.throws(function() {
189 cb();
190 }, function(error) {
191 assert.ok(error instanceof TypeError);
192 assert.equal(error.message, 'Expected just one argument');
193
194 return true;
195 });
196 }
197
198 function assertNumberArgument(arg, cb) {
199 assert.throws(function() {
200 cb();
201 }, function(error) {
202 assert.ok(error instanceof TypeError);
203 assert.equal(error.message, 'Supplied value should be a number');
204
205 return true;
206 }, 'argument was: ' + arg);
207 }
208
209 assertJustOneArgument(function() { c.setR(); });
210 assertJustOneArgument(function() { c.setG(); });
211 assertJustOneArgument(function() { c.setB(); });
212 assertJustOneArgument(function() { c.setA(); });
213
214 assertJustOneArgument(function() { c.setR(1, 2); });
215 assertJustOneArgument(function() { c.setG(1, 2); });
216 assertJustOneArgument(function() { c.setB(1, 2); });
217 assertJustOneArgument(function() { c.setA(1, 2); });
218
219 [true, false, '0', '1', '', 'omg', {}, []].forEach(function(arg) {
220 assertNumberArgument(arg, function() { c.setR(arg); });
221 assertNumberArgument(arg, function() { c.setG(arg); });
222 assertNumberArgument(arg, function() { c.setB(arg); });
223 assertNumberArgument(arg, function() { c.setA(arg); });
224 });
225 });
226 });
227
228 describe('Error', function() {
229 it('exists', function() {
230 assert(sass.types.Error);
231 });
232
233 it('has a correctly named constructor', function() {
234 assert.equal(sass.types.Error.name, 'SassError');
235 });
236
237 it('supports call constructor', function() {
238 var e = sass.types.Error('Such Error');
239 assert.ok(e instanceof sass.types.Error);
240 assert.equal(e.toString(), '[object SassError]');
241
242 // TODO: I'm not sure this object works well, it likely needs to be fleshed out more...
243 });
244
245 it('supports new constructor', function() {
246 var e = new sass.types.Error('Such Error');
247 assert.ok(e instanceof sass.types.Error);
248 assert.equal(e.toString(), '[object SassError]');
249 // TODO: I'm not sure this object works well, it likely needs to be fleshed out more...
250 });
251 });
252
253 describe('List', function() {
254 it('exists', function() {
255 assert(sass.types.List);
256 });
257
258 it('has a corectly named constructor', function() {
259 assert.equal(sass.types.List.name, 'SassList');
260 });
261
262 it('support call constructor', function() {
263 var list = sass.types.List();
264 assert.ok(list instanceof sass.types.List);
265 assert.equal(list.toString(), '[object SassList]');
266 });
267
268 it('support new constructor', function() {
269 var list = new sass.types.List();
270 assert.ok(list instanceof sass.types.List);
271 assert.equal(list.toString(), '[object SassList]');
272 });
273
274 it('support variadic constructor', function() {
275 var a = new sass.types.List();
276 assert.equal(a.getLength(), 0);
277 assert.equal(a.getSeparator(), true);
278 var b = new sass.types.List(1);
279 assert.equal(b.getSeparator(), true);
280 assert.equal(b.getLength(), 1);
281 var c = new sass.types.List(1, true);
282 assert.equal(b.getLength(), 1);
283 assert.equal(c.getSeparator(), true);
284 var d = new sass.types.List(1, false);
285 assert.equal(b.getLength(), 1);
286 assert.equal(d.getSeparator(), false);
287 var e = new sass.types.List(1, true, 2);
288 assert.equal(b.getLength(), 1);
289 assert.equal(e.getSeparator(), true);
290
291 assert.throws(function() {
292 new sass.types.List('not-a-number');
293 }, function(error) {
294 // TODO: TypeError
295 assert.equal(error.message, 'First argument should be an integer.');
296 return true;
297 });
298
299 assert.throws(function() {
300 new sass.types.List(1, 'not-a-boolean');
301 }, function(error) {
302 // TODO: TypeError
303 assert.equal(error.message, 'Second argument should be a boolean.');
304 return true;
305 });
306 });
307
308 it('supports {get,set}Separator', function() {
309 var a = new sass.types.List();
310 assert.equal(a.getSeparator(), true);
311 assert.equal(a.setSeparator(true), undefined);
312 assert.equal(a.getSeparator(), true);
313 assert.equal(a.setSeparator(false), undefined);
314 assert.equal(a.getSeparator(), false);
315
316 assert.throws(function() {
317 a.setSeparator();
318 }, function(error) {
319 assert.ok(error instanceof TypeError);
320 assert.equal(error.message, 'Expected just one argument');
321 return true;
322 });
323
324 [1, '', [], {}].forEach(function(arg) {
325 assert.throws(function() {
326 a.setSeparator(arg);
327 }, function(error) {
328 assert.ok(error instanceof TypeError);
329 assert.equal(error.message, 'Supplied value should be a boolean');
330 return true;
331 }, 'setSeparator(' + arg + ')');
332 });
333 });
334
335 it('supports setValue and getValue', function() {
336 var a = new sass.types.List();
337
338 assert.throws(function() {
339 a.getValue();
340 }, function(error) {
341 assert.ok(error instanceof TypeError);
342 assert.equal(error.message, 'Expected just one argument');
343
344 return true;
345 });
346
347 ['hi', [], {}].forEach(function(arg) {
348 assert.throws(function() {
349 a.getValue(arg);
350 }, function(error) {
351 assert.ok(error instanceof TypeError);
352 assert.equal(error.message, 'Supplied index should be an integer');
353
354 return true;
355 }, 'getValue(' + arg + ')');
356 });
357
358 assert.throws(function() {
359 a.getValue(0);
360 }, function(error) {
361 assert.ok(error instanceof RangeError);
362 assert.equal(error.message, 'Out of bound index');
363
364 return true;
365 });
366
367 assert.throws(function() {
368 a.getValue(-1);
369 }, function(error) {
370 assert.ok(error instanceof RangeError);
371 assert.equal(error.message, 'Out of bound index');
372
373 return true;
374 });
375
376 assert.throws(function() {
377 a.setValue();
378 }, function(error) {
379 assert.ok(error instanceof TypeError);
380 assert.equal(error.message, 'Expected two arguments');
381 return true;
382 });
383
384 assert.throws(function() {
385 a.setValue(1);
386 }, function(error) {
387 assert.ok(error instanceof TypeError);
388 assert.equal(error.message, 'Expected two arguments');
389 return true;
390 });
391
392 assert.throws(function() {
393 a.setValue(0, 'no-a-sass-value');
394 }, function(error) {
395 assert.ok(error instanceof TypeError);
396 assert.equal(error.message, 'Supplied value should be a SassValue object');
397 return true;
398 });
399 });
400
401 // TODO: more complex set/get value scenarios
402 });
403
404 describe('Map', function() {
405 it('exists', function() {
406 assert(sass.types.Map);
407 });
408
409 it('has a correctly named constructor', function() {
410 assert.equal(sass.types.Map.name, 'SassMap');
411 });
412
413 it('supports call constructor', function() {
414 var x = sass.types.Map();
415 assert.equal(x.toString(), '[object SassMap]');
416 });
417
418 it('supports new constructor', function() {
419 var x = new sass.types.Map();
420 assert.equal(x.toString(), '[object SassMap]');
421 });
422
423 it('supports an optional constructor argument', function() {
424 var x = new sass.types.Map();
425 var y = new sass.types.Map(1);
426 var z = new sass.types.Map(2, 3);
427
428 assert.throws(function() {
429 new sass.types.Map('OMG');
430 }, function(error) {
431 assert.equal(error.message, 'First argument should be an integer.');
432 // TODO: TypeError
433
434 return true;
435 });
436
437 assert.equal(x.getLength(), 0);
438 assert.equal(y.getLength(), 1);
439 assert.equal(z.getLength(), 2);
440 });
441
442 it('supports length', function() {
443 var y = new sass.types.Map(1);
444 var z = new sass.types.Map(2);
445
446 assert.equal(y.getLength(), 1);
447 assert.equal(z.getLength(), 2);
448 });
449
450 it('supports {get,set}Value {get,set}Key', function() {
451 var y = new sass.types.Map(1);
452 var omg = new sass.types.String('OMG');
453 y.setValue(0, omg);
454 console.log(y.getValue(0));
455 });
456 });
457
458 describe('Null', function() {
459 it('exists', function() {
460 assert(sass.types.Null);
461 });
462
463 it('has a correctly named constructor', function() {
464 assert.equal(sass.types.Null.name, 'SassNull');
465 });
466
467 it('does not support new constructor', function() {
468 assert.throws(function() {
469 new sass.types.Null();
470 }, function(error) {
471 assert.ok(error instanceof TypeError);
472 assert.equal(error.message, 'Cannot instantiate SassNull');
473 return true;
474 });
475 });
476
477 it('supports call constructor (and is a singleton)', function() {
478 assert.equal(sass.types.Null(), sass.types.Null());
479 assert.equal(sass.types.Null(), sass.types.Null.NULL);
480 });
481 });
482
483 describe('Number', function() {
484 it('exists', function() {
485 assert(sass.types.Number);
486 });
487
488 it('has a correctly named constructor', function() {
489 assert.equal(sass.types.Number.name, 'SassNumber');
490 });
491
492 it('supports new constructor', function() {
493 var number = new sass.types.Number();
494 assert.equal(number.toString(), '[object SassNumber]');
495 });
496
497 it('supports call constructor', function() {
498 var number = sass.types.Number();
499 assert.equal(number.toString(), '[object SassNumber]');
500 });
501
502 it('supports multiple constructor arguments', function() {
503 var a = new sass.types.Number();
504 var b = new sass.types.Number(1);
505 var c = new sass.types.Number(2, 'px');
506
507 assert.throws(function() {
508 new sass.types.Number('OMG');
509 }, function(error) {
510 // TODO: TypeError
511 assert.equal(error.message, 'First argument should be a number.');
512 return true;
513 });
514
515 assert.throws(function() {
516 new sass.types.Number(1, 2);
517 }, function(error) {
518 // TODO: TypeError
519 assert.equal(error.message, 'Second argument should be a string.');
520 return true;
521 });
522
523 assert.equal(a.getValue(), 0);
524 assert.equal(a.getUnit(), '');
525 assert.equal(b.getValue(), 1);
526 assert.equal(b.getUnit(), '');
527 assert.equal(c.getValue(), 2);
528 assert.equal(c.getUnit(), 'px');
529 });
530
531 it('supports get{Unit,Value}, set{Unit,Value}', function() {
532 var number = new sass.types.Number(1, 'px');
533 assert.equal(number.getValue(), 1);
534 assert.equal(number.getUnit(), 'px');
535
536 number.setValue(2);
537 assert.equal(number.getValue(), 2);
538 assert.equal(number.getUnit(), 'px');
539
540 number.setUnit('em');
541 assert.equal(number.getValue(), 2);
542 assert.equal(number.getUnit(), 'em');
543
544 assert.throws(function() {
545 number.setValue('OMG');
546 }, function(error) {
547 assert.ok(error instanceof TypeError);
548 assert.equal(error.message, 'Supplied value should be a number');
549 return true;
550 });
551
552 assert.throws(function() {
553 number.setValue();
554 }, function(error) {
555 assert.ok(error instanceof TypeError);
556 assert.equal(error.message, 'Expected just one argument');
557 return true;
558 });
559
560 assert.throws(function() {
561 number.setUnit();
562 }, function(error) {
563 assert.ok(error instanceof TypeError);
564 assert.equal(error.message, 'Expected just one argument');
565 return true;
566 });
567
568 assert.throws(function() {
569 number.setUnit(1);
570 }, function(error) {
571 assert.ok(error instanceof TypeError);
572 assert.equal(error.message, 'Supplied value should be a string');
573 return true;
574 });
575 });
576 });
577
578 describe('String', function() {
579 it('exists', function() {
580 assert(sass.types.String);
581 });
582
583 it('has a properly named constructor', function() {
584 assert.equal(sass.types.String.name, 'SassString');
585 });
586
587 it('supports call constructor', function() {
588 var x = sass.types.String('OMG');
589
590 assert.equal(x.toString(), '[object SassString]');
591 assert.equal(x.getValue(), 'OMG');
592 });
593
594 it('supports new constructor', function() {
595 var x = new sass.types.String('OMG');
596
597 assert.equal(x.toString(), '[object SassString]');
598 assert.equal(x.getValue(), 'OMG');
599 });
600
601 it('supports multiple constructor arg combinations', function() {
602 new sass.types.String();
603 new sass.types.String('OMG');
604 new sass.types.String('OMG', 'NOPE');
605
606 [null, undefined, [], {}, function() { }].forEach(function(arg) {
607 assert.throws(function() {
608 new sass.types.String(arg);
609 }, function(error) {
610 // TODO: TypeError
611 assert.equal(error.message, 'Argument should be a string.');
612 return true;
613 });
614 });
615 });
616
617 it('supports {get,set}Value', function() {
618 var x = new sass.types.String();
619
620 assert.equal(x.getValue(), '');
621 assert.equal(x.setValue('hi'), undefined);
622 assert.equal(x.getValue(), 'hi');
623 assert.equal(x.setValue('bye'), undefined);
624 assert.equal(x.getValue(), 'bye');
625
626 assert.throws(function() {
627 x.setValue();
628 }, function(error) {
629 assert.ok(error instanceof TypeError);
630 assert.equal(error.message, 'Expected just one argument');
631 return true;
632 });
633
634 assert.throws(function() {
635 x.setValue('hi', 'hi');
636 }, function(error) {
637 assert.ok(error instanceof TypeError);
638 assert.equal(error.message, 'Expected just one argument');
639 return true;
640 });
641 });
642 });
643});