[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fwd: Ruby Quiz #103: DictionaryMatcher

James Gray

11/27/2006 4:25:00 AM

Begin forwarded message:

> From: Adam <skidooer@gmail.com>
> Date: November 25, 2006 1:13:25 AM CST
> To: submission@rubyquiz.com
> Subject: Ruby Quiz #103: DictionaryMatcher
>
> class DictionaryMatcher < Array
> attr_reader :options
>
> def initialize(words = [], options = nil, lang = nil)
> @options, @kcode = options, lang
> super words
> end
>
> def =~(obj)
> collect { |word| obj =~ Regexp.new(word, @options,
> @kcode) }.compact.first
> end
> alias_method :===, :=~
> alias_method :match, :=~
>
> def casefold?
> options & Regexp::IGNORECASE
> end
>
> def kcode
> Regexp.new('', @options, @kcode).kcode
> end
>
> def to_s
> join '|'
> end
> alias_method :source, :to_s
>
> def inspect
> Regexp.new('REPLACE', @options, @kcode).inspect.sub('REPLACE',
> to_s)
> end
> end
>
> class String
> alias_method :match_object, :=~
>
> def =~(obj)
> result = match_object(obj)
> obj.is_a?(DictionaryMatcher) ? !result.nil? : result
> end
> end
>
> ------
>
> require 'test/unit'
> require 'dictionary_matcher'
>
> class DictionaryMatcherTest < Test::Unit::TestCase
> def setup
> @dictionary = DictionaryMatcher.new
> @dictionary << "string"
> @dictionary << "Ruby"
> end
>
> def test_added_terms
> assert @dictionary.include?("Ruby")
> assert ! @dictionary.include?("missing")
> assert ! @dictionary.include?("stringing you along")
> end
>
> def test_regexp_like_seach
> assert_equal 5, @dictionary =~ "long string"
> assert_equal nil, @dictionary =~ "rub you the wrong way"
> assert_equal true, "long string" =~ @dictionary
> end
>
> def test_case_sesitive_match
> assert ! @dictionary.casefold?
> assert_not_equal 5, @dictionary =~ "lONg sTrINg"
> assert_not_equal true, "lonG STRing" =~ @dictionary
> end
>
> def test_case_insesitive_match
> @dictionary.instance_variable_set('@options', Regexp::IGNORECASE)
>
> assert @dictionary.casefold?
> assert_equal 5, @dictionary =~ "lONg sTrINg"
> assert_equal true, "lonG STRing" =~ @dictionary
> end
>
> def test_new_dictionary_matcher_with_options
> dictionary = DictionaryMatcher.new(%W( here be words ),
> Regexp::EXTENDED | Regexp::IGNORECASE, 'u')
>
> assert dictionary
> assert_equal %W( words be here ).sort, dictionary.sort
> assert_equal Regexp::EXTENDED | Regexp::IGNORECASE,
> dictionary.options
> assert_equal 'utf8', dictionary.kcode
> end
>
> def test_regexp_compatibility_methods
> assert @dictionary.eql?(@dictionary)
> assert ! @dictionary.casefold?
> assert @dictionary.kcode.nil?
> assert @dictionary.options.nil?
> assert_equal @dictionary.to_s, @dictionary.source
> assert_equal 'string|Ruby', @dictionary.source
> assert_equal '/string|Ruby/', @dictionary.inspect
> end
> end