2018-01-28 02:39:03 +00:00
|
|
|
package markdown
|
2016-09-10 11:33:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2018-01-28 02:39:03 +00:00
|
|
|
|
|
|
|
"github.com/gomarkdown/markdown/html"
|
2016-09-10 11:33:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEsc(t *testing.T) {
|
2018-01-25 21:54:50 +00:00
|
|
|
t.Parallel()
|
2016-09-10 11:33:37 +00:00
|
|
|
tests := []string{
|
|
|
|
"abc", "abc",
|
|
|
|
"a&c", "a&c",
|
|
|
|
"<", "<",
|
|
|
|
"[]:<", "[]:<",
|
|
|
|
"Hello <!--", "Hello <!--",
|
|
|
|
}
|
|
|
|
for i := 0; i < len(tests); i += 2 {
|
|
|
|
var b bytes.Buffer
|
2018-01-28 02:39:03 +00:00
|
|
|
html.EscapeHTML(&b, []byte(tests[i]))
|
2016-09-10 11:33:37 +00:00
|
|
|
if !bytes.Equal(b.Bytes(), []byte(tests[i+1])) {
|
|
|
|
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
|
|
|
|
tests[i], tests[i+1], b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEscapeHTML(b *testing.B) {
|
|
|
|
tests := [][]byte{
|
|
|
|
[]byte(""),
|
|
|
|
[]byte("AT&T has an ampersand in their name."),
|
|
|
|
[]byte("AT&T is another way to write it."),
|
|
|
|
[]byte("This & that."),
|
|
|
|
[]byte("4 < 5."),
|
|
|
|
[]byte("6 > 5."),
|
|
|
|
[]byte("Here's a [link] [1] with an ampersand in the URL."),
|
2016-10-08 14:58:30 +00:00
|
|
|
[]byte("Here's a link with an ampersand in the link text: [AT&T] [2]."),
|
2016-09-10 11:33:37 +00:00
|
|
|
[]byte("Here's an inline [link](/script?foo=1&bar=2)."),
|
|
|
|
[]byte("Here's an inline [link](</script?foo=1&bar=2>)."),
|
|
|
|
[]byte("[1]: http://example.com/?foo=1&bar=2"),
|
|
|
|
[]byte("[2]: http://att.com/ \"AT&T\""),
|
|
|
|
}
|
2017-02-02 09:52:49 +00:00
|
|
|
var buf bytes.Buffer
|
2016-09-10 11:33:37 +00:00
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for _, t := range tests {
|
2018-01-28 02:39:03 +00:00
|
|
|
html.EscapeHTML(&buf, t)
|
2017-02-02 09:52:49 +00:00
|
|
|
buf.Reset()
|
2016-09-10 11:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|