text-parser.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var TextParser = require('seed/src/text-parser'),
  2. config = require('seed/src/config')
  3. describe('UNIT: TextNode Parser', function () {
  4. describe('.parse()', function () {
  5. it('should return null if no interpolate tags are present', function () {
  6. var result = TextParser.parse('hello no tags')
  7. assert.strictEqual(result, null)
  8. })
  9. it('should ignore escapped tags', function () {
  10. var result = TextParser.parse('test {{key}} {{hello}}')
  11. assert.strictEqual(result.length, 3)
  12. assert.strictEqual(result[2], ' {{hello}}')
  13. })
  14. var tokens = TextParser.parse('hello {{a}}! {{ bcd }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}')
  15. it('should extract correct amount of tokens', function () {
  16. assert.strictEqual(tokens.length, 9)
  17. })
  18. it('should extract plain strings', function () {
  19. assert.strictEqual(typeof tokens[0], 'string')
  20. assert.strictEqual(typeof tokens[2], 'string')
  21. assert.strictEqual(typeof tokens[5], 'string')
  22. assert.strictEqual(typeof tokens[7], 'string')
  23. })
  24. it('should extract basic keys', function () {
  25. assert.strictEqual(tokens[1].key, 'a')
  26. })
  27. it('should trim extracted keys', function () {
  28. assert.strictEqual(tokens[3].key, 'bcd')
  29. })
  30. it('should extract nested keys', function () {
  31. assert.strictEqual(tokens[4].key, 'd.e.f')
  32. })
  33. it('should extract expressions', function () {
  34. assert.strictEqual(tokens[6].key, 'a + (b || c) ? d : e')
  35. })
  36. it('should extract partials', function () {
  37. assert.strictEqual(tokens[8].key, '>test')
  38. })
  39. })
  40. describe('.buildRegex()', function () {
  41. before(function () {
  42. config.interpolateTags = {
  43. open: '<%',
  44. close: '%>'
  45. }
  46. TextParser.buildRegex()
  47. })
  48. it('should update the interpolate tags and work', function () {
  49. var tokens = TextParser.parse('hello <%a%>! <% bcd %><%d.e.f%> <%a + (b || c) ? d : e%>')
  50. assert.strictEqual(tokens.length, 7)
  51. assert.strictEqual(typeof tokens[0], 'string')
  52. assert.strictEqual(typeof tokens[2], 'string')
  53. assert.strictEqual(typeof tokens[5], 'string')
  54. assert.strictEqual(tokens[1].key, 'a')
  55. assert.strictEqual(tokens[3].key, 'bcd')
  56. assert.strictEqual(tokens[4].key, 'd.e.f')
  57. assert.strictEqual(tokens[6].key, 'a + (b || c) ? d : e')
  58. })
  59. after(function () {
  60. config.interpolateTags = {
  61. open: '{{',
  62. close: '}}'
  63. }
  64. TextParser.buildRegex()
  65. })
  66. })
  67. })