go-htmlescape/appendstring_test.go

74 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-02-13 05:35:22 +00:00
package htmlescape_test
import (
"testing"
"bytes"
"sourcecode.social/reiver/go-htmlescape"
)
func TestAppendString(t *testing.T) {
tests := []struct{
Bytes []byte
String string
Expected []byte
}{
2024-02-13 16:40:54 +00:00
{
Bytes: []byte("Hello world! 🙂 Take a look at this: "),
String: "",
Expected: []byte("Hello world! 🙂 Take a look at this: "),
},
2024-02-13 05:35:22 +00:00
{
Bytes: []byte("Hello world! 🙂 Take a look at this: "),
String: "This is NOT <b>bold</b>!",
Expected: []byte("Hello world! 🙂 Take a look at this: This is NOT &lt;b&gt;bold&lt;/b&gt;!"),
},
{
Bytes: []byte("Hello world! 🙂 Take a look at this: "),
String: `"this is a quotation"`,
Expected: []byte("Hello world! 🙂 Take a look at this: &#34;this is a quotation&#34;"),
},
{
Bytes: []byte("Hello world! 🙂 Take a look at this: "),
String: "\t3 < 5\r\n3 > 2\x00",
Expected: []byte("Hello world! 🙂 Take a look at this: &#x9;3 &lt; 5&#xD;&#xA;3 &gt; 2&#x0;"),
},
}
for testNumber, test := range tests {
var p []byte = append([]byte(nil), test.Bytes...)
actual, err := htmlescape.AppendString(p, test.String)
if nil != err {
t.Errorf("For test #%d, did not expect to get an error but actually got one." , testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("STRING: %q", test.String)
t.Logf("BYTES: %q", test.Bytes)
continue
}
expected := test.Expected
if !bytes.Equal(expected, actual) {
t.Errorf("For test #%d, the actual resulting value of the append is not what was expected.", testNumber)
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
t.Logf("STRING: %q", test.String)
t.Logf("BYTES: %q", test.Bytes)
continue
}
}
}