2011-08-07 06:16:54 -04:00
|
|
|
require 'helper'
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
describe Pry do
|
|
|
|
before do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
2011-12-02 00:26:22 -05:00
|
|
|
|
|
|
|
@saved_history = "1\n2\n3\n"
|
|
|
|
|
|
|
|
Pry.history.loader = proc do |&blk|
|
|
|
|
@saved_history.lines.each { |l| blk.call(l) }
|
|
|
|
end
|
|
|
|
|
|
|
|
Pry.history.saver = proc do |lines|
|
|
|
|
@saved_history << lines.map { |l| "#{l}\n" }.join
|
|
|
|
end
|
|
|
|
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.load_history
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2011-12-02 00:26:22 -05:00
|
|
|
Pry.history.clear
|
|
|
|
Pry.history.restore_default_behavior
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".load_history" do
|
|
|
|
it "should read the contents of the file" do
|
2011-12-02 00:26:22 -05:00
|
|
|
Pry.history.to_a[-2..-1].should == %w(2 3)
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".save_history" do
|
|
|
|
it "should include a trailing newline" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should =~ /4\n\z/
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not change anything if history is not changed" do
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history = "4\n5\n6\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should == "4\n5\n6\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should append new lines to the file" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should == "1\n2\n3\n4\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not clobber lines written by other Pry's in the meantime" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "5"
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history << "4\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.to_a[-3..-1].should == ["2", "3", "5"]
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should == "1\n2\n3\n4\n5\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not delete lines from the file if this session's history was cleared" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should == "1\n2\n3\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should save new lines that are added after the history was cleared" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should =~ /1\n2\n3\n4\n/
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should only append new lines the second time it is saved" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history << "5\n"
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "6"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.to_a[-4..-1].should == ["2", "3", "4", "6"]
|
2011-12-02 00:26:22 -05:00
|
|
|
@saved_history.should == "1\n2\n3\n4\n5\n6\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|