70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package tokenize2
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func ExampleCursor_move() {
|
|
c := Cursor{}
|
|
fmt.Printf("after initialization : %s\n", c)
|
|
fmt.Printf("after 'some words' : %s\n", c.move("some words"))
|
|
fmt.Printf("after '\\n' : %s\n", c.move("\n"))
|
|
fmt.Printf("after '\\r\\nskip\\nlines' : %s\n", c.move("\r\nskip\nlines"))
|
|
|
|
// Output:
|
|
// after initialization : start of file
|
|
// after 'some words' : line 1, column 11
|
|
// after '\n' : line 2, column 1
|
|
// after '\r\nskip\nlines' : line 4, column 6
|
|
}
|
|
|
|
func ExampleCursor_String() {
|
|
c := Cursor{}
|
|
fmt.Println(c.String())
|
|
|
|
c.move("\nfoobar")
|
|
fmt.Println(c.String())
|
|
|
|
// Output:
|
|
// start of file
|
|
// line 2, column 7
|
|
}
|
|
|
|
func TestGivenCursor_WhenMoving_CursorIsUpdated(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
input []string
|
|
byte int
|
|
rune int
|
|
line int
|
|
column int
|
|
}{
|
|
{"No input at all", []string{""}, 0, 0, 0, 0},
|
|
{"One ASCII char", []string{"a"}, 1, 1, 0, 1},
|
|
{"Multiple ASCII chars", []string{"abc"}, 3, 3, 0, 3},
|
|
{"One newline", []string{"\n"}, 1, 1, 1, 0},
|
|
{"Carriage return", []string{"\r\r\r"}, 3, 3, 0, 3},
|
|
{"One UTF8 3 byte char", []string{"⌘"}, 3, 1, 0, 1},
|
|
{"Mixture", []string{"Hello\n\npretty\nW⌘O⌘R⌘L⌘D"}, 31, 23, 3, 9},
|
|
{"Multiple calls", []string{"hello", "world"}, 10, 10, 0, 10},
|
|
} {
|
|
c := Cursor{}
|
|
for _, s := range test.input {
|
|
c.move(s)
|
|
}
|
|
if c.Byte != test.byte {
|
|
t.Errorf("[%s] Unexpected byte offset %d (expected %d)", test.name, c.Byte, test.byte)
|
|
}
|
|
if c.Rune != test.rune {
|
|
t.Errorf("[%s] Unexpected rune offset %d (expected %d)", test.name, c.Rune, test.rune)
|
|
}
|
|
if c.Line != test.line {
|
|
t.Errorf("[%s] Unexpected line offset %d (expected %d)", test.name, c.Line, test.line)
|
|
}
|
|
if c.Column != test.column {
|
|
t.Errorf("[%s] Unexpected column offset %d (expected %d)", test.name, c.Column, test.column)
|
|
}
|
|
}
|
|
}
|