dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit faf54bb74b875988551ba7e70388b1bf45ff246f
parent 63a84422b418797f834a914bf8af415b0ae88fca
Author: gearsix <gearsix@tuta.io>
Date:   Sat,  6 Mar 2021 12:21:05 +0000

added tests for GenerateSuperData and MergeData

Diffstat:
Msrc/data_test.go | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 78 insertions(+), 17 deletions(-)

diff --git a/src/data_test.go b/src/data_test.go @@ -28,17 +28,23 @@ func writeTestFile(path string, Data string) (e error) { func TestLoadData(t *testing.T) { var d Data var e error + var b []byte if d, e = LoadData("json", strings.NewReader(goodJson1)); e != nil { t.Error(e) } else if len(d) == 0 { t.Error("no data loaded") + } else { + if b, e = json.Marshal(d); e != nil { + t.Error(e) + } else if string(b) != goodJson1 { + t.Errorf("incorrect json: %s does not match %s", b, goodJson1) + } } if d, e = LoadData("json", strings.NewReader(badJson)); e == nil { t.Error("bad.json passed") } - if d, e = LoadData("json", strings.NewReader("")); e == nil { t.Error("empty file passed") } @@ -76,16 +82,12 @@ func TestLoadDataFiles(t *testing.T) { t.Error(e) } else if string(b) == goodJson2 { t.Error("data returned out of order") - } else if string(b) != goodJson1 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson1) } if b, e = json.Marshal(d[1]); e != nil { t.Error(e) } else if string(b) == goodJson1 { t.Error("data returned out of order") - } else if string(b) != goodJson2 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson2) } } @@ -99,16 +101,12 @@ func TestLoadDataFiles(t *testing.T) { t.Error(e) } else if string(b) == goodJson1 { t.Error("data returned out of order") - } else if string(b) != goodJson2 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson1) } if b, e = json.Marshal(d[1]); e != nil { t.Error(e) } else if string(b) == goodJson2 { t.Error("data returned out of order") - } else if string(b) != goodJson1 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson2) } } @@ -122,16 +120,12 @@ func TestLoadDataFiles(t *testing.T) { t.Error(e) } else if string(b) == goodJson1 { t.Error("data returned out of order") - } else if string(b) != goodJson2 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson1) } if b, e = json.Marshal(d[1]); e != nil { t.Error(e) } else if string(b) == goodJson2 { t.Error("data returned out of order") - } else if string(b) != goodJson1 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson2) } } @@ -145,16 +139,83 @@ func TestLoadDataFiles(t *testing.T) { t.Error(e) } else if string(b) == goodJson2 { t.Error("data returned out of order") - } else if string(b) != goodJson1 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson1) } if b, e = json.Marshal(d[1]); e != nil { t.Error(e) } else if string(b) == goodJson1 { t.Error("data returned out of order") - } else if string(b) != goodJson2 { - t.Errorf("incorrect json: %s does not match %s", b, goodJson2) } } } + +func TestMergeData(t *testing.T) { + var e error + var d []Data + var m Data + + if m, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { + d = append(d, m) + } else { + t.Skip("setup failure:", e) + } + if m, e = LoadData("json", strings.NewReader(goodJson2)); e == nil { + d = append(d, m) + } else { + t.Skip("setup failure:", e) + } + + m = nil + m = MergeData(d...) + if m["example1"] == nil || m["example2"] == nil { + t.Error("missing global keys") + } +} + +func TestGenerateSuperData(t *testing.T) { + var data Data + var e error + var gd []Data + var d []Data + var sd Data + + if data, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { + gd = append(gd, data) + } else { + t.Skip("setup failure:", e) + } + if data, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { + gd = append(gd, data) + } else { + t.Skip("setup failure:", e) + } + if data, e = LoadData("json", strings.NewReader(goodJson2)); e == nil { + gd = append(gd, data) + } else { + t.Skip("setup failure:", e) + } + if data, e = LoadData("json", strings.NewReader(goodJson1)); e == nil { + d = append(d, data) + } else { + t.Skip("setup failure:", e) + } + if data, e = LoadData("json", strings.NewReader(goodJson2)); e == nil { + d = append(d, data) + } else { + t.Skip("setup failure:", e) + } + + + sd = GenerateSuperData("testdata", d, gd...) + if sd["testdata"] == nil { + t.Log(sd) + t.Error("datakey is empty") + } + if v, ok := sd["testdata"].([]interface{}); ok { + t.Log(sd) + t.Error("unable to infer datakey 'testdata'") + } else if len(v) == 2 { + t.Log(sd) + t.Error("datakey is missing data") + } +}