Class: Cucumber::Parser::I18n::Language

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/parser/i18n/language.rb

Constant Summary collapse

KEYWORD_KEYS =
%w{name native encoding feature background scenario scenario_outline examples given when then but}
LANGUAGES =
Hash.new{|h,k| h[k] = Language.new(k)}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lang) ⇒ Language

Returns a new instance of Language.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cucumber/parser/i18n/language.rb', line 37

def initialize(lang)
  @keywords = Cucumber::LANGUAGES[lang]
  raise "Language not supported: #{lang.inspect}" if @keywords.nil?
  @keywords['grammar_name'] = @keywords['name'].gsub(/\s/, '')
  i18n_tt = File.expand_path(File.dirname(__FILE__) + '/../i18n.tt')
  template = File.open(i18n_tt, Cucumber.file_mode('r')).read
  erb = ERB.new(template)
  grammar = erb.result(binding)
  Treetop.load_from_string(grammar)
  @parser = Parser::I18n.const_get("#{@keywords['grammar_name']}Parser").new
  self.class.alias_step_definitions(@keywords)
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



35
36
37
# File 'lib/cucumber/parser/i18n/language.rb', line 35

def parser
  @parser
end

Class Method Details

.[](key) ⇒ Object



10
11
12
# File 'lib/cucumber/parser/i18n/language.rb', line 10

def [](key)
  LANGUAGES[key]
end

.alias_step_definitions(keywords) ⇒ Object

:nodoc:



14
15
16
17
# File 'lib/cucumber/parser/i18n/language.rb', line 14

def alias_step_definitions(keywords) #:nodoc:
  all_keywords = %w{given when then and but}.map{|keyword| keywords[keyword].split('|')}.flatten
  alias_steps(all_keywords)
end

.alias_steps(keywords) ⇒ Object

Sets up additional method aliases for Given, When and Then. This does not affect how feature files are parsed. If you want to create aliases in the parser, you have to do this in languages.yml. For example:

and: And|With



25
26
27
28
29
30
# File 'lib/cucumber/parser/i18n/language.rb', line 25

def alias_steps(keywords)
  keywords.each do |adverb|
    StepMother.alias_adverb(adverb)
    World.alias_adverb(adverb)
  end
end

Instance Method Details

#and_keywordsObject



74
75
76
# File 'lib/cucumber/parser/i18n/language.rb', line 74

def and_keywords
  @keywords['and'].split('|')
end

#but_keywordsObject



70
71
72
# File 'lib/cucumber/parser/i18n/language.rb', line 70

def but_keywords
  @keywords['but'].split('|')
end

#keywords(key, raw = false) ⇒ Object



56
57
58
59
60
# File 'lib/cucumber/parser/i18n/language.rb', line 56

def keywords(key, raw=false)
  return @keywords[key] if raw
  values = @keywords[key].split('|')
  values.map{|value| "'#{value}'"}.join(" / ")
end

#language_incomplete?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/cucumber/parser/i18n/language.rb', line 62

def language_incomplete?
  KEYWORD_KEYS.detect{|key| @keywords[key].nil?}
end

#parse(source, path, filter) ⇒ Object



50
51
52
53
54
# File 'lib/cucumber/parser/i18n/language.rb', line 50

def parse(source, path, filter)
  feature = @parser.parse_or_fail(source, path, filter)
  feature.language = self if feature
  feature
end

#scenario_keywordObject



66
67
68
# File 'lib/cucumber/parser/i18n/language.rb', line 66

def scenario_keyword
  @keywords['scenario'].split('|')[0] + ':'
end