From 4b7b8e09336e9aa7c004ecf55421964423c1a9c8 Mon Sep 17 00:00:00 2001 From: Andy Verprauskus Date: Thu, 2 Jul 2009 14:12:12 -0700 Subject: [PATCH] [FIX] new gestalts, step 2/2 [FIX] add "#import" as an objective_c tell --- build | 1 - ruby/gestalt.rb | 5 + ruby/gestalt/definitions.rb | 120 ++++-- src/detector.c | 4 +- .../weblogic_descriptor/foo.java | 4 + .../weblogic-application.xml | 1 + test/gestalt_files/weblogic_maven/foo.java | 4 + test/gestalt_files/weblogic_maven/pom.xml | 9 + test/gestalt_files/websphere/foo.java | 4 + .../websphere/ibm-webservices-bnd.xmi | 0 .../ruby/gestalt/csharp_using_rule_test.rb | 33 ++ test/unit/ruby/gestalt/definitions_test.rb | 402 ++++++++++++++++++ .../ruby/gestalt/dot_net_definitions_test.rb | 63 +++ test/unit/ruby/gestalt/file_rule_test.rb | 54 +++ test/unit/ruby/gestalt/filename_rule_test.rb | 19 + .../gestalt/find_java_imports_rule_test.rb | 32 ++ test/unit/ruby/gestalt/gestalt_test.rb | 6 + .../ruby/gestalt/java_definitions_test.rb | 31 ++ test/unit/ruby/gestalt/keyword_rule_test.rb | 20 + .../gestalt/maven_dependency_rule_test.rb | 60 +++ test/unit/ruby/gestalt/rule_test.rb | 11 + test/unit/ruby/ruby_test.rb | 4 +- test/unit/ruby/source_file_list_test.rb | 14 +- test/unit/ruby/test_helper.rb | 60 +++ test/unit/sourcefile_test.h | 1 + 25 files changed, 920 insertions(+), 42 deletions(-) create mode 100644 test/gestalt_files/weblogic_descriptor/foo.java create mode 100644 test/gestalt_files/weblogic_descriptor/weblogic-application.xml create mode 100644 test/gestalt_files/weblogic_maven/foo.java create mode 100644 test/gestalt_files/weblogic_maven/pom.xml create mode 100644 test/gestalt_files/websphere/foo.java create mode 100644 test/gestalt_files/websphere/ibm-webservices-bnd.xmi create mode 100644 test/unit/ruby/gestalt/csharp_using_rule_test.rb create mode 100644 test/unit/ruby/gestalt/definitions_test.rb create mode 100644 test/unit/ruby/gestalt/dot_net_definitions_test.rb create mode 100644 test/unit/ruby/gestalt/file_rule_test.rb create mode 100644 test/unit/ruby/gestalt/filename_rule_test.rb create mode 100644 test/unit/ruby/gestalt/find_java_imports_rule_test.rb create mode 100644 test/unit/ruby/gestalt/gestalt_test.rb create mode 100644 test/unit/ruby/gestalt/java_definitions_test.rb create mode 100644 test/unit/ruby/gestalt/keyword_rule_test.rb create mode 100644 test/unit/ruby/gestalt/maven_dependency_rule_test.rb create mode 100644 test/unit/ruby/gestalt/rule_test.rb create mode 100644 test/unit/ruby/test_helper.rb diff --git a/build b/build index 5f71dd0..aca8ec6 100755 --- a/build +++ b/build @@ -110,7 +110,6 @@ build_ruby_bindings() sh -c "$cc $RB_SHARED ruby/ohcount_wrap.c $files -o ruby/$RB_SHARED_NAME \ -I`ruby -rmkmf -e 'print Config::expand(CONFIG["archdir"])'` \ -lpcre" || exit 1 - sh -c "cd test/unit/gestalt && ruby gestalt_test.rb" || exit 1 sh -c "cd test/unit/ruby && ruby ruby_test.rb" || exit 1 } diff --git a/ruby/gestalt.rb b/ruby/gestalt.rb index 6c40ac3..d437ace 100644 --- a/ruby/gestalt.rb +++ b/ruby/gestalt.rb @@ -10,7 +10,11 @@ require "gestalt/trigger" require "gestalt/definition" require "gestalt/base" require "gestalt/definitions" +require "gestalt/dot_net_definitions" +require "gestalt/java_definitions" require "gestalt/gestalt_engine" +require "gestalt/gestalt_facts" + module Ohcount class SourceFile @@ -76,6 +80,7 @@ if __FILE__ == $0 sourcefile_list.add_file(file_or_path) end end + STDOUT.puts "Examining #{sourcefile_list.size} file(s) for gestalts." sourcefile_list.analyze_gestalt() sourcefile_list.gestalts.sort.each do |gestalt| puts "#{gestalt.type}\t#{gestalt.count.to_s.rjust(8)}\t#{gestalt.name}" diff --git a/ruby/gestalt/definitions.rb b/ruby/gestalt/definitions.rb index 37fe678..2881e96 100644 --- a/ruby/gestalt/definitions.rb +++ b/ruby/gestalt/definitions.rb @@ -30,18 +30,10 @@ module Ohcount filenames '\.xaml$' end - define_platform 'Dot_NET' do - language :csharp, :min_percent => 10 - end - define_platform 'VisualBasic' do language :visualbasic, :min_percent => 5 end - define_platform 'ASP_NET' do - filenames('\.(aspx|ascx|ashx|asax|axd)$') - end - define_platform 'Ruby' do language :ruby, :min_percent => 15 end @@ -65,13 +57,6 @@ module Ohcount filenames 'jquery-\d.\d.\d.min.js' end - define_platform 'SpringFramework' do - _and do - gestalt(:platform,'Java') - filenames('spring\.jar$') - end - end - define_platform 'XWindows' do c_headers 'Xlib.h', 'X11\/xpm.h', 'X11/Xlib.h' end @@ -201,35 +186,104 @@ module Ohcount filenames '\.nbm$' end + ############################ ARM ################################### + + define_platform 'arm' do + c_headers "arm4.h", "arm3.h" + c_keywords "arm_int32_t", "arm_start_application", "__arm__" + make_keywords '\b-mabi\b','\barmcc\b' + java_keywords '\barm-eabi' + assembler_keywords '\bsmlal\b', '\bsmulw\b', '\borrs\b' + gestalt(:platform, 'arm_neon') + java_import /org\.opengroup\.arm/ + end + + ########################## ARM NEON ################################ + + define_platform 'arm_neon' do + assembler_keywords '\bvld1.\d\d\b', '\bvld1.\d\d\b','\bvmov\b','\bvmov.u8\b' + make_keywords '\bneon\b','\bNEON\b' + end - ########################## Java Jars ############################### + ############################ ATOM ################################## + # Atom is hard to detect, since it implements x86 instruction set - define_java_jar do - find_filenames /([^\\^\/]*\.(jar|JAR))/, :name_from_match => 1 + define_platform 'sse3_atom_flag' do + make_keywords 'SSE3_ATOM' end + define_platform 'xL_flag' do + make_keywords 'xL', '\/QxL' + end - ######################## Java Imports ############################## + define_platform 'atom' do + gestalt(:platform, 'sse3_atom_flag') + gestalt(:platform, 'xL_flag') + end - define_java_import do - find_java_imports - end + ####################### INTEL COMPILER ############################# + define_platform 'intel_compiler' do + make_keywords '\bicc\b' + end + ########################### MOBLIN ################################# - ############################ ARM ################################### + define_platform 'clutter' do + c_keywords "ClutterActor", '\bclutter_actor_', "ClutterStage", "ClutterBehavior", "clutter_main" + perl_keywords "Clutter::Behavior", "Clutter::Actor", "Clutter::Stage" + java_keywords "ClutterActor", "ClutterStage", "ClutterShader" + ruby_keywords "Clutter::Actor", "Clutter::Stage", "Clutter::Shader", "Clutter::Cairo" + end - define_platform 'arm' do - makefile_keywords '\b-mabi\b','\barmcc\b' - assembler_keywords '\bsmlal\b', '\bsmulw\b', '\borrs\b' - gestalt(:platform, 'arm_neon') - end + define_platform 'moblin' do + c_keywords '\"org\.moblin\.', 'MOBLIN_NETBOOK_SYSTEM_TRAY_H', 'org_Moblin_', '\"org\.Moblin\.', "url=\"http://moblin.org\">http://moblin.org" + make_keywords "org.moblin.", "moblin-netbook" + filenames 'moblin-netbook-system-tray.h$' + end - ########################## ARM NEON ################################ + define_platform 'nbtk' do + c_keywords '\bnbtk_[a-z]+', '\bNbtk[A-Z][a-z]+' + java_keywords '\bnbtk_[a-z]+', '\bNbtk[A-Z][a-z]+' + ruby_keywords '\bnbtk_[a-z]+', '\bNbtk[A-Z][a-z]+' + filenames 'nbtk\/nbtk.h' + end - define_platform 'arm_neon' do - assembler_keywords '\bvld1.\d\d\b', '\bvld1.\d\d\b','\bvmov\b','\bvmov.u8\b' - makefile_keywords '\bneon\b','\bNEON\b' - end + define_platform 'moblin_all' do + gestalt(:platform, 'clutter') + gestalt(:platform, 'moblin') + gestalt(:platform, 'nbtk') + end + ########################### ANDROID ################################# + + define_platform 'android' do + java_import /\bandroid\./ + end + + ############################ iPhone ################################# + + define_platform 'iPhone' do + objective_c_keywords '\bUIApplicationMain', '\bUIWindow', '\bUIView', '\bUIResponder' + end + + ############################ Maemo ################################# + + define_platform 'hildon' do + c_keywords '\bHildonFile', '\bhildon_file_', '\bHildonProgram', '\bHildonWindow', '\bhildon_window' + c_headers 'hildon/hildon.h' + _and do + python_keywords '\bimport hildon' + python_keywords '\bimport gtk' + end + end + define_platform 'maemo' do + gestalt(:platform, 'hildon') + end + + ############################ Windows CE ############################ + define_platform 'windows_ce_incomplete' do + csharp_keywords 'Microsoft.WindowsCE', 'Microsoft.WindowsMobile' + vb_keywords 'Microsoft.WindowsCE', 'Microsoft.WindowsMobile' + end end end diff --git a/src/detector.c b/src/detector.c index c8ff3b9..c23b9f6 100644 --- a/src/detector.c +++ b/src/detector.c @@ -506,6 +506,8 @@ const char *disambiguate_m(SourceFile *sourcefile) { while (*p == ' ' || *p == '\t') p++; if (*p == '%') { // Matlab comment matlab_score++; + } else if (*p == '#' && strncmp(p, "#import", 7) == 0) { // Objective C + objective_c_score++; } else if (*p == '#') { // Limbo or Octave comment while (*p == '#') p++; if (*p == ' ' || *p == '\t') { @@ -517,7 +519,7 @@ const char *disambiguate_m(SourceFile *sourcefile) { objective_c_score++; // Objective C comment } else if (*p == '+' || *p == '-') { // Objective C method signature objective_c_score++; - } else if (*p == '@') { // Objective C method signature + } else if (*p == '@' || *p == '#') { // Objective C method signature if (strncmp(p, "@implementation", 15) == 0 || strncmp(p, "@interface", 10) == 0) objective_c_score++; diff --git a/test/gestalt_files/weblogic_descriptor/foo.java b/test/gestalt_files/weblogic_descriptor/foo.java new file mode 100644 index 0000000..a6f7059 --- /dev/null +++ b/test/gestalt_files/weblogic_descriptor/foo.java @@ -0,0 +1,4 @@ +/* Just a comment */ +{ + return a_few_lines_of_java_to_trigger_the_java_platform(); +} diff --git a/test/gestalt_files/weblogic_descriptor/weblogic-application.xml b/test/gestalt_files/weblogic_descriptor/weblogic-application.xml new file mode 100644 index 0000000..fc65d56 --- /dev/null +++ b/test/gestalt_files/weblogic_descriptor/weblogic-application.xml @@ -0,0 +1 @@ + diff --git a/test/gestalt_files/weblogic_maven/foo.java b/test/gestalt_files/weblogic_maven/foo.java new file mode 100644 index 0000000..a6f7059 --- /dev/null +++ b/test/gestalt_files/weblogic_maven/foo.java @@ -0,0 +1,4 @@ +/* Just a comment */ +{ + return a_few_lines_of_java_to_trigger_the_java_platform(); +} diff --git a/test/gestalt_files/weblogic_maven/pom.xml b/test/gestalt_files/weblogic_maven/pom.xml new file mode 100644 index 0000000..041a4ea --- /dev/null +++ b/test/gestalt_files/weblogic_maven/pom.xml @@ -0,0 +1,9 @@ + + + + org.codehaus.mojo + weblogic-maven-plugin + + + diff --git a/test/gestalt_files/websphere/foo.java b/test/gestalt_files/websphere/foo.java new file mode 100644 index 0000000..a6f7059 --- /dev/null +++ b/test/gestalt_files/websphere/foo.java @@ -0,0 +1,4 @@ +/* Just a comment */ +{ + return a_few_lines_of_java_to_trigger_the_java_platform(); +} diff --git a/test/gestalt_files/websphere/ibm-webservices-bnd.xmi b/test/gestalt_files/websphere/ibm-webservices-bnd.xmi new file mode 100644 index 0000000..e69de29 diff --git a/test/unit/ruby/gestalt/csharp_using_rule_test.rb b/test/unit/ruby/gestalt/csharp_using_rule_test.rb new file mode 100644 index 0000000..182c0f4 --- /dev/null +++ b/test/unit/ruby/gestalt/csharp_using_rule_test.rb @@ -0,0 +1,33 @@ +require File.dirname(__FILE__) + '/../../test_helper' + +class CSharpUsingRuleTest < Test::Unit::TestCase + include Ohcount::Gestalt + + + def test_sample + cs = SourceFile.new("hello.cs", :contents => <<-INLINE +using System; +using System.Foo; +using NUnit.Framework; + +namespace Hello +{ + /// Hi there +} + INLINE + ) + + r = CSharpUsingRule.new(/System/) + r.process_source_file(cs) + assert_equal 2, r.count + + r = CSharpUsingRule.new(/^System$/) + r.process_source_file(cs) + assert_equal 1, r.count + + r = CSharpUsingRule.new(/.+/) + r.process_source_file(cs) + assert_equal 3, r.count + + end +end diff --git a/test/unit/ruby/gestalt/definitions_test.rb b/test/unit/ruby/gestalt/definitions_test.rb new file mode 100644 index 0000000..4c47556 --- /dev/null +++ b/test/unit/ruby/gestalt/definitions_test.rb @@ -0,0 +1,402 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../../../ruby/gestalt' + +include Ohcount +include Ohcount::Gestalt + +class PlatformTest < Ohcount::Test + + def test_zend_framework + assert_gestalts 'zend_framework', [ + Base.new(:platform,'PHP'), + Base.new(:platform,'ZendFramework'), + Base.new(:platform,'Scripting') + ] + end + + def test_php + assert_gestalts 'php', [ + Base.new(:platform,'PHP'), + Base.new(:platform,'Scripting') + ] + end + + def test_wx_widgets + assert_gestalts 'wx_widgets', [ + Base.new(:platform,'WxWidgets') + ] + end + + def test_eclipse_platform + assert_gestalts 'eclipse_platform', [ + Base.new(:platform,'Java'), + Base.new(:platform,'EclipsePlatform'), + Base.new(:java_import,"java.text.SimpleDateFormat"), + Base.new(:java_import,"java.util.Map"), + Base.new(:java_import,"org.eclipse.core") + ] + end + + def test_win32_not_enough + assert_gestalts 'win32_not_enough', [] + end + + def test_win32_enough + assert_gestalts 'win32_enough', [ + Base.new(:platform, 'Win32') + ] + end + + def test_wpf + assert_gestalts 'wpf', [ + Base.new(:platform, 'WPF') + ] + end + + def test_asp_net + assert_gestalts 'asp_net', [ + Base.new(:platform, 'ASP_NET') + ] + end + + def test_ruby_just_enough + assert_gestalts 'ruby_just_enough', [ + Base.new(:platform, 'Ruby'), + Base.new(:platform, 'Scripting') + ] + end + + def test_ruby_not_enough + assert_gestalts 'ruby_not_enough', [] + end + + def test_cakephp + assert_gestalts 'cakephp', [ + Base.new(:platform, 'PHP'), + Base.new(:platform, 'CakePHP'), + Base.new(:platform, 'Scripting'), + ] + end + + def test_symfony + assert_platform('symfony', :PHP, :Symfony, :Scripting) + end + + def test_pear + assert_platform('pear', :PHP, :Pear, :Scripting) + end + + def test_moodle + assert_platform('moodle', :PHP, :Moodle, :Scripting) + end + + def test_spring_framework + assert_gestalts 'spring_framework', [ + Base.new(:platform, 'Java'), + Base.new(:platform, 'SpringFramework'), + Base.new(:java_jar, 'spring.jar'), + ] + end + + def test_rails + assert_platform('rails', :Ruby, :Rails, :Scripting) + end + + def test_jquery + assert_platform('jquery', :Javascript, :JQuery, :Scripting) + end + + def test_python + assert_platform('python', :Python, :Scripting) + end + + def test_mac + assert_platform('mac', :Mac) + end + + def test_plist + assert_platform('plist', :Mac) + end + + def test_posix + assert_platform('posix', :POSIX) + end + + def test_x_windows + assert_platform('xwindows', :XWindows) + end + + def test_kde + assert_platform('kde', :KDE) + end + + def test_msdos + assert_platform('msdos', :MSDos) + end + + def test_gtk + assert_platform('gtk', :GTK) + end + + def test_drupal + assert_platform('drupal', :PHP, :Drupal, :Scripting) + end + + def test_vs_1 + assert_tool('vs_1', :VisualStudio) + end + + def test_eclipse + assert_tool('eclipse', :Eclipse) + end + + def test_netbeans + assert_tool('netbeans', :NetBeans) + end + + def test_java_imports_from_java_file + java = SourceFile.new("foo.java", :contents => <<-INLINE_C + import com.foo; + import net.ohloh; + import com.foo; + // import dont.import.this; + INLINE_C + ) + + expected_gestalts = [ + Base.new(:java_import, 'com.foo', 2), + Base.new(:java_import, 'net.ohloh'), + Base.new(:platform, 'Java'), + ] + + assert_equal expected_gestalts.sort, java.gestalts.sort + end + + def test_arm + asm = SourceFile.new("foo.S", :contents => <<-INLINE_ASM + orrs 3, eax + INLINE_ASM + ) + + expected_gestalts = [ + Base.new(:platform, 'arm') + ] + + assert_equal expected_gestalts.sort, asm.gestalts.sort + end + + def test_arm_from_c_keywords + c = SourceFile.new("foo.c", :contents => <<-INLINE_C + #define __arm__ + INLINE_C + ) + expected_gestalts = [ + Base.new(:platform, 'arm') + ] + assert_equal expected_gestalts, c.gestalts + end + + def test_arm_neon + asm = SourceFile.new("foo.S", :contents => <<-INLINE_ASM + vmov u8, s + INLINE_ASM + ) + + expected_gestalts = [ + Base.new(:platform, 'arm'), + Base.new(:platform, 'arm_neon') + ] + + assert_equal expected_gestalts.sort, asm.gestalts.sort + end + + def test_imports_from_java_file + jar = SourceFile.new("foo/foo.jar", :contents => '') + + expected_gestalts = [ + Base.new(:java_jar, 'foo.jar'), + ] + + assert_equal expected_gestalts.sort, jar.gestalts.sort + end + + def test_moblin_clutter + c = SourceFile.new("foo.c", :contents => <<-INLINE_C + clutter_actor_queue_redraw (CLUTTER_ACTOR(label)); + INLINE_C + ) + expected_gestalts = [ + Base.new(:platform, 'clutter'), + Base.new(:platform, 'moblin_all') + ] + + assert_equal expected_gestalts.sort, c.gestalts.sort + end + + def test_moblin_by_filename + c = SourceFile.new("moblin-netbook-system-tray.h", :contents => <<-INLINE_PERL + #include "foo" + INLINE_PERL + ) + expected_gestalts = [ + Base.new(:platform, 'moblin'), + Base.new(:platform, 'moblin_all') + ] + + assert_equal expected_gestalts.sort, c.gestalts.sort + end + + def test_moblin_by_keyword + c = SourceFile.new("foo.c", :contents => <<-INLINE_PERL + proxy = dbus_g_proxy_new_for_name (conn, "org.moblin.connman", + INLINE_PERL + ) + expected_gestalts = [ + Base.new(:platform, 'moblin'), + Base.new(:platform, 'moblin_all') + ] + + assert_equal expected_gestalts.sort, c.gestalts.sort + end + + def test_nbtk + c = SourceFile.new("foo.c", :contents => <<-INLINE_C + button = nbtk_button_new_with_label ("Back"); + INLINE_C + ) + expected_gestalts = [ + Base.new(:platform, 'nbtk'), + Base.new(:platform, 'moblin_all') + ] + + assert_equal expected_gestalts.sort, c.gestalts.sort + end + + + def test_android + java = SourceFile.new("foo.java", :contents => <<-INLINE_C + import android.app.Activity; + // import dont.import.this; + INLINE_C + ) + + expected_gestalts = [ + Base.new(:java_import, 'android.app.Activity'), + Base.new(:platform, 'Java'), + Base.new(:platform, 'android'), + ] + + assert_equal expected_gestalts.sort, java.gestalts.sort + end + + def test_iphone + objective_c = SourceFile.new("foo.m", :contents => <<-OBJECTIVE_C + #import + #import + #import "WhackABugApp.h" + + int main(int argc, char *argv[]) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + int ret = UIApplicationMain(argc, argv, [WhackABugApp class]); + [pool release]; + return ret; + } + OBJECTIVE_C + ) + + expected_gestalts = [ + Base.new(:platform, 'iPhone'), + ] + + assert_equal expected_gestalts.sort, objective_c.gestalts.sort + end + + def test_hildon + c = SourceFile.new("foo.c", :contents => <<-INLINE_C + HildonWindow *window; + INLINE_C + ) + expected_gestalts = [ + Base.new(:platform, 'hildon'), + Base.new(:platform, 'maemo') + ] + + assert_equal expected_gestalts.sort, c.gestalts.sort + end + + def test_atom_linux + make = SourceFile.new("makefile", :contents => <<-INLINE_MAKEFILE + COMPILE_FLAGS=/QxL + INLINE_MAKEFILE + ) + expected_gestalts = [ + Base.new(:platform, 'xL_flag'), + Base.new(:platform, 'atom') + ] + assert_equal expected_gestalts.sort, make.gestalts.sort + end + + def test_atom_windows + make = SourceFile.new("makefile", :contents => <<-INLINE_MAKEFILE + CCFLAGS = -xL + INLINE_MAKEFILE + ) + expected_gestalts = [ + Base.new(:platform, 'xL_flag'), + Base.new(:platform, 'atom') + ] + assert_equal expected_gestalts.sort, make.gestalts.sort + end + + def test_atom_sse3 + make = SourceFile.new("makefile", :contents => <<-INLINE_MAKEFILE + COMPILE_FLAGS=-xSSE3_ATOM_FLAG + INLINE_MAKEFILE + ) + expected_gestalts = [ + Base.new(:platform, 'sse3_atom_flag'), + Base.new(:platform, 'atom') + ] + assert_equal expected_gestalts.sort, make.gestalts.sort + end + + def test_intel_compiler + + make = SourceFile.new("Makefile", :contents => <<-INLINE_MAKEFILE + CC = icc + INLINE_MAKEFILE + ) + expected_gestalts = [ + Base.new(:platform, 'intel_compiler'), + ] + assert_equal expected_gestalts.sort, make.gestalts.sort + end + + def test_opensso + java = SourceFile.new("foo.java", :contents => <<-INLINE_JAVA +import com.sun.identity.authentication; + INLINE_JAVA + ) + expected_gestalts = [ + Base.new(:platform, 'Java'), + Base.new(:platform, 'OpenSSO'), + Base.new(:java_import, 'com.sun.identity') + ] + + assert_equal expected_gestalts.sort, java.gestalts.sort + end + + def test_windows_ce + csharp = SourceFile.new("bam.cs", :contents => <<-INLINE_CSHARP + using System; + using Microsoft.WindowsMobile.DirectX; + INLINE_CSHARP + ) + expected_gestalts = [ + Base.new(:platform, 'windows_ce_incomplete'), + Base.new(:platform, 'Dot_NET'), + ] + + assert_equal expected_gestalts.sort, csharp.gestalts.sort + end +end diff --git a/test/unit/ruby/gestalt/dot_net_definitions_test.rb b/test/unit/ruby/gestalt/dot_net_definitions_test.rb new file mode 100644 index 0000000..9e6aafc --- /dev/null +++ b/test/unit/ruby/gestalt/dot_net_definitions_test.rb @@ -0,0 +1,63 @@ +require File.dirname(__FILE__) + '/../../test_helper' + +include Ohcount +include Ohcount::Gestalt + +class DotNetDefinitionsTest < Test::Unit::TestCase + + def test_nunit + sf = SourceFile.new('foo.cs', :contents => <<-CONTENTS +using NUnit.Framework; +CONTENTS + ) + assert_equal [ + Gestalt::Base.new(:platform, 'Dot_NET'), + Gestalt::Base.new(:platform, 'NUnit') + ], sf.gestalts.sort + end + + def test_nhibernate + sf = SourceFile.new('foo.cs', :contents => <<-CONTENTS +using NHibernate.Connection.DriverConnectionProvider; +CONTENTS + ) + assert_equal [ + Gestalt::Base.new(:platform, 'Dot_NET'), + Gestalt::Base.new(:platform, 'NHibernate') + ], sf.gestalts.sort + end + + def test_remoting_implies_enterprise + sf = SourceFile.new('foo.cs', :contents => <<-CONTENTS +using System.Runtime.Remoting; +CONTENTS + ) + assert_equal [ + Gestalt::Base.new(:platform, 'Dot_NET'), + Gestalt::Base.new(:platform, 'Dot_NET_Enterprise') + ], sf.gestalts.sort + end + + def test_biztalk_implies_enterprise + sf = SourceFile.new('foo.cs', :contents => <<-CONTENTS +using Microsoft.BizTalk; +CONTENTS + ) + assert_equal [ + Gestalt::Base.new(:platform, 'Dot_NET'), + Gestalt::Base.new(:platform, 'Dot_NET_BizTalk'), + Gestalt::Base.new(:platform, 'Dot_NET_Enterprise') + ], sf.gestalts.sort + end + + def test_linq_implies_enterprise + sf = SourceFile.new('foo.cs', :contents => <<-CONTENTS +using System.Data.Linq; +CONTENTS + ) + assert_equal [ + Gestalt::Base.new(:platform, 'Dot_NET'), + Gestalt::Base.new(:platform, 'Dot_NET_Enterprise') + ], sf.gestalts.sort + end +end diff --git a/test/unit/ruby/gestalt/file_rule_test.rb b/test/unit/ruby/gestalt/file_rule_test.rb new file mode 100644 index 0000000..0274bf6 --- /dev/null +++ b/test/unit/ruby/gestalt/file_rule_test.rb @@ -0,0 +1,54 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../../../ruby/gestalt' + +class FileRuleTest < Test::Unit::TestCase + include Ohcount::Gestalt + + def test_initialize + r = FileRule.new(:min => 5) + assert_equal 5, r.min_count + end + + def test_initialize_wrong_args + assert_raise ArgumentError do + r = FileRule.new(:boo => 1) + end + end + +# def test_trigger_c_header +# r = Ohcount::Gestalt::CHeaderRule.new('foo.h') +# s = Ohcount::SourceFile.new('header.h', :contents => <<-H_FILE +# // a comment +# include 'foo.h' +# H_FILE +# ) +# assert r.trigger_file?(s) +# end +# +# def test_c_keyword_rule +# r = Ohcount::Gestalt::CKeywordRule.new('WM_PAINT') +# s = Ohcount::SourceFile.new('header.h', :contents => <<-H_FILE +# // a comment +# (WM_PAINT) +# H_FILE +# ) +# assert r.trigger_file?(s) +# end +# +# def test_filename_rule +# r = Ohcount::Gestalt::FilenameRule.new('header.h') +# s = Ohcount::SourceFile.new('header.h') +# assert r.trigger_file?(s) +# end +# +# def test_filename_rule_advanced +# r = Ohcount::Gestalt::FilenameRule.new('f[ab]o', 'foo') +# assert r.trigger_file?(Ohcount::SourceFile.new('fao')) +# assert r.trigger_file?(Ohcount::SourceFile.new('fbo')) +# assert r.trigger_file?(Ohcount::SourceFile.new('foo')) +# assert !r.trigger_file?(Ohcount::SourceFile.new('fco')) +# end +end + + + diff --git a/test/unit/ruby/gestalt/filename_rule_test.rb b/test/unit/ruby/gestalt/filename_rule_test.rb new file mode 100644 index 0000000..5992f70 --- /dev/null +++ b/test/unit/ruby/gestalt/filename_rule_test.rb @@ -0,0 +1,19 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../../../ruby/gestalt' + +class FilenameRuleTest < Test::Unit::TestCase + include Ohcount::Gestalt + + def test_process_file + r = FilenameRule.new('foo\.rb') + s = Ohcount::SourceFile.new('/bar/com/foo.rb', :contents => <<-RUBY_CODE + # comment + code = 1 + RUBY_CODE + ) + assert r.process_source_file(s) + assert_equal 1, r.count + end +end + + diff --git a/test/unit/ruby/gestalt/find_java_imports_rule_test.rb b/test/unit/ruby/gestalt/find_java_imports_rule_test.rb new file mode 100644 index 0000000..932d787 --- /dev/null +++ b/test/unit/ruby/gestalt/find_java_imports_rule_test.rb @@ -0,0 +1,32 @@ +require File.dirname(__FILE__) + '/../../test_helper' + +class FindJavaImportsRuleTest < Test::Unit::TestCase + include Ohcount::Gestalt + + def test_truncate_name + assert_equal "", FindJavaImportsRule.truncate_name(nil, 3) + assert_equal "", FindJavaImportsRule.truncate_name("", 3) + assert_equal "", FindJavaImportsRule.truncate_name("net.ohloh.ohcount.test", 0) + assert_equal "net", FindJavaImportsRule.truncate_name("net.ohloh.ohcount.test", 1) + assert_equal "net.ohloh", FindJavaImportsRule.truncate_name("net.ohloh.ohcount.test", 2) + assert_equal "net.ohloh.ohcount", FindJavaImportsRule.truncate_name("net.ohloh.ohcount.test", 3) + assert_equal "net.ohloh.ohcount.test", FindJavaImportsRule.truncate_name("net.ohloh.ohcount.test", 4) + assert_equal "net.ohloh.ohcount.test", FindJavaImportsRule.truncate_name("net.ohloh.ohcount.test", 5) + end + + def test_arm_from_java_import + java = SourceFile.new("foo.java", :contents => <<-INLINE_C + import org.opengroup.arm40.transaction.ArmConstants; + // import dont.import.this; + INLINE_C + ) + + expected_gestalts = [ + Base.new(:java_import, 'org.opengroup.arm40'), + Base.new(:platform, 'Java'), + Base.new(:platform, 'arm'), + ] + + assert_equal expected_gestalts.sort, java.gestalts.sort + end +end diff --git a/test/unit/ruby/gestalt/gestalt_test.rb b/test/unit/ruby/gestalt/gestalt_test.rb new file mode 100644 index 0000000..ae9ad29 --- /dev/null +++ b/test/unit/ruby/gestalt/gestalt_test.rb @@ -0,0 +1,6 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/definitions_test' +require File.dirname(__FILE__) + '/file_rule_test' +require File.dirname(__FILE__) + '/filename_rule_test' +require File.dirname(__FILE__) + '/keyword_rule_test' +require File.dirname(__FILE__) + '/rule_test' diff --git a/test/unit/ruby/gestalt/java_definitions_test.rb b/test/unit/ruby/gestalt/java_definitions_test.rb new file mode 100644 index 0000000..f7c84fe --- /dev/null +++ b/test/unit/ruby/gestalt/java_definitions_test.rb @@ -0,0 +1,31 @@ +require File.dirname(__FILE__) + '/../../test_helper' +include Ohcount +include Ohcount::Gestalt + +class JavaDefinitionsTest < Ohcount::Test + + def test_weblogic_via_maven + assert_gestalts 'weblogic_maven', [ + Base.new(:platform, 'AppServer'), + Base.new(:platform, 'Java'), + Base.new(:platform, 'Maven'), + Base.new(:platform, 'WebLogic') + ] + end + + def test_weblogic_via_descriptor + assert_gestalts 'weblogic_descriptor', [ + Base.new(:platform, 'AppServer'), + Base.new(:platform, 'Java'), + Base.new(:platform, 'WebLogic') + ] + end + + def test_webshpere_via_descriptor + assert_gestalts 'websphere', [ + Base.new(:platform, 'AppServer'), + Base.new(:platform, 'Java'), + Base.new(:platform, 'WebSphere') + ] + end +end diff --git a/test/unit/ruby/gestalt/keyword_rule_test.rb b/test/unit/ruby/gestalt/keyword_rule_test.rb new file mode 100644 index 0000000..4fa1045 --- /dev/null +++ b/test/unit/ruby/gestalt/keyword_rule_test.rb @@ -0,0 +1,20 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../../../ruby/gestalt' + +class KeywordRuleTest < Test::Unit::TestCase + include Ohcount::Gestalt + + def test_process_file + r = KeywordRule.new('c', 'WIN32') + s = Ohcount::SourceFile.new('header.c', :contents => <<-C_CODE + // a comment + #ifdef WIN32 + ..some code.. + #endif + C_CODE + ) + assert r.process_source_file(s) + assert_equal 1, r.count + end +end + diff --git a/test/unit/ruby/gestalt/maven_dependency_rule_test.rb b/test/unit/ruby/gestalt/maven_dependency_rule_test.rb new file mode 100644 index 0000000..332dee3 --- /dev/null +++ b/test/unit/ruby/gestalt/maven_dependency_rule_test.rb @@ -0,0 +1,60 @@ +require File.dirname(__FILE__) + '/../../test_helper' + +class MavenDependencyTest < Test::Unit::TestCase + include Ohcount::Gestalt + + + def test_dependency + pom = SourceFile.new("pom.xml", :contents => <<-INLINE + + + + + test_group_1 + test_artifact_1A + + + test_group_1 + test_artifact_1B + + + test_group_2 + test_artifact_2A + + + test_group_2 + test_artifact_2B + + + + INLINE + ) + + r = MavenRule.new('dependency', /1$/, /B$/) + + r.process_source_file(pom) + assert_equal 1, r.count + + end + + def test_plugin + pom = SourceFile.new("pom.xml", :contents => <<-INLINE + + + + foobar + baz + + + + INLINE + ) + + r = MavenRule.new('plugin', /^foobar\b/, /^baz\b/) + + r.process_source_file(pom) + assert_equal 1, r.count + end +end diff --git a/test/unit/ruby/gestalt/rule_test.rb b/test/unit/ruby/gestalt/rule_test.rb new file mode 100644 index 0000000..459bd18 --- /dev/null +++ b/test/unit/ruby/gestalt/rule_test.rb @@ -0,0 +1,11 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../../../ruby/gestalt' + +class RuleTest < Test::Unit::TestCase + + def test_here + + end +end + + diff --git a/test/unit/ruby/ruby_test.rb b/test/unit/ruby/ruby_test.rb index d916a0b..90fee5f 100644 --- a/test/unit/ruby/ruby_test.rb +++ b/test/unit/ruby/ruby_test.rb @@ -1 +1,3 @@ -require 'source_file_list_test.rb' +require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/source_file_list_test.rb' +require File.dirname(__FILE__) + '/gestalt/gestalt_test' diff --git a/test/unit/ruby/source_file_list_test.rb b/test/unit/ruby/source_file_list_test.rb index 7e83360..f50c8e5 100644 --- a/test/unit/ruby/source_file_list_test.rb +++ b/test/unit/ruby/source_file_list_test.rb @@ -1,5 +1,5 @@ require 'test/unit' -require '../../../ruby/gestalt' +require File.dirname(__FILE__) + '/../../../ruby/gestalt' class SourceFileListTest < Test::Unit::TestCase @@ -8,12 +8,14 @@ class SourceFileListTest < Test::Unit::TestCase list = Ohcount::SourceFileList.new(:paths => paths) assert list.size > 0 # assume: the paths variable points to the directory containing this and other simple ruby test files - gestalts = [ - Ohcount::Gestalt::Base.new(:platform, 'Ruby'), - Ohcount::Gestalt::Base.new(:platform, 'Scripting') - ] + + ruby = Ohcount::Gestalt::Base.new(:platform, 'Ruby') + scripting = Ohcount::Gestalt::Base.new(:platform, 'Scripting') + list.analyze(:gestalt) # this should work - assert_equal gestalts.sort, list.gestalts.sort # and should produce something similar to the above gestalts list + assert list.gestalts.include?(ruby) + assert list.gestalts.include?(scripting) + list.each do |filename| assert_equal String, filename.class end diff --git a/test/unit/ruby/test_helper.rb b/test/unit/ruby/test_helper.rb new file mode 100644 index 0000000..93bd79b --- /dev/null +++ b/test/unit/ruby/test_helper.rb @@ -0,0 +1,60 @@ +require 'test/unit' +require 'fileutils' +require 'find' +require File.dirname(__FILE__) + '/../../../ruby/ohcount.rb' # .rb is to specify the .rb instead of .bundle +require File.dirname(__FILE__) + '/../../../ruby/gestalt' # .rb is to specify the .rb instead of .bundle + +unless defined?(TEST_DIR) + TEST_DIR = File.dirname(__FILE__) +end + +module Ohcount +end + +# Ohcount::Test is a base class which includes several helper methods for parser testing. +# All unit tests in Ohcount should derive from this class. +# +# ==== Manual Testing +# +# To manually test a parser, rebuild ohcount and run it against your test file: +# +# rake +# bin/ohcount --annotate test/src_dir/my_file.ext +# +# The +annotate+ option will emit your test file to the console, and each line will be +# labeled as code, comment, or blank. +# +class Ohcount::Test < Test::Unit::TestCase + + # For reasons unknown, the base class defines a default_test method to throw a failure. + # We override it with a no-op to prevent this 'helpful' feature. + def default_test; end + + protected + + def assert_tool(path, *tools) + gestalts = tools.map do |t| + Base.new(:tool, t.to_s) + end + assert_gestalts path, gestalts + end + + def assert_platform(path, *platforms) + gestalts = platforms.map do |p| + Base.new(:platform, p.to_s) + end + assert_gestalts path, gestalts + end + + def assert_gestalts(path, expected_gestalts) + sfl = SourceFileList.new(:paths => [test_dir(path)]) + assert sfl.size > 0 + sfl.analyze(:gestalt) + assert_equal expected_gestalts.sort, sfl.gestalts.sort + end + + def test_dir(d) + File.expand_path(File.dirname(__FILE__) + "/../../gestalt_files/#{ d }") + end +end + diff --git a/test/unit/sourcefile_test.h b/test/unit/sourcefile_test.h index ea54dd0..b9c4ccc 100644 --- a/test/unit/sourcefile_test.h +++ b/test/unit/sourcefile_test.h @@ -182,6 +182,7 @@ void test_sourcefile_list_language_facts() { SourceFileList *sfl = ohcount_sourcefile_list_new(); ohcount_sourcefile_list_add_directory(sfl, "../gestalt_files/win32_enough/"); LocList *list = ohcount_sourcefile_list_analyze_languages(sfl); + printf("%d", ohcount_loc_list_filecount(list)); assert(ohcount_loc_list_filecount(list) == 2); Loc *loc = ohcount_loc_list_get_loc(list, "c"); assert(loc->code == 2); -- 2.32.0.93.g670b81a890