Class: Cucumber::Parser::NaturalLanguage

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

Constant Summary collapse

KEYWORD_KEYS =
%w{name native encoding space_after_keyword feature background scenario scenario_outline examples given when then and but}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step_mother, lang) ⇒ NaturalLanguage

Returns a new instance of NaturalLanguage.



16
17
18
19
20
21
# File 'lib/cucumber/parser/natural_language.rb', line 16

def initialize(step_mother, lang)
  @keywords = Cucumber::LANGUAGES[lang]
  raise "Language not supported: #{lang.inspect}" if @keywords.nil?
  @keywords['grammar_name'] = @keywords['name'].gsub(/\s/, '')
  register_adverbs(step_mother) if step_mother
end

Class Method Details

.get(step_mother, lang) ⇒ Object



7
8
9
# File 'lib/cucumber/parser/natural_language.rb', line 7

def get(step_mother, lang)
  languages[lang] ||= new(step_mother, lang)
end

.inspectObject



36
37
38
# File 'lib/cucumber/parser/natural_language.rb', line 36

def @parser.inspect
  "#<#{self.class.name}>"
end

.languagesObject



11
12
13
# File 'lib/cucumber/parser/natural_language.rb', line 11

def languages
  @languages ||= {}
end

Instance Method Details

#and_keywordsObject



67
68
69
# File 'lib/cucumber/parser/natural_language.rb', line 67

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

#but_keywordsObject



63
64
65
# File 'lib/cucumber/parser/natural_language.rb', line 63

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

#incomplete?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cucumber/parser/natural_language.rb', line 55

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

#keywords(key, raw = false) ⇒ Object



48
49
50
51
52
53
# File 'lib/cucumber/parser/natural_language.rb', line 48

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

#parse(source, path, filter) ⇒ Object



42
43
44
45
46
# File 'lib/cucumber/parser/natural_language.rb', line 42

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

#parserObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cucumber/parser/natural_language.rb', line 28

def parser
  return @parser if @parser
  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
  def @parser.inspect
    "#<#{self.class.name}>"
  end
  @parser
end

#register_adverbs(step_mother) ⇒ Object



23
24
25
26
# File 'lib/cucumber/parser/natural_language.rb', line 23

def register_adverbs(step_mother)
  adverbs = %w{given when then and but}.map{|keyword| @keywords[keyword].split('|').map{|w| w.gsub(/\s/, '')}}.flatten
  step_mother.register_adverbs(adverbs) if step_mother
end

#scenario_keywordObject



59
60
61
# File 'lib/cucumber/parser/natural_language.rb', line 59

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