go-eol/readeol_test.go

242 lines
5.3 KiB
Go
Raw Normal View History

2023-11-27 15:13:38 +00:00
package eol_test
import (
"testing"
"io"
"strings"
"sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-eol"
2024-02-18 17:27:47 +00:00
"sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/crlf"
2024-03-26 06:24:08 +00:00
"sourcecode.social/reiver/go-eol/ff"
2024-02-18 17:27:47 +00:00
"sourcecode.social/reiver/go-eol/lf"
2024-02-18 18:19:16 +00:00
"sourcecode.social/reiver/go-eol/lfcr"
2024-02-18 17:27:47 +00:00
"sourcecode.social/reiver/go-eol/ls"
"sourcecode.social/reiver/go-eol/nel"
2024-03-26 06:05:26 +00:00
"sourcecode.social/reiver/go-eol/vt"
2023-11-27 15:13:38 +00:00
)
func TestReadEOL(t *testing.T) {
tests := []struct{
Value string
ExpectedEOL string
ExpectedSize int
}{
{
Value: "\n",
2024-02-18 17:27:47 +00:00
ExpectedEOL: lf.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 1,
},
2024-02-18 18:19:16 +00:00
{
Value: "\n\r",
ExpectedEOL: lfcr.String,
ExpectedSize: 2,
},
2024-03-26 06:07:21 +00:00
{
Value: "\v",
ExpectedEOL: vt.String,
ExpectedSize: 1,
},
2024-03-26 06:24:08 +00:00
{
Value: "\f",
ExpectedEOL: ff.String,
ExpectedSize: 1,
},
2023-11-27 15:13:38 +00:00
{
Value: "\r",
2024-02-18 17:27:47 +00:00
ExpectedEOL: cr.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 1,
},
{
Value: "\r\n",
2024-02-18 17:27:47 +00:00
ExpectedEOL: crlf.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 2,
},
{
Value: "\u0085",
2024-02-18 17:27:47 +00:00
ExpectedEOL: nel.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 2,
},
{
Value: "\u2028",
2024-02-18 17:27:47 +00:00
ExpectedEOL: ls.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 3,
},
{
Value: "\napple banana cherry",
2024-02-18 17:27:47 +00:00
ExpectedEOL: lf.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 1,
},
2024-03-26 06:24:08 +00:00
{
Value: "\n\rapple banana cherry",
ExpectedEOL: lfcr.String,
ExpectedSize: 2,
},
2024-03-26 06:05:26 +00:00
{
Value: "\vapple banana cherry",
ExpectedEOL: vt.String,
ExpectedSize: 1,
},
2024-02-18 18:19:16 +00:00
{
2024-03-26 06:24:08 +00:00
Value: "\fapple banana cherry",
ExpectedEOL: ff.String,
ExpectedSize: 1,
2024-02-18 18:19:16 +00:00
},
2023-11-27 15:13:38 +00:00
{
2024-03-26 06:24:08 +00:00
Value: "\rapple banana cherry",
2024-02-18 17:27:47 +00:00
ExpectedEOL: cr.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 1,
},
{
2024-03-26 06:24:08 +00:00
Value: "\r\napple banana cherry",
2024-02-18 17:27:47 +00:00
ExpectedEOL: crlf.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 2,
},
{
2024-03-26 06:24:08 +00:00
Value: "\u0085apple banana cherry",
2024-02-18 17:27:47 +00:00
ExpectedEOL: nel.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 2,
},
{
2024-03-26 06:24:08 +00:00
Value: "\u2028apple banana cherry",
2024-02-18 17:27:47 +00:00
ExpectedEOL: ls.String,
2023-11-27 15:13:38 +00:00
ExpectedSize: 3,
},
}
for testNumber, test := range tests {
var reader io.Reader = strings.NewReader(test.Value)
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
actualEOL, actualSize, err := eol.ReadEOL(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)
t.Logf("EXPECTED-EOL: %q", test.ExpectedEOL)
t.Logf("EXPECTED-SIZE: %d", test.ExpectedSize)
continue
}
{
expected := test.ExpectedEOL
actual := actualEOL
if expected != actual {
t.Errorf("For test #%d, the actual end-of-line sequence is not what was expected.", testNumber)
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
t.Logf("VALUE: %q", test.Value)
t.Logf("EXPECTED-SIZE: %d", test.ExpectedSize)
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)
t.Logf("EXPECTED-EOL: %q", test.ExpectedEOL)
continue
}
}
}
}
func TestReadEOL_fail(t *testing.T) {
tests := []struct{
Value string
ExpectedEOL string
ExpectedSize int
ExpectedError string
}{
{
Value: "apple",
ExpectedError: "eol: 'a' (U+0061) is not an end-of-line character",
},
{
Value: "banana",
ExpectedError: "eol: 'b' (U+0062) is not an end-of-line character",
},
{
Value: "cherry",
ExpectedError: "eol: 'c' (U+0063) is not an end-of-line character",
},
}
for testNumber, test := range tests {
var reader io.Reader = strings.NewReader(test.Value)
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
actualEOL, actualSize, err := eol.ReadEOL(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)
t.Logf("EXPECTED-EOL: %q", test.ExpectedEOL)
t.Logf("EXPECTED-SIZE: %d", test.ExpectedSize)
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)
t.Logf("EXPECTED-EOL: %q", test.ExpectedEOL)
t.Logf("EXPECTED-SIZE: %d", test.ExpectedSize)
continue
}
}
{
expected := test.ExpectedEOL
actual := actualEOL
if expected != actual {
t.Errorf("For test %d, the actual end-of-line sequence is not what was expected.", testNumber)
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
t.Logf("VALUE: %q", test.Value)
t.Logf("EXPECTED-SIZE: %d", test.ExpectedSize)
t.Logf("EXPECTED-ERROR: %q", test.ExpectedError)
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)
t.Logf("EXPECTED-EOL: %q", test.ExpectedEOL)
t.Logf("EXPECTED-ERROR: %q", test.ExpectedError)
continue
}
}
}
}