detectLanguage_test.go 888 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. )
  6. type testCase struct {
  7. language string
  8. code string
  9. score float64
  10. }
  11. var testCases []testCase
  12. func init() {
  13. testCases = []testCase{
  14. {"c", "testFiles/c.txt", 0.4},
  15. {"csharp", "testFiles/csharp.txt", 0.6},
  16. {"go", "testFiles/go.txt", 0.5},
  17. {"html", "testFiles/html.txt", 0.6},
  18. }
  19. }
  20. func TestCheckAndDetection(t *testing.T) {
  21. for _, test := range testCases {
  22. content, err := ioutil.ReadFile(test.code)
  23. if err != nil {
  24. t.Errorf("Error reading file %s: %s", test.code, err)
  25. }
  26. code := string(content)
  27. result := check(code, checks[test.language])
  28. if result < test.score {
  29. t.Errorf("Certainty should be at least %.1f, but is %.1f", test.score, result)
  30. }
  31. language := detectLanguage(code)
  32. if language != test.language {
  33. t.Errorf("Language should be %s, but is %s", test.language, language)
  34. }
  35. }
  36. }