Nigel Tao | 688a831 | 2010-12-08 07:55:03 +1100 | [diff] [blame] | 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 | |
| 5 | package html |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
Rob Pike | 30aa701 | 2011-11-08 15:40:58 -0800 | [diff] [blame] | 9 | "unicode/utf8" |
Nigel Tao | 688a831 | 2010-12-08 07:55:03 +1100 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | func TestEntityLength(t *testing.T) { |
| 13 | // We verify that the length of UTF-8 encoding of each value is <= 1 + len(key). |
| 14 | // The +1 comes from the leading "&". This property implies that the length of |
| 15 | // unescaped text is <= the length of escaped text. |
| 16 | for k, v := range entity { |
| 17 | if 1+len(k) < utf8.RuneLen(v) { |
| 18 | t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v)) |
| 19 | } |
Andrew Balholm | 816c972 | 2011-07-21 09:10:49 +1000 | [diff] [blame] | 20 | if len(k) > longestEntityWithoutSemicolon && k[len(k)-1] != ';' { |
| 21 | t.Errorf("entity name %s is %d characters, but longestEntityWithoutSemicolon=%d", k, len(k), longestEntityWithoutSemicolon) |
| 22 | } |
Nigel Tao | 688a831 | 2010-12-08 07:55:03 +1100 | [diff] [blame] | 23 | } |
| 24 | for k, v := range entity2 { |
| 25 | if 1+len(k) < utf8.RuneLen(v[0])+utf8.RuneLen(v[1]) { |
| 26 | t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v[0]) + string(v[1])) |
| 27 | } |
| 28 | } |
| 29 | } |