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

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