Add key value parsing tests

This commit is contained in:
Sasha Koshka 2024-04-27 20:17:00 -04:00
parent a5d4cd727b
commit 73ea9d304b
1 changed files with 170 additions and 0 deletions

170
key-value/key-value_test.go Normal file
View File

@ -0,0 +1,170 @@
package keyValue
import "strings"
import "testing"
import "git.tebibyte.media/tomo/xdg/locale"
func testParseString (test *testing.T, input string, correct File) {
got, err := Parse(strings.NewReader(input))
if err != nil {
test.Fatal("Parse function returned error:", err)
}
fail := func (v ...any) {
test.Logf (
"\n---input file---\n%s\n---correct---\n%v\n---got---\n%v\n",
input, correct, got)
test.Log(v...)
test.FailNow()
}
if len(got) != len(correct) {
fail("number of groups is different")
}
for name, group := range correct {
gotGroup, ok := got[name]
if !ok { fail("did not get group", name) }
if len(gotGroup) != len(group) {
fail("number of entries in", name, "is different")
}
for key, entry := range group {
gotEntry, ok := gotGroup[key]
if !ok { fail("did not get key", name, ">", key) }
if gotEntry.Value != entry.Value {
fail("value of ", name, ">", key, "is different")
}
if len(gotEntry.Localized) != len(entry.Localized) {
fail("number of localized values in", name, ">", key, "is different")
}
for loc, localized := range entry.Localized {
gotLocalized, ok := gotEntry.Localized[loc]
if !ok { fail("did not get value", name, ">", key, ">", loc) }
if gotLocalized != localized {
fail("value of", name, ">", key, ">", loc, "is different")
}
}
}
}
}
func testEntry (value string) Entry {
entry := newEntry()
entry.Value = value
return entry
}
func TestParseEmpty (test *testing.T) {
testParseString(test,
``,
File { })}
func TestParseFooReader (test *testing.T) {
// Directly from the spec
testParseString(test, `
[Desktop Entry]
Version=1.0
Type=Application
Name=Foo Viewer
Comment=The best viewer for Foo objects available!
TryExec=fooview
Exec=fooview %F
Icon=fooview
MimeType=image/x-foo;
Actions=Gallery;Create;
[Desktop Action Gallery]
Exec=fooview --gallery
Name=Browse Gallery
[Desktop Action Create]
Exec=fooview --create-new
Name=Create a new Foo!
Icon=fooview-new
`,
File {
"Desktop Entry": Group {
"Version": testEntry("1.0"),
"Type": testEntry("Application"),
"Name": testEntry("Foo Viewer"),
"Comment": testEntry("The best viewer for Foo objects available!"),
"TryExec": testEntry("fooview"),
"Exec": testEntry("fooview %F"),
"Icon": testEntry("fooview"),
"MimeType": testEntry("image/x-foo;"),
"Actions": testEntry("Gallery;Create;"),
},
"Desktop Action Gallery": Group {
"Exec": testEntry("fooview --gallery"),
"Name": testEntry("Browse Gallery"),
},
"Desktop Action Create": Group {
"Exec": testEntry("fooview --create-new"),
"Name": testEntry("Create a new Foo!"),
"Icon": testEntry("fooview-new"),
},
})}
func TestParseFooReaderLocalized (test *testing.T) {
testParseString(test, `
[Desktop Entry]
Version=1.0
Type=Application
Name=Foo Viewer
Comment=The best viewer for Foo objects available!
TryExec=fooview
Exec=fooview %F
Icon=fooview
MimeType=image/x-foo;
Actions=Gallery;Create;
[Desktop Action Gallery]
Exec=fooview --gallery
Name=Browse Gallery
[Desktop Action Create]
Exec=fooview --create-new
Name=Create a new Foo!
Name[en_US]=Create a new Foo!
Name[xx_XX.UTF-8]=Zweep zoop flooble glorp
Icon=fooview-new
`,
File {
"Desktop Entry": Group {
"Version": testEntry("1.0"),
"Type": testEntry("Application"),
"Name": testEntry("Foo Viewer"),
"Comment": testEntry("The best viewer for Foo objects available!"),
"TryExec": testEntry("fooview"),
"Exec": testEntry("fooview %F"),
"Icon": testEntry("fooview"),
"MimeType": testEntry("image/x-foo;"),
"Actions": testEntry("Gallery;Create;"),
},
"Desktop Action Gallery": Group {
"Exec": testEntry("fooview --gallery"),
"Name": testEntry("Browse Gallery"),
},
"Desktop Action Create": Group {
"Exec": testEntry("fooview --create-new"),
"Name": Entry {
Value: "Create a new Foo!",
Localized: map[locale.Locale] string {
locale.Locale {
Lang: "en",
Country: "US",
}: "Create a new Foo!",
locale.Locale {
Lang: "xx",
Country: "XX",
Encoding: "UTF-8",
}: "Zweep zoop flooble glorp",
},
},
"Icon": testEntry("fooview-new"),
},
})}