2017-04-04 11:10:35 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-04-10 09:30:23 +00:00
|
|
|
commitTest = "fe8b44ab8a225b1ffa75b983b916ea22fee5b6f7"
|
|
|
|
|
2017-04-04 11:10:35 +00:00
|
|
|
// Languages test
|
|
|
|
ymlTestFile = "test_files/languages.test.yml"
|
|
|
|
langGold = "test_files/languages.gold"
|
2017-04-17 06:14:46 +00:00
|
|
|
languagesTestTmplPath = "test_files/languages.test.go.tmpl"
|
|
|
|
languagesTestTmplName = "languages.test.go.tmpl"
|
2017-04-05 14:01:31 +00:00
|
|
|
|
|
|
|
// Heuristics test
|
2017-04-10 09:30:23 +00:00
|
|
|
heuristicsTestFile = "test_files/heuristics.test.rb"
|
|
|
|
contentGold = "test_files/content.gold"
|
|
|
|
contentTestTmplPath = "test_files/content.test.go.tmpl"
|
|
|
|
contentTestTmplName = "content.test.go.tmpl"
|
2017-04-06 15:31:17 +00:00
|
|
|
|
|
|
|
// Vendor test
|
2017-04-07 07:25:49 +00:00
|
|
|
vendorTestFile = "test_files/vendor.test.yml"
|
|
|
|
vendorGold = "test_files/vendor.gold"
|
|
|
|
vendorTestTmplPath = "test_files/vendor.test.go.tmpl"
|
|
|
|
vendorTestTmplName = "vendor.test.go.tmpl"
|
2017-04-10 09:30:23 +00:00
|
|
|
|
|
|
|
// Documentation test
|
|
|
|
documentationTestFile = "test_files/documentation.test.yml"
|
|
|
|
documentationGold = "test_files/documentation.gold"
|
|
|
|
documentationTestTmplPath = "test_files/documentation.test.go.tmpl"
|
|
|
|
documentationTestTmplName = "documentation.test.go.tmpl"
|
2017-04-11 09:26:23 +00:00
|
|
|
|
|
|
|
// Types test
|
|
|
|
typesTestFile = "test_files/type.test.yml"
|
|
|
|
typesGold = "test_files/type.gold"
|
|
|
|
typesTestTmplPath = "test_files/type.test.go.tmpl"
|
|
|
|
typesTestTmplName = "type.test.go.tmpl"
|
2017-04-17 06:19:53 +00:00
|
|
|
|
|
|
|
// Interpreters test
|
|
|
|
interpretersTestFile = "test_files/interpreters.test.yml"
|
|
|
|
interpretersGold = "test_files/interpreters.gold"
|
|
|
|
interpretersTestTmplPath = "test_files/interpreters.test.go.tmpl"
|
|
|
|
interpretersTestTmplName = "interpreters.test.go.tmpl"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFromFile(t *testing.T) {
|
2017-04-17 06:14:46 +00:00
|
|
|
goldLang, err := ioutil.ReadFile(langGold)
|
2017-04-04 11:10:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-04-17 06:14:46 +00:00
|
|
|
goldContent, err := ioutil.ReadFile(contentGold)
|
2017-04-05 14:01:31 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-04-17 06:14:46 +00:00
|
|
|
goldVendor, err := ioutil.ReadFile(vendorGold)
|
2017-04-06 15:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-04-17 06:14:46 +00:00
|
|
|
goldDocumentation, err := ioutil.ReadFile(documentationGold)
|
2017-04-10 09:30:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-04-17 06:14:46 +00:00
|
|
|
goldTypes, err := ioutil.ReadFile(typesGold)
|
2017-04-11 09:26:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-04-17 06:19:53 +00:00
|
|
|
goldInterpreters, err := ioutil.ReadFile(interpretersGold)
|
|
|
|
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())
|
|
|
|
|
2017-04-07 07:25:49 +00:00
|
|
|
outPathVendor, err := ioutil.TempFile("/tmp", "generator-test-")
|
2017-04-06 15:31:17 +00:00
|
|
|
assert.NoError(t, err)
|
2017-04-10 09:30:23 +00:00
|
|
|
defer os.Remove(outPathVendor.Name())
|
|
|
|
|
|
|
|
outPathDocumentation, err := ioutil.TempFile("/tmp", "generator-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.Remove(outPathDocumentation.Name())
|
2017-04-06 15:31:17 +00:00
|
|
|
|
2017-04-11 09:26:23 +00:00
|
|
|
outPathTypes, err := ioutil.TempFile("/tmp", "generator-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.Remove(outPathTypes.Name())
|
|
|
|
|
2017-04-17 06:19:53 +00:00
|
|
|
outPathInterpreters, err := ioutil.TempFile("/tmp", "generator-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.Remove(outPathInterpreters.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,
|
2017-04-10 09:30:23 +00:00
|
|
|
commit: commitTest,
|
2017-04-04 11:10:35 +00:00
|
|
|
generate: Languages,
|
|
|
|
wantOut: goldLang,
|
|
|
|
},
|
2017-04-05 14:01:31 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Heuristics",
|
|
|
|
fileToParse: heuristicsTestFile,
|
|
|
|
outPath: outPathContent.Name(),
|
|
|
|
tmplPath: contentTestTmplPath,
|
|
|
|
tmplName: contentTestTmplName,
|
2017-04-10 09:30:23 +00:00
|
|
|
commit: commitTest,
|
2017-04-05 14:01:31 +00:00
|
|
|
generate: Heuristics,
|
|
|
|
wantOut: goldContent,
|
|
|
|
},
|
2017-04-06 15:31:17 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Vendor",
|
|
|
|
fileToParse: vendorTestFile,
|
2017-04-07 07:25:49 +00:00
|
|
|
outPath: outPathVendor.Name(),
|
|
|
|
tmplPath: vendorTestTmplPath,
|
|
|
|
tmplName: vendorTestTmplName,
|
2017-04-10 09:30:23 +00:00
|
|
|
commit: commitTest,
|
2017-04-06 15:31:17 +00:00
|
|
|
generate: Vendor,
|
2017-04-07 07:25:49 +00:00
|
|
|
wantOut: goldVendor,
|
2017-04-06 15:31:17 +00:00
|
|
|
},
|
2017-04-10 09:30:23 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Documentation",
|
|
|
|
fileToParse: documentationTestFile,
|
|
|
|
outPath: outPathDocumentation.Name(),
|
|
|
|
tmplPath: documentationTestTmplPath,
|
|
|
|
tmplName: documentationTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Documentation,
|
|
|
|
wantOut: goldDocumentation,
|
|
|
|
},
|
2017-04-11 09:26:23 +00:00
|
|
|
{
|
2017-04-17 06:14:46 +00:00
|
|
|
name: "TestFromFile_Types",
|
2017-04-11 09:26:23 +00:00
|
|
|
fileToParse: typesTestFile,
|
|
|
|
outPath: outPathTypes.Name(),
|
|
|
|
tmplPath: typesTestTmplPath,
|
|
|
|
tmplName: typesTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Types,
|
|
|
|
wantOut: goldTypes,
|
|
|
|
},
|
2017-04-17 06:19:53 +00:00
|
|
|
{
|
|
|
|
name: "TestFromFile_Interpreters",
|
|
|
|
fileToParse: interpretersTestFile,
|
|
|
|
outPath: outPathInterpreters.Name(),
|
|
|
|
tmplPath: interpretersTestTmplPath,
|
|
|
|
tmplName: interpretersTestTmplName,
|
|
|
|
commit: commitTest,
|
|
|
|
generate: Interpreters,
|
|
|
|
wantOut: goldInterpreters,
|
|
|
|
},
|
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)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|