go-eol/readnel_test.go

210 lines
5.2 KiB
Go
Raw Normal View History

2023-11-27 19:32:08 +00:00
package eol_test
import (
"testing"
"io"
"strings"
"sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-eol"
)
func TestReadNEL(t *testing.T) {
tests := []struct{
Value string
ExpectedSize int
}{
{
Value: "\u0085",
ExpectedSize: 2,
},
{
Value: "\u0085apple banana cherry",
ExpectedSize: 2,
},
}
for testNumber, test := range tests {
var reader io.Reader = strings.NewReader(test.Value)
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
actualSize, err := eol.ReadNEL(runescanner)
if nil != err {
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("VALUE: %q", test.Value)
continue
}
{
expected := test.ExpectedSize
actual := actualSize
if expected != actual {
t.Errorf("For test #%d, the actual size is not what was expected.", testNumber)
t.Logf("EXPECTED: %d", expected)
t.Logf("ACTUAL: %d", actual)
t.Logf("VALUE: %q", test.Value)
continue
}
}
}
}
func TestReadNEL_fail(t *testing.T) {
tests := []struct{
Value string
ExpectedError string
}{
{
Value: "",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: problem reading character №1 of end-of-line sequence: EOF`,
2023-11-27 19:32:08 +00:00
},
{
Value: "\n",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '\n' (U+000A)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "\r",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '\r' (U+000D)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "\u2028",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '\u2028' (U+2028)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "😈",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '😈' (U+1F608)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "\napple banana cherry",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '\n' (U+000A)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "\rapple banana cherry",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '\r' (U+000D)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "\u2028apple banana cherry",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '\u2028' (U+2028)`,
2023-11-27 19:32:08 +00:00
},
{
Value: "😈apple banana cherry",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '😈' (U+1F608)`,
2023-11-27 19:32:08 +00:00
},
{
Value: " \n",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found ' ' (U+0020)`,
2023-11-27 19:32:08 +00:00
},
{
Value: " \r",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found ' ' (U+0020)`,
2023-11-27 19:32:08 +00:00
},
{
Value: " \u0085",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found ' ' (U+0020)`,
2023-11-27 19:32:08 +00:00
},
{
Value: " \u2028",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found ' ' (U+0020)`,
2023-11-27 19:32:08 +00:00
},
{
Value: " 😈",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found ' ' (U+0020)`,
2023-11-27 19:32:08 +00:00
},
{
Value: ".\n",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '.' (U+002E)`,
2023-11-27 19:32:08 +00:00
},
{
Value: ".\r",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '.' (U+002E)`,
2023-11-27 19:32:08 +00:00
},
{
Value: ".\u0085",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '.' (U+002E)`,
2023-11-27 19:32:08 +00:00
},
{
Value: ".\u2028",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '.' (U+002E)`,
2023-11-27 19:32:08 +00:00
},
{
Value: ".😈",
2023-11-28 14:26:36 +00:00
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line character №1 — instead found '.' (U+002E)`,
2023-11-27 19:32:08 +00:00
},
}
for testNumber, test := range tests {
var reader io.Reader = strings.NewReader(test.Value)
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
actualSize, err := eol.ReadNEL(runescanner)
if nil == err {
t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber)
t.Logf("EXPECTED-ERROR: %q", test.ExpectedError)
t.Logf("VALUE: %q", test.Value)
continue
}
{
expected := test.ExpectedError
actual := err.Error()
if expected != actual {
t.Errorf("For test #%d, the actual error is not what was expected.", testNumber)
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
t.Logf("VALUE: %q", test.Value)
continue
}
}
{
expected := 0
actual := actualSize
if expected != actual {
t.Errorf("For test #%d, the actual size is not what was expected.", testNumber)
t.Logf("EXPECTED: %d", expected)
t.Logf("ACTUAL: %d", actual)
t.Logf("VALUE: %q", test.Value)
continue
}
}
}
}