aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/language
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2019-09-29 18:01:32 +0200
committerBenoit Daloze <eregontp@gmail.com>2019-09-29 18:01:32 +0200
commita17bc04d159ec9839cc8cfb02dc0cdd2802110f4 (patch)
treea10d4121aeb1517c198ab5b1577bca93912dc1f9 /spec/ruby/language
parentf9a9f3c7c6cf3fe534d4934dd3b502d721151b81 (diff)
downloadruby-a17bc04d159ec9839cc8cfb02dc0cdd2802110f4.tar.gz
Update to ruby/spec@e69a14c
Diffstat (limited to 'spec/ruby/language')
-rw-r--r--spec/ruby/language/optional_assignments_spec.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/spec/ruby/language/optional_assignments_spec.rb b/spec/ruby/language/optional_assignments_spec.rb
index 04abc2496b..3d9e0dbb65 100644
--- a/spec/ruby/language/optional_assignments_spec.rb
+++ b/spec/ruby/language/optional_assignments_spec.rb
@@ -42,6 +42,18 @@ describe 'Optional variable assignments' do
a.should == 10
end
+
+ it 'returns the new value if set to false' do
+ a = false
+
+ (a ||= 20).should == 20
+ end
+
+ it 'returns the original value if truthy' do
+ a = 10
+
+ (a ||= 20).should == 10
+ end
end
describe 'using a accessor' do
@@ -89,6 +101,49 @@ describe 'Optional variable assignments' do
@a.b.should == 10
end
+
+ it 'returns the new value if set to false' do
+ def @a.b=(x)
+ :v
+ end
+
+ @a.b = false
+ (@a.b ||= 20).should == 20
+ end
+
+ it 'returns the original value if truthy' do
+ def @a.b=(x)
+ @b = x
+ :v
+ end
+
+ @a.b = 10
+ (@a.b ||= 20).should == 10
+ end
+
+ it 'works when writer is private' do
+ klass = Class.new do
+ def t
+ self.b = false
+ (self.b ||= 10).should == 10
+ (self.b ||= 20).should == 10
+ end
+
+ def b
+ @b
+ end
+
+ def b=(x)
+ @b = x
+ :v
+ end
+
+ private :b=
+ end
+
+ klass.new.t
+ end
+
end
end