nasin/config/file_test.go
2024-08-22 13:38:12 -04:00

147 lines
3.6 KiB
Go

package config
import "math"
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 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()
}
}