github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/html/escape_test.go (about) 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. 4 5 package html 6 7 import "testing" 8 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 16 } 17 18 var unescapeTests = []unescapeTest{ 19 // Handle no entities. 20 { 21 "copy", 22 "A\ttext\nstring", 23 "A\ttext\nstring", 24 }, 25 // Handle simple named entities. 26 { 27 "simple", 28 "& > <", 29 "& > <", 30 }, 31 // Handle hitting the end of the string. 32 { 33 "stringEnd", 34 "& &", 35 "& &", 36 }, 37 // Handle entities with two codepoints. 38 { 39 "multiCodepoint", 40 "text ⋛︀ blah", 41 "text \u22db\ufe00 blah", 42 }, 43 // Handle decimal numeric entities. 44 { 45 "decimalEntity", 46 "Delta = Δ ", 47 "Delta = Δ ", 48 }, 49 // Handle hexadecimal numeric entities. 50 { 51 "hexadecimalEntity", 52 "Lambda = λ = λ ", 53 "Lambda = λ = λ ", 54 }, 55 // Handle numeric early termination. 56 { 57 "numericEnds", 58 "&# &#x €43 © = ©f = ©", 59 "&# &#x €43 © = ©f = ©", 60 }, 61 // Handle numeric ISO-8859-1 entity replacements. 62 { 63 "numericReplacements", 64 "Footnote‡", 65 "Footnote‡", 66 }, 67 // Handle single ampersand. 68 { 69 "copySingleAmpersand", 70 "&", 71 "&", 72 }, 73 // Handle ampersand followed by non-entity. 74 { 75 "copyAmpersandNonEntity", 76 "text &test", 77 "text &test", 78 }, 79 // Handle "&#". 80 { 81 "copyAmpersandHash", 82 "text &#", 83 "text &#", 84 }, 85 } 86 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) 92 } 93 } 94 } 95 96 func TestUnescapeEscape(t *testing.T) { 97 ss := []string{ 98 ``, 99 `abc def`, 100 `a & b`, 101 `a&b`, 102 `a & b`, 103 `"`, 104 `"`, 105 `"<&>"`, 106 `"<&>"`, 107 `3&5==1 && 0<1, "0<1", a+acute=á`, 108 `The special characters are: <, >, &, ' and "`, 109 } 110 for _, s := range ss { 111 if got := UnescapeString(EscapeString(s)); got != s { 112 t.Errorf("got %q want %q", got, s) 113 } 114 } 115 }