redeyed-function-config-extra-params.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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('function - config passing idx and tokens', function(t) {
  9. var args = []
  10. var opts001 = {
  11. Boolean: {
  12. _default: identity
  13. }
  14. , Keyword: {
  15. _default: identity
  16. }
  17. , Identifier: {
  18. _default: identity
  19. }
  20. , Punctuator: {
  21. _default: identity
  22. }
  23. }
  24. var code = 'var fn = function () { return true; }'
  25. function identity(s, info) {
  26. args.push({ value: s, idx: info.tokenIndex, tokens: info.tokens, code: info.code })
  27. // returning unchanged string will keep the splits be equal to the original tokens
  28. return s
  29. }
  30. t.test(inspect(opts001) + ' -- ' + code, function(t) {
  31. var result = redeyed(code, opts001, { splits: true })
  32. var tokens = result.tokens
  33. t.equals(args.length, tokens.length, 'called with all tokens')
  34. for (var i = 0; i < tokens.length; i++) {
  35. var token = tokens[i]
  36. var arg = args[i]
  37. t.equals(arg.value, token.value, 'passes correct value: ' + inspect([ arg.value, token.value ]))
  38. t.equals(arg.idx, i, 'passes correct index')
  39. t.equals(arg.code, code, 'passes code')
  40. t.deepEquals(arg.tokens, tokens, 'passes all tokens')
  41. }
  42. t.end()
  43. })
  44. t.end()
  45. })