ut/utini/load_test.go

98 lines
1.9 KiB
Go
Raw Normal View History

package utini
import (
"testing"
"github.com/stretchr/testify/assert"
"opslag.de/schobers/ut/utio"
)
func AssertLoadMapString(t *testing.T, s string, v interface{}) {
file := AssertLoadString(t, s)
err := MapFromFile(file, v)
assert.Nil(t, err)
}
func AssertLoadString(t *testing.T, s string) *File {
lines := utio.Lines()
utio.DecodeFromString(s, lines)
file, err := load(lines.Lines)
assert.Nil(t, err)
return file
}
type testData struct {
NoSection string
Primitives testDataSectionPrimitives
OnlySub testDataSectionOnlySub
NoSection2 string
}
type testDataSectionPrimitives struct {
DelimitedString string
Integer int
SingleQuotes string
UnsignedByte uint8
}
type testDataSectionOnlySub struct {
Sub testDataSectionSub
}
type testDataSectionSub struct {
Test string
}
type testDataDefault struct {
Integer int `ini:"default=12345678"`
String string `ini:"default=default"`
}
var testData1IniFile = `no_section = "no section"
no_section2 = "no section 2"
[only_sub.sub]
test = "value"
[primitives]
delimited_string = "value"
integer = 12345678
single_quotes = 'single quote delimited"'
unsigned_byte = 101
`
var testData1Struct = testData{
NoSection: "no section",
NoSection2: "no section 2",
Primitives: testDataSectionPrimitives{
DelimitedString: "value",
Integer: 12345678,
SingleQuotes: "single quote delimited\"",
UnsignedByte: 101,
},
OnlySub: testDataSectionOnlySub{
Sub: testDataSectionSub{
Test: "value",
},
},
}
func TestMapFromFile(t *testing.T) {
file := AssertLoadString(t, testData1IniFile)
data := &testData{}
err := MapFromFile(file, data)
assert.Nil(t, err)
assert.Equal(t, testData1Struct, *data)
}
func TestLoadDefaultValues(t *testing.T) {
data := &testDataDefault{}
AssertLoadMapString(t, ``, data)
assert.Equal(t, 12345678, data.Integer)
assert.Equal(t, `default`, data.String)
}