2022-04-05 07:07:01 +02:00
|
|
|
package lib
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-10-11 07:04:59 +02:00
|
|
|
"reflect"
|
2022-01-09 06:59:28 +01:00
|
|
|
"testing"
|
2022-01-10 01:39:13 +01:00
|
|
|
"time"
|
2022-04-05 07:07:01 +02:00
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2022-09-21 07:28:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/hctx"
|
2022-11-26 19:31:43 +01:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-10-28 06:53:47 +02:00
|
|
|
"github.com/ddworken/hishtory/shared/testutils"
|
2023-09-24 01:40:03 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2023-09-24 04:35:21 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Set env variable
|
|
|
|
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")()
|
|
|
|
os.Setenv("HISHTORY_TEST", "1")
|
2023-11-10 08:00:20 +01:00
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireEntriesEqual(t *testing.T, expected, actual data.HistoryEntry) {
|
|
|
|
require.Equal(t, normalizeEntryTimezone(expected), normalizeEntryTimezone(actual))
|
2023-09-24 04:35:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
func TestPersist(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, hctx.InitConfig())
|
2022-09-21 07:28:40 +02:00
|
|
|
db := hctx.GetDb(hctx.MakeContext())
|
2022-04-07 07:43:07 +02:00
|
|
|
|
2022-10-28 06:53:47 +02:00
|
|
|
entry := testutils.MakeFakeHistoryEntry("ls ~/")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, db.Create(entry).Error)
|
2022-04-08 05:59:40 +02:00
|
|
|
var historyEntries []*data.HistoryEntry
|
2022-04-07 07:43:07 +02:00
|
|
|
result := db.Find(&historyEntries)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, result.Error)
|
2022-04-07 07:43:07 +02:00
|
|
|
if len(historyEntries) != 1 {
|
|
|
|
t.Fatalf("DB has %d entries, expected 1!", len(historyEntries))
|
|
|
|
}
|
|
|
|
dbEntry := historyEntries[0]
|
2023-11-10 08:00:20 +01:00
|
|
|
requireEntriesEqual(t, entry, *dbEntry)
|
2022-04-07 07:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearch(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, hctx.InitConfig())
|
2022-11-01 20:15:35 +01:00
|
|
|
ctx := hctx.MakeContext()
|
|
|
|
db := hctx.GetDb(ctx)
|
2022-04-07 07:43:07 +02:00
|
|
|
|
|
|
|
// Insert data
|
2022-10-28 06:53:47 +02:00
|
|
|
entry1 := testutils.MakeFakeHistoryEntry("ls /foo")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, db.Create(entry1).Error)
|
2022-10-28 06:53:47 +02:00
|
|
|
entry2 := testutils.MakeFakeHistoryEntry("ls /bar")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, db.Create(entry2).Error)
|
2022-04-07 07:43:07 +02:00
|
|
|
|
|
|
|
// Search for data
|
2022-11-01 20:15:35 +01:00
|
|
|
results, err := Search(ctx, db, "ls", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-04-07 07:43:07 +02:00
|
|
|
if len(results) != 2 {
|
2022-11-13 15:35:03 +01:00
|
|
|
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
|
2022-04-07 07:43:07 +02:00
|
|
|
}
|
2023-11-10 08:00:20 +01:00
|
|
|
requireEntriesEqual(t, entry2, *results[0])
|
|
|
|
requireEntriesEqual(t, entry1, *results[1])
|
2023-02-14 07:32:42 +01:00
|
|
|
|
|
|
|
// Search but exclude bar
|
|
|
|
results, err = Search(ctx, db, "ls -bar", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 1 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 1, results=%#v", len(results), results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search but exclude foo
|
|
|
|
results, err = Search(ctx, db, "ls -foo", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 1 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 1, results=%#v", len(results), results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search but include / also
|
|
|
|
results, err = Search(ctx, db, "ls /", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 2 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 1, results=%#v", len(results), results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search but exclude slash
|
|
|
|
results, err = Search(ctx, db, "ls -/", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 0 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 0, results=%#v", len(results), results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests for escaping
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, db.Create(testutils.MakeFakeHistoryEntry("ls -baz")).Error)
|
2023-02-14 07:32:42 +01:00
|
|
|
results, err = Search(ctx, db, "ls", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 3 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 3, results=%#v", len(results), results)
|
|
|
|
}
|
|
|
|
results, err = Search(ctx, db, "ls -baz", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 2 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
|
|
|
|
}
|
|
|
|
results, err = Search(ctx, db, "ls \\-baz", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-14 07:32:42 +01:00
|
|
|
if len(results) != 1 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 1, results=%#v", len(results), results)
|
|
|
|
}
|
2023-02-19 06:46:51 +01:00
|
|
|
|
|
|
|
// A malformed search query, but we should just ignore the dash since this is a common enough thing
|
|
|
|
results, err = Search(ctx, db, "ls -", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-19 06:46:51 +01:00
|
|
|
if len(results) != 3 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 3, results=%#v", len(results), results)
|
|
|
|
}
|
2023-02-21 00:46:39 +01:00
|
|
|
|
|
|
|
// A search for an entry containing a backslash
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, db.Create(testutils.MakeFakeHistoryEntry("echo '\\'")).Error)
|
2023-02-21 00:46:39 +01:00
|
|
|
results, err = Search(ctx, db, "\\\\", 5)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-02-21 00:46:39 +01:00
|
|
|
if len(results) != 1 {
|
|
|
|
t.Fatalf("Search() returned %d results, expected 3, results=%#v", len(results), results)
|
|
|
|
}
|
2022-04-07 07:44:10 +02:00
|
|
|
}
|
2022-04-09 22:09:46 +02:00
|
|
|
|
2022-10-11 07:04:59 +02:00
|
|
|
func TestChunks(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
input []int
|
|
|
|
chunkSize int
|
|
|
|
output [][]int
|
|
|
|
}{
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 2, [][]int{{1, 2}, {3, 4}, {5}}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 3, [][]int{{1, 2, 3}, {4, 5}}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 1, [][]int{{1}, {2}, {3}, {4}, {5}}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 4, [][]int{{1, 2, 3, 4}, {5}}},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2022-11-26 19:31:43 +01:00
|
|
|
actual := shared.Chunks(tc.input, tc.chunkSize)
|
2022-10-11 07:04:59 +02:00
|
|
|
if !reflect.DeepEqual(actual, tc.output) {
|
|
|
|
t.Fatal("chunks failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 05:35:32 +02:00
|
|
|
func TestZshWeirdness(t *testing.T) {
|
|
|
|
testcases := []struct {
|
2022-11-13 01:30:59 +01:00
|
|
|
input string
|
|
|
|
output string
|
2022-10-18 05:35:32 +02:00
|
|
|
}{
|
2022-11-13 01:30:59 +01:00
|
|
|
{": 1666062975:0;bash", "bash"},
|
|
|
|
{": 16660:0;ls", "ls"},
|
|
|
|
{"ls", "ls"},
|
|
|
|
{"0", "0"},
|
|
|
|
{"hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv", "hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv"},
|
2022-10-18 05:35:32 +02:00
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2022-11-13 01:30:59 +01:00
|
|
|
actual := stripZshWeirdness(tc.input)
|
|
|
|
if !reflect.DeepEqual(actual, tc.output) {
|
2022-10-18 05:35:32 +02:00
|
|
|
t.Fatalf("weirdness failure for %#v", tc.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 19:03:49 +01:00
|
|
|
|
|
|
|
func TestParseTimeGenerously(t *testing.T) {
|
|
|
|
ts, err := parseTimeGenerously("2006-01-02T15:04:00-08:00")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02 T15:04:00 -08:00")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_T15:04:00_-08:00")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02T15:04:00")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_T15:04:00")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_15:04:00")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02T15:04")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_15:04")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2022-11-01 19:03:49 +01:00
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 0 || ts.Minute() != 0 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
2023-08-27 23:24:59 +02:00
|
|
|
ts, err = parseTimeGenerously("1693163976")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-08-27 23:24:59 +02:00
|
|
|
if ts.Year() != 2023 || ts.Month() != time.August || ts.Day() != 27 || ts.Hour() != 12 || ts.Minute() != 19 || ts.Second() != 36 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d %s", ts.Unix(), ts.GoString())
|
|
|
|
}
|
2022-11-01 19:03:49 +01:00
|
|
|
}
|
2023-02-14 07:26:02 +01:00
|
|
|
|
2023-02-21 00:46:39 +01:00
|
|
|
func TestUnescape(t *testing.T) {
|
2023-02-14 07:26:02 +01:00
|
|
|
testcases := []struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{"f bar", "f bar"},
|
|
|
|
{"f \\bar", "f bar"},
|
|
|
|
{"f\\:bar", "f:bar"},
|
|
|
|
{"f\\:bar\\", "f:bar"},
|
2023-02-14 17:47:38 +01:00
|
|
|
{"\\f\\:bar\\", "f:bar"},
|
|
|
|
{"", ""},
|
2023-02-21 00:46:39 +01:00
|
|
|
{"\\", ""},
|
|
|
|
{"\\\\", "\\"},
|
2023-02-14 07:26:02 +01:00
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2023-02-21 00:46:39 +01:00
|
|
|
actual := unescape(tc.input)
|
2023-02-14 07:26:02 +01:00
|
|
|
if !reflect.DeepEqual(actual, tc.output) {
|
|
|
|
t.Fatalf("unescape failure for %#v, actual=%#v", tc.input, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainsUnescaped(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
input string
|
|
|
|
token string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"f bar", "f", true},
|
|
|
|
{"f bar", "f bar", true},
|
|
|
|
{"f bar", "f r", false},
|
|
|
|
{"f bar", "f ", true},
|
|
|
|
{"foo:bar", ":", true},
|
|
|
|
{"foo:bar", "-", false},
|
|
|
|
{"foo\\:bar", ":", false},
|
|
|
|
{"foo\\-bar", "-", false},
|
|
|
|
{"foo\\-bar", "foo", true},
|
|
|
|
{"foo\\-bar", "bar", true},
|
|
|
|
{"foo\\-bar", "a", true},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
|
|
actual := containsUnescaped(tc.input, tc.token)
|
|
|
|
if !reflect.DeepEqual(actual, tc.expected) {
|
2023-12-13 07:20:49 +01:00
|
|
|
t.Fatalf("failure for containsUnescaped(%#v, %#v), actual=%#v", tc.input, tc.token, actual)
|
2023-02-14 07:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitEscaped(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
input string
|
|
|
|
char rune
|
|
|
|
limit int
|
|
|
|
expected []string
|
|
|
|
}{
|
2023-12-20 00:01:15 +01:00
|
|
|
// Basic tests
|
2023-02-14 07:26:02 +01:00
|
|
|
{"foo bar", ' ', 2, []string{"foo", "bar"}},
|
|
|
|
{"foo bar baz", ' ', 2, []string{"foo", "bar baz"}},
|
|
|
|
{"foo bar baz", ' ', 3, []string{"foo", "bar", "baz"}},
|
|
|
|
{"foo bar baz", ' ', 1, []string{"foo bar baz"}},
|
|
|
|
{"foo bar baz", ' ', -1, []string{"foo", "bar", "baz"}},
|
2023-12-20 00:01:15 +01:00
|
|
|
// Tests for escaping
|
2023-12-13 07:20:49 +01:00
|
|
|
{"foo\\ bar baz", ' ', -1, []string{"foo bar", "baz"}},
|
|
|
|
{"foo\\bar baz", ' ', -1, []string{"foobar", "baz"}},
|
|
|
|
{"foo\\bar baz foob", ' ', 2, []string{"foobar", "baz foob"}},
|
|
|
|
{"foo\\ bar\\ baz", ' ', -1, []string{"foo bar baz"}},
|
|
|
|
{"foo\\ bar\\ baz", ' ', -1, []string{"foo bar ", "baz"}},
|
2023-12-20 00:01:15 +01:00
|
|
|
// Tests for single quotes
|
|
|
|
{"'foo bar'", ' ', -1, []string{"foo bar"}},
|
|
|
|
{"'foo bar' ' '", ' ', -1, []string{"foo bar", " "}},
|
|
|
|
{"'foo bar baz' and", ' ', -1, []string{"foo bar baz", "and"}},
|
|
|
|
{"'foo bar baz", ' ', -1, []string{"'foo", "bar", "baz"}},
|
|
|
|
{"'foo bar baz\\''", ' ', -1, []string{"foo bar baz'"}},
|
|
|
|
{"cwd:'foo bar :baz\\''", ':', -1, []string{"cwd", "foo bar :baz'"}},
|
2024-01-08 03:56:30 +01:00
|
|
|
{"cwd:'foo bar :baz\\''", ' ', -1, []string{"cwd:foo bar \\:baz'"}},
|
2023-12-20 00:01:15 +01:00
|
|
|
// Tests for double quotes
|
2023-12-13 07:20:49 +01:00
|
|
|
{"\"foo bar\"", ' ', -1, []string{"foo bar"}},
|
|
|
|
{"\"foo bar\" \" \"", ' ', -1, []string{"foo bar", " "}},
|
|
|
|
{"\"foo bar baz\" and", ' ', -1, []string{"foo bar baz", "and"}},
|
2023-12-20 00:01:15 +01:00
|
|
|
{"\"foo bar baz", ' ', -1, []string{"\"foo", "bar", "baz"}},
|
2023-12-13 07:20:49 +01:00
|
|
|
{"\"foo bar baz\\\"\"", ' ', -1, []string{"foo bar baz\""}},
|
|
|
|
{"cwd:\"foo bar :baz\\\"\"", ':', -1, []string{"cwd", "foo bar :baz\""}},
|
2024-01-08 03:56:30 +01:00
|
|
|
{"cwd:\"foo bar :baz\\\"\"", ' ', -1, []string{"cwd:foo bar \\:baz\""}},
|
2023-12-20 00:01:15 +01:00
|
|
|
// Tests for complex quotes
|
|
|
|
{"\"foo'bar\"", ' ', -1, []string{"foo'bar"}},
|
|
|
|
{"'foo\"bar'", ' ', -1, []string{"foo\"bar"}},
|
|
|
|
{"\"foo'bar", ' ', -1, []string{"\"foo'bar"}},
|
|
|
|
{"'foo\"bar", ' ', -1, []string{"'foo\"bar"}},
|
|
|
|
{"\"foo'\\\"bar\"", ' ', -1, []string{"foo'\"bar"}},
|
|
|
|
{"'foo\"\\'bar'", ' ', -1, []string{"foo\"'bar"}},
|
|
|
|
{"''", ' ', -1, []string{""}},
|
|
|
|
{"\"\"", ' ', -1, []string{""}},
|
|
|
|
{"\\\"", ' ', -1, []string{"\""}},
|
|
|
|
{"\\'", ' ', -1, []string{"'"}},
|
|
|
|
// Tests the behavior of quotes with
|
|
|
|
{"it's", ' ', -1, []string{"it's"}},
|
|
|
|
{"'foo bar", ' ', -1, []string{"'foo", "bar"}},
|
|
|
|
// Tests for various complex/interesting escaping
|
2023-12-13 07:20:49 +01:00
|
|
|
{"ls \\-foo", ' ', -1, []string{"ls", "\\-foo"}},
|
|
|
|
{"ls \\-foo \\a \\\\", ' ', -1, []string{"ls", "\\-foo", "a", "\\\\"}},
|
|
|
|
{"foo:bar", ':', -1, []string{"foo", "bar"}},
|
|
|
|
{"foo:bar", ' ', -1, []string{"foo:bar"}},
|
|
|
|
{"foo\\:bar", ':', -1, []string{"foo\\:bar"}},
|
|
|
|
{"foo\\:bar", ' ', -1, []string{"foo\\:bar"}},
|
2024-01-08 03:56:30 +01:00
|
|
|
// Tests for quoting colons
|
|
|
|
{"foo:bar", ' ', -1, []string{"foo:bar"}},
|
|
|
|
{"'foo:bar'", ' ', -1, []string{"foo\\:bar"}},
|
|
|
|
{"\"foo:bar\"", ' ', -1, []string{"foo\\:bar"}},
|
2023-02-14 07:26:02 +01:00
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
|
|
actual := splitEscaped(tc.input, tc.char, tc.limit)
|
|
|
|
if !reflect.DeepEqual(actual, tc.expected) {
|
2023-12-13 07:20:49 +01:00
|
|
|
t.Fatalf("failure for splitEscaped(%#v, %#v, %#v), actual=%#v", tc.input, string(tc.char), tc.limit, actual)
|
2023-02-14 07:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|