mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
clean up history tests a bit
This commit is contained in:
parent
dabe9c50da
commit
3ae259b2b7
2 changed files with 32 additions and 28 deletions
|
@ -7,7 +7,11 @@ class Pry
|
|||
def initialize
|
||||
@history = []
|
||||
@saved_lines = 0
|
||||
restore_default_behavior
|
||||
end
|
||||
|
||||
# Assign the default methods for loading, saving, pushing, and clearing.
|
||||
def restore_default_behavior
|
||||
@loader = method(:read_from_file)
|
||||
@saver = method(:write_to_file)
|
||||
@pusher = method(:push_to_readline)
|
||||
|
@ -24,7 +28,7 @@ class Pry
|
|||
@saved_lines = @history.length
|
||||
end
|
||||
|
||||
# Writes this session's history using `History.saver`.
|
||||
# Write this session's history using `History.saver`.
|
||||
# @return [Integer] The number of lines saved
|
||||
def save
|
||||
history_to_save = @history[@saved_lines..-1]
|
||||
|
@ -33,7 +37,7 @@ class Pry
|
|||
history_to_save.length
|
||||
end
|
||||
|
||||
# Adds a line to the input history, ignoring blank and duplicate lines.
|
||||
# Add a line to the input history, ignoring blank and duplicate lines.
|
||||
# @param [String] line
|
||||
# @return [String] The same line that was passed in
|
||||
def push(line)
|
||||
|
@ -45,7 +49,7 @@ class Pry
|
|||
end
|
||||
alias << push
|
||||
|
||||
# Clears all history. Anything the user entered before this point won't be
|
||||
# Clear all history. Anything the user entered before this point won't be
|
||||
# saved, but anything they put in afterwards will still be appended to the
|
||||
# history file on exit.
|
||||
def clear
|
||||
|
@ -54,7 +58,7 @@ class Pry
|
|||
@saved_lines = 0
|
||||
end
|
||||
|
||||
# Returns an Array containing all stored history.
|
||||
# Return an Array containing all stored history.
|
||||
# @return [Array<String>] An Array containing all lines of history loaded
|
||||
# or entered by the user in the current session.
|
||||
def to_a
|
||||
|
|
|
@ -2,26 +2,30 @@ require 'helper'
|
|||
require 'tempfile'
|
||||
|
||||
describe Pry do
|
||||
|
||||
before do
|
||||
Pry.history.clear
|
||||
@file = Tempfile.new(["pry", ".pry_history"])
|
||||
@hist = @file.path
|
||||
File.open(@hist, 'w') {|f| f << "1\n2\n3\n" }
|
||||
@old_hist = Pry.config.history.file
|
||||
Pry.config.history.file = @hist
|
||||
|
||||
@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
|
||||
|
||||
Pry.load_history
|
||||
end
|
||||
|
||||
after do
|
||||
@file.close
|
||||
File.unlink(@hist)
|
||||
Pry.config.history.file = @old_hist
|
||||
Pry.history.clear
|
||||
Pry.history.restore_default_behavior
|
||||
end
|
||||
|
||||
describe ".load_history" do
|
||||
it "should read the contents of the file" do
|
||||
Pry.history.to_a[-2..-1].should === ["2", "3"]
|
||||
Pry.history.to_a[-2..-1].should == %w(2 3)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -29,56 +33,52 @@ describe Pry do
|
|||
it "should include a trailing newline" do
|
||||
Pry.history << "4"
|
||||
Pry.save_history
|
||||
File.read(@hist).should =~ /4\n\z/
|
||||
@saved_history.should =~ /4\n\z/
|
||||
end
|
||||
|
||||
it "should not change anything if history is not changed" do
|
||||
File.open(@hist, 'w') {|f| f << "4\n5\n6\n" }
|
||||
@saved_history = "4\n5\n6\n"
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "4\n5\n6\n"
|
||||
@saved_history.should == "4\n5\n6\n"
|
||||
end
|
||||
|
||||
it "should append new lines to the file" do
|
||||
Pry.history << "4"
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "1\n2\n3\n4\n"
|
||||
@saved_history.should == "1\n2\n3\n4\n"
|
||||
end
|
||||
|
||||
it "should not clobber lines written by other Pry's in the meantime" do
|
||||
Pry.history << "5"
|
||||
File.open(@hist, 'a') {|f| f << "4\n" }
|
||||
@saved_history << "4\n"
|
||||
Pry.save_history
|
||||
|
||||
Pry.history.to_a[-3..-1].should == ["2", "3", "5"]
|
||||
File.read(@hist).should == "1\n2\n3\n4\n5\n"
|
||||
@saved_history.should == "1\n2\n3\n4\n5\n"
|
||||
end
|
||||
|
||||
it "should not delete lines from the file if this session's history was cleared" do
|
||||
Pry.history.clear
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "1\n2\n3\n"
|
||||
@saved_history.should == "1\n2\n3\n"
|
||||
end
|
||||
|
||||
it "should save new lines that are added after the history was cleared" do
|
||||
Pry.history.clear
|
||||
Pry.history << "4"
|
||||
|
||||
# doing this twice as libedit on 1.8.7 has bugs and sometimes ignores the
|
||||
# first line in history
|
||||
Pry.history << "4"
|
||||
Pry.save_history
|
||||
File.read(@hist).should =~ /1\n2\n3\n4\n/
|
||||
@saved_history.should =~ /1\n2\n3\n4\n/
|
||||
end
|
||||
|
||||
it "should only append new lines the second time it is saved" do
|
||||
Pry.history << "4"
|
||||
Pry.save_history
|
||||
File.open(@hist, 'a') {|f| f << "5\n" }
|
||||
@saved_history << "5\n"
|
||||
Pry.history << "6"
|
||||
Pry.save_history
|
||||
|
||||
Pry.history.to_a[-4..-1].should == ["2", "3", "4", "6"]
|
||||
File.read(@hist).should == "1\n2\n3\n4\n5\n6\n"
|
||||
@saved_history.should == "1\n2\n3\n4\n5\n6\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue