2023-11-27 19:32:08 +00:00
package eol
import (
"testing"
"io"
"strings"
2023-11-28 16:05:15 +00:00
"sourcecode.social/reiver/go-opt"
2023-11-27 19:32:08 +00:00
"sourcecode.social/reiver/go-utf8"
2024-02-18 17:27:47 +00:00
"sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/ls"
"sourcecode.social/reiver/go-eol/nel"
2023-11-27 19:32:08 +00:00
)
func TestReadThisRune ( t * testing . T ) {
tests := [ ] struct {
Value string
Rune rune
ExpectedSize int
} {
{
Value : "\n" ,
Rune : '\n' ,
ExpectedSize : 1 ,
} ,
{
Value : "\r" ,
Rune : '\r' ,
ExpectedSize : 1 ,
} ,
{
Value : "\u0085" ,
Rune : '\u0085' ,
ExpectedSize : 2 ,
} ,
{
Value : "\u2028" ,
Rune : '\u2028' ,
ExpectedSize : 3 ,
} ,
{
Value : "😈" ,
Rune : '😈' ,
ExpectedSize : 4 ,
} ,
{
Value : "\napple banana cherry" ,
Rune : '\n' ,
ExpectedSize : 1 ,
} ,
{
Value : "\rapple banana cherry" ,
Rune : '\r' ,
ExpectedSize : 1 ,
} ,
{
Value : "\u0085apple banana cherry" ,
Rune : '\u0085' ,
ExpectedSize : 2 ,
} ,
{
Value : "\u2028apple banana cherry" ,
Rune : '\u2028' ,
ExpectedSize : 3 ,
} ,
{
Value : "😈apple banana cherry" ,
Rune : '😈' ,
ExpectedSize : 4 ,
} ,
}
for testNumber , test := range tests {
var reader io . Reader = strings . NewReader ( test . Value )
var runescanner io . RuneScanner = utf8 . NewRuneScanner ( reader )
2023-11-28 16:05:15 +00:00
const characterNumber = 999
circumstance := specifyCircumstance ( opt . Something ( "UNIT-TEST" ) , characterNumber )
actualSize , err := readthisrune ( circumstance , runescanner , test . Rune )
2023-11-27 19:32:08 +00:00
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 ( "RUNE: %q" , test . Rune )
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 ( "RUNE: %q" , test . Rune )
continue
}
}
}
}
func TestReadThisRune_fail ( t * testing . T ) {
tests := [ ] struct {
Value string
Rune rune
2023-11-28 16:05:15 +00:00
EOLSequence opt . Optional [ string ]
2023-11-28 14:26:36 +00:00
RuneNumber uint64
2023-11-27 19:32:08 +00:00
ExpectedError string
} {
{
Value : "" ,
Rune : '\n' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( lf . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 7 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: problem reading character №7 of end-of-line sequence "\n": EOF ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : "" ,
Rune : '\r' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( cr . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 8 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: problem reading character №8 of end-of-line sequence "\r": EOF ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : "" ,
Rune : '\u0085' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( nel . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 9 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: problem reading character №9 of end-of-line sequence "\u0085": EOF ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : "" ,
Rune : '\u2028' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( ls . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 10 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: problem reading character №10 of end-of-line sequence "\u2028": EOF ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : "" ,
Rune : '😈' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "😈" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 11 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: problem reading character №11 of end-of-line sequence "😈": EOF ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : " \n" ,
Rune : '\n' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( lf . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 12 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: line-feed (LF) character ('\n') (U+000A) not found for end-of-line sequence "\n" character №12 — instead found ' ' (U+0020) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : " \r" ,
Rune : '\r' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( cr . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 13 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: carriage-return (CR) character ('\r') (U+000D) not found for end-of-line sequence "\r" character №13 — instead found ' ' (U+0020) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : " \u0085" ,
Rune : '\u0085' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( nel . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 14 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: next-line (NEL) character (U+0085) not found for end-of-line sequence "\u0085" character №14 — instead found ' ' (U+0020) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : " \u2028" ,
Rune : '\u2028' ,
2024-02-18 17:27:47 +00:00
EOLSequence : opt . Something ( ls . String ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 15 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: line-separator (LS) character (U+2028) not found for end-of-line sequence "\u2028" character №15 — instead found ' ' (U+0020) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : " 😈" ,
Rune : '😈' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "😈" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 16 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: '😈' character (U+1F608) not found for sequence "😈" character №16 — instead found ' ' (U+0020) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : ".\n" ,
Rune : '\n' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "\n" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 17 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: line-feed (LF) character ('\n') (U+000A) not found for end-of-line sequence "\n" character №17 — instead found '.' (U+002E) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : ".\r" ,
Rune : '\r' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "\r" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 18 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: carriage-return (CR) character ('\r') (U+000D) not found for end-of-line sequence "\r" character №18 — instead found '.' (U+002E) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : ".\u0085" ,
Rune : '\u0085' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "\u0085" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 19 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: next-line (NEL) character (U+0085) not found for end-of-line sequence "\u0085" character №19 — instead found '.' (U+002E) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : ".\u2028" ,
Rune : '\u2028' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "\u2028" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 20 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: line-separator (LS) character (U+2028) not found for end-of-line sequence "\u2028" character №20 — instead found '.' (U+002E) ` ,
2023-11-27 19:32:08 +00:00
} ,
{
Value : ".😈" ,
Rune : '😈' ,
2023-11-28 16:05:15 +00:00
EOLSequence : opt . Something ( "😈" ) ,
2023-11-28 14:26:36 +00:00
RuneNumber : 21 ,
2023-11-28 16:05:15 +00:00
ExpectedError : ` eol: '😈' character (U+1F608) not found for sequence "😈" character №21 — 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 )
2023-11-28 16:05:15 +00:00
var circumstance internalCircumstance = specifyCircumstance ( test . EOLSequence , test . RuneNumber )
actualSize , err := readthisrune ( circumstance , runescanner , test . Rune )
2023-11-27 19:32:08 +00:00
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 ( "RUNE: %q" , test . Rune )
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 ( "RUNE: %q" , test . Rune )
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 )
t . Logf ( "RUNE: %q" , test . Rune )
continue
}
}
}
}