diff options
Diffstat (limited to 'spec/rubyspec/core/thread/backtrace')
9 files changed, 196 insertions, 0 deletions
diff --git a/spec/rubyspec/core/thread/backtrace/location/absolute_path_spec.rb b/spec/rubyspec/core/thread/backtrace/location/absolute_path_spec.rb new file mode 100644 index 0000000000..6810bdcd78 --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/absolute_path_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#absolute_path' do + before :each do + @frame = ThreadBacktraceLocationSpecs.locations[0] + end + + it 'returns the absolute path of the call frame' do + @frame.absolute_path.should == File.realpath(__FILE__) + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/base_label_spec.rb b/spec/rubyspec/core/thread/backtrace/location/base_label_spec.rb new file mode 100644 index 0000000000..cba7e3f34c --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/base_label_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#base_label' do + before :each do + @frame = ThreadBacktraceLocationSpecs.locations[0] + end + + it 'returns the base label of the call frame' do + @frame.base_label.should == '<top (required)>' + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/fixtures/classes.rb b/spec/rubyspec/core/thread/backtrace/location/fixtures/classes.rb new file mode 100644 index 0000000000..3e42d8cf81 --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/fixtures/classes.rb @@ -0,0 +1,17 @@ +module ThreadBacktraceLocationSpecs + MODULE_LOCATION = caller_locations(0) rescue nil + + def self.locations + caller_locations + end + + def self.method_location + caller_locations(0) + end + + def self.block_location + 1.times do + return caller_locations(0) + end + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/fixtures/main.rb b/spec/rubyspec/core/thread/backtrace/location/fixtures/main.rb new file mode 100644 index 0000000000..d2d14ac957 --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/fixtures/main.rb @@ -0,0 +1,5 @@ +def example + caller_locations[0].path +end + +print example diff --git a/spec/rubyspec/core/thread/backtrace/location/inspect_spec.rb b/spec/rubyspec/core/thread/backtrace/location/inspect_spec.rb new file mode 100644 index 0000000000..56d440c04a --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/inspect_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#inspect' do + before :each do + @frame = ThreadBacktraceLocationSpecs.locations[0] + @line = __LINE__ - 1 + end + + it 'converts the call frame to a String' do + @frame.inspect.should include("#{__FILE__}:#{@line}:in ") + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/label_spec.rb b/spec/rubyspec/core/thread/backtrace/location/label_spec.rb new file mode 100644 index 0000000000..4e67509d0f --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/label_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#label' do + it 'returns the base label of the call frame' do + ThreadBacktraceLocationSpecs.locations[0].label.should include('<top (required)>') + end + + it 'returns the method name for a method location' do + ThreadBacktraceLocationSpecs.method_location[0].label.should == "method_location" + end + + it 'returns the block name for a block location' do + ThreadBacktraceLocationSpecs.block_location[0].label.should == "block in block_location" + end + + it 'returns the module name for a module location' do + ThreadBacktraceLocationSpecs::MODULE_LOCATION[0].label.should include "ThreadBacktraceLocationSpecs" + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/lineno_spec.rb b/spec/rubyspec/core/thread/backtrace/location/lineno_spec.rb new file mode 100644 index 0000000000..7d203008e5 --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/lineno_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#lineno' do + before :each do + @frame = ThreadBacktraceLocationSpecs.locations[0] + @line = __LINE__ - 1 + end + + it 'returns the absolute path of the call frame' do + @frame.lineno.should == @line + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/path_spec.rb b/spec/rubyspec/core/thread/backtrace/location/path_spec.rb new file mode 100644 index 0000000000..c2f2058990 --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/path_spec.rb @@ -0,0 +1,91 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#path' do + context 'outside a main script' do + it 'returns an absolute path' do + frame = ThreadBacktraceLocationSpecs.locations[0] + + frame.path.should == __FILE__ + end + end + + context 'in a main script' do + before do + @script = fixture(__FILE__, 'main.rb') + end + + context 'when the script is in the working directory' do + before do + @directory = File.dirname(@script) + end + + context 'when using a relative script path' do + it 'returns a path relative to the working directory' do + Dir.chdir(@directory) { + ruby_exe('main.rb') + }.should == 'main.rb' + end + end + + context 'when using an absolute script path' do + it 'returns an absolute path' do + Dir.chdir(@directory) { + ruby_exe(@script) + }.should == @script + end + end + end + + context 'when the script is in a sub directory of the working directory' do + context 'when using a relative script path' do + it 'returns a path relative to the working directory' do + path = 'fixtures/main.rb' + directory = File.dirname(__FILE__) + Dir.chdir(directory) { + ruby_exe(path) + }.should == path + end + end + + context 'when using an absolute script path' do + it 'returns an absolute path' do + ruby_exe(@script).should == @script + end + end + end + + context 'when the script is outside of the working directory' do + before do + @parent_dir = tmp('path_outside_pwd') + @sub_dir = File.join(@parent_dir, 'sub') + @script = File.join(@parent_dir, 'main.rb') + source = fixture(__FILE__, 'main.rb') + + mkdir_p(@sub_dir) + + cp(source, @script) + end + + after do + rm_r(@script) + rm_r(@sub_dir) + rm_r(@parent_dir) + end + + context 'when using a relative script path' do + it 'returns a path relative to the working directory' do + Dir.chdir(@sub_dir) { + ruby_exe('../main.rb') + }.should == '../main.rb' + end + end + + context 'when using an absolute path' do + it 'returns an absolute path' do + ruby_exe(@script).should == @script + end + end + end + end +end diff --git a/spec/rubyspec/core/thread/backtrace/location/to_s_spec.rb b/spec/rubyspec/core/thread/backtrace/location/to_s_spec.rb new file mode 100644 index 0000000000..486d7da4c9 --- /dev/null +++ b/spec/rubyspec/core/thread/backtrace/location/to_s_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe 'Thread::Backtrace::Location#to_s' do + before :each do + @frame = ThreadBacktraceLocationSpecs.locations[0] + @line = __LINE__ - 1 + end + + it 'converts the call frame to a String' do + @frame.to_s.should include("#{__FILE__}:#{@line}:in ") + end +end |