redeyed-before-after-config.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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('\nbefore/after config, keywords', function(t) {
  17. var opts001 = { Keyword: { _default: { _before: '*', _after: '&' } } }
  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': { _before: '^' }
  27. , 'return': { _before: '(', _after: ')' }
  28. , _default: { _before: '*', _after: '&' }
  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. })