redeyed-function-config.js 4.2 KB

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