commit 514c02e9a0510f7912ef79225215d64b03ee8eb8
parent fdd4844ed2ce30662a427a90d623284e95697ae4
Author: gearsix <gearsix@tuta.io>
Date: Sun, 14 Feb 2021 16:02:37 +0000
added tests for data.go funcs
Diffstat:
A | src/data_test.go | | | 83 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 83 insertions(+), 0 deletions(-)
diff --git a/src/data_test.go b/src/data_test.go
@@ -0,0 +1,83 @@
+package main
+
+import (
+ "strings"
+ "testing"
+ "os"
+)
+
+const goodJson = `{"example": 1}`
+const badJson = `{"example":2:]}}`
+
+func writeTestFile(path string, data string) (e error) {
+ var f *os.File
+
+ if f, e = os.Create(path); e != nil {
+ return
+ }
+ if _, e = f.WriteString(data); e != nil {
+ return
+ }
+ f.Close()
+
+ return
+}
+
+func TestLoadDataFiles(t *testing.T) {
+ var e error
+ var p []string
+ var d map[string]data
+ tdir := t.TempDir()
+
+ p = append(p, tdir+"/good.json")
+ if e = writeTestFile(p[0], goodJson); e != nil {
+ t.Skip("setup failure:", e)
+ }
+ p = append(p, tdir+"/bad.json")
+ if e = writeTestFile(p[1], badJson); e != nil {
+ t.Skip("setup failure:", e)
+ }
+
+ d = LoadDataFiles(tdir)
+ if len(d) == len(p) {
+ t.Error("bad.json passed")
+ }
+ d = LoadDataFiles(p...)
+ if len(d) == len(p) {
+ t.Error("bad.json passed")
+ }
+}
+
+func TestLoadDataFile(t *testing.T) {
+ var e error
+ tdir := t.TempDir()
+
+ if e = writeTestFile(tdir+"good.json", goodJson); e != nil {
+ t.Skip("setup failure:", e)
+ }
+ if e = writeTestFile(tdir+"bad.json", badJson); e != nil {
+ t.Skip("setup failure:", e)
+ }
+
+ if _, e = LoadDataFile(tdir+"good.json"); e != nil {
+ t.Error(e)
+ }
+ if _, e = LoadDataFile(tdir+"bad.json"); e == nil {
+ t.Error("bad.json passed")
+ }
+
+ return
+}
+
+func TestLoadData(t *testing.T) {
+ var e error
+
+ if _, e = LoadData("json", strings.NewReader(goodJson)); e != nil {
+ t.Error(e)
+ }
+ if _, e = LoadData("json", strings.NewReader(badJson)); e == nil {
+ t.Error("bad.json passed")
+ }
+
+ return
+}