fantasdic r332 - in trunk: . lib/fantasdic test
- From: mblondel svn gnome org
- To: svn-commits-list gnome org
- Subject: fantasdic r332 - in trunk: . lib/fantasdic test
- Date: Sat, 23 Aug 2008 07:06:15 +0000 (UTC)
Author: mblondel
Date: Sat Aug 23 07:06:15 2008
New Revision: 332
URL: http://svn.gnome.org/viewvc/fantasdic?rev=332&view=rev
Log:
* test/test_source_base.rb: Added unit test.
* lib/fantasdic/source_base.rb: Allow to define :max_cache as an option.
Added:
trunk/test/test_source_base.rb
Modified:
trunk/ChangeLog
trunk/lib/fantasdic/source_base.rb
Modified: trunk/lib/fantasdic/source_base.rb
==============================================================================
--- trunk/lib/fantasdic/source_base.rb (original)
+++ trunk/lib/fantasdic/source_base.rb Sat Aug 23 07:06:15 2008
@@ -90,8 +90,9 @@
def_field :disable_search_all_databases, :no_databases
end
- def initialize(hash)
+ def initialize(hash={})
@hash = hash
+ @max_cache = hash[:max_cache] ? hash[:max_cache] : MAX_CACHE
end
# Methods which should be implemented by children classes
@@ -246,7 +247,7 @@
def update_cache(cache)
self.class.cache_queue.unshift(cache)
- if self.class.cache_queue.length > MAX_CACHE
+ if self.class.cache_queue.length > @max_cache
self.class.cache_queue.pop
end
end
Added: trunk/test/test_source_base.rb
==============================================================================
--- (empty file)
+++ trunk/test/test_source_base.rb Sat Aug 23 07:06:15 2008
@@ -0,0 +1,147 @@
+# Fantasdic
+# Copyright (C) 2008 Mathieu Blondel
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+#Âwith this program; if not, write to the Free Software Foundation, Inc.,
+#Â51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+test_dir = File.expand_path(File.dirname(__FILE__))
+top_dir = File.expand_path(File.join(test_dir, ".."))
+lib_dir = File.expand_path(File.join(top_dir, "lib"))
+$LOAD_PATH.unshift(lib_dir)
+
+require "test/unit"
+require "fantasdic"
+
+class TestSourceBase < Test::Unit::TestCase
+
+ class MySource < Fantasdic::Source::Base
+
+ attr_reader :n_define_calls, :n_match_calls
+
+ def initialize(*args)
+ super(*args)
+
+ @n_define_calls = 0
+ @n_match_calls = 0
+ end
+
+ def define(db, number)
+ # Return number ^ 2
+ definition = Fantasdic::Source::Base::Definition.new
+ definition.word = number
+ definition.body = (number.to_i ** 2).to_s
+ definition.database = "db"
+ definition.description = "Database"
+
+ @n_define_calls += 1
+
+ [definition]
+ end
+
+ def match(db, strat, number)
+ @n_match_calls += 1
+ {"db1" => [(number.to_i ** 2).to_s]}
+ end
+
+ end
+
+ def setup
+ @source = MySource.new({:max_cache => 3})
+ end
+
+ def test_define
+ defs = @source.define("*", "2")
+ assert_equal(defs.length, 1)
+ assert_equal(defs.first.body, "4")
+ end
+
+ def test_define_number(number, result)
+ defs = @source.cached_multiple_define(["db1"], number)
+ assert_equal(defs.length, 1)
+ assert_equal(defs.first.body, result)
+ end
+ private :test_define_number
+
+ def test_cached_multiple_define
+ test_define_number("2", "4")
+ assert_equal(@source.n_define_calls, 1)
+
+ test_define_number("2", "4")
+ assert_equal(@source.n_define_calls, 1)
+
+ test_define_number("3", "9")
+ assert_equal(@source.n_define_calls, 2)
+
+ test_define_number("4", "16")
+ assert_equal(@source.n_define_calls, 3)
+
+ test_define_number("2", "4")
+ assert_equal(@source.n_define_calls, 3)
+
+ test_define_number("5", "25")
+ assert_equal(@source.n_define_calls, 4)
+
+ # max_cache reached
+ test_define_number("2", "4")
+ assert_equal(@source.n_define_calls, 5)
+
+ test_define_number("4", "16")
+ assert_equal(@source.n_define_calls, 5)
+
+ test_define_number("5", "25")
+ assert_equal(@source.n_define_calls, 5)
+ end
+
+ def test_match
+ matches = @source.match("*", "prefix", "2")
+ assert_equal(matches, {"db1" => ["4"]})
+ end
+
+ def test_match_number(number, result)
+ matches = @source.cached_multiple_match(["db1"], "prefix", number)
+ assert_equal(matches, {"db1" => [result]})
+ end
+ private :test_match_number
+
+ def test_cached_multiple_define
+ test_match_number("2", "4")
+ assert_equal(@source.n_match_calls, 1)
+
+ test_match_number("2", "4")
+ assert_equal(@source.n_match_calls, 1)
+
+ test_match_number("3", "9")
+ assert_equal(@source.n_match_calls, 2)
+
+ test_match_number("4", "16")
+ assert_equal(@source.n_match_calls, 3)
+
+ test_match_number("2", "4")
+ assert_equal(@source.n_match_calls, 3)
+
+ test_match_number("5", "25")
+ assert_equal(@source.n_match_calls, 4)
+
+ # max_cache reached
+ test_match_number("2", "4")
+ assert_equal(@source.n_match_calls, 5)
+
+ test_match_number("4", "16")
+ assert_equal(@source.n_match_calls, 5)
+
+ test_match_number("5", "25")
+ assert_equal(@source.n_match_calls, 5)
+ end
+
+end
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]