From df63c8bd2eeb061c8f90fb3f0fec73a3f4ddaa18 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Tue, 10 Jul 2018 21:33:46 -0700 Subject: [PATCH] utf8s.RuneScanner --- runescanner_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/runescanner_test.go b/runescanner_test.go index 2654980..4d45d3a 100644 --- a/runescanner_test.go +++ b/runescanner_test.go @@ -596,3 +596,65 @@ func TestRuneScanners(t *testing.T) { } } } + +func TestRuneScannerUnread(t *testing.T) { + + tests := []struct{ + Reader io.Reader + Instructions []rune + ExpectedRune []rune + ExpectedSize []int + }{ + { + Reader: strings.NewReader("a ≡ b\r\n۰۱۲۳۴۵۶۷۸۹ \U00010001"), + Instructions: []rune{'r', 'u', 'r', 'u', 'u', 'r', 'r', 'u', 'r', 'r', 'u', 'r', 'u', 'u', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'u', 'u', 'u', 'r', 'r', 'r', 'r', 'r', 'r', 'r'}, + ExpectedRune: []rune{'a', 'a', 'a', ' ', ' ', '≡', '≡', '≡', ' ', 'b', '\r', '\n', '۰', '۱', '۲', '۳', '۴', '۵', '۵', '۶', '۷', '۸', '۹', ' ', '\U00010001'}, + ExpectedSize: []int{ 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 4}, + }, + } + + + TestLoop: for testNumber, test := range tests { + + runeScanner := NewRuneScanner(test.Reader) + + var readCount int + for instructionNumber, instruction := range test.Instructions { + + switch instruction { + case 'r': // = rea + + actualRune, actualSize, err := runeScanner.ReadRune() + if nil != err { + t.Errorf("For test #%d and instruction #%d, did not expected an error, but actually got one: (%T) %q", testNumber, instructionNumber, err, err) + continue TestLoop + } + + expectedRune := test.ExpectedRune[readCount] + expectedSize := test.ExpectedSize[readCount] + + if expected, actual := expectedRune, actualRune; expected != actual { + t.Errorf("For test #%d and instruction #%d, expected rune %q / %d, but actually got %q / %d", testNumber, instructionNumber, expected, expected, actual, actual) + continue TestLoop + } + + if expected, actual := expectedSize, actualSize; expected != actual { + t.Errorf("For test #%d and instruction #%d, for rune %q / %d expected size %d, but actually got size %d", testNumber, instructionNumber, expectedRune, expectedRune, expected, actual) + continue TestLoop + } + + readCount++ + case 'u': // = unread + if err := runeScanner.UnreadRune(); nil != err { + t.Errorf("For test #%d and instruction #%d, did not expected an error, but actually got one: (%T) %q", testNumber, instructionNumber, err, err) + continue TestLoop + } + default: + t.Errorf("For test #%d and instruction #%d, UNKNOWN INSTRUCTION!!!:... %q", testNumber, instructionNumber, instruction) + continue TestLoop + } + + } + + } +}