libgo: Merge from revision 18783:00cce3a34d7e of master library.
[official-gcc.git] / libgo / go / html / escape_test.go
blob2d7ad8ac266c22968578d2ef211abb9500805b8a
1 // Copyright 2013 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.
5 package html
7 import "testing"
9 type unescapeTest struct {
10 // A short description of the test case.
11 desc string
12 // The HTML text.
13 html string
14 // The unescaped text.
15 unescaped string
18 var unescapeTests = []unescapeTest{
19 // Handle no entities.
21 "copy",
22 "A\ttext\nstring",
23 "A\ttext\nstring",
25 // Handle simple named entities.
27 "simple",
28 "& > <",
29 "& > <",
31 // Handle hitting the end of the string.
33 "stringEnd",
34 "&amp &amp",
35 "& &",
37 // Handle entities with two codepoints.
39 "multiCodepoint",
40 "text &gesl; blah",
41 "text \u22db\ufe00 blah",
43 // Handle decimal numeric entities.
45 "decimalEntity",
46 "Delta = &#916; ",
47 "Delta = Δ ",
49 // Handle hexadecimal numeric entities.
51 "hexadecimalEntity",
52 "Lambda = &#x3bb; = &#X3Bb ",
53 "Lambda = λ = λ ",
55 // Handle numeric early termination.
57 "numericEnds",
58 "&# &#x &#128;43 &copy = &#169f = &#xa9",
59 "&# &#x €43 © = ©f = ©",
61 // Handle numeric ISO-8859-1 entity replacements.
63 "numericReplacements",
64 "Footnote&#x87;",
65 "Footnote‡",
67 // Handle single ampersand.
69 "copySingleAmpersand",
70 "&",
71 "&",
73 // Handle ampersand followed by non-entity.
75 "copyAmpersandNonEntity",
76 "text &test",
77 "text &test",
79 // Handle "&#".
81 "copyAmpersandHash",
82 "text &#",
83 "text &#",
87 func TestUnescape(t *testing.T) {
88 for _, tt := range unescapeTests {
89 unescaped := UnescapeString(tt.html)
90 if unescaped != tt.unescaped {
91 t.Errorf("TestUnescape %s: want %q, got %q", tt.desc, tt.unescaped, unescaped)
96 func TestUnescapeEscape(t *testing.T) {
97 ss := []string{
98 ``,
99 `abc def`,
100 `a & b`,
101 `a&amp;b`,
102 `a &amp b`,
103 `&quot;`,
104 `"`,
105 `"<&>"`,
106 `&quot;&lt;&amp;&gt;&quot;`,
107 `3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
108 `The special characters are: <, >, &, ' and "`,
110 for _, s := range ss {
111 if got := UnescapeString(EscapeString(s)); got != s {
112 t.Errorf("got %q want %q", got, s)