aboutsummaryrefslogtreecommitdiff
path: root/test/test_classifier.rb
blob: ac7fbb0d71c7a948c1bd7a837f5ba43a44acceb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'linguist/classifier'
require 'linguist/language'
require 'linguist/sample'
require 'linguist/tokenizer'

require 'test/unit'

class TestClassifier < Test::Unit::TestCase
  include Linguist

  def samples_path
    File.expand_path("../../samples", __FILE__)
  end

  def fixture(name)
    File.read(File.join(samples_path, name))
  end

  def test_instance_freshness
    # Just warn, it shouldn't scare people off by breaking the build.
    unless Classifier.instance.eql?(Linguist::Sample.classifier)
      warn "Classifier database is out of date. Run `bundle exec rake classifier`."
    end
  end

  def test_classify
    classifier = Classifier.new
    classifier.train Language["Ruby"], fixture("ruby/foo.rb")
    classifier.train Language["Objective-C"], fixture("objective-c/Foo.h")
    classifier.train Language["Objective-C"], fixture("objective-c/Foo.m")

    results = classifier.classify(fixture("objective-c/hello.m"))
    assert_equal Language["Objective-C"], results.first[0]

    tokens  = Tokenizer.tokenize(fixture("objective-c/hello.m"))
    results = classifier.classify(tokens)
    assert_equal Language["Objective-C"], results.first[0]
  end

  def test_restricted_classify
    classifier = Classifier.new
    classifier.train Language["Ruby"], fixture("ruby/foo.rb")
    classifier.train Language["Objective-C"], fixture("objective-c/Foo.h")
    classifier.train Language["Objective-C"], fixture("objective-c/Foo.m")

    results = classifier.classify(fixture("objective-c/hello.m"), [Language["Objective-C"]])
    assert_equal Language["Objective-C"], results.first[0]

    results = classifier.classify(fixture("objective-c/hello.m"), [Language["Ruby"]])
    assert_equal Language["Ruby"], results.first[0]
  end

  def test_instance_classify_empty
    results = Classifier.instance.classify("")
    assert results.first[1] < 0.5, results.first.inspect
  end

  def test_instance_classify_nil
    assert_equal [], Classifier.instance.classify(nil)
  end

  def test_verify
    assert Classifier.instance.verify
  end

  def test_gc
    Classifier.instance.gc
  end

  def test_classify_ambiguous_languages
    Sample.each do |sample|
      # TODO: These tests are pending
      next if sample.path =~ /hello.h/
      next if sample.path =~ /MainMenuViewController.h/

      next unless sample.language.overrides.any?

      extname   = File.extname(sample.path)
      languages = Language.all.select { |l| l.extensions.include?(extname) }
      next unless languages.length > 1

      results = Classifier.instance.classify(sample.data, languages)
      assert_equal sample.language, results.first[0], "#{sample.path}\n#{results.inspect}"
    end
  end
end