1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/spec/command_state_spec.rb
Kyrylo Silin 09ebd358e1 pry_instance: factor out command state to be global
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).
2019-04-29 20:06:51 +03:00

47 lines
1.4 KiB
Ruby

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