|
1 | 1 | require_relative '../spec_helper'
|
| 2 | +require_relative '../fixtures/constants' |
2 | 3 |
|
3 | 4 | describe 'Optional variable assignments' do
|
4 | 5 | describe 'using ||=' do
|
@@ -353,60 +354,105 @@ def []=(k, v)
|
353 | 354 | end
|
354 | 355 |
|
355 | 356 | describe 'Optional constant assignment' do
|
356 |
| - it 'correctly defines non-existing constants' do |
357 |
| - ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT1 ||= :assigned |
358 |
| - ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT1.should == :assigned |
359 |
| - end |
| 357 | + describe 'with ||=' do |
| 358 | + it "assigns a scoped constant if previously undefined" do |
| 359 | + ConstantSpecs.should_not have_constant(:OpAssignUndefined) |
| 360 | + module ConstantSpecs |
| 361 | + OpAssignUndefined ||= 42 |
| 362 | + end |
| 363 | + ConstantSpecs::OpAssignUndefined.should == 42 |
| 364 | + ConstantSpecs::OpAssignUndefinedOutside ||= 42 |
| 365 | + ConstantSpecs::OpAssignUndefinedOutside.should == 42 |
| 366 | + ConstantSpecs.send(:remove_const, :OpAssignUndefined) |
| 367 | + ConstantSpecs.send(:remove_const, :OpAssignUndefinedOutside) |
| 368 | + end |
360 | 369 |
|
361 |
| - it 'correctly overwrites nil constants' do |
362 |
| - suppress_warning do # already initialized constant |
363 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT1 = nil |
364 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT1 ||= :assigned |
365 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT1.should == :assigned |
| 370 | + it "assigns a global constant if previously undefined" do |
| 371 | + OpAssignGlobalUndefined ||= 42 |
| 372 | + ::OpAssignGlobalUndefinedExplicitScope ||= 42 |
| 373 | + OpAssignGlobalUndefined.should == 42 |
| 374 | + ::OpAssignGlobalUndefinedExplicitScope.should == 42 |
| 375 | + Object.send :remove_const, :OpAssignGlobalUndefined |
| 376 | + Object.send :remove_const, :OpAssignGlobalUndefinedExplicitScope |
366 | 377 | end
|
367 |
| - end |
368 | 378 |
|
369 |
| - it 'causes side-effects of the module part to be applied only once (for undefined constant)' do |
370 |
| - x = 0 |
371 |
| - (x += 1; ConstantSpecs::ClassA)::OR_ASSIGNED_CONSTANT2 ||= :assigned |
372 |
| - x.should == 1 |
373 |
| - ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT2.should == :assigned |
374 |
| - end |
| 379 | + it 'correctly defines non-existing constants' do |
| 380 | + ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT1 ||= :assigned |
| 381 | + ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT1.should == :assigned |
| 382 | + end |
375 | 383 |
|
376 |
| - it 'causes side-effects of the module part to be applied (for nil constant)' do |
377 |
| - suppress_warning do # already initialized constant |
378 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT2 = nil |
379 |
| - x = 0 |
380 |
| - (x += 1; ConstantSpecs::ClassA)::NIL_OR_ASSIGNED_CONSTANT2 ||= :assigned |
381 |
| - x.should == 1 |
382 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT2.should == :assigned |
| 384 | + it 'correctly overwrites nil constants' do |
| 385 | + suppress_warning do # already initialized constant |
| 386 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT1 = nil |
| 387 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT1 ||= :assigned |
| 388 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT1.should == :assigned |
| 389 | + end |
383 | 390 | end
|
384 |
| - end |
385 | 391 |
|
386 |
| - it 'does not evaluate the right-hand side if the module part raises an exception (for undefined constant)' do |
387 |
| - x = 0 |
388 |
| - y = 0 |
| 392 | + it 'causes side-effects of the module part to be applied only once (for undefined constant)' do |
| 393 | + x = 0 |
| 394 | + (x += 1; ConstantSpecs::ClassA)::OR_ASSIGNED_CONSTANT2 ||= :assigned |
| 395 | + x.should == 1 |
| 396 | + ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT2.should == :assigned |
| 397 | + end |
389 | 398 |
|
390 |
| - -> { |
391 |
| - (x += 1; raise Exception; ConstantSpecs::ClassA)::OR_ASSIGNED_CONSTANT3 ||= (y += 1; :assigned) |
392 |
| - }.should raise_error(Exception) |
| 399 | + it 'causes side-effects of the module part to be applied (for nil constant)' do |
| 400 | + suppress_warning do # already initialized constant |
| 401 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT2 = nil |
| 402 | + x = 0 |
| 403 | + (x += 1; ConstantSpecs::ClassA)::NIL_OR_ASSIGNED_CONSTANT2 ||= :assigned |
| 404 | + x.should == 1 |
| 405 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT2.should == :assigned |
| 406 | + end |
| 407 | + end |
393 | 408 |
|
394 |
| - x.should == 1 |
395 |
| - y.should == 0 |
396 |
| - defined?(ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT3).should == nil |
397 |
| - end |
| 409 | + it 'does not evaluate the right-hand side if the module part raises an exception (for undefined constant)' do |
| 410 | + x = 0 |
| 411 | + y = 0 |
398 | 412 |
|
399 |
| - it 'does not evaluate the right-hand side if the module part raises an exception (for nil constant)' do |
400 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT3 = nil |
401 |
| - x = 0 |
402 |
| - y = 0 |
| 413 | + -> { |
| 414 | + (x += 1; raise Exception; ConstantSpecs::ClassA)::OR_ASSIGNED_CONSTANT3 ||= (y += 1; :assigned) |
| 415 | + }.should raise_error(Exception) |
403 | 416 |
|
404 |
| - -> { |
405 |
| - (x += 1; raise Exception; ConstantSpecs::ClassA)::NIL_OR_ASSIGNED_CONSTANT3 ||= (y += 1; :assigned) |
406 |
| - }.should raise_error(Exception) |
| 417 | + x.should == 1 |
| 418 | + y.should == 0 |
| 419 | + defined?(ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT3).should == nil |
| 420 | + end |
407 | 421 |
|
408 |
| - x.should == 1 |
409 |
| - y.should == 0 |
410 |
| - ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT3.should == nil |
| 422 | + it 'does not evaluate the right-hand side if the module part raises an exception (for nil constant)' do |
| 423 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT3 = nil |
| 424 | + x = 0 |
| 425 | + y = 0 |
| 426 | + |
| 427 | + -> { |
| 428 | + (x += 1; raise Exception; ConstantSpecs::ClassA)::NIL_OR_ASSIGNED_CONSTANT3 ||= (y += 1; :assigned) |
| 429 | + }.should raise_error(Exception) |
| 430 | + |
| 431 | + x.should == 1 |
| 432 | + y.should == 0 |
| 433 | + ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT3.should == nil |
| 434 | + end |
| 435 | + end |
| 436 | + |
| 437 | + describe "with &&=" do |
| 438 | + it "re-assigns a scoped constant if already true" do |
| 439 | + module ConstantSpecs |
| 440 | + OpAssignTrue = true |
| 441 | + end |
| 442 | + suppress_warning do |
| 443 | + ConstantSpecs::OpAssignTrue &&= 1 |
| 444 | + end |
| 445 | + ConstantSpecs::OpAssignTrue.should == 1 |
| 446 | + ConstantSpecs.send :remove_const, :OpAssignTrue |
| 447 | + end |
| 448 | + |
| 449 | + it "leaves scoped constant if not true" do |
| 450 | + module ConstantSpecs |
| 451 | + OpAssignFalse = false |
| 452 | + end |
| 453 | + ConstantSpecs::OpAssignFalse &&= 1 |
| 454 | + ConstantSpecs::OpAssignFalse.should == false |
| 455 | + ConstantSpecs.send :remove_const, :OpAssignFalse |
| 456 | + end |
411 | 457 | end
|
412 | 458 | end
|
0 commit comments