tartrazine/internal/code-generator/generator/generator_test.go

150 lines
4.2 KiB
Go
Raw Normal View History

2017-04-04 11:10:35 +00:00
package generator
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const (
commitTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
2017-04-04 11:10:35 +00:00
// Languages test
extensionsTestFile = "test_files/extensions.test.yml"
extensionsGold = "test_files/extensions.gold"
extensionsTestTmplPath = "../assets/extensions.go.tmpl"
extensionsTestTmplName = "extensions.go.tmpl"
2017-04-05 14:01:31 +00:00
// Heuristics test
heuristicsTestFile = "test_files/heuristics.test.rb"
contentGold = "test_files/content.gold"
contentTestTmplPath = "../assets/content.go.tmpl"
contentTestTmplName = "content.go.tmpl"
2017-04-06 15:31:17 +00:00
// Vendor test
vendorTestFile = "test_files/vendor.test.yml"
vendorGold = "test_files/vendor.gold"
vendorTestTmplPath = "../assets/vendor.go.tmpl"
vendorTestTmplName = "vendor.go.tmpl"
// Documentation test
documentationTestFile = "test_files/documentation.test.yml"
documentationGold = "test_files/documentation.gold"
documentationTestTmplPath = "../assets/documentation.go.tmpl"
documentationTestTmplName = "documentation.go.tmpl"
2017-04-11 09:26:23 +00:00
// Types test
typesTestFile = "test_files/type.test.yml"
typesGold = "test_files/type.gold"
typesTestTmplPath = "../assets/type.go.tmpl"
typesTestTmplName = "type.go.tmpl"
// Interpreters test
interpretersTestFile = "test_files/interpreters.test.yml"
interpretersGold = "test_files/interpreters.gold"
interpretersTestTmplPath = "../assets/interpreters.go.tmpl"
interpretersTestTmplName = "interpreters.go.tmpl"
// Filenames test
filenamesTestFile = "test_files/filenames.test.yml"
filenamesGold = "test_files/filenames.gold"
filenamesTestTmplPath = "../assets/filenames.go.tmpl"
filenamesTestTmplName = "filenames.go.tmpl"
2017-04-04 11:10:35 +00:00
)
func TestFromFile(t *testing.T) {
tests := []struct {
name string
fileToParse string
tmplPath string
tmplName string
commit string
generate Func
wantOut string
2017-04-04 11:10:35 +00:00
}{
{
name: "TestFromFile_Language",
fileToParse: extensionsTestFile,
tmplPath: extensionsTestTmplPath,
tmplName: extensionsTestTmplName,
commit: commitTest,
generate: Extensions,
wantOut: extensionsGold,
2017-04-04 11:10:35 +00:00
},
2017-04-05 14:01:31 +00:00
{
name: "TestFromFile_Heuristics",
fileToParse: heuristicsTestFile,
tmplPath: contentTestTmplPath,
tmplName: contentTestTmplName,
commit: commitTest,
2017-04-05 14:01:31 +00:00
generate: Heuristics,
wantOut: contentGold,
2017-04-05 14:01:31 +00:00
},
2017-04-06 15:31:17 +00:00
{
name: "TestFromFile_Vendor",
fileToParse: vendorTestFile,
tmplPath: vendorTestTmplPath,
tmplName: vendorTestTmplName,
commit: commitTest,
2017-04-06 15:31:17 +00:00
generate: Vendor,
wantOut: vendorGold,
2017-04-06 15:31:17 +00:00
},
{
name: "TestFromFile_Documentation",
fileToParse: documentationTestFile,
tmplPath: documentationTestTmplPath,
tmplName: documentationTestTmplName,
commit: commitTest,
generate: Documentation,
wantOut: documentationGold,
},
2017-04-11 09:26:23 +00:00
{
name: "TestFromFile_Types",
2017-04-11 09:26:23 +00:00
fileToParse: typesTestFile,
tmplPath: typesTestTmplPath,
tmplName: typesTestTmplName,
commit: commitTest,
generate: Types,
wantOut: typesGold,
2017-04-11 09:26:23 +00:00
},
{
name: "TestFromFile_Interpreters",
fileToParse: interpretersTestFile,
tmplPath: interpretersTestTmplPath,
tmplName: interpretersTestTmplName,
commit: commitTest,
generate: Interpreters,
wantOut: interpretersGold,
},
{
name: "TestFromFile_Filenames",
fileToParse: filenamesTestFile,
tmplPath: filenamesTestTmplPath,
tmplName: filenamesTestTmplName,
commit: commitTest,
generate: Filenames,
wantOut: filenamesGold,
},
2017-04-04 11:10:35 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gold, err := ioutil.ReadFile(tt.wantOut)
assert.NoError(t, err)
outPath, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPath.Name())
err = FromFile(tt.fileToParse, outPath.Name(), tt.tmplPath, tt.tmplName, tt.commit, tt.generate)
2017-04-04 11:10:35 +00:00
assert.NoError(t, err)
out, err := ioutil.ReadFile(outPath.Name())
2017-04-04 11:10:35 +00:00
assert.NoError(t, err)
assert.EqualValues(t, gold, out, fmt.Sprintf("FromFile() = %v, want %v", string(out), string(tt.wantOut)))
2017-04-04 11:10:35 +00:00
})
}
}