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

data_test.go (3344B)


      1 package dati
      2 
      3 /*
      4 Copyright (C) 2021 gearsix <gearsix@tuta.io>
      5 
      6 This program is free software: you can redistribute it and/or modify
      7 it under the terms of the GNU General Public License as published by
      8 the Free Software Foundation, either version 3 of the License, or
      9 at your option) any later version.
     10 
     11 This program is distributed in the hope that it will be useful,
     12 but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 GNU General Public License for more details.
     15 
     16 You should have received a copy of the GNU General Public License
     17 along with this program.  If not, see <https://www.gnu.org/licenses/>.
     18 */
     19 
     20 import (
     21 	"encoding/json"
     22 	"github.com/pelletier/go-toml"
     23 	"gopkg.in/yaml.v3"
     24 	"os"
     25 	"strings"
     26 	"testing"
     27 )
     28 
     29 func TestIsSupportedDataLang(t *testing.T) {
     30 	exts := []string{
     31 		".json", "json", "JSON", ".JSON",
     32 		".yaml", "yaml", "YAML", ".YAML",
     33 		".toml", "toml", "TOML", ".TOML",
     34 		".misc", "-", ".", "",
     35 	}
     36 
     37 	for i, ext := range exts {
     38 		var target int
     39 		if i < 4 {
     40 			target = 0
     41 		} else if i < 8 {
     42 			target = 1
     43 		} else if i < 12 {
     44 			target = 2
     45 		} else {
     46 			target = -1
     47 		}
     48 
     49 		if IsSupportedDataLang(ext) != target {
     50 			if target == -1 {
     51 				t.Fatalf("%s is not a supported data language", ext)
     52 			} else {
     53 				t.Fatalf("%s did not return %s", ext, SupportedDataLangs[target])
     54 			}
     55 		}
     56 	}
     57 }
     58 
     59 var good = map[string]string{
     60 	"json": `{"eg":0}`,
     61 	"yaml": `eg: 0
     62 `,
     63 	"toml": `eg = 0
     64 `,
     65 }
     66 
     67 const badData = `{"json"!:2:]}}`
     68 
     69 func writeTestFile(t *testing.T, path string, Data string) {
     70 	f, e := os.Create(path)
     71 	defer f.Close()
     72 	if e != nil {
     73 		t.Skipf("setup failure: %s", e)
     74 	}
     75 	_, e = f.WriteString(Data)
     76 	if e != nil {
     77 		t.Skipf("setup failure: %s", e)
     78 	}
     79 
     80 	return
     81 }
     82 
     83 func validateData(t *testing.T, d interface{}, e error, lang string) {
     84 	var b []byte
     85 
     86 	if e != nil {
     87 		t.Fatal(e)
     88 	}
     89 
     90 	switch lang {
     91 	case "json":
     92 		b, e = json.Marshal(d)
     93 	case "yaml":
     94 		b, e = yaml.Marshal(d)
     95 	case "toml":
     96 		b, e = toml.Marshal(d)
     97 	}
     98 
     99 	if e != nil {
    100 		t.Error(e)
    101 	}
    102 	if string(b) != good[lang] {
    103 		t.Fatalf("incorrect %s: %s does not match %s", lang, b, good[lang])
    104 	}
    105 }
    106 
    107 func TestLoadData(t *testing.T) {
    108 	var d interface{}
    109 	var e error
    110 
    111 	for lang, data := range good {
    112 		e = LoadData(lang, strings.NewReader(data), &d)
    113 		validateData(t, d, e, lang)
    114 	}
    115 
    116 	if e = LoadData("json", strings.NewReader(badData), &d); e == nil {
    117 		t.Fatalf("bad data passed")
    118 	}
    119 	if e = LoadData("toml", strings.NewReader(""), &d); e != nil {
    120 		t.Fatalf("empty data failed %s, %s", d, e)
    121 	}
    122 	if e = LoadData("void", strings.NewReader("shouldn't pass"), &d); e == nil {
    123 		t.Fatalf("invalid data language passed")
    124 	}
    125 
    126 	return
    127 }
    128 
    129 func TestLoadDataFilepath(t *testing.T) {
    130 	var d interface{}
    131 	var e error
    132 	var p string
    133 	tdir := os.TempDir()
    134 
    135 	for lang, data := range good {
    136 		p = tdir + "/good." + lang
    137 		writeTestFile(t, p, data)
    138 		e = LoadDataFilepath(p, &d)
    139 		validateData(t, d, e, lang)
    140 	}
    141 
    142 	p = tdir + "/bad.json"
    143 	writeTestFile(t, p, badData)
    144 	e = LoadDataFilepath(p, &d)
    145 	if e == nil {
    146 		t.Fatalf("bad data passed")
    147 	}
    148 
    149 	p = tdir + "/empty.json"
    150 	writeTestFile(t, p, "")
    151 	e = LoadDataFilepath(p, &d)
    152 	if e != nil {
    153 		t.Fatalf("empty file failed: %s", e)
    154 	}
    155 
    156 	if e = LoadDataFilepath("non-existing-file.toml", &d); e == nil {
    157 		t.Fatalf("non-existing file passed: %s, %s", d, e)
    158 	}
    159 
    160 	return
    161 }