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"
|
|
|
|
"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-13 22:08:09 +00:00
|
|
|
func (s *TSuite) TestGetLanguageByContentH(c *C) {
|
|
|
|
s.testGetLanguageByContent(c, "Objective-C")
|
|
|
|
s.testGetLanguageByContent(c, "C++")
|
2016-07-13 20:21:18 +00:00
|
|
|
s.testGetLanguageByContent(c, "C")
|
2016-07-13 22:08:09 +00:00
|
|
|
s.testGetLanguageByContent(c, "Common Lisp")
|
|
|
|
s.testGetLanguageByContent(c, "Cool")
|
|
|
|
s.testGetLanguageByContent(c, "OpenCL")
|
|
|
|
s.testGetLanguageByContent(c, "Groff")
|
|
|
|
s.testGetLanguageByContent(c, "PicoLisp")
|
|
|
|
s.testGetLanguageByContent(c, "PicoLisp")
|
|
|
|
s.testGetLanguageByContent(c, "NewLisp")
|
|
|
|
s.testGetLanguageByContent(c, "Lex")
|
|
|
|
s.testGetLanguageByContent(c, "TeX")
|
|
|
|
s.testGetLanguageByContent(c, "Visual Basic")
|
|
|
|
s.testGetLanguageByContent(c, "Matlab")
|
|
|
|
s.testGetLanguageByContent(c, "Mathematica")
|
|
|
|
s.testGetLanguageByContent(c, "Prolog")
|
|
|
|
s.testGetLanguageByContent(c, "Perl")
|
|
|
|
s.testGetLanguageByContent(c, "Perl6")
|
2016-07-14 13:14:32 +00:00
|
|
|
s.testGetLanguageByContent(c, "Hack")
|
2016-07-13 20:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TSuite) testGetLanguageByContent(c *C, expected string) {
|
|
|
|
files, err := filepath.Glob(path.Join(".linguist/samples", expected, "*"))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
s, _ := os.Stat(file)
|
|
|
|
if s.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
content, _ := ioutil.ReadFile(file)
|
|
|
|
obtained, _ := GetLanguageByContent(path.Base(file), content)
|
2016-07-13 22:08:09 +00:00
|
|
|
if obtained == OtherLanguage {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(obtained, Equals, expected, Commentf(file))
|
2016-07-13 20:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
total++
|
|
|
|
expected := filepath.Base(filepath.Dir(path))
|
|
|
|
filename := filepath.Base(path)
|
|
|
|
content, _ := ioutil.ReadFile(path)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|