redeyed-function-config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. })
  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. })
  84. test('#\n function config - replacing', function (t) {
  85. var opts001 = {
  86. Keyword: {
  87. 'var': function () { return 'const' }
  88. }
  89. };
  90. t.test('\n# type default and root default (type wins)' + inspect(opts001), function (t) {
  91. t.assertSurrounds('var n = new Test();', opts001, 'const n = new Test();').end();
  92. })
  93. var opts002 = {
  94. Keyword: {
  95. _default: function () { return 'const' }
  96. }
  97. };
  98. t.test('\n# type default' + inspect(opts002), function (t) {
  99. t.assertSurrounds('var n = new Test();', opts002, 'const n = const Test();').end();
  100. })
  101. var opts003 = {
  102. Keyword: {
  103. 'new': function () { return 'NEW'; }
  104. , _default: function () { return 'const' }
  105. }
  106. };
  107. t.test('\n# specific and type default' + inspect(opts003), function (t) {
  108. t.assertSurrounds('var n = new Test();', opts003, 'const n = NEW Test();').end();
  109. })
  110. var opts004 = {
  111. Keyword: {
  112. _default: function (s) { return s.toUpperCase() }
  113. }
  114. , _default: function (s) { return 'not applied'; }
  115. };
  116. t.test('\n# type default and root default (type wins)' + inspect(opts004), function (t) {
  117. t.assertSurrounds('var n = new Test();', opts004, 'VAR n = NEW Test();').end();
  118. })
  119. var opts005 = {
  120. Keyword: { }
  121. , _default: function (s) { return s.toUpperCase() }
  122. };
  123. t.test('\n# no type default only root default - not applied' + inspect(opts005), function (t) {
  124. t.assertSurrounds('var n = new Test();', opts005, 'var n = new Test();').end();
  125. })
  126. })