xdg/locale/locale_test.go
2024-04-26 18:51:27 -04:00

107 lines
2.3 KiB
Go

package locale
import "testing"
func TestString (test *testing.T) {
testCase := func (correct string, locale Locale) {
got := locale.String()
if got != correct {
test.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", correct, got)
}
}
testCase("lang_COUNTRY.ENCODING@MODIFIER", Locale {
Lang: "lang",
Country: "COUNTRY",
Encoding: "ENCODING",
Modifier: "MODIFIER",
})
testCase("lang.ENCODING@MODIFIER", Locale {
Lang: "lang",
Encoding: "ENCODING",
Modifier: "MODIFIER",
})
testCase("lang_COUNTRY@MODIFIER", Locale {
Lang: "lang",
Country: "COUNTRY",
Modifier: "MODIFIER",
})
testCase("lang_COUNTRY.ENCODING", Locale {
Lang: "lang",
Country: "COUNTRY",
Encoding: "ENCODING",
})
testCase("lang_COUNTRY", Locale {
Lang: "lang",
Country: "COUNTRY",
})
testCase("lang.ENCODING", Locale {
Lang: "lang",
Encoding: "ENCODING",
})
testCase("lang@MODIFIER", Locale {
Lang: "lang",
Modifier: "MODIFIER",
})
}
func TestParse (test *testing.T) {
testCase := func (input string, correct Locale) {
got, err := Parse(input)
if err != nil {
test.Fatal(err)
}
if got != correct {
test.Fatalf("expected:\n\t%v\ngot:\n\t%v\n", correct, got)
}
}
testCase("lang_COUNTRY.ENCODING@MODIFIER", Locale {
Lang: "lang",
Country: "COUNTRY",
Encoding: "ENCODING",
Modifier: "MODIFIER",
})
testCase("lang.ENCODING@MODIFIER", Locale {
Lang: "lang",
Encoding: "ENCODING",
Modifier: "MODIFIER",
})
testCase("lang_COUNTRY@MODIFIER", Locale {
Lang: "lang",
Country: "COUNTRY",
Modifier: "MODIFIER",
})
testCase("lang_COUNTRY.ENCODING", Locale {
Lang: "lang",
Country: "COUNTRY",
Encoding: "ENCODING",
})
testCase("lang_COUNTRY", Locale {
Lang: "lang",
Country: "COUNTRY",
})
testCase("lang.ENCODING", Locale {
Lang: "lang",
Encoding: "ENCODING",
})
testCase("lang@MODIFIER", Locale {
Lang: "lang",
Modifier: "MODIFIER",
})
}
func TestParseErr (test *testing.T) {
testCase := func (input string, correct error) {
_, err := Parse(input)
if err != correct {
test.Fatalf("expected:\n\t%v\ngot:\n\t%v\n", correct, err)
}
}
testCase("lang.ENCODING.MODIFIER", ErrUnexpectedRune)
testCase("lang@COUNTRY.ENCODING", ErrUnexpectedRune)
testCase("lang.@COUNTRY.ENCODING", ErrUnexpectedRune)
testCase("_COUNTRY.ENCODING@MODIFIER", ErrLangEmpty)
}