Sha256: cceef8f1f6a9c380d286028f913131fe2e17be03001431c747a616f85742bf64

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

module AnySpec
  class TestCase
  
    attr_accessor :test_code,
                  :assertion_code,
                  :target_executable,
                  :path,
                  :last_assertion_result,
                  :message,
                  :test_output,
                  :exit_status,
                  :assertions     
  
    def initialize(path, target_executable)
      raise Exception, "\n\nNo test case exists at path: #{path}" unless(File.exist?(path))
      raise Exception, "\n\nTest case is empty: #{path}" if(File.size(path) == 0)
      raw_test = File.open(path).read
      test_parts = raw_test.split("----")
      raise Exception, "\n\nTest case formatted incorrectly: #{path}" unless(test_parts.length == 2)
      @test_code = test_parts[0].strip
      @assertion_code = test_parts[1].strip
      @path = path
      @target_executable = target_executable
    end
    
    def run
      temporary_filename = File.join( File.split(@path)[0], "#{Time.now.to_i}-any-spec" + File.extname(@path) )
      tmp = File.open(temporary_filename, "w")
      tmp.write(@test_code)
      tmp.close
      @test_output = `#{@target_executable} #{temporary_filename} 2>&1`
      @exit_status = $?.exitstatus
      @last_assertion_result = true
      @message = ""
      @assertions = 0
      unless(@assertion_code.include?("assert_execution_success") || @assertion_code.include?("assert_execution_failure"))
        @assertion_code = "\nassert_execution_success\n" + @assertion_code
      end
      begin
        AnySpec::Assertions.new(self).instance_eval(@assertion_code, @path)
      rescue Exception => e
        @message = "Error in assertion code:\n"
        @message += e.message
        @message += "\n" + e.backtrace.join("\n")
        @last_assertion_result = false
      end
      return @last_assertion_result
    ensure
      File.delete(temporary_filename)
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
any-spec-0.1.0 lib/any-spec/test-case.rb
any-spec-0.0.1 lib/any-spec/test-case.rb
any-spec-0.0.0 lib/any-spec/test-case.rb