Sha256: 7523568867f8cb1a828936bdf7b34124a49c3e610577688a40b79125f80d3553

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'helper'
require 'tempfile'

describe Pry do
  before do
    Pry.history.clear

    @saved_history = "1\n2\n3\n"

    Pry.history.loader = proc do |&blk|
      @saved_history.lines.each { |l| blk.call(l) }
    end

    Pry.load_history
  end

  after do
    Pry.history.clear
    Pry.history.restore_default_behavior
  end

  describe '#push' do
    it "should not record duplicated lines" do
      Pry.history << '3'
      Pry.history << '_ += 1'
      Pry.history << '_ += 1'
      Pry.history.to_a.grep('_ += 1').count.should == 1
    end

    it "should not record empty lines" do
      c = Pry.history.to_a.count
      Pry.history << ''
      Pry.history.to_a.count.should == c
    end
  end

  describe ".load_history" do
    it "should read the contents of the file" do
      Pry.history.to_a[-2..-1].should == %w(2 3)
    end
  end

  describe "saving to a file" do
    before do
      @histfile = Tempfile.new(["pryhistory", "txt"])
      @history = Pry::History.new(:file_path => @histfile.path)
      Pry.config.history.should_save = true
      @history.pusher = proc{ }
    end

    after do
      @histfile.close(true)
      Pry.config.history.should_save = false
    end

    it "should save lines to a file as they are written" do
      @history.push "5"
      File.read(@histfile.path).should == "5\n"
    end

    it "should interleave lines from many places" do
      @history.push "5"
      File.open(@histfile.path, 'a'){ |f| f.puts "6" }
      @history.push "7"

      File.read(@histfile.path).should == "5\n6\n7\n"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pry-1.0.0.pre1-i386-mswin32 spec/pry_history_spec.rb
pry-1.0.0.pre1-i386-mingw32 spec/pry_history_spec.rb