320 lines
4.7 KiB
Go
320 lines
4.7 KiB
Go
package line
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"testing"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type GlyphIndexTest struct {
|
|||
|
|
data string
|
|||
|
|
glyph int
|
|||
|
|
index int
|
|||
|
|
target int
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestGlyphIndex(t *testing.T) {
|
|||
|
|
var tests = []GlyphIndexTest{
|
|||
|
|
{
|
|||
|
|
data: "",
|
|||
|
|
index: 0,
|
|||
|
|
glyph: 0,
|
|||
|
|
target: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\n",
|
|||
|
|
index: 0,
|
|||
|
|
glyph: 0,
|
|||
|
|
target: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\t🤔",
|
|||
|
|
index: 0,
|
|||
|
|
glyph: 0,
|
|||
|
|
target: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\t🤔",
|
|||
|
|
index: 0,
|
|||
|
|
glyph: 0,
|
|||
|
|
target: 1,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\t🤔",
|
|||
|
|
index: 1,
|
|||
|
|
glyph: 8,
|
|||
|
|
target: 8,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\t🤔**",
|
|||
|
|
index: 5, // \t = 0, 🤔 = 1-4, * = 5
|
|||
|
|
glyph: 10, // \t = 0, 🤔 = 8, * = 10.
|
|||
|
|
target: 10, // *
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\t🤔*",
|
|||
|
|
index: 6, // \t = 0, 🤔 = 1-4, * = 5
|
|||
|
|
glyph: 11, // \t = 0, 🤔 = 8, * = 10.
|
|||
|
|
target: 11, // *
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
data: "你是谁?",
|
|||
|
|
index: 3, // 你 size is 3 bytes.
|
|||
|
|
glyph: 2, // 你 width is 2.
|
|||
|
|
target: 2, // 是
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "磨杵成针",
|
|||
|
|
index: 12, // 针
|
|||
|
|
glyph: 8, // 针
|
|||
|
|
target: 8, // Doesn't exist; return end of last rune.
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "你好!",
|
|||
|
|
index: 6, // !
|
|||
|
|
glyph: 4, // !
|
|||
|
|
target: 5,
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for n, test := range tests {
|
|||
|
|
i, g := New(test.data).FindGlyphIndex(test.target)
|
|||
|
|
if i != test.index {
|
|||
|
|
t.Errorf(
|
|||
|
|
"#%d: expected index %d, but got %d",
|
|||
|
|
n, test.index, i,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
if g != test.glyph {
|
|||
|
|
t.Errorf(
|
|||
|
|
"#%d: expected glyph %d, but got %d",
|
|||
|
|
n, test.glyph, g,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
type WidthTest struct {
|
|||
|
|
data string
|
|||
|
|
expected int
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestWidth(t *testing.T) {
|
|||
|
|
var tests = []WidthTest{
|
|||
|
|
{
|
|||
|
|
data: "",
|
|||
|
|
expected: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\n",
|
|||
|
|
expected: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\t",
|
|||
|
|
expected: 8,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "Hello, World!",
|
|||
|
|
expected: 13,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "Hello, 世界!",
|
|||
|
|
expected: 12,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "💻💩",
|
|||
|
|
expected: 4,
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for _, test := range tests {
|
|||
|
|
if w := New(test.data).Width(); w != test.expected {
|
|||
|
|
t.Errorf(
|
|||
|
|
"expected width %d, but got %d",
|
|||
|
|
test.expected, w,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
type RenderTest struct {
|
|||
|
|
data string
|
|||
|
|
offset int
|
|||
|
|
width int
|
|||
|
|
expected string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestRender(t *testing.T) {
|
|||
|
|
var tests = []RenderTest{
|
|||
|
|
{
|
|||
|
|
data: "",
|
|||
|
|
offset: 0,
|
|||
|
|
width: 10,
|
|||
|
|
expected: "",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "hello",
|
|||
|
|
offset: 0,
|
|||
|
|
width: 10,
|
|||
|
|
expected: "hello",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "hello",
|
|||
|
|
offset: 2,
|
|||
|
|
width: 2,
|
|||
|
|
expected: "ll",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "\ta",
|
|||
|
|
offset: 0,
|
|||
|
|
width: 9,
|
|||
|
|
expected: " a",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "世界",
|
|||
|
|
offset: 0,
|
|||
|
|
width: 3,
|
|||
|
|
expected: "世",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "世界",
|
|||
|
|
offset: 1,
|
|||
|
|
width: 3,
|
|||
|
|
expected: " 界",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "hello",
|
|||
|
|
offset: -1,
|
|||
|
|
width: 5,
|
|||
|
|
expected: "",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "hello",
|
|||
|
|
offset: 0,
|
|||
|
|
width: 0,
|
|||
|
|
expected: "",
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for n, test := range tests {
|
|||
|
|
got := New(test.data).Render(test.offset, test.width)
|
|||
|
|
if got != test.expected {
|
|||
|
|
t.Errorf(
|
|||
|
|
"#%d: expected %q, but got %q",
|
|||
|
|
n, test.expected, got,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
type InsertTest struct {
|
|||
|
|
data string
|
|||
|
|
index int
|
|||
|
|
r rune
|
|||
|
|
expected string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestInsert(t *testing.T) {
|
|||
|
|
var tests = []InsertTest{
|
|||
|
|
{
|
|||
|
|
data: "",
|
|||
|
|
index: 0,
|
|||
|
|
r: 'a',
|
|||
|
|
expected: "a",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "bc",
|
|||
|
|
index: 0,
|
|||
|
|
r: 'a',
|
|||
|
|
expected: "abc",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "ac",
|
|||
|
|
index: 1,
|
|||
|
|
r: 'b',
|
|||
|
|
expected: "abc",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "ab",
|
|||
|
|
index: 2,
|
|||
|
|
r: 'c',
|
|||
|
|
expected: "abc",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "世",
|
|||
|
|
index: 3,
|
|||
|
|
r: '界',
|
|||
|
|
expected: "世界",
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for n, test := range tests {
|
|||
|
|
l := New(test.data)
|
|||
|
|
l.Insert(test.index, test.r)
|
|||
|
|
if got := l.String(); got != test.expected {
|
|||
|
|
t.Errorf(
|
|||
|
|
"#%d: expected %q, but got %q",
|
|||
|
|
n, test.expected, got,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
type DeleteRuneTest struct {
|
|||
|
|
data string
|
|||
|
|
index int
|
|||
|
|
deleted rune
|
|||
|
|
expected string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestDeleteRune(t *testing.T) {
|
|||
|
|
var tests = []DeleteRuneTest{
|
|||
|
|
{
|
|||
|
|
data: "",
|
|||
|
|
index: 0,
|
|||
|
|
deleted: 0,
|
|||
|
|
expected: "",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "abc",
|
|||
|
|
index: 0,
|
|||
|
|
deleted: 'a',
|
|||
|
|
expected: "bc",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "abc",
|
|||
|
|
index: 1,
|
|||
|
|
deleted: 'b',
|
|||
|
|
expected: "ac",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "世界",
|
|||
|
|
index: 0,
|
|||
|
|
deleted: '世',
|
|||
|
|
expected: "界",
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
data: "世界",
|
|||
|
|
index: 3,
|
|||
|
|
deleted: '界',
|
|||
|
|
expected: "世",
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for n, test := range tests {
|
|||
|
|
l := New(test.data)
|
|||
|
|
r := l.DeleteRune(test.index)
|
|||
|
|
if r != test.deleted {
|
|||
|
|
t.Errorf(
|
|||
|
|
"#%d: expected deleted %q, but got %q",
|
|||
|
|
n, test.deleted, r,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
if got := l.String(); got != test.expected {
|
|||
|
|
t.Errorf(
|
|||
|
|
"#%d: expected %q, but got %q",
|
|||
|
|
n, test.expected, got,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|