parseutils_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package shared
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestDecodeEntities(t *testing.T) {
  7. tests := []struct {
  8. str string
  9. res string
  10. }{
  11. {"", ""},
  12. {"foo", "foo"},
  13. {"&lt;foo&gt;", "<foo>"},
  14. {"a &quot;b&quot; &apos;c&apos;", "a \"b\" 'c'"},
  15. {"foo &amp;&amp; bar", "foo && bar"},
  16. {"&#34;foo&#34;", "\"foo\""},
  17. {"&#x61;&#x062;&#x0063;", "abc"},
  18. {"r&#xe9;sum&#x00E9;", "résumé"},
  19. }
  20. for _, test := range tests {
  21. res, err := DecodeEntities(test.str)
  22. assert.Nil(t, err, "cannot decode %q", test.str)
  23. assert.Equal(t, res, test.res,
  24. "%q was decoded to %q instead of %q",
  25. test.str, res, test.res)
  26. }
  27. }
  28. func TestDecodeEntitiesInvalid(t *testing.T) {
  29. tests := []string{
  30. // Predefined entities
  31. "&", // truncated
  32. "&foo", // truncated
  33. "&foo;", // unknown
  34. "&lt", // known but truncated
  35. // Numerical character references
  36. "&#", // truncated
  37. "&#;", // missing number
  38. "&#x;", // missing hexadecimal number
  39. "&#12a;", // invalid decimal number
  40. "&#xfoo;", // invalid hexadecimal number
  41. }
  42. for _, test := range tests {
  43. res, err := DecodeEntities(test)
  44. assert.NotNil(t, err, "%q was decoded to %q", test, res)
  45. }
  46. }