1 require File.dirname(__FILE__) + '/../test_helper'
4 # DetectorTest covers all Detector scenarios.
6 # The directory <tt>test/detect_files</tt> contains test files for the detector.
7 # These files are not used in parser testing; they are strictly for detection.
11 # To manually test an addition to the detector, rebuild ohcount and run it against
15 # bin/ohcount --detect test/detect_files/my_file.ext
17 # If the detector is working, you should see the name of your expected language:
19 # my_language test/detect_files/my_file.ext
21 class Ohcount::DetectorTest < Ohcount::Test
23 def do_detect(filename, filenames = [])
24 file_location = File.dirname(__FILE__) + "/../detect_files/" + filename
25 sfc = Ohcount::SimpleFileContext.new(filename, filenames, nil, file_location)
26 Ohcount::Detector.detect(sfc)
29 # Nonrecursively adds files from the file's directory to the context
30 def do_detect_with_siblings(filename)
31 file_location = File.dirname(__FILE__) + "/../detect_files/" + filename
32 filenames = Dir.entries(File.dirname(__FILE__) + File.dirname("/../detect_files/" + filename)) - [filename]
33 sfc = Ohcount::SimpleFileContext.new(filename, filenames, nil, file_location)
34 Ohcount::Detector.detect(sfc)
37 def test_matlab_or_objective_c
38 assert_equal 'objective_c', do_detect("t1.m")
39 assert_equal 'objective_c', do_detect("t2.m")
42 def text_fortran_fixedfree
43 assert_equal 'fortranfixed', do_detect("fortranfixed.f")
44 assert_equal 'fortranfree', do_detect("fortranfree.f")
47 def test_detect_polyglot
48 assert_equal "c", do_detect("foo.c")
49 assert_equal "c", do_detect("foo.c")
50 assert_equal "c", do_detect("uses_no_cpp.h")
51 assert_equal "cpp", do_detect("uses_cpp_headers.h")
52 assert_equal "cpp", do_detect("uses_cpp_stdlib_headers.h")
53 assert_equal "cpp", do_detect("uses_cpp_keywords.h")
54 assert_equal "ruby", do_detect("foo.rb")
55 assert_equal "make", do_detect("foo.mk")
56 assert_equal "matlab", do_detect("foo_matlab.m", ["foo_matlab.m", "bar.m", "README"])
57 assert_equal "objective_c", do_detect("foo_objective_c.m", ["foo_objective_c.m", "bar.h", "README"])
58 assert_equal "objective_c", do_detect("foo_objective_c.h", ["foo_objective_c.h, different_than_foo.m"])
59 assert_equal "php", do_detect("upper_case_php")
60 assert_equal "smalltalk", do_detect("example.st")
61 assert_equal "vala", do_detect("foo.vala")
62 assert_equal "tex", do_detect("foo.tex")
63 assert_equal "xslt", do_detect("example.xsl")
64 assert_equal "lisp", do_detect("core.lisp")
65 assert_equal "dmd", do_detect("foo.d")
66 assert_equal "vim", do_detect("foo.vim")
67 assert_equal "ebuild", do_detect("foo.ebuild")
68 assert_equal "ebuild", do_detect("foo.eclass")
69 assert_equal "exheres", do_detect("foo.exheres-0")
70 assert_equal "exheres", do_detect("foo.exlib")
71 assert_equal "eiffel", do_detect("eiffel.e")
72 assert_equal "ocaml", do_detect("ocaml.ml")
73 assert_equal "stratego", do_detect("stratego.str")
76 def test_upper_case_extensions
77 assert_equal "cpp", do_detect("foo_upper_case.C")
78 assert_equal "ruby", do_detect("foo_upper_case.RB")
81 def test_no_extensions
82 assert_equal "python", do_detect("py_script", [])
83 assert_equal "ruby", do_detect("ruby_script", [])
84 assert_equal "shell", do_detect("bourne_again_script", [])
85 assert_equal "shell", do_detect("bash_script", [])
86 assert_equal "perl", do_detect("perl_w", [])
87 assert_equal "dmd", do_detect("d_script", [])
91 assert_equal "autoconf", do_detect("configure.ac")
92 assert_equal "autoconf", do_detect("configure.in")
93 assert_equal "automake", do_detect("Makefile.am")
94 assert_equal "make", do_detect("Makefile")
97 def test_csharp_or_clearsilver
98 assert_equal 'csharp', do_detect("cs1.cs")
99 assert_equal 'clearsilver_template', do_detect("clearsilver_template1.cs")
103 assert_equal "structured_basic", do_detect("visual_basic.bas")
104 assert_equal "visualbasic", do_detect("visual_basic.bas", ["frx1.frx"])
105 assert_equal "classic_basic", do_detect("classic_basic.b")
106 assert_equal "structured_basic", do_detect("structured_basic.b")