nasin/config/file_test.go

193 lines
4.4 KiB
Go

package config
import "math"
import "maps"
import "strings"
import "testing"
func TestParseEntryUnquotedString (test *testing.T) {
testParseEntry(test, "stringValue = Unquoted\\nString", "stringValue", ValueString("Unquoted\nString"))
}
func TestParseEntryQuotedString (test *testing.T) {
testParseEntry(test, "stringValue = \"Quoted\\nString\"", "stringValue", ValueString("Quoted\nString"))
}
func TestParseEntryNumber (test *testing.T) {
testParseEntry(test, "numberValue = -349.29034", "numberValue", ValueNumber(-349.29034))
}
func TestParseEntryNumberInf (test *testing.T) {
testParseEntry(test, "numberValue = Inf", "numberValue", ValueNumber(math.Inf(1)))
}
func TestParseEntryNumberNegativeInf (test *testing.T) {
testParseEntry(test, "numberValue = -Inf", "numberValue", ValueNumber(math.Inf(-1)))
}
func TestParseEntryNumberNaN (test *testing.T) {
testParseEntry(test, "numberValue = NaN", "numberValue", ValueNumber(math.NaN()))
}
func TestParseEntryBoolTrue (test *testing.T) {
testParseEntry(test, "boolValue = true", "boolValue", ValueBool(true))
}
func TestParseEntryBoolFalse (test *testing.T) {
testParseEntry(test, "boolValue = false", "boolValue", ValueBool(false))
}
func TestParseEntryComplexKey (test *testing.T) {
testParseEntry (
test, "--Something.OtherThing...another-Thing-=value",
"--Something.OtherThing...another-Thing-", ValueString("value"))
}
func TestParse (test *testing.T) {
testParse(test,
` thing =something
# comment
otherThing = otherValue
otherThing = otherValue1
otherThing = otherValue2 # not a comment
`,
`thing="something"
# comment
otherThing="otherValue"
otherThing="otherValue1"
otherThing="otherValue2 # not a comment"
`)
}
func TestGetValue (test *testing.T) {
file := parseFileString(test,
`key=askdhj
# some comment
key = value
key1 = 7`)
got, err := file.Get("key")
if err != nil { test.Fatal(err) }
testValueString(test, got, `"value"`)
}
func TestModifyValue (test *testing.T) {
file := parseFileString(test,
`key=askdhj
# some comment
key = value
key1 = 7`)
err := file.Set("key", ValueNumber(324980.2349))
if err != nil { test.Fatal(err) }
testFileString(test, file,
`key="askdhj"
# some comment
key=324980.2349
key1=7
`)
}
func TestResetValue (test *testing.T) {
file := parseFileString(test,
`key=askdhj
# some comment
key = value
key1 = 7`)
err := file.Reset("key")
if err != nil { test.Fatal(err) }
testFileString(test, file,
`# some comment
key1=7
`)
}
func TestDiffNone (test *testing.T) {
str := `
thing = something
otherThing = otherValue
# comment
otherThing = true
otherThing = 234
yetAnotherThing = 0.23498
`
file1 := parseFileString(test, str)
file2 := parseFileString(test, str)
diff := file1.Diff(file2)
if len(diff) != 0 {
test.Fatalf("diff not empty:\n%v", diff)
}
}
func TestDiff (test *testing.T) {
file1 := parseFileString(test,
`key4=0
key1=value1
# comment
key2=34`)
file2 := parseFileString(test,
`key1=value2
key2=34
# comment
key3=0.2`)
diff := file1.Diff(file2)
correct := map[string] struct { } {
"key1": struct { } { },
"key3": struct { } { },
"key4": struct { } { },
}
if !maps.Equal(diff, correct) {
test.Error("diffs do not match")
test.Errorf("EXPECTED:\n%v", correct)
test.Errorf("GOT:\n%v", diff)
test.Fail()
}
}
func testParseEntry (test *testing.T, str string, key string, value Value) {
ent, err := parseEntry(str)
if err != nil { test.Fatal(err) }
if ent.key != key {
test.Fatalf("expected key: [%s]\ngot key: [%s]", key, ent.key)
}
if ent.value.String() != value.String() {
test.Fatalf("expected value: [%s]\ngot value: [%s]", value.String(), ent.value.String())
}
}
func testParse (test *testing.T, str, correct string) {
if correct == "" { correct = str }
testFileString(test, parseFileString(test, str), correct)
}
func parseFileString (test *testing.T, str string) *File {
file, err := Parse(strings.NewReader(str))
if err != nil { test.Fatal(err) }
return file
}
func testFileString (test *testing.T, file *File, correct string) {
got := strings.Builder { }
file.WriteTo(&got)
if got.String() != correct {
test.Error("strings do not match")
test.Errorf("EXPECTED:\n%s", correct)
test.Errorf("GOT:\n%s", got.String())
test.Fail()
}
}
func testValueString (test *testing.T, got Value, correct string) {
if got.String() != correct {
test.Error("strings do not match")
test.Errorf("EXPECTED:\n%s", correct)
test.Errorf("GOT:\n%s", got.String())
test.Fail()
}
}