codeframe.spec.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { generateCodeFrame } from '../src/codeframe'
  2. describe('compiler: codeframe', () => {
  3. const source = `
  4. <div>
  5. <template key="one"></template>
  6. <ul>
  7. <li v-for="foobar">hi</li>
  8. </ul>
  9. <template key="two"></template>
  10. </div>
  11. `.trim()
  12. test('line near top', () => {
  13. const keyStart = source.indexOf(`key="one"`)
  14. const keyEnd = keyStart + `key="one"`.length
  15. expect(generateCodeFrame(source, keyStart, keyEnd)).toMatchSnapshot()
  16. })
  17. test('line in middle', () => {
  18. // should cover 5 lines
  19. const forStart = source.indexOf(`v-for=`)
  20. const forEnd = forStart + `v-for="foobar"`.length
  21. expect(generateCodeFrame(source, forStart, forEnd)).toMatchSnapshot()
  22. })
  23. test('line near bottom', () => {
  24. const keyStart = source.indexOf(`key="two"`)
  25. const keyEnd = keyStart + `key="two"`.length
  26. expect(generateCodeFrame(source, keyStart, keyEnd)).toMatchSnapshot()
  27. })
  28. test('multi-line highlights', () => {
  29. const source = `
  30. <div attr="some
  31. multiline
  32. attr
  33. ">
  34. </div>
  35. `.trim()
  36. const attrStart = source.indexOf(`attr=`)
  37. const attrEnd = source.indexOf(`">`) + 1
  38. expect(generateCodeFrame(source, attrStart, attrEnd)).toMatchSnapshot()
  39. })
  40. })