2017-04-04 11:10:35 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
2019-02-14 11:47:45 +00:00
|
|
|
"flag"
|
2022-12-03 08:49:09 +00:00
|
|
|
"fmt"
|
2017-04-04 11:10:35 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-05-25 10:33:26 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-02-20 14:46:27 +00:00
|
|
|
"strings"
|
2017-04-04 11:10:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-03-28 21:47:57 +00:00
|
|
|
"github.com/go-enry/go-enry/v2/internal/tokenizer"
|
|
|
|
|
2017-04-04 11:10:35 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-28 21:47:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-25 10:33:26 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2020-03-19 18:47:22 +00:00
|
|
|
var (
|
2018-04-28 13:12:03 +00:00
|
|
|
linguistURL = "https://github.com/github/linguist.git"
|
2017-10-26 13:37:18 +00:00
|
|
|
linguistClonedEnvVar = "ENRY_TEST_REPO"
|
2022-06-09 20:09:50 +00:00
|
|
|
commit = "d7799da826e01acdb8f84694d33116dccaabe9c2"
|
2018-04-28 13:12:03 +00:00
|
|
|
samplesDir = "samples"
|
2020-03-19 18:47:22 +00:00
|
|
|
languagesFile = filepath.Join("lib", "linguist", "languages.yml")
|
2018-04-28 13:12:03 +00:00
|
|
|
|
|
|
|
testDir = "test_files"
|
2020-03-19 18:47:22 +00:00
|
|
|
assetsDir = filepath.Join("..", "assets")
|
2017-04-10 09:30:23 +00:00
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
// Extensions test
|
2020-03-19 18:47:22 +00:00
|
|
|
extensionGold = filepath.Join(testDir, "extension.gold")
|
|
|
|
extensionTestTmplPath = filepath.Join(assetsDir, "extension.go.tmpl")
|
2017-06-13 11:56:07 +00:00
|
|
|
extensionTestTmplName = "extension.go.tmpl"
|
2017-04-05 14:01:31 +00:00
|
|
|
|
|
|
|
// Heuristics test
|
2020-03-19 18:47:22 +00:00
|
|
|
heuristicsTestFile = filepath.Join("lib", "linguist", "heuristics.yml")
|
|
|
|
contentGold = filepath.Join(testDir, "content.gold")
|
|
|
|
contentTestTmplPath = filepath.Join(assetsDir, "content.go.tmpl")
|
2017-04-20 06:12:56 +00:00
|
|
|
contentTestTmplName = "content.go.tmpl"
|
2017-04-06 15:31:17 +00:00
|
|
|
|
|
|
|
// Vendor test
|
2020-03-19 18:47:22 +00:00
|
|
|
vendorTestFile = filepath.Join("lib", "linguist", "vendor.yml")
|
|
|
|
vendorGold = filepath.Join(testDir, "vendor.gold")
|
|
|
|
vendorTestTmplPath = filepath.Join(assetsDir, "vendor.go.tmpl")
|
2017-04-20 06:12:56 +00:00
|
|
|
vendorTestTmplName = "vendor.go.tmpl"
|
2017-04-10 09:30:23 +00:00
|
|
|
|
|
|
|
// Documentation test
|
2020-03-19 18:47:22 +00:00
|
|
|
documentationTestFile = filepath.Join("lib", "linguist", "documentation.yml")
|
|
|
|
documentationGold = filepath.Join(testDir, "documentation.gold")
|
|
|
|
documentationTestTmplPath = filepath.Join(assetsDir, "documentation.go.tmpl")
|
2017-04-20 06:12:56 +00:00
|
|
|
documentationTestTmplName = "documentation.go.tmpl"
|
2017-04-11 09:26:23 +00:00
|
|
|
|
|
|
|
// Types test
|
2020-03-19 18:47:22 +00:00
|
|
|
typeGold = filepath.Join(testDir, "type.gold")
|
|
|
|
typeTestTmplPath = filepath.Join(assetsDir, "type.go.tmpl")
|
2017-06-13 11:56:07 +00:00
|
|
|
typeTestTmplName = "type.go.tmpl"
|
2017-04-17 06:19:53 +00:00
|
|
|
|
|
|
|
// Interpreters test
|
2020-03-19 18:47:22 +00:00
|
|
|
interpreterGold = filepath.Join(testDir, "interpreter.gold")
|
|
|
|
interpreterTestTmplPath = filepath.Join(assetsDir, "interpreter.go.tmpl")
|
2017-06-13 11:56:07 +00:00
|
|
|
interpreterTestTmplName = "interpreter.go.tmpl"
|
2017-04-19 08:22:46 +00:00
|
|
|
|
|
|
|
// Filenames test
|
2020-03-19 18:47:22 +00:00
|
|
|
filenameGold = filepath.Join(testDir, "filename.gold")
|
|
|
|
filenameTestTmplPath = filepath.Join(assetsDir, "filename.go.tmpl")
|
2017-06-13 11:56:07 +00:00
|
|
|
filenameTestTmplName = "filename.go.tmpl"
|
2017-05-04 13:03:54 +00:00
|
|
|
|
|
|
|
// Aliases test
|
2020-03-19 18:47:22 +00:00
|
|
|
aliasGold = filepath.Join(testDir, "alias.gold")
|
|
|
|
aliasTestTmplPath = filepath.Join(assetsDir, "alias.go.tmpl")
|
2017-06-13 11:56:07 +00:00
|
|
|
aliasTestTmplName = "alias.go.tmpl"
|
2017-05-25 10:33:26 +00:00
|
|
|
|
|
|
|
// Frequencies test
|
2020-03-19 18:47:22 +00:00
|
|
|
frequenciesGold = filepath.Join(testDir, "frequencies.gold")
|
|
|
|
frequenciesTestTmplPath = filepath.Join(assetsDir, "frequencies.go.tmpl")
|
2017-05-25 10:33:26 +00:00
|
|
|
frequenciesTestTmplName = "frequencies.go.tmpl"
|
2017-06-21 13:18:27 +00:00
|
|
|
|
|
|
|
// commit test
|
2020-03-19 18:47:22 +00:00
|
|
|
commitGold = filepath.Join(testDir, "commit.gold")
|
|
|
|
commitTestTmplPath = filepath.Join(assetsDir, "commit.go.tmpl")
|
2017-06-21 13:18:27 +00:00
|
|
|
commitTestTmplName = "commit.go.tmpl"
|
2017-07-11 09:13:49 +00:00
|
|
|
|
|
|
|
// mime test
|
2020-03-19 18:47:22 +00:00
|
|
|
mimeTypeGold = filepath.Join(testDir, "mimeType.gold")
|
|
|
|
mimeTypeTestTmplPath = filepath.Join(assetsDir, "mimeType.go.tmpl")
|
2017-07-11 09:13:49 +00:00
|
|
|
mimeTypeTestTmplName = "mimeType.go.tmpl"
|
2019-07-19 20:28:57 +00:00
|
|
|
|
|
|
|
// colors test
|
2020-03-19 18:47:22 +00:00
|
|
|
colorsGold = filepath.Join(testDir, "colors.gold")
|
|
|
|
colorsTestTmplPath = filepath.Join(assetsDir, "colors.go.tmpl")
|
2019-07-19 20:28:57 +00:00
|
|
|
colorsTestTmplName = "colors.go.tmpl"
|
2020-03-21 13:37:39 +00:00
|
|
|
|
|
|
|
// colors test
|
|
|
|
groupsGold = filepath.Join(testDir, "groups.gold")
|
|
|
|
groupsTestTmplPath = filepath.Join(assetsDir, "groups.go.tmpl")
|
|
|
|
groupsTestTmplName = "groups.go.tmpl"
|
2017-04-04 11:10:35 +00:00
|
|
|
)
|
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
type GeneratorTestSuite struct {
|
|
|
|
suite.Suite
|
2022-11-23 18:06:24 +00:00
|
|
|
tmpLinguistDir string
|
|
|
|
isCleanupNeeded bool
|
|
|
|
testCases []testCase
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
fileToParse string
|
|
|
|
samplesDir string
|
|
|
|
tmplPath string
|
|
|
|
tmplName string
|
|
|
|
commit string
|
|
|
|
generate File
|
|
|
|
wantOut string
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateGold = flag.Bool("update_gold", false, "Update golden test files")
|
|
|
|
|
|
|
|
func Test_GeneratorTestSuite(t *testing.T) {
|
2017-05-29 08:05:16 +00:00
|
|
|
suite.Run(t, new(GeneratorTestSuite))
|
|
|
|
}
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
func (s *GeneratorTestSuite) maybeCloneLinguist() {
|
2017-06-21 13:18:27 +00:00
|
|
|
var err error
|
2022-10-17 15:45:11 +00:00
|
|
|
s.tmpLinguistDir = os.Getenv(linguistClonedEnvVar)
|
2022-11-23 18:06:24 +00:00
|
|
|
isLinguistCloned := s.tmpLinguistDir != ""
|
|
|
|
if !isLinguistCloned {
|
2022-10-17 15:45:11 +00:00
|
|
|
s.tmpLinguistDir, err = ioutil.TempDir("", "linguist-")
|
2022-11-23 21:05:50 +00:00
|
|
|
require.NoError(s.T(), err)
|
2022-11-23 18:06:24 +00:00
|
|
|
|
|
|
|
s.T().Logf("Cloning Linguist repo to '%s' as %s was not set\n",
|
|
|
|
s.tmpLinguistDir, linguistClonedEnvVar)
|
2022-11-23 21:04:53 +00:00
|
|
|
cmd := exec.Command("git", "clone", "--depth", "100", linguistURL, s.tmpLinguistDir)
|
2017-09-28 18:58:13 +00:00
|
|
|
err = cmd.Run()
|
2022-11-23 21:05:50 +00:00
|
|
|
require.NoError(s.T(), err)
|
2022-11-23 18:06:24 +00:00
|
|
|
s.isCleanupNeeded = true
|
|
|
|
}
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2022-11-23 18:06:24 +00:00
|
|
|
cwd, err := os.Getwd()
|
2022-11-23 21:05:50 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2022-11-23 18:06:24 +00:00
|
|
|
err = os.Chdir(s.tmpLinguistDir)
|
2022-11-23 21:05:50 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2022-11-23 18:06:24 +00:00
|
|
|
cmd := exec.Command("git", "checkout", commit)
|
|
|
|
err = cmd.Run()
|
2022-11-23 21:05:50 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2022-11-23 18:06:24 +00:00
|
|
|
err = os.Chdir(cwd)
|
2022-11-23 21:05:50 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
func (s *GeneratorTestSuite) SetupSuite() {
|
|
|
|
s.maybeCloneLinguist()
|
|
|
|
s.testCases = []testCase{
|
2017-04-04 11:10:35 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Extensions()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: extensionTestTmplPath,
|
|
|
|
tmplName: extensionTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-19 09:24:01 +00:00
|
|
|
generate: Extensions,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: extensionGold,
|
2017-04-04 11:10:35 +00:00
|
|
|
},
|
2017-04-05 14:01:31 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Heuristics()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, heuristicsTestFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
2017-04-05 14:01:31 +00:00
|
|
|
tmplPath: contentTestTmplPath,
|
|
|
|
tmplName: contentTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
2019-02-14 11:47:45 +00:00
|
|
|
generate: GenHeuristics,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: contentGold,
|
2017-04-05 14:01:31 +00:00
|
|
|
},
|
2017-04-06 15:31:17 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Vendor()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, vendorTestFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
2017-04-07 07:25:49 +00:00
|
|
|
tmplPath: vendorTestTmplPath,
|
|
|
|
tmplName: vendorTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
2017-04-06 15:31:17 +00:00
|
|
|
generate: Vendor,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: vendorGold,
|
2017-04-06 15:31:17 +00:00
|
|
|
},
|
2017-04-10 09:30:23 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Documentation()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, documentationTestFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
2017-04-10 09:30:23 +00:00
|
|
|
tmplPath: documentationTestTmplPath,
|
|
|
|
tmplName: documentationTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
2017-04-10 09:30:23 +00:00
|
|
|
generate: Documentation,
|
2017-04-19 08:22:46 +00:00
|
|
|
wantOut: documentationGold,
|
2017-04-10 09:30:23 +00:00
|
|
|
},
|
2017-04-11 09:26:23 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Types()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: typeTestTmplPath,
|
|
|
|
tmplName: typeTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-11 09:26:23 +00:00
|
|
|
generate: Types,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: typeGold,
|
2017-04-11 09:26:23 +00:00
|
|
|
},
|
2017-04-17 06:19:53 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Interpreters()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: interpreterTestTmplPath,
|
|
|
|
tmplName: interpreterTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-17 06:19:53 +00:00
|
|
|
generate: Interpreters,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: interpreterGold,
|
2017-04-19 08:22:46 +00:00
|
|
|
},
|
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Filenames()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
|
|
|
samplesDir: filepath.Join(s.tmpLinguistDir, samplesDir),
|
2017-06-13 11:56:07 +00:00
|
|
|
tmplPath: filenameTestTmplPath,
|
|
|
|
tmplName: filenameTestTmplName,
|
|
|
|
commit: commit,
|
2017-04-19 08:22:46 +00:00
|
|
|
generate: Filenames,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: filenameGold,
|
2017-04-17 06:19:53 +00:00
|
|
|
},
|
2017-05-04 13:03:54 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Aliases()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2017-06-13 11:56:07 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: aliasTestTmplPath,
|
|
|
|
tmplName: aliasTestTmplName,
|
|
|
|
commit: commit,
|
2017-05-04 13:03:54 +00:00
|
|
|
generate: Aliases,
|
2017-06-13 11:56:07 +00:00
|
|
|
wantOut: aliasGold,
|
2017-05-04 13:03:54 +00:00
|
|
|
},
|
2017-05-25 10:33:26 +00:00
|
|
|
{
|
2017-06-13 11:56:07 +00:00
|
|
|
name: "Frequencies()",
|
2022-10-17 15:45:11 +00:00
|
|
|
samplesDir: filepath.Join(s.tmpLinguistDir, samplesDir),
|
2017-05-25 10:33:26 +00:00
|
|
|
tmplPath: frequenciesTestTmplPath,
|
|
|
|
tmplName: frequenciesTestTmplName,
|
2017-06-13 11:56:07 +00:00
|
|
|
commit: commit,
|
|
|
|
generate: Frequencies,
|
2017-05-25 10:33:26 +00:00
|
|
|
wantOut: frequenciesGold,
|
|
|
|
},
|
2017-06-21 13:18:27 +00:00
|
|
|
{
|
|
|
|
name: "Commit()",
|
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: commitTestTmplPath,
|
|
|
|
tmplName: commitTestTmplName,
|
|
|
|
commit: commit,
|
|
|
|
generate: Commit,
|
|
|
|
wantOut: commitGold,
|
|
|
|
},
|
2017-07-11 09:13:49 +00:00
|
|
|
{
|
|
|
|
name: "MimeType()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2017-07-11 09:13:49 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: mimeTypeTestTmplPath,
|
|
|
|
tmplName: mimeTypeTestTmplName,
|
|
|
|
commit: commit,
|
|
|
|
generate: MimeType,
|
|
|
|
wantOut: mimeTypeGold,
|
|
|
|
},
|
2019-07-19 20:28:57 +00:00
|
|
|
{
|
|
|
|
name: "Colors()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2019-07-19 20:28:57 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: colorsTestTmplPath,
|
|
|
|
tmplName: colorsTestTmplName,
|
|
|
|
commit: commit,
|
|
|
|
generate: Colors,
|
|
|
|
wantOut: colorsGold,
|
|
|
|
},
|
2020-03-21 13:37:39 +00:00
|
|
|
{
|
|
|
|
name: "Groups()",
|
2022-10-17 15:45:11 +00:00
|
|
|
fileToParse: filepath.Join(s.tmpLinguistDir, languagesFile),
|
2020-03-21 13:37:39 +00:00
|
|
|
samplesDir: "",
|
|
|
|
tmplPath: groupsTestTmplPath,
|
|
|
|
tmplName: groupsTestTmplName,
|
|
|
|
commit: commit,
|
|
|
|
generate: Groups,
|
|
|
|
wantOut: groupsGold,
|
|
|
|
},
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
2019-02-14 11:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GeneratorTestSuite) TearDownSuite() {
|
2022-11-23 18:06:24 +00:00
|
|
|
if s.isCleanupNeeded {
|
2022-10-17 15:45:11 +00:00
|
|
|
err := os.RemoveAll(s.tmpLinguistDir)
|
2022-11-23 18:06:24 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2019-02-14 11:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
// TestUpdateGeneratorTestSuiteGold is a Gold results generation automation.
|
|
|
|
// It should only be enabled&run manually on every new Linguist version
|
|
|
|
// to update *.gold files.
|
|
|
|
func (s *GeneratorTestSuite) TestUpdateGeneratorTestSuiteGold() {
|
|
|
|
if !*updateGold {
|
|
|
|
s.T().Skip()
|
|
|
|
}
|
|
|
|
s.T().Logf("Generating new *.gold test files")
|
|
|
|
for _, test := range s.testCases {
|
|
|
|
dst := test.wantOut
|
|
|
|
s.T().Logf("Generating %s from %s\n", dst, test.fileToParse)
|
|
|
|
err := test.generate(test.fileToParse, test.samplesDir, dst, test.tmplPath, test.tmplName, test.commit)
|
|
|
|
assert.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GeneratorTestSuite) TestGenerationFiles() {
|
|
|
|
for _, test := range s.testCases {
|
2017-05-25 10:33:26 +00:00
|
|
|
gold, err := ioutil.ReadFile(test.wantOut)
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
|
2020-03-19 18:47:22 +00:00
|
|
|
outPath, err := ioutil.TempFile("", "generator-test-")
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
defer os.Remove(outPath.Name())
|
2017-06-13 11:56:07 +00:00
|
|
|
err = test.generate(test.fileToParse, test.samplesDir, outPath.Name(), test.tmplPath, test.tmplName, test.commit)
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-05-25 10:33:26 +00:00
|
|
|
out, err := ioutil.ReadFile(outPath.Name())
|
2017-06-21 13:18:27 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2019-02-20 14:46:27 +00:00
|
|
|
|
|
|
|
expected := normalizeSpaces(string(gold))
|
|
|
|
actual := normalizeSpaces(string(out))
|
2022-12-03 08:49:09 +00:00
|
|
|
// this produces large unreadable output, so we do it 'manually' instead
|
|
|
|
// assert.Equal(s.T(), expected, actual, "Test %s", test.name)
|
2020-03-25 13:20:26 +00:00
|
|
|
if expected != actual {
|
2022-12-03 08:49:09 +00:00
|
|
|
assert.Fail(s.T(), fmt.Sprintf("%s output is different from %q", test.name, test.wantOut))
|
2022-12-12 19:48:35 +00:00
|
|
|
diff, err := text_diff(gold, out)
|
2022-12-03 08:49:09 +00:00
|
|
|
if err != nil {
|
|
|
|
s.T().Logf("Failed produce a diff between expected and actual: %s", err.Error())
|
|
|
|
s.T().Logf("Expected %q", expected[:400])
|
|
|
|
s.T().Logf("Actual %q", actual[:400])
|
|
|
|
}
|
|
|
|
s.T().Logf("\n%s", diff)
|
2020-03-25 13:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 10:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 14:46:27 +00:00
|
|
|
|
2020-03-28 21:47:57 +00:00
|
|
|
func (s *GeneratorTestSuite) TestTokenizerOnATS() {
|
|
|
|
const suspiciousSample = "samples/ATS/csv_parse.hats"
|
2022-10-17 15:45:11 +00:00
|
|
|
sFile := filepath.Join(s.tmpLinguistDir, suspiciousSample)
|
2020-03-28 21:47:57 +00:00
|
|
|
content, err := ioutil.ReadFile(sFile)
|
|
|
|
require.NoError(s.T(), err)
|
|
|
|
|
|
|
|
tokens := tokenizer.Tokenize(content)
|
|
|
|
assert.Equal(s.T(), 381, len(tokens), "Number of tokens using LF as line endings")
|
|
|
|
}
|
|
|
|
|
2019-02-21 08:33:17 +00:00
|
|
|
// normalizeSpaces returns a copy of str with whitespaces normalized.
|
2019-02-20 14:46:27 +00:00
|
|
|
// We use this to compare generated source as gofmt format may change.
|
2019-02-21 08:33:17 +00:00
|
|
|
// E.g for changes between Go 1.10 and 1.11 see
|
2019-02-20 14:46:27 +00:00
|
|
|
// https://go-review.googlesource.com/c/go/+/122295/
|
|
|
|
func normalizeSpaces(str string) string {
|
|
|
|
return strings.Join(strings.Fields(str), " ")
|
|
|
|
}
|