UNPKG

2.79 kBJavaScriptView Raw
1'use strict';
2
3var assert = require('assert');
4var constaninople = require('../');
5
6describe('isConstant(src)', function() {
7 it('handles "[5 + 3 + 10]"', function() {
8 assert(constaninople.isConstant('[5 + 3 + 10]') === true);
9 });
10 it('handles "/[a-z]/i.test(\'a\')"', function() {
11 assert(constaninople.isConstant("/[a-z]/i.test('a')") === true);
12 });
13 it("handles \"{'class': [('data')]}\"", function() {
14 assert(constaninople.isConstant("{'class': [('data')]}") === true);
15 });
16 it('handles "Math.random()"', function() {
17 assert(constaninople.isConstant('Math.random()') === false);
18 });
19 it('handles "Math.random("', function() {
20 assert(constaninople.isConstant('Math.random(') === false);
21 });
22 it('handles "Math.floor(10.5)" with {Math: Math} as constants', function() {
23 assert(constaninople.isConstant('Math.floor(10.5)', {Math: Math}) === true);
24 });
25 it('handles "this.myVar"', function() {
26 assert(constaninople.isConstant('this.myVar') === false);
27 });
28 it('handles "(function () { while (true); return 10; }())"', function() {
29 assert(
30 constaninople.isConstant(
31 '(function () { while (true); return 10; }())'
32 ) === false
33 );
34 });
35 it('handles "({}).toString.constructor("console.log(1)")()"', function() {
36 assert(
37 constaninople.isConstant(
38 '({}).toString.constructor("console.log(1)")()'
39 ) === false
40 );
41 });
42});
43
44describe('toConstant(src)', function() {
45 it('handles "[5 + 3 + 10]"', function() {
46 assert.deepEqual(constaninople.toConstant('[5 + 3 + 10]'), [5 + 3 + 10]);
47 });
48 it('handles "/[a-z]/i.test(\'a\')"', function() {
49 assert(constaninople.toConstant("/[a-z]/i.test('a')") === true);
50 });
51 it("handles \"{'class': [('data')]}\"", function() {
52 assert.deepEqual(constaninople.toConstant("{'class': [('data')]}"), {
53 class: ['data'],
54 });
55 });
56 it('handles "Math.random()"', function() {
57 try {
58 constaninople.toConstant('Math.random()');
59 } catch (ex) {
60 return;
61 }
62 assert(false, 'Math.random() should result in an error');
63 });
64 it('handles "Math.random("', function() {
65 try {
66 constaninople.toConstant('Math.random(');
67 } catch (ex) {
68 return;
69 }
70 assert(false, 'Math.random( should result in an error');
71 });
72 it('handles "Math.floor(10.5)" with {Math: Math} as constants', function() {
73 assert(constaninople.toConstant('Math.floor(10.5)', {Math: Math}) === 10);
74 });
75 it('handles "(function () { while (true); return 10; }())"', function() {
76 try {
77 constaninople.toConstant('(function () { while (true); return 10; }())');
78 } catch (ex) {
79 return;
80 }
81 assert(
82 false,
83 '(function () { while (true); return 10; }()) should result in an error'
84 );
85 });
86});