Permalink
Browse files

Added various tests.

Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
  • Loading branch information...
1 parent 164f65a commit f681fc0de0e769a9c0712940e895a48536dc1f98 @YorickPeterse committed Apr 13, 2012
View
@@ -26,7 +26,7 @@ module Aspell
[:pointer, :string],
:string
- attach_function 'config_get_default',
+ attach_function 'config_retrieve_default',
'aspell_config_get_default',
[:pointer, :string],
:string
View
@@ -16,8 +16,12 @@ class Speller
# @param [Hash] options A hash containing extra configuration options,
# such as the "personal" option to set.
#
- def initialize(language, options = {})
+ def initialize(language = nil, options = {})
+ @config = Aspell.config_new
+ options['lang'] = language if language
+
+ options.each { |k, v| set(k, v) }
end
##
@@ -28,7 +32,15 @@ def initialize(language, options = {})
# @return [TrueClass|FalseClass]
#
def correct?(word)
+ unless word.respond_to?(:to_s)
+ raise(TypeError, 'Words should respond to #to_s()')
+ end
+ return Aspell.speller_check(
+ Aspell.speller_new(@config),
+ word.to_s,
+ word.length
+ )
end
##
@@ -41,20 +53,85 @@ def correct?(word)
# could not be set.
#
def set(key, value)
+ unless key.respond_to?(:to_s)
+ raise(TypeError, 'Configuration keys should respond to #to_s()')
+ end
+
+ unless value.respond_to?(:to_s)
+ raise(TypeError, 'Configuration values should respond to #to_s()')
+ end
+ unless Aspell.config_replace(@config, key.to_s, value.to_s)
+ raise(ConfigError, "Failed to set the configuration item #{key}")
+ end
end
##
# Retrieves the value of the specified configuration item.
#
# @since 13-04-2012
- # @param [String] key The configuration key to retrieve.
+ # @param [#to_s] key The configuration key to retrieve.
# @return [String]
# @raise [FFI::Aspell::ConfigError] Raised when the configuration item
# does not exist.
#
def get(key)
+ unless key.respond_to?(:to_s)
+ raise(TypeError, 'Configuration keys should respond to #to_s()')
+ end
+
+ value = Aspell.config_retrieve(@config, key.to_s)
+
+ if value
+ return value
+ else
+ raise(ConfigError, "The configuration item #{key} does not exist")
+ end
+ end
+
+ ##
+ # Retrieves the default value for the given configuration key.
+ #
+ # @since 13-04-2012
+ # @param [#to_s] key The name of the configuration key.
+ # @return [String]
+ # @raise [FFI::Aspell::ConfigError] Raised when the configuration item
+ # does not exist.
+ #
+ def get_default(key)
+ unless key.respond_to?(:to_s)
+ raise(TypeError, 'Configuration keys should respond to #to_s()')
+ end
+
+ value = Aspell.config_retrieve_default(@config, key.to_s)
+
+ if value
+ return value
+ else
+ raise(ConfigError, "The configuration item #{key} does not exist")
+ end
+ end
+
+ ##
+ # Resets a configuration item to its default value.
+ #
+ # @since 13-04-2012
+ # @param [#to_s] key The name of the configuration item to reset.
+ # @raise [FFI::Aspell::ConfigError] Raised when the configuration item
+ # could not be reset.
+ #
+ def reset(key)
+ unless key.respond_to?(:to_s)
+ raise(TypeError, 'Configuration keys should respond to #to_s()')
+ end
+ unless Aspell.config_remove(@config, key.to_s)
+ raise(
+ ConfigError,
+ "The configuration item #{key} could not be reset, most likely " \
+ "it doesn't exist"
+ )
+ end
end
end # Speller
end # Aspell
View
@@ -1,15 +0,0 @@
-require File.expand_path('../../../helper', __FILE__)
-
-# Basic set of specs to verify if FFI was able to create all the required
-# functions.
-describe 'FFI::Aspell' do
- it 'All required C functions should exist' do
- FFI::Aspell.respond_to?(:config_new).should == true
- FFI::Aspell.respond_to?(:config_retrieve).should == true
- FFI::Aspell.respond_to?(:config_get_default).should == true
- FFI::Aspell.respond_to?(:config_replace).should == true
- FFI::Aspell.respond_to?(:config_remove).should == true
- FFI::Aspell.respond_to?(:speller_new).should == true
- FFI::Aspell.respond_to?(:speller_check).should == true
- end
-end
View
@@ -1,7 +1,98 @@
require File.expand_path('../../../helper', __FILE__)
describe 'FFI::Aspell::Speller' do
- it 'Create a new instance of FFI::Aspell::Speller' do
+ it 'Set the language in the constructor' do
+ FFI::Aspell::Speller.new.get('lang').should == 'en_US'
+ FFI::Aspell::Speller.new('en').get('lang').should == 'en'
+ FFI::Aspell::Speller.new('nl').get('lang').should == 'nl'
+ end
+
+ it 'Set options in the constructor' do
+ FFI::Aspell::Speller.new.get('personal').should == '.aspell.en_US.pws'
+
+ FFI::Aspell::Speller.new('en', :personal => 'foo') \
+ .get(:personal).should == 'foo'
+ end
+
+ it 'Raise when setting a non existing option' do
+ should.raise(FFI::Aspell::ConfigError) do
+ FFI::Aspell::Speller.new.set('foo', 'bar')
+ end
+ end
+
+ it 'Raise when retrieving a non existing option' do
+ should.raise(FFI::Aspell::ConfigError) do
+ FFI::Aspell::Speller.new.get('foo')
+ end
+ end
+
+ it 'Retrieve the default value of an option' do
+ FFI::Aspell::Speller.new.get_default('personal') \
+ .should == '.aspell.en_US.pws'
+ end
+
+ it 'Reset an option to its default value' do
+ speller = FFI::Aspell::Speller.new
+
+ speller.set('personal', 'foo')
+
+ speller.get('personal').should == 'foo'
+
+ speller.reset('personal')
+
+ speller.get('personal').should == '.aspell.en_US.pws'
+ end
+
+ it 'Validate various English words' do
speller = FFI::Aspell::Speller.new('en')
+
+ speller.correct?('cookie').should == true
+ speller.correct?('werld').should == false
+ speller.correct?('house').should == true
+ speller.correct?('huis').should == false
+ end
+
+ it 'Validate various Dutch words' do
+ speller = FFI::Aspell::Speller.new('nl')
+
+ speller.correct?('koekje').should == true
+ speller.correct?('werld').should == false
+ speller.correct?('huis').should == true
+ end
+
+ it 'Change the language of an existing speller object' do
+ speller = FFI::Aspell::Speller.new
+
+ speller.correct?('house').should == true
+ speller.correct?('huis').should == false
+
+ speller.set('lang', 'nl')
+
+ speller.correct?('house').should == true
+ speller.correct?('huis').should == true
+ end
+
+ it 'Use an English personal word list' do
+ speller = FFI::Aspell::Speller.new('en')
+
+ speller.correct?('github').should == false
+ speller.correct?('nodoc').should == false
+
+ speller.set(:personal, File.join(FIXTURES, 'personal.en.pws'))
+
+ speller.correct?('github').should == true
+ speller.correct?('nodoc').should == true
+ end
+
+ it 'Use a Dutch personal word list' do
+ speller = FFI::Aspell::Speller.new('nl')
+
+ speller.correct?('github').should == false
+ speller.correct?('nodoc').should == false
+
+ speller.set(:personal, File.join(FIXTURES, 'personal.nl.pws'))
+
+ speller.correct?('github').should == true
+ speller.correct?('nodoc').should == true
end
end
@@ -0,0 +1,3 @@
+personal_ws-1.1 en 2 utf-8
+github
+nodoc
@@ -0,0 +1,3 @@
+personal_ws-1.1 nl 2 utf-8
+github
+nodoc
View
@@ -3,3 +3,5 @@
Bacon.extend(Bacon::TapOutput)
Bacon.summary_on_exit
+
+FIXTURES = File.expand_path('../fixtures', __FILE__)

0 comments on commit f681fc0

Please sign in to comment.