Class: AnySpec::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/any-spec/test-case.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, target_executable) ⇒ TestCase

Returns a new instance of TestCase.

Raises:

  • (Exception)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/any-spec/test-case.rb', line 14

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

Instance Attribute Details

#assertion_codeObject

Returns the value of attribute assertion_code.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def assertion_code
  @assertion_code
end

#assertionsObject

Returns the value of attribute assertions.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def assertions
  @assertions
end

#exit_statusObject

Returns the value of attribute exit_status.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def exit_status
  @exit_status
end

#last_assertion_resultObject

Returns the value of attribute last_assertion_result.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def last_assertion_result
  @last_assertion_result
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def message
  @message
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def path
  @path
end

#target_executableObject

Returns the value of attribute target_executable.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def target_executable
  @target_executable
end

#test_codeObject

Returns the value of attribute test_code.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def test_code
  @test_code
end

#test_outputObject

Returns the value of attribute test_output.



4
5
6
# File 'lib/any-spec/test-case.rb', line 4

def test_output
  @test_output
end

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/any-spec/test-case.rb', line 26

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