dati

data and template interface
git clone git://git.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit cd53c14313b6e89c5cbfb80c67434fbadfaf1651
parent 96e514da02afdb212bc6335828dd89615ec3625c
Author: gearsix <gearsix@tuta.io>
Date:   Wed, 28 Jul 2021 13:50:09 +0100

added tests for IsSupported*; improvements & fixes

Diffstat:
Mdata.go | 5++++-
Mdata_test.go | 32+++++++++++++++++++++++++++++++-
Mtemplate.go | 3++-
Mtemplate_test.go | 30++++++++++++++++++++++++++++++
4 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/data.go b/data.go @@ -26,6 +26,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" ) // SupportedDataLangs provides a list of supported languages for data files (lower-case) @@ -35,7 +36,8 @@ var SupportedDataLangs = []string{"json", "yaml", "toml"} // If `lang` is not in `SupportedDataLangs`, `-1` will be returned. // File extensions can be passed in `lang`, the prefixed `.` will be trimmed. func IsSupportedDataLang(lang string) int { - if lang[0] == '.' { + lang = strings.ToLower(lang) + if len(lang) > 0 && lang[0] == '.' { lang = lang[1:] } for i, l := range SupportedDataLangs { @@ -64,6 +66,7 @@ func LoadData(lang string, in io.Reader, outp interface{}) error { case 2: e = toml.Unmarshal(inbuf, outp) case -1: + fallthrough default: e = fmt.Errorf("'%s' is not a supported data language", lang) } diff --git a/data_test.go b/data_test.go @@ -26,6 +26,36 @@ import ( "testing" ) +func TestIsSupportedDataLang(t *testing.T) { + exts := []string{ + ".json", "json", "JSON", ".JSON", + ".yaml", "yaml", "YAML", ".YAML", + ".toml", "toml", "TOML", ".TOML", + ".misc", "-", ".", "", + } + + for i, ext := range exts { + var target int + if i < 4 { + target = 0 + } else if i < 8 { + target = 1 + } else if i < 12 { + target = 2 + } else { + target = -1 + } + + if IsSupportedDataLang(ext) != target { + if target == -1 { + t.Fatalf("%s is not a supported data language", ext) + } else { + t.Fatalf("%s did not return %s", ext, SupportedDataLangs[target]) + } + } + } +} + var good = map[string]string{ "json": `{"eg":0}`, "yaml": `eg: 0 @@ -90,7 +120,7 @@ func TestLoadData(t *testing.T) { t.Fatalf("empty data failed %s, %s", d, e) } if e = LoadData("void", strings.NewReader("shouldn't pass"), &d); e == nil { - t.Fatalf("invalid data language passed: %s, %s", d, e) + t.Fatalf("invalid data language passed") } return diff --git a/template.go b/template.go @@ -37,7 +37,8 @@ var SupportedTemplateLangs = []string{"tmpl", "hmpl", "mst"} // If `lang` is not in `SupportedTemplateLangs`, `-1` will be returned. // File extensions can be passed in `lang`, the prefixed `.` will be trimmed. func IsSupportedTemplateLang(lang string) int { - if lang[0] == '.' { + lang = strings.ToLower(lang) + if len(lang) > 0 && lang[0] == '.' { lang = lang[1:] } for i, l := range SupportedTemplateLangs { diff --git a/template_test.go b/template_test.go @@ -25,6 +25,36 @@ import ( "testing" ) +func TestIsSupportedTemplateLang(t *testing.T) { + exts := []string{ + ".tmpl", "tmpl", "TMPL", ".TMPL", + ".hmpl", "hmpl", "HMPL", ".HMPL", + ".mst", "mst", "MST", ".MST", + ".NONE", "-", ".", "", + } + + for i, ext := range exts { + var target int + if i < 4 { + target = 0 + } else if i < 8 { + target = 1 + } else if i < 12 { + target = 2 + } else { + target = -1 + } + + if IsSupportedTemplateLang(ext) != target { + if target == -1 { + t.Fatalf("%s is not a supported data language", ext) + } else { + t.Fatalf("%s did not return %s", ext, SupportedTemplateLangs[target]) + } + } + } +} + const tmplRootGood = "{{.eg}} {{ template \"tmplPartialGood.tmpl\" . }}" const tmplPartialGood = "{{range .data}}{{.eg}}{{end}}" const tmplResult = "0 00"