text-parser.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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}}')
  15. it('should extract correct amount of tokens', function () {
  16. assert.strictEqual(tokens.length, 7)
  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. })
  23. it('should extract basic keys', function () {
  24. assert.strictEqual(tokens[1].key, 'a')
  25. })
  26. it('should trim extracted keys', function () {
  27. assert.strictEqual(tokens[3].key, 'bcd')
  28. })
  29. it('should extract nested keys', function () {
  30. assert.strictEqual(tokens[4].key, 'd.e.f')
  31. })
  32. it('should extract expressions', function () {
  33. assert.strictEqual(tokens[6].key, 'a + (b || c) ? d : e')
  34. })
  35. })
  36. describe('.buildRegex()', function () {
  37. before(function () {
  38. config.interpolateTags = {
  39. open: '<%',
  40. close: '%>'
  41. }
  42. TextParser.buildRegex()
  43. })
  44. it('should update the interpolate tags and work', function () {
  45. var tokens = TextParser.parse('hello <%a%>! <% bcd %><%d.e.f%> <%a + (b || c) ? d : e%>')
  46. assert.strictEqual(tokens.length, 7)
  47. assert.strictEqual(typeof tokens[0], 'string')
  48. assert.strictEqual(typeof tokens[2], 'string')
  49. assert.strictEqual(typeof tokens[5], 'string')
  50. assert.strictEqual(tokens[1].key, 'a')
  51. assert.strictEqual(tokens[3].key, 'bcd')
  52. assert.strictEqual(tokens[4].key, 'd.e.f')
  53. assert.strictEqual(tokens[6].key, 'a + (b || c) ? d : e')
  54. })
  55. after(function () {
  56. config.interpolateTags = {
  57. open: '{{',
  58. close: '}}'
  59. }
  60. TextParser.buildRegex()
  61. })
  62. })
  63. })