1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
require 'test_helper'
class ShouldTest < PARENT_TEST_CASE
should "be able to define a should statement outside of a context" do
assert true
end
should "see the name of my class as ShouldTest" do
assert_equal "ShouldTest", self.class.name
end
def self.should_see_class_methods
should "be able to see class methods" do
assert true
end
end
def self.should_be_able_to_setup_a_should_eventually_in_a_class_method
should "be able to setup a should eventually in a class method"
end
def self.should_see_a_context_block_like_a_test_case_class
should "see a context block as a Test::Unit class" do
assert_equal "ShouldTest", self.class.name
end
end
def self.should_see_blah
should "see @blah through a macro" do
assert @blah
end
end
def self.should_not_see_blah
should "not see @blah through a macro" do
assert !instance_variable_defined?(:@blah)
end
end
def self.should_be_able_to_make_context_macros(prefix = nil)
context "a macro" do
should "have the tests named correctly" do
assert_match(
Regexp.new(
"^" +
build_expected_test_name(
"#{prefix}a macro should have the tests named correctly"
)
),
test_name
)
end
end
end
context "Context" do
should_see_class_methods
should_see_a_context_block_like_a_test_case_class
should_be_able_to_make_context_macros("Context ")
should_be_able_to_setup_a_should_eventually_in_a_class_method
should "not define @blah" do
assert ! self.instance_variables.include?("@blah")
end
should_not_see_blah
should "be able to define a should statement" do
assert true
end
should "see the name of my class as ShouldTest" do
assert_equal "ShouldTest", self.class.name
end
context "with a subcontext" do
should_be_able_to_make_context_macros("Context with a subcontext ")
end
end
context "Context with setup block" do
setup do
@blah = "blah"
end
should "have @blah == 'blah'" do
assert_equal "blah", @blah
end
should_see_blah
should "have name set right" do
assert_match(
Regexp.new(
"^" +
build_expected_test_name("Context with setup block")
),
test_name
)
end
context "and a subcontext" do
setup do
@blah = "#{@blah} twice"
end
should "be named correctly" do
assert_match(
Regexp.new(
"^" +
build_expected_test_name(
"Context with setup block and a subcontext should be named correctly"
)
),
test_name
)
end
should "run the setup methods in order" do
assert_equal @blah, "blah twice"
end
should_see_blah
end
end
context "Another context with setup block" do
setup do
@blah = "foo"
end
should "have @blah == 'foo'" do
assert_equal "foo", @blah
end
should "have name set right" do
assert_match(
Regexp.new(
"^" +
build_expected_test_name("Another context with setup block")
),
test_name
)
end
should_see_blah
end
should_eventually "pass, since it's a should_eventually" do
flunk "what?"
end
# Context creation and naming
def test_should_create_a_new_context
assert_nothing_raised do
Shoulda::Context::Context.new("context name", self.class) do; end
end
end
def test_should_create_a_new_context_even_if_block_is_omitted
old_verbose, $VERBOSE = $VERBOSE, nil
assert_nothing_raised do
Shoulda::Context::Context.new("context without a block", self.class)
end
ensure
$VERBOSE = old_verbose
end
def test_should_create_a_nested_context
assert_nothing_raised do
parent = Shoulda::Context::Context.new("Parent", self.class) do; end
child = Shoulda::Context::Context.new("Child", parent) do; end
raise unless child.instance_of? Shoulda::Context::Context
end
end
def test_should_name_a_contexts_correctly
parent = Shoulda::Context::Context.new("Parent", self.class) do; end
child = Shoulda::Context::Context.new("Child", parent) do; end
grandchild = Shoulda::Context::Context.new("GrandChild", child) do; end
assert_equal "Parent", parent.full_name
assert_equal "Parent Child", child.full_name
assert_equal "Parent Child GrandChild", grandchild.full_name
end
def test_should_raise_on_duplicate_naming
context = Shoulda::Context::Context.new("DupContext", self.class) do
should "dup" do; end
should "dup" do; end
end
assert_raises Shoulda::Context::DuplicateTestError do
context.build
end
end
# Should statements
def test_should_have_should_hashes_when_given_should_statements
context = Shoulda::Context::Context.new("name", self.class) do
should "be good" do; end
should "another" do; end
end
names = context.shoulds.map {|s| s[:name]}
assert_equal ["another", "be good"], names.sort
end
# setup and teardown
def test_should_capture_setup_and_teardown_blocks
context = Shoulda::Context::Context.new("name", self.class) do
setup do; "setup"; end
teardown do; "teardown"; end
end
assert_equal "setup", context.setup_blocks.first.call
assert_equal "teardown", context.teardown_blocks.first.call
end
# building
def test_should_create_shoulda_test_for_each_should_on_build
context = Shoulda::Context::Context.new("name", self.class) do
should "one" do; end
should "two" do; end
end
context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
context.build
end
def test_should_create_test_methods_on_build
tu_class = self.class
context = Shoulda::Context::Context.new("A Context", tu_class) do
should "define the test" do; end
end
tu_class.
expects(:define_method).
with(
build_expected_test_name("A Context should define the test. ").to_sym
)
context.build
end
def test_should_create_test_methods_on_build_when_subcontext
tu_class = self.class
context = Shoulda::Context::Context.new("A Context", tu_class) do
context "with a child" do
should "define the test" do; end
end
end
tu_class.
expects(:define_method).
with(
build_expected_test_name(
"A Context with a child should define the test. "
).to_sym
)
context.build
end
# Test::Unit integration
def test_should_create_a_new_context_and_build_it_on_test_case_context
c = mock("context")
c.expects(:build)
Shoulda::Context::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
self.class.context "foo" do; end
end
def test_should_create_a_one_off_context_and_build_it_on_test_case_should
s = mock("test")
Shoulda::Context::Context.any_instance.expects(:should).with("rock", {}).returns(s)
Shoulda::Context::Context.any_instance.expects(:build)
self.class.should "rock" do; end
end
def test_should_create_a_one_off_context_and_build_it_on_test_case_should_eventually
s = mock("test")
Shoulda::Context::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
Shoulda::Context::Context.any_instance.expects(:build)
self.class.should_eventually "rock" do; end
end
should "run a :before proc", :before => lambda { @value = "before" } do
assert_equal "before", @value
end
context "A :before proc" do
setup do
assert_equal "before", @value
@value = "setup"
end
should "run before the current setup", :before => lambda { @value = "before" } do
assert_equal "setup", @value
end
end
context "a before statement" do
setup do
assert_equal "before", @value
@value = "setup"
end
before_should "run before the current setup" do
@value = "before"
end
end
context "A context" do
setup do
@value = "outer"
end
context "with a subcontext and a :before proc" do
before = lambda do
assert "outer", @value
@value = "before"
end
should "run after the parent setup", :before => before do
assert_equal "before", @value
end
end
end
def test_name
name
end
def build_expected_test_name(value)
if TEST_FRAMEWORK == "minitest"
if value.is_a?(Regexp)
Regexp.new("^test_: #{value.source}")
else
"test_: #{value}"
end
elsif value.is_a?(Regexp)
Regexp.new("^test: #{value.source}")
else
"test: #{value}"
end
end
# Minitest removed assert_nothing_raised a while back;
# see here: <http://www.zenspider.com/ruby/2012/01/assert_nothing_tested.html>
def assert_nothing_raised
yield
end
end
class RedTestarossaDriver; end
class RedTestarossaDriverTest < PARENT_TEST_CASE
class DummyMatcher
def description
"fail to construct the proper test name with a 'should_not'"
end
def matches?(*)
false
end
def failure_message_when_negated
"dummy failure message"
end
end
should "call Shoulda::Context::Context.new using the correct context name" do
assert_equal "RedTestarossaDriver", @shoulda_context.name
end
should "see the name of the test case class as RedTestarossaDriverTest" do
assert_equal "RedTestarossaDriverTest", self.class.name
end
should "include the correct context name in the full name of the test" do
assert_match(
build_expected_test_name(/RedTestarossaDriver/),
test_name
)
end
def test_should_property_construct_test_name_for_should_eventually
context = Shoulda::Context::Context.new("whatever", self.class) do
"this is just a placeholder"
end
Shoulda::Context::Context.
expects(:new).
with("RedTestarossaDriver", RedTestarossaDriverTest).
returns(context)
self.class.should_eventually("do something") {}
end
def test_should_property_construct_test_name_for_should_not
context = Shoulda::Context::Context.new("whatever", self.class) do
"this is just a placeholder"
end
Shoulda::Context::Context.
expects(:new).
with("RedTestarossaDriver", RedTestarossaDriverTest).
returns(context)
self.class.should_not(DummyMatcher.new)
end
private
def test_name
name
end
def build_expected_test_name(value)
if TEST_FRAMEWORK == "minitest"
if value.is_a?(Regexp)
Regexp.new("^test_: #{value.source}")
else
"test_: #{value}"
end
elsif value.is_a?(Regexp)
Regexp.new("^test: #{value.source}")
else
"test: #{value}"
end
end
end
|