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 test_with_source_file_without_backing_file
26 require File.dirname(__FILE__) + '/../config/boot'
27 require 'commands/generate'
30 source_file = SourceFile.new("generate", :contents => contents)
31 assert_equal 'ruby', source_file.polyglot
34 def do_detect(filename, filenames = [])
35 filepath = File.dirname(__FILE__) + "/../detect_files/" + filename
36 SourceFile.new(filepath, {:filenames => filenames}).polyglot
39 # Nonrecursively adds files from the file's directory to the context
40 def do_detect_with_siblings(filename)
41 filepath = File.dirname(__FILE__) + "/../detect_files/" + filename
42 filenames = Dir.entries(File.dirname(__FILE__) + File.dirname("/../detect_files/" + filename)) - [filename]
43 SourceFile.new(filepath, {:filenames => filename}).detect
47 assert_equal "smalltalk", do_detect("example.st")
50 def test_matlab_or_objective_c
51 assert_equal 'objective_c', do_detect("t1.m")
52 assert_equal 'objective_c', do_detect("t2.m")
55 def test_fortran_fixedfree
56 assert_equal 'fortranfixed', do_detect("fortranfixed.f")
57 assert_equal 'fortranfree', do_detect("fortranfree.f")
60 def test_detect_polyglot
61 assert_equal "c", do_detect("foo.c")
62 assert_equal "c", do_detect("uses_no_cpp.h")
63 assert_equal "cpp", do_detect("uses_cpp_headers.h")
64 assert_equal "cpp", do_detect("uses_cpp_stdlib_headers.h")
65 assert_equal "cpp", do_detect("uses_cpp_keywords.h")
66 assert_equal "ruby", do_detect("foo.rb")
67 assert_equal "make", do_detect("foo.mk")
68 assert_equal "matlab", do_detect("foo_matlab.m", ["foo_matlab.m", "bar.m", "README"])
69 assert_equal "objective_c", do_detect("foo_objective_c.m", ["foo_objective_c.m", "bar.h", "README"])
70 assert_equal "objective_c", do_detect("foo_objective_c.h", ["foo_objective_c.h, different_than_foo.m"])
71 assert_equal "php", do_detect("upper_case_php")
72 assert_equal "smalltalk", do_detect("example.st")
73 assert_equal "vala", do_detect("foo.vala")
74 assert_equal "tex", do_detect("foo.tex")
75 assert_equal "xslt", do_detect("example.xsl")
76 assert_equal "lisp", do_detect("core.lisp")
77 assert_equal "dmd", do_detect("foo.d")
78 assert_equal "vim", do_detect("foo.vim")
79 assert_equal "ebuild", do_detect("foo.ebuild")
80 assert_equal "ebuild", do_detect("foo.eclass")
81 assert_equal "exheres", do_detect("foo.exheres-0")
82 assert_equal "exheres", do_detect("foo.exlib")
83 assert_equal "eiffel", do_detect("eiffel.e")
84 assert_equal "ocaml", do_detect("ocaml.ml")
85 assert_equal "stratego", do_detect("stratego.str")
86 assert_equal "r",do_detect("foo.R")
87 assert_equal "glsl", do_detect("foo.glsl")
88 assert_equal "glsl", do_detect("foo_glsl.vert")
89 assert_equal "glsl", do_detect("foo_glsl.frag")
92 def test_upper_case_extensions
93 assert_equal "cpp", do_detect("foo_upper_case.C")
94 assert_equal "ruby", do_detect("foo_upper_case.RB")
97 def test_no_extensions
98 assert_equal "python", do_detect("py_script", [])
99 assert_equal "ruby", do_detect("ruby_script", [])
100 assert_equal "shell", do_detect("bourne_again_script", [])
101 assert_equal "shell", do_detect("bash_script", [])
102 assert_equal "perl", do_detect("perl_w", [])
103 assert_equal "dmd", do_detect("d_script", [])
105 assert_equal "tcl", do_detect("tcl_script", [])
106 assert_equal "python", do_detect("python.data", [])
107 assert_equal "python", do_detect("python2.data", [])
111 assert_equal "autoconf", do_detect("configure.ac")
112 assert_equal "autoconf", do_detect("configure.in")
113 assert_equal "automake", do_detect("Makefile.am")
114 assert_equal "make", do_detect("Makefile")
117 def test_csharp_or_clearsilver
118 assert_equal 'csharp', do_detect("cs1.cs")
119 assert_equal 'clearsilver_template', do_detect("clearsilver_template1.cs")
123 assert_equal "structured_basic", do_detect("visual_basic.bas")
124 assert_equal "visualbasic", do_detect("visual_basic.bas", ["frx1.frx"])
125 assert_equal "classic_basic", do_detect("classic_basic.b")
126 assert_equal "structured_basic", do_detect("structured_basic.b")