detectLanguage_test.go 849 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. }
  18. }
  19. func TestCheckAndDetection(t *testing.T) {
  20. for _, test := range testCases {
  21. content, err := ioutil.ReadFile(test.code)
  22. if err != nil {
  23. t.Errorf("Error reading file %s: %s", test.code, err)
  24. }
  25. code := string(content)
  26. result := check(code, checks[test.language])
  27. if result < test.score {
  28. t.Errorf("Certainty should be at least %.1f, but is %.1f", test.score, result)
  29. }
  30. language := detectLanguage(code)
  31. if language != test.language {
  32. t.Errorf("Language should be %s, but is %s", test.language, language)
  33. }
  34. }
  35. }