You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
2.4 KiB
Go
137 lines
2.4 KiB
Go
package parse
|
|
|
|
import (
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestReadLines(t *testing.T) {
|
|
// Create a temporary file and write some lines to it
|
|
tmpFile, err := os.CreateTemp("", "testfile")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
lines := []string{
|
|
"Line 1",
|
|
"Line 2",
|
|
"Line 3",
|
|
}
|
|
|
|
for _, line := range lines {
|
|
_, err := tmpFile.WriteString(line + "\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Close the file before reading from it
|
|
err = tmpFile.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
fileName := tmpFile.Name()
|
|
|
|
// Call the ReadLines function
|
|
result := ReadLines(fileName)
|
|
|
|
// Check if the lines read from file match the expected lines
|
|
for i, line := range result {
|
|
if line != lines[i] {
|
|
t.Errorf("Expected line %d to be '%s', but got '%s'", i+1, lines[i], line)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseIntArray(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
separator string
|
|
want []int64
|
|
expectFail bool
|
|
}{
|
|
{
|
|
name: "space separated",
|
|
input: "1 2 3",
|
|
separator: " ",
|
|
want: []int64{1, 2, 3},
|
|
},
|
|
{
|
|
name: "comma separated",
|
|
input: "1,2,3",
|
|
separator: ",",
|
|
want: []int64{1, 2, 3},
|
|
},
|
|
{
|
|
name: "with leading/trailing spaces",
|
|
input: " 1, 2, 3 ",
|
|
separator: ",",
|
|
want: []int64{1, 2, 3},
|
|
},
|
|
{
|
|
name: "empty string",
|
|
input: "",
|
|
separator: ",",
|
|
want: []int64{},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ParseIntArray[int64](tt.input, tt.separator)
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("ParseIntArray() = %v, want %v",
|
|
got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSplitNoBlank(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
sep string
|
|
want []string
|
|
}{
|
|
{
|
|
name: "no blank",
|
|
input: "abc",
|
|
sep: "",
|
|
want: []string{"a", "b", "c"},
|
|
},
|
|
{
|
|
name: "with blanks",
|
|
input: "a b c",
|
|
sep: " ",
|
|
want: []string{"a", "b", "c"},
|
|
},
|
|
{
|
|
name: "leading/trailing blanks",
|
|
input: " a b c ",
|
|
sep: " ",
|
|
want: []string{"a", "b", "c"},
|
|
},
|
|
{
|
|
name: "empty string",
|
|
input: "",
|
|
sep: " ",
|
|
want: []string{},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := SplitNoBlank(tt.input, tt.sep)
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("SplitNoBlank() = %v, want %v",
|
|
got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|