[NEW] Implement LocList += LocDelta and LocList += LocDeltaList
[ohcount] / test / test_helper.rb
1 require 'test/unit'
2 require 'fileutils'
3 require 'find'
4
5 unless defined?(TEST_DIR)
6         TEST_DIR = File.dirname(__FILE__)
7 end
8 require TEST_DIR + '/../lib/ohcount'
9
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.
12 #
13 # ==== Manual Testing
14 #
15 # To manually test a parser, rebuild ohcount and run it against your test file:
16 #
17 #   rake
18 #   bin/ohcount --annotate test/src_dir/my_file.ext
19 #
20 # The +annotate+ option will emit your test file to the console, and each line will be
21 # labeled as code, comment, or blank.
22 #
23 class Ohcount::Test < Test::Unit::TestCase
24
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.
27         def default_test
28         end
29
30         def src_dir
31                 File.expand_path(File.join(TEST_DIR, "src_dir"))
32         end
33
34         def expected_dir
35                 File.expand_path(File.join(TEST_DIR, "expected_dir"))
36         end
37
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.
40         #
41         # The file to be parsed must be in directory <tt>test/src_dir</tt>.
42         #
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.
46         #
47         # To create a new test case:
48         #
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>.
51         #
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.
55         #
56         # If you've cheated and written your code before your test, then you can simply
57         # use ohcount itself to create this file:
58         #
59         #     <tt>bin/ohcount --annotate src_dir/my_file.ext > expected_dir/myfile.ext</tt>
60         #
61         # Be sure to carefully confirm this result or your test will be meaningless!
62         #
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)
66                 buffer = ''
67                 if source.polyglot
68                         Ohcount::parse(source.contents, source.polyglot) do |language, semantic, line|
69                                 buffer << "#{language}\t#{semantic}\t#{line}"
70                         end
71                 end
72                 expected = File.read(File.join(expected_dir, file))
73
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`
78                 #       end
79
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)}."
82         end
83
84         def entities_array(src_string, polyglot, *entities)
85                 arr = Array.new
86                 Ohcount::parse_entities(src_string, polyglot) do |lang, entity, s, e|
87                         arr << src_string[s...e] if entities.include?(entity)
88                 end
89                 arr
90         end
91 end
92