Skip to content

Commit

Permalink
Merge pull request #2311 from andrehjr/remove-ostruct-specs
Browse files Browse the repository at this point in the history
Remove OpenStruct usage from specs
  • Loading branch information
andrehjr committed Apr 16, 2024
2 parents 6a4d7d3 + 33af7ce commit c64a8ea
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 58 deletions.
42 changes: 24 additions & 18 deletions spec/command_integration_spec.rb
@@ -1,16 +1,22 @@
# frozen_string_literal: true

describe "commands" do
CommandHelper = Struct.new(:bs1, :bs2, :bs3, :self, :bong) do
def reset
@bs1, @bs2, @bs3, @self, @bong = nil
end
end.new

before do
@str_output = StringIO.new
@o = Object.new

# Shortcuts. They save a lot of typing.
@bs1 = "Pad.bs1 = pry_instance.binding_stack.dup"
@bs2 = "Pad.bs2 = pry_instance.binding_stack.dup"
@bs3 = "Pad.bs3 = pry_instance.binding_stack.dup"
@bs1 = "CommandHelper.bs1 = pry_instance.binding_stack.dup"
@bs2 = "CommandHelper.bs2 = pry_instance.binding_stack.dup"
@bs3 = "CommandHelper.bs3 = pry_instance.binding_stack.dup"

@self = "Pad.self = self"
@self = "CommandHelper.self = self"

@command_tester = Pry::CommandSet.new do
command "command1", "command 1 test" do
Expand All @@ -22,11 +28,11 @@
end
end

Pad.bong = "bong"
CommandHelper.bong = "bong"
end

after do
Pad.clear
CommandHelper.reset
Pry.reset_defaults
end

Expand Down Expand Up @@ -126,9 +132,9 @@
Pry.start(@o, commands: set)
end

expect(Pad.bs1.size).to eq 7
expect(Pad.self).to eq @o
expect(Pad.bs2.size).to eq 1
expect(CommandHelper.bs1.size).to eq 7
expect(CommandHelper.self).to eq @o
expect(CommandHelper.bs2.size).to eq 1
end

it 'should allow running of cd command when contained in a single string' do
Expand All @@ -143,9 +149,9 @@
Pry.start(@o, commands: set)
end

expect(Pad.bs1.size).to eq 7
expect(Pad.self).to eq @o
expect(Pad.bs2.size).to eq 1
expect(CommandHelper.bs1.size).to eq 7
expect(CommandHelper.self).to eq @o
expect(CommandHelper.bs2.size).to eq 1
end

it 'should allow running of cd command when split into array' do
Expand All @@ -160,9 +166,9 @@
Pry.start(@o, commands: set)
end

expect(Pad.bs1.size).to eq 7
expect(Pad.self).to eq @o
expect(Pad.bs2.size).to eq 1
expect(CommandHelper.bs1.size).to eq 7
expect(CommandHelper.self).to eq @o
expect(CommandHelper.bs2.size).to eq 1
end

it 'should run a command from within a command' do
Expand Down Expand Up @@ -239,7 +245,7 @@
end

# rubocop:disable Lint/InterpolationCheck
expect(pry_tester(commands: set).eval('hello #{Pad.bong}')).to match(/bong/)
expect(pry_tester(commands: set).eval('hello #{CommandHelper.bong}')).to match(/bong/)
# rubocop:enable Lint/InterpolationCheck
end

Expand Down Expand Up @@ -268,8 +274,8 @@
end

# rubocop:disable Lint/InterpolationCheck
expect(pry_tester(commands: set).eval('hello #{Pad.bong}'))
.to match(/Pad\.bong/)
expect(pry_tester(commands: set).eval('hello #{CommandHelper.bong}'))
.to match(/CommandHelper\.bong/)
# rubocop:enable Lint/InterpolationCheck
end

Expand Down
36 changes: 21 additions & 15 deletions spec/commands/edit_spec.rb
Expand Up @@ -4,6 +4,12 @@
require 'tempfile'

describe "edit" do
EditHelper = Struct.new(:required, :counter, :le) do
def reset
@required, @counter, @ie = nil
end
end.new

before do
@old_editor = Pry.config.editor
@file = @line = @contents = nil
Expand Down Expand Up @@ -76,65 +82,65 @@
Pry.config.editor = lambda { |file, _line|
File.open(file, 'w') { |f| f << 'require_relative "baz.rb"' }
File.open(file.gsub('bar.rb', 'baz.rb'), 'w') do |f|
f << "Pad.required = true; FileUtils.rm(__FILE__)"
f << "EditHelper.required = true; FileUtils.rm(__FILE__)"
end
nil
}
pry_eval "edit #{@tf_path}"
expect(Pad.required).to eq true
expect(EditHelper.required).to eq true
end
end

describe do
before do
Pad.counter = 0
EditHelper.counter = 0
Pry.config.editor = lambda { |file, _line|
File.open(file, 'w') { |f| f << "Pad.counter = Pad.counter + 1" }
File.open(file, 'w') { |f| f << "EditHelper.counter = EditHelper.counter + 1" }
nil
}
end

it "should reload the file if it is a ruby file" do
temp_file do |tf|
counter = Pad.counter
counter = EditHelper.counter
path = tf.path

pry_eval "edit #{path}"

expect(Pad.counter).to eq counter + 1
expect(EditHelper.counter).to eq counter + 1
end
end

it "should not reload the file if it is not a ruby file" do
temp_file('.py') do |tf|
counter = Pad.counter
counter = EditHelper.counter
path = tf.path

pry_eval "edit #{path}"

expect(Pad.counter).to eq counter
expect(EditHelper.counter).to eq counter
end
end

it "should not reload a ruby file if -n is given" do
temp_file do |tf|
counter = Pad.counter
counter = EditHelper.counter
path = tf.path

pry_eval "edit -n #{path}"

expect(Pad.counter).to eq counter
expect(EditHelper.counter).to eq counter
end
end

it "should reload a non-ruby file if -r is given" do
temp_file('.pryrc') do |tf|
counter = Pad.counter
counter = EditHelper.counter
path = tf.path

pry_eval "edit -r #{path}"

expect(Pad.counter).to eq counter + 1
expect(EditHelper.counter).to eq counter + 1
end
end
end
Expand Down Expand Up @@ -212,16 +218,16 @@ def last_exception
nil
}

Pad.le = @t.last_exception
EditHelper.le = @t.last_exception
redirect_pry_io(InputTester.new("def broken_method", "binding.pry", "end",
"broken_method",
"pry_instance.last_exception = Pad.le",
"pry_instance.last_exception = EditHelper.le",
"edit --ex -n", "exit-all", "exit-all")) do
Object.new.pry
end

expect(source_location).to contain_exactly(%r{(/private)?#{@path}}, 3)
Pad.clear
EditHelper.reset
end

it "should not reload the file if -n is passed" do
Expand Down
2 changes: 1 addition & 1 deletion spec/commands/hist_spec.rb
Expand Up @@ -43,7 +43,7 @@ def next_input
@hist.push "cd 2"

@t.eval("hist --replay 0..2")
stack = @t.eval("Pad.stack = pry_instance.binding_stack.dup")
stack = @t.eval("stack = pry_instance.binding_stack.dup")
expect(stack.map { |b| b.eval("self") }).to eq [TOPLEVEL_BINDING.eval("self"), 1, 2]
end

Expand Down
26 changes: 16 additions & 10 deletions spec/commands/raise_up_spec.rb
@@ -1,14 +1,20 @@
# frozen_string_literal: true

describe "raise-up" do
RaiseHelper = Struct.new(:self, :inner, :outer, :deep) do
def reset
@self, @inner, @outer, @deep = nil
end
end.new

before do
@self = "Pad.self = self"
@inner = "Pad.inner = self"
@outer = "Pad.outer = self"
@self = "RaiseHelper.self = self"
@inner = "RaiseHelper.inner = self"
@outer = "RaiseHelper.outer = self"
end

after do
Pad.clear
RaiseHelper.reset
end

it "should raise the exception with raise-up" do
Expand All @@ -29,8 +35,8 @@
Pry.start(:outer)
end

expect(Pad.inner).to eq :inner
expect(Pad.outer).to eq :outer
expect(RaiseHelper.inner).to eq :inner
expect(RaiseHelper.outer).to eq :outer
end

it "should raise the most recently raised exception" do
Expand All @@ -40,15 +46,15 @@

it "should allow you to cd up and (eventually) out" do
redirect_pry_io(InputTester.new("cd :inner", "raise NoMethodError", @inner,
"deep = :deep", "cd deep", "Pad.deep = self",
"deep = :deep", "cd deep", "RaiseHelper.deep = self",
"raise-up NoMethodError", "raise-up", @outer,
"raise-up", "exit-all")) do
expect { Pry.start(:outer) }.to raise_error NoMethodError
end

expect(Pad.deep).to eq :deep
expect(Pad.inner).to eq :inner
expect(Pad.outer).to eq :outer
expect(RaiseHelper.deep).to eq :deep
expect(RaiseHelper.inner).to eq :inner
expect(RaiseHelper.outer).to eq :outer
end

it "should jump immediately out of nested contexts with !" do
Expand Down
4 changes: 0 additions & 4 deletions spec/commands/show_source_spec.rb
Expand Up @@ -16,10 +16,6 @@ def @o.sample_method
Object.const_set(:Test, Module.new)
end

after do
Pad.clear
end

it "should output a method's source" do
expect(pry_eval(binding, 'show-source @o.sample_method')).to match(/def @o.sample/)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/commands/whereami_spec.rb
Expand Up @@ -4,6 +4,8 @@

describe "whereami" do
it 'should work with methods that have been undefined' do
Pad = Struct.new(:binding).new

class Cor
def blimey!
Cor.send :undef_method, :blimey!
Expand Down
4 changes: 2 additions & 2 deletions spec/hooks_spec.rb
Expand Up @@ -413,8 +413,8 @@ class << o; attr_accessor :value; end

describe "after_session hook" do
it 'should always run, even if uncaught exception bubbles out of repl' do
o = OpenStruct.new
o.great_escape = Class.new(StandardError)
class MyCustomException < StandardError; end
o = double(great_escape: MyCustomException)

old_ew = Pry.config.unrescued_exceptions
Pry.config.unrescued_exceptions << o.great_escape
Expand Down
12 changes: 7 additions & 5 deletions spec/pry_spec.rb
Expand Up @@ -127,8 +127,9 @@ class Hello
end

it 'should be able to operate inside the BasicObject class' do
pry_eval(BasicObject, ":foo", "Pad.obj = _")
expect(Pad.obj).to eq :foo
ObjectStore = Struct.new(:obj).new
pry_eval(BasicObject, ":foo", "ObjectStore.obj = _")
expect(ObjectStore.obj).to eq :foo
end

it 'should set an ivar on an object' do
Expand Down Expand Up @@ -323,10 +324,11 @@ class Hello
end

it 'store exceptions' do
mock_pry("foo!", "Pad.in = _in_[-1]; Pad.out = _out_[-1]")
InOut = Struct.new(:in, :out).new
mock_pry("foo!", "InOut.in = _in_[-1]; InOut.out = _out_[-1]")

expect(Pad.in).to eq "foo!\n"
expect(Pad.out).to be_a_kind_of NoMethodError
expect(InOut.in).to eq "foo!\n"
expect(InOut.out).to be_a_kind_of NoMethodError
end
end

Expand Down
3 changes: 0 additions & 3 deletions spec/spec_helper.rb
Expand Up @@ -6,7 +6,6 @@
require 'pry/testable'
require 'English'
require 'stringio'
require 'ostruct'

Dir['./spec/support/**/*.rb'].map do |file|
require file
Expand All @@ -20,8 +19,6 @@ class Module
# rubocop:enable Style/AccessModifierDeclarations
end

Pad = OpenStruct.new

# to help with tracking down bugs that cause an infinite loop in the test suite
if ENV["SET_TRACE_FUNC"]
set_trace_func(
Expand Down

0 comments on commit c64a8ea

Please sign in to comment.