summaryrefslogtreecommitdiff
path: root/spec/history_spec.rb
blob: 0a7c66dc1eb8db53cc3c1186559654a5cec1b847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require_relative '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
    Pry.history.instance_variable_set(:@original_lines, 0)
  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 "#clear" do
    before do
      @old_file = Pry.config.history.file
      @hist_file_path = File.expand_path('spec/fixtures/pry_history')
      Pry.config.history.file = @hist_file_path
      Pry.history.clear
      Pry.history.restore_default_behavior
      Pry.load_history
    end

    after do
      Pry.config.history.file = @old_file
    end

    it "clears this session's history" do
      Pry.history.to_a.size.should > 0
      Pry.history.clear
      Pry.history.to_a.size.should == 0
    end

    it "doesn't affect the contents of the history file" do
      Pry.history.to_a.size.should == 3
      Pry.history.clear

      File.open(@hist_file_path, 'r') { |fh|
        file = fh.to_a

        file.length.should == 3
        file.any? { |a| a =~ /athos/ }.should == true
      }
    end
  end

  describe "#history_line_count" do
    it "counts entries in history" do
      Pry.history.clear
      saved_history = "olgierd\ngustlik\njanek\ngrzes\ntomek\n"
      Pry.history.loader = proc do |&blk|
        saved_history.lines.each { |l| blk.call(l) }
      end
      Pry.load_history

      Pry.history.history_line_count.should == 5
    end
  end

  describe "#restore_default_behavior" do
    it "restores loader" do
      Pry.history.loader = proc {}
      Pry.history.restore_default_behavior
      Pry.history.loader.class.should == Method
      Pry.history.loader.name.to_sym.should == :read_from_file
    end

    it "restores saver" do
      Pry.history.saver = proc {}
      Pry.history.restore_default_behavior
      Pry.history.saver.class.should == Method
      Pry.history.saver.name.to_sym.should == :save_to_file
    end

    it "restores pusher" do
      Pry.history.pusher = proc {}
      Pry.history.restore_default_behavior
      Pry.history.pusher.class.should == Method
      Pry.history.pusher.name.to_sym.should == :push_to_readline
    end

    it "restores clearer" do
      Pry.history.clearer = proc {}
      Pry.history.restore_default_behavior
      Pry.history.clearer.class.should == Method
      Pry.history.clearer.name.to_sym.should == :clear_readline
    end
  end

  describe "#session_line_count" do
    it "returns the number of lines in history from just this session" do
      Pry.history << 'you?'
      Pry.history << 'you are so precious'
      Pry.history.session_line_count.should == 2
    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