[NEW] Zend Framework Platform
[ohcount] / test / unit / license_sniffer_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class Ohcount::LicensesSnifferTest < Ohcount::Test
4
5   # To make test creation easy & straighforward, we use a
6   # file-based convention, as follows:
7   #
8   # test/src_licenses/<license_name>_t1.c   <-- a 'C' file
9   #                                             containing <license_name>
10   #
11   # test/expected_licences/<license_name>_t1  <-- a text file containg
12   #                                               space-delimited list of
13   #                                               expected licenses
14   #
15   # This test loops over every file in the src_licenses directory ensuring
16   # the expected results are achieved.
17   #
18   def test_expected_licenses
19     src_dir = File.dirname(__FILE__) + "/../src_licenses"
20     expected_dir = File.dirname(__FILE__) + "/../expected_licenses"
21
22     Dir.entries(src_dir).each do |f|
23       filename = src_dir + "/" + f
24       next if File.directory?(filename)
25       next if f[0..0] == "."
26
27                         detected_licenses = Ohcount::SourceFile.new(filename).licenses
28                         # sort them
29       detected_licenses = detected_licenses.sort { |a,b| a.to_s <=> b.to_s }
30
31       # expected_licenses
32       begin
33         f =~ /^([^\.]*)/
34         expected_filename = $1
35         expected_licenses = File.new(expected_dir + "/" + expected_filename).read.split.sort.collect { |l| l.intern }
36       rescue
37         case $!
38         when Errno::ENOENT
39           assert false, "Missing expected_licenses file for #{ f }"
40         end
41       end
42
43       # match?
44       assert_equal expected_licenses, detected_licenses, "Mismatch in file #{ f }"
45     end
46   end
47
48 end