index.js 2.7 KB

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