2016-07-13 20:21:18 +00:00
|
|
|
package slinguist
|
|
|
|
|
|
|
|
import (
|
2016-07-14 13:14:32 +00:00
|
|
|
"fmt"
|
2016-07-13 20:21:18 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-07-14 13:14:32 +00:00
|
|
|
"text/tabwriter"
|
2016-07-13 20:21:18 +00:00
|
|
|
|
|
|
|
. "gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
2016-07-14 13:14:32 +00:00
|
|
|
func (s *TSuite) TestGetLanguageByContentLinguistCorpus(c *C) {
|
|
|
|
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
|
|
|
|
}
|
2016-07-14 16:12:12 +00:00
|
|
|
|
2016-07-14 13:14:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := filepath.Base(filepath.Dir(path))
|
|
|
|
filename := filepath.Base(path)
|
2016-07-14 16:12:12 +00:00
|
|
|
extension := filepath.Ext(path)
|
2016-07-14 13:14:32 +00:00
|
|
|
content, _ := ioutil.ReadFile(path)
|
|
|
|
|
2016-07-14 16:12:12 +00:00
|
|
|
if extension == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
total++
|
2016-07-14 13:14:32 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|