From 036d706f9c71fac02cc38853b71cc8643f65ecef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mon=20ou=C3=AFe?= Date: Thu, 13 Oct 2011 20:54:01 +0200 Subject: [PATCH] Fixed history and added tests for it --- Rakefile | 3 ++ lib/coolline/history.rb | 15 ++++---- test/helpers.rb | 12 ++++++ test/history_test.rb | 85 +++++++++++++++++++++++++++++++++++++++++ test/run_all.rb | 5 +++ 5 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 test/helpers.rb create mode 100644 test/history_test.rb create mode 100644 test/run_all.rb diff --git a/Rakefile b/Rakefile index e69de29..fe4b8be 100644 --- a/Rakefile +++ b/Rakefile @@ -0,0 +1,3 @@ +task :test do + ruby "test/run_all.rb" +end diff --git a/lib/coolline/history.rb b/lib/coolline/history.rb index 8b61426..6a13348 100644 --- a/lib/coolline/history.rb +++ b/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 diff --git a/test/helpers.rb b/test/helpers.rb new file mode 100644 index 0000000..d9b5da7 --- /dev/null +++ b/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 diff --git a/test/history_test.rb b/test/history_test.rb new file mode 100644 index 0000000..89449ed --- /dev/null +++ b/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 diff --git a/test/run_all.rb b/test/run_all.rb new file mode 100644 index 0000000..3fda5e3 --- /dev/null +++ b/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