mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-05-23 16:40:08 -03:00
changed test LinguistCorpus to use GetLanguage and fail if not assert
This commit is contained in:
parent
1fc8cf7a5d
commit
5f0e92b1a8
106
common_test.go
106
common_test.go
@ -6,7 +6,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"text/tabwriter"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
@ -205,59 +204,6 @@ func (s *SimpleLinguistTestSuite) TestGetLanguageByExtension() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleLinguistTestSuite) TestGetLanguageByContentLinguistCorpus() {
|
|
||||||
var total, failed, ok, other, unsafe int
|
|
||||||
|
|
||||||
w := new(tabwriter.Writer)
|
|
||||||
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
|
|
||||||
|
|
||||||
filepath.Walk(".linguist/samples", func(path string, f os.FileInfo, err error) error {
|
|
||||||
if f.IsDir() {
|
|
||||||
if f.Name() == "filenames" {
|
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := filepath.Base(filepath.Dir(path))
|
|
||||||
filename := filepath.Base(path)
|
|
||||||
extension := filepath.Ext(path)
|
|
||||||
content, _ := ioutil.ReadFile(path)
|
|
||||||
|
|
||||||
if extension == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
total++
|
|
||||||
obtained, safe := GetLanguageByContent(filename, content)
|
|
||||||
if obtained == OtherLanguage {
|
|
||||||
other++
|
|
||||||
}
|
|
||||||
|
|
||||||
var status string
|
|
||||||
if expected == obtained {
|
|
||||||
status = "ok"
|
|
||||||
ok++
|
|
||||||
} else {
|
|
||||||
status = "failed"
|
|
||||||
failed++
|
|
||||||
if !safe {
|
|
||||||
unsafe++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\n", filename, expected, obtained, safe, status)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
fmt.Fprintln(w)
|
|
||||||
w.Flush()
|
|
||||||
|
|
||||||
fmt.Printf("total files: %d, ok: %d, failed: %d, unsafe: %d, other: %d\n", total, ok, failed, unsafe, other)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SimpleLinguistTestSuite) TestGetLanguageByClassifier() {
|
func (s *SimpleLinguistTestSuite) TestGetLanguageByClassifier() {
|
||||||
const samples = `.linguist/samples/`
|
const samples = `.linguist/samples/`
|
||||||
test := []struct {
|
test := []struct {
|
||||||
@ -349,3 +295,55 @@ func (s *SimpleLinguistTestSuite) TestGetLanguageByAlias() {
|
|||||||
assert.Equal(s.T(), test.expectedOk, ok, fmt.Sprintf("%v: ok = %v, expected: %v", test.name, ok, test.expectedOk))
|
assert.Equal(s.T(), test.expectedOk, ok, fmt.Sprintf("%v: ok = %v, expected: %v", test.name, ok, test.expectedOk))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SimpleLinguistTestSuite) TestLinguistCorpus() {
|
||||||
|
const (
|
||||||
|
samplesDir = ".linguist/samples"
|
||||||
|
filenamesDir = "filenames"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cornerCases = map[string]bool{
|
||||||
|
"hello.ms": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var total, failed, ok, other int
|
||||||
|
var expected string
|
||||||
|
filepath.Walk(samplesDir, func(path string, f os.FileInfo, err error) error {
|
||||||
|
if f.IsDir() {
|
||||||
|
if f.Name() != filenamesDir {
|
||||||
|
expected = f.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := filepath.Base(path)
|
||||||
|
content, _ := ioutil.ReadFile(path)
|
||||||
|
|
||||||
|
total++
|
||||||
|
obtained := GetLanguage(filename, content)
|
||||||
|
if obtained == OtherLanguage {
|
||||||
|
other++
|
||||||
|
}
|
||||||
|
|
||||||
|
var status string
|
||||||
|
if expected == obtained {
|
||||||
|
status = "ok"
|
||||||
|
ok++
|
||||||
|
} else {
|
||||||
|
status = "failed"
|
||||||
|
failed++
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := cornerCases[filename]; ok {
|
||||||
|
fmt.Printf("\t\t[condidered corner case] %s\t%s\t%s\t%s\n", filename, expected, obtained, status)
|
||||||
|
} else {
|
||||||
|
assert.Equal(s.T(), expected, obtained, fmt.Sprintf("%s\t%s\t%s\t%s\n", filename, expected, obtained, status))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Printf("\t\ttotal files: %d, ok: %d, failed: %d, other: %d\n", total, ok, failed, other)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user