Module: Spec::Matchers

Defined in:
lib/spec-i18n/matchers/be.rb,
lib/spec-i18n/matchers/method_missing.rb,
lib/spec-i18n/matchers/register_all_matchers.rb,
lib/spec-i18n/matchers/translate_basic_matchers.rb

Constant Summary collapse

RSPEC_MATCHERS =
[:be_close, :be_an_instance_of, :be_a_kind_of,
:eql, :equal, :exist, :have, :have_at_least, 
:have_at_most, :have_exactly, :include, :match, 
:raise_error, :satisfy ]
MATCHERS_WITH_QUESTIONS =
[ :eql, :equal ]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Method Missing that returns Predicate for the respective sym word Search the translate be word in the languages.yml for the rspec-i18n try to comunicate with the Rspec



8
9
10
11
12
13
14
# File 'lib/spec-i18n/matchers/method_missing.rb', line 8

def method_missing(sym, *args, &block) # :nodoc:\
  matchers = natural_language.keywords['matchers']
  be_word = matchers['be'] if matchers
  sym = be_to_english(sym, be_word)
  return Matchers::BePredicate.new(sym, *args, &block) if be_predicate?(sym)
  return Matchers::Has.new(sym, *args, &block) if have_predicate?(sym)
end

Class Method Details

.matcher_be_some(option) ⇒ Object



52
53
54
# File 'lib/spec-i18n/matchers/be.rb', line 52

def matcher_be_some(option)
  natural_language.word_be(option.to_s).collect { |word| word.to_sym}
end

.register_all_matchersObject



4
5
6
7
8
9
10
11
# File 'lib/spec-i18n/matchers/register_all_matchers.rb', line 4

def self.register_all_matchers
  ["true", "false", "nil", "empty", "matcher"].each do |matcher|
    class_eval <<-BE_WORD
      translate_be_#{matcher}
    BE_WORD
  end
  translate_basic_matchers
end

.translate_basic_matchersObject

Translate all the basic matcher for the Natural Language



14
15
16
17
18
19
# File 'lib/spec-i18n/matchers/translate_basic_matchers.rb', line 14

def translate_basic_matchers
  RSPEC_MATCHERS.each do |rspec_matcher|
    matcher = natural_language.find_matcher(rspec_matcher)
    translate_matcher(matcher, rspec_matcher)
  end
end

.translate_be_emptyObject



42
43
44
45
46
47
48
49
50
# File 'lib/spec-i18n/matchers/be.rb', line 42

def translate_be_empty
  matcher_be_some(:empty).each do |matcher|
    Spec::Matchers.define(matcher) do
      match do |actual|
        actual.empty?
      end
    end
  end
end

.translate_be_falseObject



22
23
24
25
26
27
28
29
30
# File 'lib/spec-i18n/matchers/be.rb', line 22

def translate_be_false
  matcher_be_some(:false).each do |matcher|
    Spec::Matchers.define(matcher) do
      match do |actual|
        !actual
      end
    end
  end
end

.translate_be_matcherObject



8
9
10
# File 'lib/spec-i18n/matchers/be.rb', line 8

def translate_be_matcher
  natural_language.keywords_of_be_word.collect { |be_value| alias_method be_value, :be }
end

.translate_be_nilObject



32
33
34
35
36
37
38
39
40
# File 'lib/spec-i18n/matchers/be.rb', line 32

def translate_be_nil
  matcher_be_some(:nil).each do |matcher|
    Spec::Matchers.define(matcher) do
      match do |actual|
        actual.nil?
      end
    end
  end
end

.translate_be_trueObject



12
13
14
15
16
17
18
19
20
# File 'lib/spec-i18n/matchers/be.rb', line 12

def translate_be_true
  matcher_be_some(:true).each do |matcher|
    Spec::Matchers.define(matcher) do
      match do |actual|
        !!actual
      end
    end
  end
end

.translate_matcher(matcher, rspec_matcher) ⇒ Object

Translate a matcher from a rspec_matcher

Example:

 pt:
   matchers:
     be_close: estar_proximo
     equal: igual

rspec_matcher = :be_close  ->  matcher = { "be_close" => ['estar_proximo']}

rspec_matcher = :equal     ->  matcher = { "equal"    => ['igual']}

and so on ...


35
36
37
38
39
40
# File 'lib/spec-i18n/matchers/translate_basic_matchers.rb', line 35

def translate_matcher(matcher, rspec_matcher)
  matcher[rspec_matcher.to_s].collect do |matcher_value|
    translate_matcher_with_question(matcher_value, rspec_matcher)
    alias_method matcher_value, rspec_matcher
  end
end

.translate_matcher_with_question(matcher_value, rspec_matcher) ⇒ Object

Translate a method with the ‘?’



45
46
47
48
49
# File 'lib/spec-i18n/matchers/translate_basic_matchers.rb', line 45

def translate_matcher_with_question(matcher_value, rspec_matcher)
  Object.class_eval do
    alias_method "#{matcher_value}?", "#{rspec_matcher}?" if MATCHERS_WITH_QUESTIONS.include?(rspec_matcher)
  end
end

Instance Method Details

#be_predicate?(sym) ⇒ Boolean

Return something true(equivalent) for Be Predicate

Returns:

  • (Boolean)


18
19
20
# File 'lib/spec-i18n/matchers/method_missing.rb', line 18

def be_predicate?(sym)
  sym.to_s =~ /^be_/
end

#be_to_english(sym, be_word) ⇒ Object Also known as: to_english

Transform the word in be for rspec work properly

be_to_english(:ser_feliz, :ser) # => :be_feliz be_to_english(:estar_incluso, :estar) # => :be_incluso be_to_english(:ser_feliz, ‘ser|estar’) # => :be_feliz



34
35
36
37
# File 'lib/spec-i18n/matchers/method_missing.rb', line 34

def be_to_english(sym, be_word)
  be_word = be_word || 'be' 
  sym.to_s.gsub(/#{be_word}/, 'be').to_sym
end

#have_predicate?(sym) ⇒ Boolean

Return something true(equivalent) for Have predicate

Returns:

  • (Boolean)


24
25
26
# File 'lib/spec-i18n/matchers/method_missing.rb', line 24

def have_predicate?(sym)
  sym.to_s =~ /^have_/
end