Class: DTest::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/dtest/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(let = nil) ⇒ Context

Returns a new instance of Context.



37
38
39
40
# File 'lib/dtest/core.rb', line 37

def initialize(let = nil)
  # テスト記述側(ブロック)から__stateが参照されないのが前提
  @__state = {:let => let}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

:let value getter



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dtest/core.rb', line 70

def method_missing(name, *args, &block)
  if @__state[:let] && @__state[:let].public_methods.map(&:to_sym).include?(name)
    # getter value
    @__state[:let].send(name)
  else
    file, line, error_line = DTest::failure_line(caller(1).first)
    e = NameError.new("undefined local variable or method `#{name}'")
    e.set_backtrace(["#{file}:#{line}", error_line])
    raise e
  end
end

Instance Method Details

#abort_case_if(condition, message = nil) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/dtest/core.rb', line 167

def abort_case_if(condition, message = nil)
  if condition
    str = "Abort TestCase"
    str += ": #{message}\n" if message
    failed(str)
    raise AbortTestCase.new(str)
  end
end

#abort_global_if(condition, message = nil) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/dtest/core.rb', line 176

def abort_global_if(condition, message = nil)
  if condition
    str = "Abort global"
    str += ": #{message}\n" if message
    failed(str)
    raise AbortGlobal.new(str)
  end
end

#abort_if(condition, message = nil) ⇒ Object

abort



158
159
160
161
162
163
164
165
# File 'lib/dtest/core.rb', line 158

def abort_if(condition, message = nil)
  if condition
    str = "Abort"
    str += ": #{message}\n" if message
    failed(str)
    raise AbortTest.new(str)
  end
end

#assert_equal(expected, actual, message = nil) ⇒ Object



124
125
126
127
128
129
# File 'lib/dtest/core.rb', line 124

def assert_equal(expected, actual, message = nil)
  unless expected == actual
    failed_equal(expected, actual, message)
    abort_assert
  end
end

#assert_error(*errors, &block) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dtest/core.rb', line 138

def assert_error(*errors, &block)
  begin
    block.call
  rescue *errors => actual_error
    raised_expected_error = true
  rescue RuntimeError, Exception => actual_error
    raised_expected_error = false
  else
    str = "exception expected but none was thrown\n"
    failed(str)
    return
  end

  unless raised_expected_error
    str = "exception expected #{errors.join(', ')} but #{actual_error.inspect}\n"
    failed(str)
  end
end

#assert_failure?Boolean

abort type when assertion failed

Returns:

  • (Boolean)


88
89
90
# File 'lib/dtest/core.rb', line 88

def assert_failure?
  @__state[:option][:assert_abort]
end

#assert_false(condition, message = nil) ⇒ Object



117
118
119
120
121
122
# File 'lib/dtest/core.rb', line 117

def assert_false(condition, message = nil)
  if condition
    failed_false(message)
    abort_assert
  end
end

#assert_not_equal(expected, actual, message = nil) ⇒ Object



131
132
133
134
135
136
# File 'lib/dtest/core.rb', line 131

def assert_not_equal(expected, actual, message = nil)
  if expected == actual
    failed_equal(expected, actual, message)
    abort_assert
  end
end

#assert_true(condition, message = nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/dtest/core.rb', line 110

def assert_true(condition, message = nil)
  unless condition
    failed_true(message)
    abort_assert
  end
end

#call(state, block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dtest/core.rb', line 42

def call(state, block)
  @__state = @__state.merge(state)
  begin
    instance_eval(&block)
  rescue AbortTest, AbortTestCase, AbortGlobal => e
    # スルー
    raise e
  rescue StandardError, Exception => e
    # ブロック内の例外はabortとして処理する 
    catch_exception(e)
    abort_assert
  end
end

#expect_equal(expected, actual, message = nil) ⇒ Object



102
103
104
# File 'lib/dtest/core.rb', line 102

def expect_equal(expected, actual, message = nil)
  failed_equal(expected, actual, message) unless expected == actual
end

#expect_false(condition, message = nil) ⇒ Object



98
99
100
# File 'lib/dtest/core.rb', line 98

def expect_false(condition, message = nil)
  failed_false(message) if condition
end

#expect_not_equal(expected, actual, message = nil) ⇒ Object



106
107
108
# File 'lib/dtest/core.rb', line 106

def expect_not_equal(expected, actual, message = nil)
  failed_equal(expected, actual, message) if expected == actual
end

#expect_true(condition, message = nil) ⇒ Object

expect/assert



94
95
96
# File 'lib/dtest/core.rb', line 94

def expect_true(condition, message = nil)
  failed_true(message) unless condition
end

#paramObject

value-parameterized test parameters



83
84
85
# File 'lib/dtest/core.rb', line 83

def param
  @__state[:parameter]
end

#set(name, val) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dtest/core.rb', line 56

def set(name, val)
  return if @__state[:let] == nil

  # set variable
  @__state[:let].instance_variable_set("@#{name}", val)
  # define getter method
  @__state[:let].instance_eval <<-EOS
    def #{name}
      @#{name}
    end
  EOS
end