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

216 lines
5.5 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 (
// FromFile test
formatedLangGold = "test_files/formated_languages.gold"
formatedContentGold = "test_files/formated_content.gold"
formatedVendorGold = "test_files/formated_vendor.gold"
2017-04-04 11:10:35 +00:00
// Languages test
ymlTestFile = "test_files/languages.test.yml"
langGold = "test_files/languages.gold"
languagesTestTmplPath = "test_files/languages.test.tmpl"
languagesTestTmplName = "languages.test.tmpl"
commitLangTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
2017-04-05 14:01:31 +00:00
// Heuristics test
heuristicsTestFile = "test_files/heuristics.test.rb"
contentGold = "test_files/content.gold"
contentTestTmplPath = "test_files/content.test.go.tmpl"
contentTestTmplName = "content.test.go.tmpl"
commitHeuristicsTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
2017-04-06 15:31:17 +00:00
// Vendor test
vendorTestFile = "test_files/vendor.test.yml"
vendorGold = "test_files/vendor.gold"
vendorTestTmplPath = "test_files/vendor.test.go.tmpl"
vendorTestTmplName = "vendor.test.go.tmpl"
commitVendorTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
2017-04-04 11:10:35 +00:00
)
func TestFromFile(t *testing.T) {
goldLang, err := ioutil.ReadFile(formatedLangGold)
assert.NoError(t, err)
2017-04-05 14:01:31 +00:00
goldContent, err := ioutil.ReadFile(formatedContentGold)
assert.NoError(t, err)
goldVendor, err := ioutil.ReadFile(formatedVendorGold)
2017-04-06 15:31:17 +00:00
assert.NoError(t, err)
2017-04-04 11:10:35 +00:00
outPathLang, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathLang.Name())
2017-04-05 14:01:31 +00:00
outPathContent, err := ioutil.TempFile("/tmp", "generator-test-")
assert.NoError(t, err)
defer os.Remove(outPathContent.Name())
outPathVendor, err := ioutil.TempFile("/tmp", "generator-test-")
2017-04-06 15:31:17 +00:00
assert.NoError(t, err)
defer os.Remove(outPathContent.Name())
2017-04-04 11:10:35 +00:00
tests := []struct {
name string
fileToParse string
outPath string
tmplPath string
tmplName string
commit string
generate Func
wantOut []byte
}{
{
name: "TestFromFile_Language",
fileToParse: ymlTestFile,
outPath: outPathLang.Name(),
tmplPath: languagesTestTmplPath,
tmplName: languagesTestTmplName,
commit: commitLangTest,
generate: Languages,
wantOut: goldLang,
},
2017-04-05 14:01:31 +00:00
{
name: "TestFromFile_Heuristics",
fileToParse: heuristicsTestFile,
outPath: outPathContent.Name(),
tmplPath: contentTestTmplPath,
tmplName: contentTestTmplName,
commit: commitHeuristicsTest,
generate: Heuristics,
wantOut: goldContent,
},
2017-04-06 15:31:17 +00:00
{
name: "TestFromFile_Vendor",
fileToParse: vendorTestFile,
outPath: outPathVendor.Name(),
tmplPath: vendorTestTmplPath,
tmplName: vendorTestTmplName,
2017-04-06 15:31:17 +00:00
commit: commitVendorTest,
generate: Vendor,
wantOut: goldVendor,
2017-04-06 15:31:17 +00:00
},
2017-04-04 11:10:35 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := FromFile(tt.fileToParse, tt.outPath, tt.tmplPath, tt.tmplName, tt.commit, tt.generate)
assert.NoError(t, err)
out, err := ioutil.ReadFile(tt.outPath)
assert.NoError(t, err)
assert.EqualValues(t, tt.wantOut, out, fmt.Sprintf("FromFile() = %v, want %v", string(out), string(tt.wantOut)))
})
}
}
func TestLanguages(t *testing.T) {
gold, err := ioutil.ReadFile(langGold)
assert.NoError(t, err)
input, err := ioutil.ReadFile(ymlTestFile)
assert.NoError(t, err)
tests := []struct {
name string
input []byte
tmplPath string
tmplName string
commit string
wantOut []byte
}{
{
name: "TestLanguages",
input: input,
tmplPath: languagesTestTmplPath,
tmplName: languagesTestTmplName,
commit: commitLangTest,
wantOut: gold,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := Languages(tt.input, tt.tmplPath, tt.tmplName, tt.commit)
assert.NoError(t, err)
assert.EqualValues(t, tt.wantOut, out, fmt.Sprintf("Languages() = %v, want %v", string(out), string(tt.wantOut)))
})
}
}
2017-04-05 14:01:31 +00:00
func TestHeuristics(t *testing.T) {
gold, err := ioutil.ReadFile(contentGold)
assert.NoError(t, err)
input, err := ioutil.ReadFile(heuristicsTestFile)
assert.NoError(t, err)
tests := []struct {
name string
input []byte
tmplPath string
tmplName string
commit string
wantOut []byte
}{
{
name: "TestHeuristics",
input: input,
tmplPath: contentTestTmplPath,
tmplName: contentTestTmplName,
commit: commitHeuristicsTest,
wantOut: gold,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := Heuristics(tt.input, tt.tmplPath, tt.tmplName, tt.commit)
assert.NoError(t, err)
assert.EqualValues(t, tt.wantOut, out, fmt.Sprintf("Heuristics() = %v, want %v", string(out), string(tt.wantOut)))
})
}
}
2017-04-06 15:31:17 +00:00
func TestVendor(t *testing.T) {
gold, err := ioutil.ReadFile(vendorGold)
2017-04-06 15:31:17 +00:00
assert.NoError(t, err)
input, err := ioutil.ReadFile(vendorTestFile)
assert.NoError(t, err)
tests := []struct {
name string
input []byte
tmplPath string
tmplName string
commit string
wantOut []byte
}{
{
name: "TestVendor",
input: input,
tmplPath: vendorTestTmplPath,
tmplName: vendorTestTmplName,
2017-04-06 15:31:17 +00:00
commit: commitVendorTest,
wantOut: gold,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := Vendor(tt.input, tt.tmplPath, tt.tmplName, tt.commit)
assert.NoError(t, err)
assert.EqualValues(t, tt.wantOut, out, fmt.Sprintf("Vendor() = %v, want %v", string(out), string(tt.wantOut)))
})
}
}