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 e9af8418fcc9c6ae30ff90de43d001f60b9d97c5
parent 9e9f19905a1ebc70c09a411f4c1b5cd3dd5e7d55
Author: gearsix <gearsix@tuta.io>
Date:   Tue,  7 Feb 2023 12:10:15 +0000

added IsDataFormat and IsTemplateLanguage + passing tests

Diffstat:
Mdata.go | 5+++++
Mdata_test.go | 29++++++++++++++++++++++-------
Mtemplate.go | 4++++
Mtemplate_test.go | 30+++++++++++++++++++++++-------
4 files changed, 54 insertions(+), 14 deletions(-)

diff --git a/data.go b/data.go @@ -39,6 +39,11 @@ const ( TOML DataFormat = "toml" ) +// IsDataFile checks if `path` is one of the known *DatFormat*s. +func IsDataFormat(path string) bool { + return ReadDataFormat(path) != "" +} + // ReadDataFormat returns the *DataFormat* that the file // extension of `path` matches. If the file extension of `path` does // not match any *DataFormat*, then an "" is returned. diff --git a/data_test.go b/data_test.go @@ -26,15 +26,30 @@ import ( "testing" ) -func TestReadDataFormat(t *testing.T) { - exts := []string{ - ".json", "json", "JSON", ".JSON", - ".yaml", "yaml", "YAML", ".YAML", - ".toml", "toml", "TOML", ".TOML", - ".misc", "-", ".", "", +var dataExts = []string{ + ".json", "json", "JSON", ".JSON", + ".yaml", "yaml", "YAML", ".YAML", + ".toml", "toml", "TOML", ".TOML", + ".misc", "-", ".", "", +} + +func TestIsDataFormat(t *testing.T) { + for i, ext := range dataExts { + var target bool + + if i < 12 { + target = true + } + + is := IsDataFormat(ext) + if is != target { + t.Fatalf("%t did not return %t", is, target) + } } +} - for i, ext := range exts { +func TestReadDataFormat(t *testing.T) { + for i, ext := range dataExts { var target DataFormat if i < 4 { diff --git a/template.go b/template.go @@ -41,6 +41,10 @@ const ( MST TemplateLanguage = "mst" ) +func IsTemplateLanguage(path string) bool { + return ReadTemplateLangauge(path) != "" +} + // ReadTemplateLanguage returns the *TemplateLanguage* that the file // extension of `path` matches. If the file extension of `path` does // not match any *TemplateLanguage*, then an "" is returned. diff --git a/template_test.go b/template_test.go @@ -45,16 +45,32 @@ const mstResult = `0 0` const mstRootBad = `{{> badPartial.mst}}{{#doesnt-exist}}{{/exit}}` const mstPartialBad = `p{{$}}{{ > noexist}` -func TestReadTemplateLanguage(t *testing.T) { - exts := []string{ - ".tmpl", "tmpl", "TMPL", ".TMPL", - ".hmpl", "hmpl", "HMPL", ".HMPL", - ".mst", "mst", "MST", ".MST", - ".NONE", "-", ".", "", +var templateExts = []string{ + ".tmpl", "tmpl", "TMPL", ".TMPL", + ".hmpl", "hmpl", "HMPL", ".HMPL", + ".mst", "mst", "MST", ".MST", + ".NONE", "-", ".", "", +} + +func TestIsTemplateLanguage(t *testing.T) { + for i, ext := range templateExts { + var target bool + + if i < 12 { + target = true + } + + is := IsTemplateLanguage(ext) + if is != target { + t.Fatalf("%t did not return %t", is, target) + } } +} - for i, ext := range exts { +func TestReadTemplateLanguage(t *testing.T) { + for i, ext := range templateExts { var target TemplateLanguage + if i < 4 { target = TMPL } else if i < 8 {