diff options
author | Jean Boussier <byroot@ruby-lang.org> | 2023-07-13 11:49:28 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2023-07-24 14:51:20 +0200 |
commit | 43a5c191358699fe8b19314763998cb8ca77ed90 (patch) | |
tree | efebe1570ca2c77a66bfb81edeb3f86044e63174 /spec | |
parent | 14d16bdb1ad8e98d76ec2c43b2c1c412ff707d0b (diff) | |
download | ruby-43a5c191358699fe8b19314763998cb8ca77ed90.tar.gz |
Use the caller location as default filename for eval family of methods
[Feature #19755]
Before (in /tmp/test.rb):
```ruby
Object.class_eval("p __FILE__") # => "(eval)"
```
After:
```ruby
Object.class_eval("p __FILE__") # => "(eval at /tmp/test.rb:1)"
```
This makes it much easier to track down generated code in case
the author forgot to provide a filename argument.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/ruby/core/basicobject/instance_eval_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/binding/eval_spec.rb | 16 | ||||
-rw-r--r-- | spec/ruby/core/kernel/eval_spec.rb | 39 | ||||
-rw-r--r-- | spec/ruby/core/module/shared/class_eval.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/tracepoint/path_spec.rb | 31 | ||||
-rw-r--r-- | spec/ruby/language/defined_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/language/file_spec.rb | 12 |
7 files changed, 87 insertions, 26 deletions
diff --git a/spec/ruby/core/basicobject/instance_eval_spec.rb b/spec/ruby/core/basicobject/instance_eval_spec.rb index 699c965171..1f3a43f341 100644 --- a/spec/ruby/core/basicobject/instance_eval_spec.rb +++ b/spec/ruby/core/basicobject/instance_eval_spec.rb @@ -84,6 +84,13 @@ describe "BasicObject#instance_eval" do end + ruby_version_is "3.3" do + it "uses the caller location as default location" do + f = Object.new + f.instance_eval("[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1] + end + end + it "has access to receiver's instance variables" do BasicObjectSpecs::IVars.new.instance_eval { @secret }.should == 99 BasicObjectSpecs::IVars.new.instance_eval("@secret").should == 99 diff --git a/spec/ruby/core/binding/eval_spec.rb b/spec/ruby/core/binding/eval_spec.rb index 1b3b670b5e..bb2036f739 100644 --- a/spec/ruby/core/binding/eval_spec.rb +++ b/spec/ruby/core/binding/eval_spec.rb @@ -60,10 +60,12 @@ describe "Binding#eval" do bind.eval("#foo\n__LINE__", "(test)", 88).should == 89 end - it "uses (eval) as __FILE__ if single argument given" do - obj = BindingSpecs::Demo.new(1) - bind = obj.get_binding - bind.eval("__FILE__").should == '(eval)' + ruby_version_is ""..."3.3" do + it "uses (eval) as __FILE__ if single argument given" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__FILE__").should == '(eval)' + end end it "uses 1 as __LINE__" do @@ -104,4 +106,10 @@ describe "Binding#eval" do bind.eval("'bar'.foo").should == "foo" end + + ruby_version_is "3.3" do + it "uses the caller location as default filename" do + binding.eval("[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1] + end + end end diff --git a/spec/ruby/core/kernel/eval_spec.rb b/spec/ruby/core/kernel/eval_spec.rb index e6761ab789..4acb87c729 100644 --- a/spec/ruby/core/kernel/eval_spec.rb +++ b/spec/ruby/core/kernel/eval_spec.rb @@ -159,20 +159,37 @@ describe "Kernel#eval" do end end - it "uses (eval) filename if none is provided" do - eval("__FILE__").should == "(eval)" - eval("__FILE__", binding).should == "(eval)" - eval("__FILE__", binding, "success").should == "success" - eval("eval '__FILE__', binding").should == "(eval)" - eval("eval '__FILE__', binding", binding).should == "(eval)" - eval("eval '__FILE__', binding", binding, 'success').should == '(eval)' - eval("eval '__FILE__', binding, 'success'", binding).should == 'success' - end + ruby_version_is ""..."3.3" do + it "uses (eval) filename if none is provided" do + eval("__FILE__").should == "(eval)" + eval("__FILE__", binding).should == "(eval)" + eval("__FILE__", binding, "success").should == "success" + eval("eval '__FILE__', binding").should == "(eval)" + eval("eval '__FILE__', binding", binding).should == "(eval)" + eval("eval '__FILE__', binding", binding, 'success').should == '(eval)' + eval("eval '__FILE__', binding, 'success'", binding).should == 'success' + end - it 'uses (eval) for __FILE__ and 1 for __LINE__ with a binding argument' do - eval("[__FILE__, __LINE__]", binding).should == ["(eval)", 1] + it 'uses (eval) for __FILE__ and 1 for __LINE__ with a binding argument' do + eval("[__FILE__, __LINE__]", binding).should == ["(eval)", 1] + end end + ruby_version_is "3.3" do + it "uses (eval at __FILE__:__LINE__) if none is provided" do + eval("__FILE__").should == "(eval at #{__FILE__}:#{__LINE__})" + eval("__FILE__", binding).should == "(eval at #{__FILE__}:#{__LINE__})" + eval("__FILE__", binding, "success").should == "success" + eval("eval '__FILE__', binding").should == "(eval at (eval at #{__FILE__}:#{__LINE__}):1)" + eval("eval '__FILE__', binding", binding).should == "(eval at (eval at #{__FILE__}:#{__LINE__}):1)" + eval("eval '__FILE__', binding", binding, 'success').should == "(eval at success:1)" + eval("eval '__FILE__', binding, 'success'", binding).should == 'success' + end + + it 'uses (eval at __FILE__:__LINE__) for __FILE__ and 1 for __LINE__ with a binding argument' do + eval("[__FILE__, __LINE__]", binding).should == ["(eval at #{__FILE__}:#{__LINE__})", 1] + end + end # Found via Rubinius bug github:#149 it "does not alter the value of __FILE__ in the binding" do first_time = EvalSpecs.call_eval diff --git a/spec/ruby/core/module/shared/class_eval.rb b/spec/ruby/core/module/shared/class_eval.rb index 9ef7b5be44..b1d5cb3814 100644 --- a/spec/ruby/core/module/shared/class_eval.rb +++ b/spec/ruby/core/module/shared/class_eval.rb @@ -52,6 +52,12 @@ describe :module_class_eval, shared: true do ModuleSpecs.send(@method, "[__FILE__, __LINE__]", "test", 102).should == ["test", 102] end + ruby_version_is "3.3" do + it "uses the caller location as default filename" do + ModuleSpecs.send(@method, "[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1] + end + end + it "converts a non-string filename to a string using to_str" do (file = mock(__FILE__)).should_receive(:to_str).and_return(__FILE__) ModuleSpecs.send(@method, "1+1", file) diff --git a/spec/ruby/core/tracepoint/path_spec.rb b/spec/ruby/core/tracepoint/path_spec.rb index 5b6c6d4cfc..dc2ca840b8 100644 --- a/spec/ruby/core/tracepoint/path_spec.rb +++ b/spec/ruby/core/tracepoint/path_spec.rb @@ -13,14 +13,29 @@ describe 'TracePoint#path' do path.should == "#{__FILE__}" end - it 'equals (eval) inside an eval for :end event' do - path = nil - TracePoint.new(:end) { |tp| - next unless TracePointSpec.target_thread? - path = tp.path - }.enable do - eval("module TracePointSpec; end") + ruby_version_is ""..."3.3" do + it 'equals (eval) inside an eval for :end event' do + path = nil + TracePoint.new(:end) { |tp| + next unless TracePointSpec.target_thread? + path = tp.path + }.enable do + eval("module TracePointSpec; end") + end + path.should == '(eval)' + end + end + + ruby_version_is "3.3" do + it 'equals "(eval at __FILE__:__LINE__)" inside an eval for :end event' do + path = nil + TracePoint.new(:end) { |tp| + next unless TracePointSpec.target_thread? + path = tp.path + }.enable do + eval("module TracePointSpec; end") + end + path.should == "(eval at #{__FILE__}:#{__LINE__ - 2})" end - path.should == '(eval)' end end diff --git a/spec/ruby/language/defined_spec.rb b/spec/ruby/language/defined_spec.rb index 104434adfe..b84fe9394a 100644 --- a/spec/ruby/language/defined_spec.rb +++ b/spec/ruby/language/defined_spec.rb @@ -203,7 +203,7 @@ describe "The defined? keyword when called with a method name" do it "warns about the void context when parsing it" do -> { eval "defined?(DefinedSpecs.side_effects / 2); 42" - }.should complain("(eval):1: warning: possibly useless use of defined? in void context\n", verbose: true) + }.should complain(/warning: possibly useless use of defined\? in void context/, verbose: true) end end end diff --git a/spec/ruby/language/file_spec.rb b/spec/ruby/language/file_spec.rb index 136b262d07..59563d9642 100644 --- a/spec/ruby/language/file_spec.rb +++ b/spec/ruby/language/file_spec.rb @@ -7,8 +7,16 @@ describe "The __FILE__ pseudo-variable" do -> { eval("__FILE__ = 1") }.should raise_error(SyntaxError) end - it "equals (eval) inside an eval" do - eval("__FILE__").should == "(eval)" + ruby_version_is ""..."3.3" do + it "equals (eval) inside an eval" do + eval("__FILE__").should == "(eval)" + end + end + + ruby_version_is "3.3" do + it "equals (eval at __FILE__:__LINE__) inside an eval" do + eval("__FILE__").should == "(eval at #{__FILE__}:#{__LINE__})" + end end end |