golang-lib/file/toml_test.go

54 lines
1.1 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"
)
func TestReadTOML(t *testing.T) {
assert := assert.New(t)
a := struct {
Text string `toml:"text"`
}{}
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 {
Value int `toml:"v"`
}
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())
}