text-parser.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. describe('UNIT: TextNode Parser', function () {
  2. var TextParser = require('vue/src/text-parser')
  3. describe('.parse()', function () {
  4. it('should return null if no interpolate tags are present', function () {
  5. var result = TextParser.parse('hello no tags')
  6. assert.strictEqual(result, null)
  7. })
  8. it('should ignore escaped tags', function () {
  9. var result = TextParser.parse('test {{key}} {{hello}}')
  10. assert.strictEqual(result.length, 3)
  11. assert.strictEqual(result[2], ' {{hello}}')
  12. })
  13. var tokens = TextParser.parse('hello {{a}}! {{ bcd }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}')
  14. it('should extract correct amount of tokens', function () {
  15. assert.strictEqual(tokens.length, 9)
  16. })
  17. it('should extract plain strings', function () {
  18. assert.strictEqual(typeof tokens[0], 'string')
  19. assert.strictEqual(typeof tokens[2], 'string')
  20. assert.strictEqual(typeof tokens[5], 'string')
  21. assert.strictEqual(typeof tokens[7], '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. it('should extract partials', function () {
  36. assert.strictEqual(tokens[8].key, '>test')
  37. })
  38. })
  39. describe('.parseAttr()', function () {
  40. it('should return Directive.parse friendly expression', function () {
  41. assert.strictEqual(TextParser.parseAttr('{{msg}}'), 'msg')
  42. assert.strictEqual(TextParser.parseAttr('{{msg + "123"}}'), 'msg + "123"')
  43. assert.strictEqual(TextParser.parseAttr('ha {{msg + "123"}} ho'), '"ha "+msg + "123"+" ho"')
  44. })
  45. })
  46. })