index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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]/.test(\'a\')"', function () {
  9. assert(constaninople.isConstant('/[a-z]/.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(constaninople.isConstant('(function () { while (true); return 10; }())') === false)
  28. })
  29. })
  30. describe('toConstant(src)', function () {
  31. it('handles "[5 + 3 + 10]"', function () {
  32. assert.deepEqual(constaninople.toConstant('[5 + 3 + 10]'), [5 + 3 + 10])
  33. })
  34. it('handles "/[a-z]/.test(\'a\')"', function () {
  35. assert(constaninople.toConstant('/[a-z]/.test(\'a\')') === true)
  36. })
  37. it('handles "{\'class\': [(\'data\')]}"', function () {
  38. assert.deepEqual(constaninople.toConstant('{\'class\': [(\'data\')]}'), {'class': ['data']})
  39. })
  40. it('handles "Math.random()"', function () {
  41. try {
  42. constaninople.toConstant('Math.random()')
  43. } catch (ex) {
  44. return
  45. }
  46. assert(false, 'Math.random() should result in an error')
  47. })
  48. it('handles "Math.random("', function () {
  49. try {
  50. constaninople.toConstant('Math.random(')
  51. } catch (ex) {
  52. return
  53. }
  54. assert(false, 'Math.random( should result in an error')
  55. })
  56. it('handles "Math.floor(10.5)" with {Math: Math} as constants', function () {
  57. assert(constaninople.toConstant('Math.floor(10.5)', {Math: Math}) === 10)
  58. })
  59. it('handles "(function () { while (true); return 10; }())"', function () {
  60. try {
  61. constaninople.toConstant('(function () { while (true); return 10; }())')
  62. } catch (ex) {
  63. return
  64. }
  65. assert(false, '(function () { while (true); return 10; }()) should result in an error')
  66. })
  67. })