redeyed-before-after-config.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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('\nbefore/after config, keywords', function (t) {
  18. var opts001 = { Keyword: { _default: { _before: '*', _after: '&' } } };
  19. t.test('\n# ' + inspect(opts001), function (t) {
  20. t.assertSurrounds('this', opts001, '*this&')
  21. t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
  22. t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
  23. t.end()
  24. })
  25. var opts002 = {
  26. Keyword: {
  27. 'function': { _before: '^' }
  28. , 'return': { _before: '(', _after: ')' }
  29. , _default: { _before: '*' , _after: '&' }
  30. }
  31. };
  32. t.test('\n# ' + inspect(opts002), function (t) {
  33. t.assertSurrounds(
  34. [ 'function foo (bar) {'
  35. , ' var a = 3;'
  36. , ' return bar + a;'
  37. , '}'
  38. ].join('\n')
  39. , opts002
  40. , [ '^function& foo (bar) {'
  41. , ' *var& a = 3;'
  42. , ' (return) bar + a;'
  43. , '}'
  44. ].join('\n'))
  45. t.end()
  46. })
  47. })