redeyed-string-config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict'
  2. var test = require('tape')
  3. var util = require('util')
  4. var redeyed = require('..')
  5. function inspect(obj) {
  6. return util.inspect(obj, false, 5, true)
  7. }
  8. test('adding custom asserts ... ', function(t) {
  9. t.constructor.prototype.assertSurrounds = function(code, opts, expected) {
  10. var result = redeyed(code, opts).code
  11. this.equals(result, expected, inspect(code) + ' => ' + inspect(expected))
  12. return this
  13. }
  14. t.end()
  15. })
  16. test('\nstring config, keywords', function(t) {
  17. var opts001 = { Keyword: { _default: '*:&' } }
  18. t.test('\n# ' + inspect(opts001), function(t) {
  19. t.assertSurrounds('this', opts001, '*this&')
  20. t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
  21. t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
  22. t.end()
  23. })
  24. var opts002 = {
  25. Keyword: {
  26. 'function': '^:'
  27. , 'return': '(:)'
  28. , _default: '*:&'
  29. }
  30. }
  31. t.test('\n# ' + inspect(opts002), function(t) {
  32. t.assertSurrounds(
  33. [ 'function foo (bar) {'
  34. , ' var a = 3;'
  35. , ' return bar + a;'
  36. , '}'
  37. ].join('\n')
  38. , opts002
  39. , [ '^function& foo (bar) {'
  40. , ' *var& a = 3;'
  41. , ' (return) bar + a;'
  42. , '}'
  43. ].join('\n'))
  44. t.end()
  45. })
  46. t.end()
  47. })
  48. test('\nstring configs resolve from type and root', function(t) {
  49. var code = 'var a = new Test();'
  50. function run(t, conf, expected, code_) {
  51. t.test('\n# ' + inspect(conf), function(t) {
  52. t.assertSurrounds(code_ || code, conf, expected)
  53. t.end()
  54. })
  55. }
  56. // at least the token kind has to be configured in order for the root_default to be applied
  57. // otherwise a root._default would affect all tokens, even the ones we want to leave unchanged
  58. run(t, { _default: '*:' }, 'var a = new Test();')
  59. t.test('\n\n# only before or after specified, but no root._default', function(t) {
  60. run(t, { Keyword: { _default: '*:' } }, '*var a = *new Test();')
  61. run(t, { Keyword: { _default: ':-' } }, 'var- a = new- Test();')
  62. t.end()
  63. })
  64. t.test('\n\n# resolve missing from root._default', function(t) {
  65. run(t, { Keyword: { _default: '*:' }, _default: '(:-' }, '*var- a = *new- Test();')
  66. run(t, { Keyword: { _default: ':-' }, _default: '*:)' }, '*var- a = *new- Test();')
  67. t.end()
  68. })
  69. t.test('\n\n# no resolve if all specified', function(t) {
  70. run(t, { Keyword: { _default: '+:-' }, _default: '*:)' }, '+var- a = +new- Test();')
  71. run(t, { Keyword: { _default: ':-' }, _default: ':)' }, 'var- a = new- Test();')
  72. t.end()
  73. })
  74. t.test('\n\n# resolve specific token no defaults', function(t) {
  75. run(t, { Keyword: { 'var': '*:' } }, '*var a = new Test();')
  76. run(t, { Keyword: { 'var': ':-' } }, 'var- a = new Test();')
  77. t.end()
  78. })
  79. t.test('\n\n# resolve specific token with type defaults', function(t) {
  80. run(t, { Keyword: { 'var': '*:', _default: ':-' } }, '*var- a = new- Test();')
  81. run(t, { Keyword: { 'var': '*:', _default: '(:-' } }, '*var- a = (new- Test();')
  82. run(t, { Keyword: { 'var': ':-', _default: '*:' } }, '*var- a = *new Test();')
  83. run(t, { Keyword: { 'var': ':-', _default: '*:)' } }, '*var- a = *new) Test();')
  84. run(t, { Keyword: { 'var': ':-', 'new': ':&', _default: '*:' } }, '*var- a = *new& Test();')
  85. t.end()
  86. })
  87. t.test(
  88. '\n\n# resolve specific token with root defaults, but no type default - root default not applied to unspecified tokens'
  89. , function(t) {
  90. run(t, { Keyword: { 'var': '*:' }, _default: ':-' }, '*var- a = new Test();')
  91. run(t, { Keyword: { 'var': ':-' }, _default: '*:' }, '*var- a = new Test();')
  92. t.end()
  93. }
  94. )
  95. t.test('\n\n# resolve specific token with type and root defaults', function(t) {
  96. run(t, { Keyword: { 'var': '*:', _default: '+:-' }, _default: ':)' }, '*var- a = +new- Test();')
  97. run(t, { Keyword: { 'var': ':-', _default: '*:+' }, _default: '(:' }, '*var- a = *new+ Test();')
  98. t.end()
  99. })
  100. t.test('all exact tokens undefined, but type default', function(t) {
  101. run(t, { 'Boolean': { 'true': undefined, 'false': undefined, _default: '+:-' } }, 'return +true- || +false-;', 'return true || false;')
  102. t.end()
  103. })
  104. t.end()
  105. })