From 688a83128d1304c2a1fe62416ca7e6d703faad3e Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 8 Dec 2010 07:55:03 +1100 Subject: [PATCH] html: move the sanity checking of the entity map from runtime (during init) to test-time (via gotest). R=gri CC=golang-dev https://golang.org/cl/3466044 --- src/pkg/html/entity.go | 20 -------------------- src/pkg/html/entity_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/pkg/html/entity_test.go diff --git a/src/pkg/html/entity.go b/src/pkg/html/entity.go index f12d5f061d..1530290cb3 100644 --- a/src/pkg/html/entity.go +++ b/src/pkg/html/entity.go @@ -4,10 +4,6 @@ package html -import ( - "utf8" -) - // entity is a map from HTML entity names to their values. The semicolon matters: // http://www.whatwg.org/specs/web-apps/current-work/multipage/named-character-references.html // lists both "amp" and "amp;" as two separate entries. @@ -2252,19 +2248,3 @@ var entity2 = map[string][2]int{ "vsupnE;": {'\u2ACC', '\uFE00'}, "vsupne;": {'\u228B', '\uFE00'}, } - -func init() { - // We verify that the length of UTF-8 encoding of each value is <= 1 + len(key). - // The +1 comes from the leading "&". This property implies that the length of - // unescaped text is <= the length of escaped text. - for k, v := range entity { - if 1+len(k) < utf8.RuneLen(v) { - panic("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v)) - } - } - for k, v := range entity2 { - if 1+len(k) < utf8.RuneLen(v[0])+utf8.RuneLen(v[1]) { - panic("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v[0]) + string(v[1])) - } - } -} diff --git a/src/pkg/html/entity_test.go b/src/pkg/html/entity_test.go new file mode 100644 index 0000000000..a1eb4d4f01 --- /dev/null +++ b/src/pkg/html/entity_test.go @@ -0,0 +1,26 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package html + +import ( + "testing" + "utf8" +) + +func TestEntityLength(t *testing.T) { + // We verify that the length of UTF-8 encoding of each value is <= 1 + len(key). + // The +1 comes from the leading "&". This property implies that the length of + // unescaped text is <= the length of escaped text. + for k, v := range entity { + if 1+len(k) < utf8.RuneLen(v) { + t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v)) + } + } + for k, v := range entity2 { + if 1+len(k) < utf8.RuneLen(v[0])+utf8.RuneLen(v[1]) { + t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v[0]) + string(v[1])) + } + } +} -- 2.50.0