golang-lib/file/toml_test.go

69 lines
1.4 KiB
Go
Raw Normal View History

2018-08-23 21:02:51 +02:00
package file
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
2021-06-02 17:06:27 +02:00
func TestTOMLDuration(t *testing.T) {
assert := assert.New(t)
var d TOMLDuration
err := d.UnmarshalText([]byte("5m"))
assert.NoError(err)
err = d.UnmarshalText([]byte("5z"))
assert.Error(err)
txt, err := d.MarshalText()
assert.NoError(err)
assert.Equal("5m0s", string(txt))
}
2018-08-23 21:02:51 +02:00
func TestReadTOML(t *testing.T) {
assert := assert.New(t)
a := struct {
2022-08-14 16:50:52 +02:00
Text string `config:"text" toml:"text"`
2018-08-23 21:02:51 +02:00
}{}
err := ReadTOML("testfiles/donoexists", &a)
assert.Error(err, "could find file ^^")
err = ReadTOML("testfiles/trash.txt", &a)
assert.Error(err, "could marshel file ^^")
err = ReadTOML("testfiles/ok.toml", &a)
assert.NoError(err)
assert.Equal("hallo", a.Text)
}
2021-06-01 13:17:51 +02:00
func TestSaveTOML(t *testing.T) {
2018-08-23 21:02:51 +02:00
assert := assert.New(t)
2021-06-01 13:17:51 +02:00
type to struct {
2022-08-14 16:50:52 +02:00
Value int `config:"v" toml:"v"`
2021-06-01 13:17:51 +02:00
}
toSave := to{Value: 3}
2018-08-23 21:02:51 +02:00
tmpfile, _ := ioutil.TempFile("/tmp", "lib-json-testfile.json")
2021-06-01 13:17:51 +02:00
err := SaveTOML(tmpfile.Name(), &toSave)
2018-08-23 21:02:51 +02:00
assert.NoError(err, "could not save temp")
2021-06-01 13:17:51 +02:00
err = SaveTOML(tmpfile.Name(), 3)
2018-08-23 21:02:51 +02:00
assert.Error(err, "could not save func")
2021-06-01 13:17:51 +02:00
toSave.Value = 4
err = SaveTOML("/proc/readonly", &toSave)
2018-08-23 21:02:51 +02:00
assert.Error(err, "could not save to /dev/null")
2021-06-01 13:17:51 +02:00
testvalue := to{}
err = ReadTOML(tmpfile.Name(), &testvalue)
2018-08-23 21:02:51 +02:00
assert.NoError(err)
2021-06-01 13:17:51 +02:00
assert.Equal(3, testvalue.Value)
2018-08-23 21:02:51 +02:00
os.Remove(tmpfile.Name())
}