escape-value.test.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. const tap = require('tap')
  3. const escapeValue = require('./escape-value')
  4. tap.test('throws for bad input', async t => {
  5. t.throws(
  6. () => escapeValue(42),
  7. Error('value must be a string')
  8. )
  9. })
  10. tap.test('reserved chars', t => {
  11. t.test('space', async t => {
  12. const input = ' has a leading and trailing space '
  13. const expected = '\\20has a leading and trailing space\\20'
  14. const result = escapeValue(input)
  15. t.equal(result, expected)
  16. })
  17. t.test('leading #', async t => {
  18. t.equal(escapeValue('#hashtag'), '\\23hashtag')
  19. })
  20. t.test('pompous name', async t => {
  21. t.equal(
  22. escapeValue('James "Jim" Smith, III'),
  23. 'James \\22Jim\\22 Smith\\2c III'
  24. )
  25. })
  26. t.test('carriage return', async t => {
  27. t.equal(escapeValue('Before\rAfter'), 'Before\\0dAfter')
  28. })
  29. t.end()
  30. })
  31. tap.test('2-byte utf-8', t => {
  32. t.test('Lučić', async t => {
  33. const expected = 'Lu\\c4\\8di\\c4\\87'
  34. t.equal(escapeValue('Lučić'), expected)
  35. })
  36. t.end()
  37. })
  38. tap.test('3-byte utf-8', t => {
  39. t.test('₠', async t => {
  40. t.equal(escapeValue('₠'), '\\e2\\82\\a0')
  41. })
  42. t.end()
  43. })
  44. tap.test('4-byte utf-8', t => {
  45. t.test('😀', async t => {
  46. t.equal(escapeValue('😀'), '\\f0\\9f\\98\\80')
  47. })
  48. t.end()
  49. })