diff options
Diffstat (limited to 'spec/ruby/language')
-rw-r--r-- | spec/ruby/language/array_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/language/constants_spec.rb | 19 | ||||
-rw-r--r-- | spec/ruby/language/fixtures/array.rb | 21 | ||||
-rw-r--r-- | spec/ruby/language/optional_assignments_spec.rb | 22 |
4 files changed, 60 insertions, 9 deletions
diff --git a/spec/ruby/language/array_spec.rb b/spec/ruby/language/array_spec.rb index 2198d7a28b..2583cffbf7 100644 --- a/spec/ruby/language/array_spec.rb +++ b/spec/ruby/language/array_spec.rb @@ -36,6 +36,13 @@ describe "Array literals" do [1, *nil, 3].should == [1, 3] [*nil, *nil, *nil].should == [] end + + it "evaluates each argument exactly once" do + se = ArraySpec::SideEffect.new + se.array_result(true) + se.array_result(false) + se.call_count.should == 4 + end end describe "Bareword array literal" do diff --git a/spec/ruby/language/constants_spec.rb b/spec/ruby/language/constants_spec.rb index 49ff8844af..5b111f4e81 100644 --- a/spec/ruby/language/constants_spec.rb +++ b/spec/ruby/language/constants_spec.rb @@ -458,16 +458,17 @@ describe "Module#private_constant marked constants" do lambda {mod::Foo}.should raise_error(NameError) end - it "sends #const_missing to the original class or module" do - mod = Module.new - mod.const_set :Foo, true - mod.send :private_constant, :Foo - def mod.const_missing(name) - @const_missing_arg = name - name == :Foo ? name : super - end + ruby_version_is "2.6" do + it "sends #const_missing to the original class or module" do + mod = Module.new + mod.const_set :Foo, true + mod.send :private_constant, :Foo + def mod.const_missing(name) + name == :Foo ? name : super + end - mod::Foo.should == :Foo + mod::Foo.should == :Foo + end end describe "in a module" do diff --git a/spec/ruby/language/fixtures/array.rb b/spec/ruby/language/fixtures/array.rb index 4d8ce74ed6..c1036575ff 100644 --- a/spec/ruby/language/fixtures/array.rb +++ b/spec/ruby/language/fixtures/array.rb @@ -8,4 +8,25 @@ module ArraySpec [a, b, c, d] end end + + class SideEffect + def initialize() + @call_count = 0 + end + + attr_reader :call_count + + def array_result(a_number) + [result(a_number), result(a_number)] + end + + def result(a_number) + @call_count += 1 + if a_number + 1 + else + :thing + end + end + end end diff --git a/spec/ruby/language/optional_assignments_spec.rb b/spec/ruby/language/optional_assignments_spec.rb index a83c0da272..91b580e084 100644 --- a/spec/ruby/language/optional_assignments_spec.rb +++ b/spec/ruby/language/optional_assignments_spec.rb @@ -185,6 +185,19 @@ describe 'Optional variable assignments' do describe 'using a #[]' do before do @a = {} + klass = Class.new do + def [](k) + @hash ||= {} + @hash[k] + end + + def []=(k, v) + @hash ||= {} + @hash[k] = v + 7 + end + end + @b = klass.new end it 'leaves new variable unassigned' do @@ -226,6 +239,15 @@ describe 'Optional variable assignments' do @a[:k].should == 20 end + + it 'returns the assigned value, not the result of the []= method with ||=' do + (@b[:k] ||= 12).should == 12 + end + + it 'returns the assigned value, not the result of the []= method with +=' do + @b[:k] = 17 + (@b[:k] += 12).should == 29 + end end end |