golang-lib/file/json_test.go

48 lines
1013 B
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 TestReadJSON(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 := ReadJSON("testfiles/donoexists", &a)
assert.Error(err, "could find file ^^")
err = ReadJSON("testfiles/trash.txt", &a)
assert.Error(err, "could marshel file ^^")
err = ReadJSON("testfiles/ok.json", &a)
assert.NoError(err)
assert.Equal("hallo", a.Text)
}
func TestSaveJSON(t *testing.T) {
assert := assert.New(t)
tmpfile, _ := ioutil.TempFile("/tmp", "lib-json-testfile.json")
err := SaveJSON(tmpfile.Name(), 3)
assert.NoError(err, "could not save temp")
err = SaveJSON(tmpfile.Name(), tmpfile.Name)
assert.Error(err, "could not save func")
2018-08-24 01:21:35 +02:00
err = SaveJSON("/proc/readonly", 4)
2018-08-23 21:02:51 +02:00
assert.Error(err, "could not save to /dev/null")
var testvalue int
err = ReadJSON(tmpfile.Name(), &testvalue)
assert.NoError(err)
assert.Equal(3, testvalue)
os.Remove(tmpfile.Name())
}