diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-04-29 19:21:54 +0300 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2019-04-29 20:06:51 +0300 |
commit | 09ebd358e19ca169b5c5e5debdbc021dfaccf524 (patch) | |
tree | 2af260903896bee9498d2dff0bdd701a9fe719d3 /spec | |
parent | ba18ef32adf0c43bfe4130251b612be44513d990 (diff) | |
download | pry-09ebd358e19ca169b5c5e5debdbc021dfaccf524.tar.gz |
pry_instance: factor out command state to be globalcommand-global-state
This change is aimed to simplify #1843 (Rework the Pry config).
Current command state implementation gets in the way. We would like to simplify
the Config class. The current implementation penetrates Pry codebase everywhere,
and during my rework of the config I discovered that `watch` uses global command
state.
It means the state should survive `Pry.new` calls. With my (unpublished yet)
implementation, the `watch` command fails to do so. I realised that we can
refactor command state implementation to be global. It makes sense to me and
also helps with the Config refactoring.
With help of a dedicated class we can easily manage the command
state (resetting).
Diffstat (limited to 'spec')
-rw-r--r-- | spec/command_spec.rb | 8 | ||||
-rw-r--r-- | spec/command_state_spec.rb | 47 | ||||
-rw-r--r-- | spec/commands/cd_spec.rb | 10 | ||||
-rw-r--r-- | spec/commands/shell_command_spec.rb | 4 | ||||
-rw-r--r-- | spec/control_d_handler_spec.rb | 2 |
5 files changed, 61 insertions, 10 deletions
diff --git a/spec/command_spec.rb b/spec/command_spec.rb index f56d92db..5991304f 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -416,6 +416,12 @@ RSpec.describe Pry::Command do end end + describe ".state" do + it "returns a command state" do + expect(described_class.state).to be_an(OpenStruct) + end + end + describe "#run" do let(:command_set) do set = Pry::CommandSet.new @@ -475,7 +481,7 @@ RSpec.describe Pry::Command do subject { Class.new(described_class).new(pry_instance: Pry.new) } it "returns a state hash" do - expect(subject.state).to be_a(Pry::Config) + expect(subject.state).to be_an(OpenStruct) end it "remembers the state" do diff --git a/spec/command_state_spec.rb b/spec/command_state_spec.rb new file mode 100644 index 00000000..10097b2b --- /dev/null +++ b/spec/command_state_spec.rb @@ -0,0 +1,47 @@ +RSpec.describe Pry::CommandState do + describe ".default" do + it "returns the default command state" do + expect(described_class.default).to be_a(described_class) + end + + context "when called multiple times" do + it "returns the same command state" do + first_state = described_class.default + second_state = described_class.default + expect(first_state).to eql(second_state) + end + end + end + + describe "#state_for" do + it "returns a state for the matching command" do + subject.state_for('command').foobar = 1 + expect(subject.state_for('command').foobar).to eq(1) + end + + it "returns new state for new command" do + expect(subject.state_for('command')) + .not_to equal(subject.state_for('other-command')) + end + + it "memoizes state for the same command" do + expect(subject.state_for('command')).to equal(subject.state_for('command')) + end + end + + describe "#reset" do + it "resets the command state for the given command" do + subject.state_for('command').foobar = 1 + subject.reset('command') + expect(subject.state_for('command').foobar).to be_nil + end + + it "doesn't reset command state for other commands" do + subject.state_for('command').foobar = 1 + subject.state_for('other-command').foobar = 1 + subject.reset('command') + + expect(subject.state_for('other-command').foobar).to eq(1) + end + end +end diff --git a/spec/commands/cd_spec.rb b/spec/commands/cd_spec.rb index d2120a6e..38830ace 100644 --- a/spec/commands/cd_spec.rb +++ b/spec/commands/cd_spec.rb @@ -16,20 +16,16 @@ describe 'cd' do end def command_state - pry.command_state["cd"] + pry.commands['cd'].state end def old_stack - pry.command_state['cd'].old_stack.dup + pry.commands['cd'].state.old_stack.dup end end end - describe 'state' do - it 'should not to be set up in fresh instance' do - expect(@t.command_state).to equal nil - end - end + after { Pry::CommandState.default.reset('cd') } describe 'old stack toggling with `cd -`' do describe 'in fresh pry instance' do diff --git a/spec/commands/shell_command_spec.rb b/spec/commands/shell_command_spec.rb index 4417f0ee..62526b3c 100644 --- a/spec/commands/shell_command_spec.rb +++ b/spec/commands/shell_command_spec.rb @@ -5,11 +5,13 @@ describe Pry::Command::ShellCommand do @t = pry_tester(@o) do def command_state - pry.command_state[Pry::Command::ShellCommand.match] + Pry::CommandState.default.state_for(Pry::Command::ShellCommand.match) end end end + after { Pry::CommandState.default.reset(Pry::Command::ShellCommand.match) } + describe ".cd" do before do allow(Dir).to receive(:chdir) diff --git a/spec/control_d_handler_spec.rb b/spec/control_d_handler_spec.rb index 2c5a45ae..5e21ccbe 100644 --- a/spec/control_d_handler_spec.rb +++ b/spec/control_d_handler_spec.rb @@ -37,7 +37,7 @@ RSpec.describe Pry::ControlDHandler do it "saves a dup of the current binding stack in the 'cd' command" do described_class.default(eval_string, pry_instance) - cd_state = pry_instance.command_state['cd'] + cd_state = pry_instance.commands['cd'].state expect(cd_state.old_stack).to eq([binding1, binding2]) end |