Skip to content

Commit

Permalink
Fixed history and added tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-Ouie committed Oct 13, 2011
1 parent 39e3ab6 commit 036d706
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
task :test do
ruby "test/run_all.rb"
end
15 changes: 8 additions & 7 deletions lib/coolline/history.rb
Original file line number Diff line number Diff line change
@@ -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+')
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/helpers.rb
Original file line number Diff line number Diff line change
@@ -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
85 changes: 85 additions & 0 deletions test/history_test.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/run_all.rb
Original file line number Diff line number Diff line change
@@ -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.