entity_test.go 1022 B

1234567891011121314151617181920212223242526272829
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package html
  5. import (
  6. "testing"
  7. "unicode/utf8"
  8. )
  9. func TestEntityLength(t *testing.T) {
  10. // We verify that the length of UTF-8 encoding of each value is <= 1 + len(key).
  11. // The +1 comes from the leading "&". This property implies that the length of
  12. // unescaped text is <= the length of escaped text.
  13. for k, v := range entity {
  14. if 1+len(k) < utf8.RuneLen(v) {
  15. t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v))
  16. }
  17. if len(k) > longestEntityWithoutSemicolon && k[len(k)-1] != ';' {
  18. t.Errorf("entity name %s is %d characters, but longestEntityWithoutSemicolon=%d", k, len(k), longestEntityWithoutSemicolon)
  19. }
  20. }
  21. for k, v := range entity2 {
  22. if 1+len(k) < utf8.RuneLen(v[0])+utf8.RuneLen(v[1]) {
  23. t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v[0]) + string(v[1]))
  24. }
  25. }
  26. }