fix(tests): fix all problems related to test

This commit is contained in:
verse91
2026-05-09 23:37:13 +07:00
parent 17f33ff480
commit e587f02798
11 changed files with 301 additions and 172 deletions
+27 -16
View File
@@ -1,4 +1,4 @@
package core_test
package tests
import (
"reflect"
@@ -9,21 +9,28 @@ import (
func TestTokenize(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{"", []string{""}},
{"git ", []string{"git", ""}},
{"git add", []string{"git", "add"}},
{"git commit -m \"hello world\"", []string{"git", "commit", "-m", "hello world"}},
{"git commit -m \"hello", []string{"git", "commit", "-m", "hello"}},
{"git commit -m 'hello world'", []string{"git", "commit", "-m", "hello world"}},
{"git commit -m 'hello", []string{"git", "commit", "-m", "hello"}},
{"ls -l \"file name\"", []string{"ls", "-l", "file name"}},
// REQUIREMENT: Empty input
{"Empty input", "", []string{""}},
// REQUIREMENT: Trailing space("git " -> 2 tokens, the last token is "")
{"Trailing space", "git ", []string{"git", ""}},
// REQUIREMENT: Multi-space("git add")
{"Multi-space", "git add", []string{"git", "add"}},
// REQUIREMENT: Quoted string("git commit -m \"hello world\"")
{"Quoted string", "git commit -m \"hello world\"", []string{"git", "commit", "-m", "hello world"}},
// REQUIREMENT: Quote not closed
{"Quote not closed", "git commit -m \"hello", []string{"git", "commit", "-m", "hello"}},
// REQUIREMENT: Single quote vs double quote
{"Single quote", "git commit -m 'hello world'", []string{"git", "commit", "-m", "hello world"}},
// REQUIREMENT: Backslash escape
{"Backslash escape", "git commit -m \"hello\\ world\"", []string{"git", "commit", "-m", "hello world"}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := core.Tokenize(tt.input)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("Tokenize(%q) = %v; want %v", tt.input, got, tt.expected)
@@ -34,19 +41,23 @@ func TestTokenize(t *testing.T) {
func TestHasPrefix(t *testing.T) {
tests := []struct {
name string
s string
prefix string
want bool
}{
{"Hello", "hel", true},
{"Thử nghiệm", "thử", true},
{"Iris", "Iris-Longer", false},
{"Iris", "", true},
{"", "a", false},
// REQUIREMENT: Case insensitive match
{"Case insensitive", "Hello", "hel", true},
// REQUIREMENT: Unicode support (Vietnamese)
{"Unicode support", "Thử nghiệm", "thử", true},
// REQUIREMENT: Prefix longer than string -> false
{"Prefix longer", "Iris", "Iris-Longer", false},
// REQUIREMENT: Empty prefix -> true
{"Empty prefix", "Iris", "", true},
}
for _, tt := range tests {
t.Run(tt.s+"_"+tt.prefix, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
if got := core.HasPrefix(tt.s, tt.prefix); got != tt.want {
t.Errorf("HasPrefix(%q, %q) = %v; want %v", tt.s, tt.prefix, got, tt.want)
}