fantasdic r324 - in trunk: lib/fantasdic test test/data
- From: mblondel svn gnome org
- To: svn-commits-list gnome org
- Subject: fantasdic r324 - in trunk: lib/fantasdic test test/data
- Date: Wed, 20 Aug 2008 23:25:39 +0000 (UTC)
Author: mblondel
Date: Wed Aug 20 23:25:39 2008
New Revision: 324
URL: http://svn.gnome.org/viewvc/fantasdic?rev=324&view=rev
Log:
* lib/fantasdic/preferences.rb: Split Preferences into Preferences
and PreferencesBase; support edge cases in some functions.
* test/test_preferences.rb: Added unit test.
* test/data/config.yaml: Data used by the unit test.
Added:
trunk/test/data/
trunk/test/data/config.yaml
trunk/test/test_preferences.rb
Modified:
trunk/lib/fantasdic/preferences.rb
Modified: trunk/lib/fantasdic/preferences.rb
==============================================================================
--- trunk/lib/fantasdic/preferences.rb (original)
+++ trunk/lib/fantasdic/preferences.rb Wed Aug 20 23:25:39 2008
@@ -32,16 +32,13 @@
DEFAULT_CONFIG_FILE = File.join(DATA_DIR, "config", "default.yaml")
end
- class Preferences
- include Singleton
+ class PreferencesBase
- def initialize
- unless(FileTest.exists?(Config::CONFIG_DIR))
- Dir.mkdir(Config::CONFIG_DIR)
- end
+ def initialize(config_file)
+ @config_file = config_file
- @config = YAML.load(File.open(Config::CONFIG_FILE,
- File::CREAT|File::RDWR))
+ @config = YAML.load(File.open(@config_file,
+ File::CREAT|File::RDWR))
if @config and @config.is_a? Hash
# merge with the default config in case of new parameters
dflt_config = YAML.load(File.open(Config::DEFAULT_CONFIG_FILE))
@@ -56,7 +53,7 @@
end
def save!
- File.open(Config::CONFIG_FILE,
+ File.open(@config_file,
File::CREAT|File::TRUNC|File::RDWR, 0600) do |f|
YAML.dump(@config, f)
end
@@ -77,23 +74,29 @@
end
def delete_dictionary(name)
+ return if not dictionary_exists?(name)
self.dictionaries.delete(name)
self.dictionaries_infos.delete(name)
end
def dictionary_up(name)
+ return if not dictionary_exists?(name)
new_index = self.dictionaries.index(name) + 1
+ return if new_index == self.dictionaries.length
self.dictionaries.delete(name)
self.dictionaries.insert(new_index, name)
end
def dictionary_down(name)
+ return if not dictionary_exists?(name)
new_index = self.dictionaries.index(name) - 1
+ return if new_index == -1
self.dictionaries.delete(name)
self.dictionaries.insert(new_index, name)
end
def dictionary_replace_name(old, new)
+ return if not dictionary_exists?(old)
index = self.dictionaries.index(old)
self.dictionaries[index] = new
self.dictionaries_infos[new] = self.dictionaries_infos[old]
@@ -123,4 +126,16 @@
end
+ class Preferences < PreferencesBase
+ include Singleton
+
+ def initialize
+ unless(FileTest.exists?(Config::CONFIG_DIR))
+ Dir.mkdir(Config::CONFIG_DIR)
+ end
+
+ super(Config::CONFIG_FILE)
+ end
+ end
+
end
Added: trunk/test/data/config.yaml
==============================================================================
--- (empty file)
+++ trunk/test/data/config.yaml Wed Aug 20 23:25:39 2008
@@ -0,0 +1,121 @@
+---
+scan_clipboard: false
+dictionaries:
+- English
+- English <-> French
+- Spanish
+- Japanese
+last_searches:
+- &id001
+ :strategy: define
+ :word: test
+ :dictionary: English
+window_maximized: false
+sidepane_position: 160
+watched_words: []
+
+window_position:
+- 196
+- 110
+view_toolbar: true
+history_nb_rows: 15
+selected_dictionary: English
+keep_above: false
+view_history: true
+dictionaries_infos:
+ English:
+ :all_dbs: false
+ :auth: false
+ :server: dict.org
+ :avail_strats:
+ - prefix
+ - soundex
+ - regexp
+ - exact
+ - word
+ - substring
+ - lev
+ - re
+ - suffix
+ :sel_dbs:
+ - foldoc
+ - gcide
+ - wn
+ - jargon
+ :port: "2628"
+ :sel_strat: define
+ :login: ""
+ :password: ""
+ :selected: 1
+ English <-> French:
+ :all_dbs: false
+ :auth: false
+ :server: dict.org
+ :avail_strats:
+ - prefix
+ - soundex
+ - regexp
+ - exact
+ - word
+ - substring
+ - lev
+ - re
+ - suffix
+ :sel_dbs:
+ - eng-fra
+ - fra-eng
+ :port: "2628"
+ :sel_strat: define
+ :login: ""
+ :password: ""
+ :selected: 1
+ Spanish:
+ :all_dbs: true
+ :auth: false
+ :server: es.dict.org
+ :avail_strats:
+ - prefix
+ - soundex
+ - regexp
+ - exact
+ - word
+ - substring
+ - lev
+ - re
+ - suffix
+ :sel_dbs: []
+
+ :port: "2628"
+ :sel_strat: define
+ :login: ""
+ :password: ""
+ :selected: 1
+ Japanese:
+ :all_dbs: true
+ :auth: false
+ :server: nihongobenkyo.org
+ :avail_strats:
+ - prefix
+ - soundex
+ - regexp
+ - exact
+ - word
+ - substring
+ - lev
+ - re
+ - suffix
+ :sel_dbs: []
+
+ :port: "2628"
+ :sel_strat: define
+ :login: ""
+ :password: ""
+ :selected: 1
+dont_show_at_startup: false
+dont_quit: true
+show_in_tray: true
+window_size:
+- 550
+- 500
+last_search: *id001
+view_statusbar: true
Added: trunk/test/test_preferences.rb
==============================================================================
--- (empty file)
+++ trunk/test/test_preferences.rb Wed Aug 20 23:25:39 2008
@@ -0,0 +1,193 @@
+# 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"))
+$config_file = File.expand_path(File.join(test_dir, "data", "config.yaml"))
+$LOAD_PATH.unshift(lib_dir)
+
+require "test/unit"
+require "fantasdic"
+
+class TestPreferences < Test::Unit::TestCase
+
+ def setup
+ @prefs = Fantasdic::PreferencesBase.new($config_file)
+ end
+
+ def teardown
+ @prefs = nil
+ end
+
+ def test_attributes
+ require "pp"
+ assert_equal(@prefs.scan_clipboard, false)
+ assert_equal(@prefs.window_position, [196, 110])
+ assert_equal(@prefs.history_nb_rows, 15)
+ assert_equal(@prefs.dictionaries_infos,
+ {"English"=>
+ {:login=>"",
+ :password=>"",
+ :all_dbs=>false,
+ :selected=>1,
+ :sel_dbs=>["foldoc", "gcide", "wn", "jargon"],
+ :avail_strats=>
+ ["prefix",
+ "soundex",
+ "regexp",
+ "exact",
+ "word",
+ "substring",
+ "lev",
+ "re",
+ "suffix"],
+ :auth=>false,
+ :server=>"dict.org",
+ :sel_strat=>"define",
+ :port=>"2628"},
+ "Spanish"=>
+ {:login=>"",
+ :password=>"",
+ :all_dbs=>true,
+ :selected=>1,
+ :sel_dbs=>[],
+ :avail_strats=>
+ ["prefix",
+ "soundex",
+ "regexp",
+ "exact",
+ "word",
+ "substring",
+ "lev",
+ "re",
+ "suffix"],
+ :auth=>false,
+ :server=>"es.dict.org",
+ :sel_strat=>"define",
+ :port=>"2628"},
+ "English <-> French"=>
+ {:login=>"",
+ :password=>"",
+ :all_dbs=>false,
+ :selected=>1,
+ :sel_dbs=>["eng-fra", "fra-eng"],
+ :avail_strats=>
+ ["prefix",
+ "soundex",
+ "regexp",
+ "exact",
+ "word",
+ "substring",
+ "lev",
+ "re",
+ "suffix"],
+ :auth=>false,
+ :server=>"dict.org",
+ :sel_strat=>"define",
+ :port=>"2628"},
+ "Japanese"=>
+ {:login=>"",
+ :password=>"",
+ :all_dbs=>true,
+ :selected=>1,
+ :sel_dbs=>[],
+ :avail_strats=>
+ ["prefix",
+ "soundex",
+ "regexp",
+ "exact",
+ "word",
+ "substring",
+ "lev",
+ "re",
+ "suffix"],
+ :auth=>false,
+ :server=>"nihongobenkyo.org",
+ :sel_strat=>"define",
+ :port=>"2628"}})
+ assert_equal(@prefs.last_search,
+ {:strategy=>"define", :word=>"test",
+ :dictionary=>"English"})
+ end
+
+ def test_update_dictionary
+ assert_not_equal(@prefs.dictionaries_infos["Japanese"], {})
+ @prefs.update_dictionary("Japanese", {})
+ assert_equal(@prefs.dictionaries_infos["Japanese"], {})
+ end
+
+ def test_add_dictionary
+ assert_not_equal(@prefs.dictionaries_infos["Japanese"], {})
+ @prefs.add_dictionary("Japanese", {})
+ assert_equal(@prefs.dictionaries_infos["Japanese"], {})
+
+ assert_equal(@prefs.dictionaries_infos.has_key?("New"), false)
+ @prefs.add_dictionary("New", {})
+ assert_equal(@prefs.dictionaries_infos["New"], {})
+ end
+
+ def test_delete_dictionary
+ assert_equal(@prefs.dictionaries.include?("Japanese"), true)
+ assert_equal(@prefs.dictionaries_infos.has_key?("Japanese"), true)
+ @prefs.delete_dictionary("Japanese")
+ assert_equal(@prefs.dictionaries.include?("Japanese"), false)
+ assert_equal(@prefs.dictionaries_infos.has_key?("Japanese"), false)
+
+ @prefs.delete_dictionary("DontExist")
+ end
+
+ def test_dictionary_up
+ assert_equal(@prefs.dictionaries.index("English"), 0)
+ @prefs.dictionary_up("English")
+ assert_equal(@prefs.dictionaries.index("English"), 1)
+
+ assert_equal(@prefs.dictionaries.index("Japanese"), 3)
+ @prefs.dictionary_up("Japanese")
+ assert_equal(@prefs.dictionaries.index("Japanese"), 3)
+
+ @prefs.dictionary_up("DontExist")
+ end
+
+ def test_dictionary_down
+ assert_equal(@prefs.dictionaries.index("English"), 0)
+ @prefs.dictionary_down("English")
+ assert_equal(@prefs.dictionaries.index("English"), 0)
+
+ assert_equal(@prefs.dictionaries.index("Japanese"), 3)
+ @prefs.dictionary_down("Japanese")
+ assert_equal(@prefs.dictionaries.index("Japanese"), 2)
+
+ @prefs.dictionary_down("DontExist")
+ end
+
+ def test_dictionary_replace_name
+ assert_equal(@prefs.dictionary_exists?("Japanese"), true)
+ assert_equal(@prefs.dictionary_exists?("New"), false)
+ @prefs.dictionary_replace_name("Japanese", "New")
+ assert_equal(@prefs.dictionary_exists?("Japanese"), false)
+ assert_equal(@prefs.dictionary_exists?("New"), true)
+
+ @prefs.dictionary_replace_name("DontExist", "New")
+ end
+
+ def test_dictionary_exists
+ assert_equal(@prefs.dictionary_exists?("Japanese"), true)
+ assert_equal(@prefs.dictionary_exists?("DontExist"), false)
+ end
+
+end
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]