Skip to content
Browse files

Fixed history and added tests for it

  • Loading branch information...
1 parent 39e3ab6 commit 036d706f9c71fac02cc38853b71cc8643f65ecef @Mon-Ouie committed
Showing with 113 additions and 7 deletions.
  1. +3 −0 Rakefile
  2. +8 −7 lib/coolline/history.rb
  3. +12 −0 test/helpers.rb
  4. +85 −0 test/history_test.rb
  5. +5 −0 test/run_all.rb
View
3 Rakefile
@@ -0,0 +1,3 @@
+task :test do
+ ruby "test/run_all.rb"
+end
View
15 lib/coolline/history.rb
@@ -1,4 +1,6 @@
class Coolline
+ # Class used to keep track of input. It keeps a certain amount of lines at
+ # most in memory, and stores them in a file.
class History
def initialize(filename, max_size = 5000)
@io = File.open(filename, 'a+')
@@ -22,7 +24,7 @@ def close
end
def search(pattern, first_line = -1)
- return to_enum(:search, pattern) unless block_given?
+ return to_enum(:search, pattern, first_line) unless block_given?
return if size == 0
first_line %= size
@@ -36,7 +38,7 @@ def <<(el)
@io.flush
@lines << el.dup
- @lines.unshift if size > @max_size
+ @lines.delete_at(0) if @lines.size > @max_size
self
end
@@ -53,16 +55,15 @@ def size
private
def load_lines
- @io.seek 0, IO::SEEK_END
- line_count = @io.lineno
- byte_index = @io.pos
+ line_count = @io.count
@io.rewind
if line_count < @max_size
- @lines = @io.lines.map(&:chomp)
+ @lines.concat @io.lines.map(&:chomp)
else
@io.each do |line| # surely inefficient
- self << line.chomp
+ @lines << line.chomp
+ @lines.delete_at(0) if @lines.size > @max_size
end
end
end
View
12 test/helpers.rb
@@ -0,0 +1,12 @@
+$LOAD_PATH.unshift File.expand_path(File.join("../lib"), File.dirname(__FILE__))
+
+require 'riot'
+require 'coolline'
+
+Riot.reporter = Riot::PrettyDotMatrixReporter
+
+TestDir = File.dirname(__FILE__)
+
+def path_to(file)
+ File.join(TestDir, file)
+end
View
85 test/history_test.rb
@@ -0,0 +1,85 @@
+require File.expand_path("helpers.rb", File.dirname(__FILE__))
+
+context "a newly created history" do
+ setup do
+ Coolline::History.new(path_to("history"), 10)
+ end
+
+ asserts(:size).equals 0
+ asserts(:max_size).equals 10
+
+ context "after inserting a new-line" do
+ hookup do
+ topic << "1"
+ end
+
+ asserts(:size).equals 1
+ asserts(:max_size).equals 10
+
+ asserts(:[], 0).equals "1"
+
+ context "and many others" do
+ hookup do
+ 2.upto(11) { |n| topic << n.to_s }
+ end
+
+ asserts(:size).equals 10
+ asserts(:max_size).equals 10
+
+ 2.upto(11) do |n|
+ asserts(:[], n - 2).equals n.to_s
+ end
+ end
+ end
+
+ teardown do
+ File.delete path_to("history")
+ end
+end
+
+context "an history from an existing file" do
+ setup do
+ open(path_to("history"), 'w') do |io|
+ io.puts((1..13).to_a)
+ end
+
+ Coolline::History.new(path_to("history"), 15)
+ end
+
+ asserts(:size).equals 13
+ asserts(:max_size).equals 15
+
+ 1.upto(13) do |n|
+ asserts(:[], n - 1).equals n.to_s
+ end
+
+ asserts("search for 3") { topic.search(/3/).to_a }.equals [["13",12], ["3",2]]
+ asserts("search for 3 before last line") {
+ topic.search(/3/, -2).to_a
+ }.equals [["3",2]]
+
+ teardown do
+ File.delete path_to("history")
+ end
+end
+
+context "an history from a huge existing file" do
+ setup do
+ open(path_to("history"), 'w') do |io|
+ io.puts((1..20).to_a)
+ end
+
+ Coolline::History.new(path_to("history"), 15)
+ end
+
+ asserts(:size).equals 15
+ asserts(:max_size).equals 15
+
+ 6.upto(20) do |n|
+ asserts(:[], n - 6).equals n.to_s
+ end
+
+ teardown do
+ File.delete path_to("history")
+ end
+end
View
5 test/run_all.rb
@@ -0,0 +1,5 @@
+require File.expand_path("helpers.rb", File.dirname(__FILE__))
+
+Dir.glob(File.join(File.dirname(__FILE__), "**/*_test.rb")) do |file|
+ load file
+end

0 comments on commit 036d706

Please sign in to comment.
Something went wrong with that request. Please try again.