Sha256: 380a0dc868f395f24ab3deaeaa9bbe88de9bc517d828ec7e7605f5e20224d686

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

#
# File::      ExternalProgramTestCase.rb
# Author::    wkm
# Copyright:: 2009
# License::   GPl
#
# Lightweight utility that will effectively scrap
# the entire test case if the program being tested doesn't exist.
#

require 'test/unit'
require 'term/ansicolor'

module SiteFuel
  module External

    include Term::ANSIColor

    class Test::Unit::TestCase
      # exposes the private #define_method function to the world
      def self.publicly_define_method(name, &block)
        define_method(name, &block)
      end
    end

    module ExternalProgramTestCase 
      
      @@message_posted = {}

      def get_tested_class
        name = self.class.to_s.gsub(/^(.*?::)?Test(.*)$/, "\\2")
        
        # ensure the class exists
        cls = Kernel.const_get(name)
        
        return cls if cls != nil
        return nil
      end
      
      def initialize(*args)
        cls = get_tested_class
        unless cls == nil
          if cls.program_found?
            # amusing pun.
            super(*args)
          else
            if not @@message_posted[self.class]
              puts "Ignoring #{cls} unit tests. Program #{cls.program_name} not found.".bold
              @@message_posted[self.class] = true
            end
            
            # fun part. Over-ride every method beginning with test* so they are nops
            methods = self.methods
            methods.each do |name|
              if name =~ /^test.*$/
                self.class.publicly_define_method(name) {}
              end
            end

            super(*args)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitefuel-0.0.0b lib/sitefuel/external/ExternalProgramTestCase.rb
sitefuel-0.0.0a lib/sitefuel/external/ExternalProgramTestCase.rb