redeyed-function-config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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('\nfunction config, keywords', function(t) {
  17. var opts001 = { Keyword: { _default: function(s) { return '*' + s + '&' } } }
  18. t.test('\n# ' + inspect(opts001), function(t) {
  19. t.assertSurrounds('this', opts001, '*this&')
  20. t.assertSurrounds('this ', opts001, '*this& ')
  21. t.assertSurrounds(' this', opts001, ' *this&')
  22. t.assertSurrounds(' this ', opts001, ' *this& ')
  23. t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
  24. t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
  25. t.assertSurrounds(
  26. [ 'function foo (bar) {'
  27. , ' var a = 3;'
  28. , ' return bar + a;'
  29. , '}'
  30. ].join('\n')
  31. , opts001
  32. , [ '*function& foo (bar) {'
  33. , ' *var& a = 3;'
  34. , ' *return& bar + a;'
  35. , '}'
  36. ].join('\n'))
  37. t.end()
  38. })
  39. var opts002 = {
  40. Keyword: {
  41. 'function': function(s) { return '^' + s + '&' }
  42. , 'return': function(s) { return '(' + s + ')' }
  43. , _default: function(s) { return '*' + s + '&' }
  44. }
  45. }
  46. t.test('\n# ' + inspect(opts002), function(t) {
  47. t.assertSurrounds(
  48. [ 'function foo (bar) {'
  49. , ' var a = 3;'
  50. , ' return bar + a;'
  51. , '}'
  52. ].join('\n')
  53. , opts002
  54. , [ '^function& foo (bar) {'
  55. , ' *var& a = 3;'
  56. , ' (return) bar + a;'
  57. , '}'
  58. ].join('\n'))
  59. t.end()
  60. })
  61. t.end()
  62. })
  63. test('#\n functin config - resolving', function(t) {
  64. var opts001 = {
  65. Keyword: {
  66. 'var': function(s) { return '^' + s + '&' }
  67. }
  68. , _default: function(s) { return '*' + s + '&' }
  69. }
  70. t.test('\n# specific but no type default and root default - root default not applied' + inspect(opts001), function(t) {
  71. t.assertSurrounds('var n = new Test();', opts001, '^var& n = new Test();').end()
  72. })
  73. var opts002 = {
  74. Keyword: {
  75. 'var': function(s) { return '^' + s + '&' }
  76. , _default: function(s) { return '*' + s + '&' }
  77. }
  78. , _default: function(s) { return '(' + s + ')' }
  79. }
  80. t.test('\n# no type default but root default' + inspect(opts002), function(t) {
  81. t.assertSurrounds('var n = new Test();', opts002, '^var& n = *new& Test();').end()
  82. })
  83. t.end()
  84. })
  85. test('#\n function config - replacing', function(t) {
  86. var opts001 = {
  87. Keyword: {
  88. 'var': function() { return 'const' }
  89. }
  90. }
  91. t.test('\n# type default and root default (type wins)' + inspect(opts001), function(t) {
  92. t.assertSurrounds('var n = new Test();', opts001, 'const n = new Test();').end()
  93. })
  94. var opts002 = {
  95. Keyword: {
  96. _default: function() { return 'const' }
  97. }
  98. }
  99. t.test('\n# type default' + inspect(opts002), function(t) {
  100. t.assertSurrounds('var n = new Test();', opts002, 'const n = const Test();').end()
  101. })
  102. var opts003 = {
  103. Keyword: {
  104. 'new': function() { return 'NEW' }
  105. , _default: function() { return 'const' }
  106. }
  107. }
  108. t.test('\n# specific and type default' + inspect(opts003), function(t) {
  109. t.assertSurrounds('var n = new Test();', opts003, 'const n = NEW Test();').end()
  110. })
  111. var opts004 = {
  112. Keyword: {
  113. _default: function(s) { return s.toUpperCase() }
  114. }
  115. , _default: function(s) { return 'not applied' }
  116. }
  117. t.test('\n# type default and root default (type wins)' + inspect(opts004), function(t) {
  118. t.assertSurrounds('var n = new Test();', opts004, 'VAR n = NEW Test();').end()
  119. })
  120. var opts005 = {
  121. Keyword: { }
  122. , _default: function(s) { return s.toUpperCase() }
  123. }
  124. t.test('\n# no type default only root default - not applied' + inspect(opts005), function(t) {
  125. t.assertSurrounds('var n = new Test();', opts005, 'var n = new Test();').end()
  126. })
  127. t.end()
  128. })