Skip to content

Commit a6594a9

Browse files
author
Nicolas Laurent
committed
move or-assign specs to optional_assignments_spec.rb
1 parent 7a72f40 commit a6594a9

File tree

2 files changed

+90
-90
lines changed

2 files changed

+90
-90
lines changed

spec/ruby/language/constants_spec.rb

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -381,52 +381,6 @@ class << self
381381
ConstantSpecs::ClassA.constx.should == :CS_CONSTX
382382
ConstantSpecs::ClassA.new.constx.should == :CS_CONSTX
383383
end
384-
385-
describe "with ||=" do
386-
it "assigns a scoped constant if previously undefined" do
387-
ConstantSpecs.should_not have_constant(:OpAssignUndefined)
388-
module ConstantSpecs
389-
OpAssignUndefined ||= 42
390-
end
391-
ConstantSpecs::OpAssignUndefined.should == 42
392-
ConstantSpecs::OpAssignUndefinedOutside ||= 42
393-
ConstantSpecs::OpAssignUndefinedOutside.should == 42
394-
ConstantSpecs.send(:remove_const, :OpAssignUndefined)
395-
ConstantSpecs.send(:remove_const, :OpAssignUndefinedOutside)
396-
end
397-
398-
it "assigns a global constant if previously undefined" do
399-
OpAssignGlobalUndefined ||= 42
400-
::OpAssignGlobalUndefinedExplicitScope ||= 42
401-
OpAssignGlobalUndefined.should == 42
402-
::OpAssignGlobalUndefinedExplicitScope.should == 42
403-
Object.send :remove_const, :OpAssignGlobalUndefined
404-
Object.send :remove_const, :OpAssignGlobalUndefinedExplicitScope
405-
end
406-
407-
end
408-
409-
describe "with &&=" do
410-
it "re-assigns a scoped constant if already true" do
411-
module ConstantSpecs
412-
OpAssignTrue = true
413-
end
414-
suppress_warning do
415-
ConstantSpecs::OpAssignTrue &&= 1
416-
end
417-
ConstantSpecs::OpAssignTrue.should == 1
418-
ConstantSpecs.send :remove_const, :OpAssignTrue
419-
end
420-
421-
it "leaves scoped constant if not true" do
422-
module ConstantSpecs
423-
OpAssignFalse = false
424-
end
425-
ConstantSpecs::OpAssignFalse &&= 1
426-
ConstantSpecs::OpAssignFalse.should == false
427-
ConstantSpecs.send :remove_const, :OpAssignFalse
428-
end
429-
end
430384
end
431385

432386
describe "Constant resolution within a singleton class (class << obj)" do

spec/ruby/language/optional_assignments_spec.rb

Lines changed: 90 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../spec_helper'
2+
require_relative '../fixtures/constants'
23

34
describe 'Optional variable assignments' do
45
describe 'using ||=' do
@@ -353,60 +354,105 @@ def []=(k, v)
353354
end
354355

355356
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
360369

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
366377
end
367-
end
368378

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
375383

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
383390
end
384-
end
385391

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
389398

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
393408

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
398412

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)
403416

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
407421

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
411457
end
412458
end

0 commit comments

Comments
 (0)