5 unless defined?(TEST_DIR)
6 TEST_DIR = File.dirname(__FILE__)
8 require TEST_DIR + '/../lib/ohcount'
10 # Ohcount::Test is a base class which includes several helper methods for parser testing.
11 # All unit tests in Ohcount should derive from this class.
15 # To manually test a parser, rebuild ohcount and run it against your test file:
18 # bin/ohcount --annotate test/src_dir/my_file.ext
20 # The +annotate+ option will emit your test file to the console, and each line will be
21 # labeled as code, comment, or blank.
23 class Ohcount::Test < Test::Unit::TestCase
25 # For reasons unknown, the base class defines a default_test method to throw a failure.
26 # We override it with a no-op to prevent this 'helpful' feature.
31 File.expand_path(File.join(TEST_DIR, "src_dir"))
35 File.expand_path(File.join(TEST_DIR, "expected_dir"))
38 # verify_parse runs a full test against a specified file. Detector is used to determine
39 # the correct parser, then the file is parsed and compared against expected results.
41 # The file to be parsed must be in directory <tt>test/src_dir</tt>.
43 # The expected results must be stored in directory <tt>test/expected_dir</tt>, and
44 # must be in the format produced by <tt>bin/ohcount --annotate</tt>. That is, each line
45 # of the expected file should be prefixed with the tab-delimited language and semantic.
47 # To create a new test case:
49 # 1. Create a new source code file in <tt>test/src_dir</tt>.
50 # For example, <tt>test/src_dir/my_file.ext</tt>.
52 # 2. Copy your source file to the <tt>test/expected_dir</tt> directory.
53 # Use a text editor to insert the language and semantic at the front of each line.
54 # These must be tab-delimited from the rest of the line.
56 # If you've cheated and written your code before your test, then you can simply
57 # use ohcount itself to create this file:
59 # <tt>bin/ohcount --annotate src_dir/my_file.ext > expected_dir/myfile.ext</tt>
61 # Be sure to carefully confirm this result or your test will be meaningless!
63 # There are numerous examples in the test directories to help you out.
64 def verify_parse(file, filenames=[])
65 source = Ohcount::SourceFile.new(File.join(src_dir, file), :filenames => filenames)
68 Ohcount::parse(source.contents, source.polyglot) do |language, semantic, line|
69 buffer << "#{language}\t#{semantic}\t#{line}"
72 expected = File.read(File.join(expected_dir, file))
74 # Uncomment the following lines if you need to see a diff explaining why your test is failing
75 # if expected != buffer
76 # File.open("/tmp/ohcount","w") { |f| f.write buffer }
77 # puts `diff #{File.join(expected_dir, file)} /tmp/ohcount`
80 puts buffer if expected != buffer
81 assert expected == buffer, "Parse result of #{File.join(src_dir, file)} did not match expected #{File.join(expected_dir, file)}."
84 def entities_array(src_string, polyglot, *entities)
86 Ohcount::parse_entities(src_string, polyglot) do |lang, entity, s, e|
87 arr << src_string[s...e] if entities.include?(entity)