Class: Cucumber::Formatter::AstLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/formatter/ast_lookup.rb

Defined Under Namespace

Classes: KeywordLookupBuilder, KeywordSearchNode, ScenarioOutlineSource, ScenarioSource, StepSource, TestCaseLookupBuilder, TestStepLookupBuilder

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AstLookup

Returns a new instance of AstLookup.


6
7
8
9
10
11
12
# File 'lib/cucumber/formatter/ast_lookup.rb', line 6

def initialize(config)
  @gherkin_documents = {}
  @test_case_lookups = {}
  @test_step_lookups = {}
  @step_keyword_lookups = {}
  config.on_event :gherkin_source_parsed, &method(:on_gherkin_source_parsed)
end

Instance Method Details

#gherkin_document(uri) ⇒ Object


18
19
20
# File 'lib/cucumber/formatter/ast_lookup.rb', line 18

def gherkin_document(uri)
  @gherkin_documents[uri]
end

#on_gherkin_source_parsed(event) ⇒ Object


14
15
16
# File 'lib/cucumber/formatter/ast_lookup.rb', line 14

def on_gherkin_source_parsed(event)
  @gherkin_documents[event.gherkin_document.uri] = event.gherkin_document
end

#scenario_source(test_case) ⇒ Object


22
23
24
25
26
# File 'lib/cucumber/formatter/ast_lookup.rb', line 22

def scenario_source(test_case)
  uri = test_case.location.file
  @test_case_lookups[uri] ||= TestCaseLookupBuilder.new(gherkin_document(uri)).lookup_hash
  @test_case_lookups[uri][test_case.location.lines.max]
end

#snippet_step_keyword(test_step) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cucumber/formatter/ast_lookup.rb', line 34

def snippet_step_keyword(test_step)
  uri = test_step.location.file
  document = gherkin_document(uri)
  dialect = ::Gherkin::Dialect.for(document.feature.language)
  given_when_then_keywords = [dialect.given_keywords, dialect.when_keywords, dialect.then_keywords].flatten.uniq.reject { |kw| kw == '* ' }
  keyword_lookup = step_keyword_lookup(uri)
  keyword = nil
  node = keyword_lookup[test_step.location.lines.min]
  while keyword.nil?
    if given_when_then_keywords.include?(node.keyword)
      keyword = node.keyword
      break
    end
    break if node.previous_node.nil?
    node = node.previous_node
  end
  keyword = dialect.given_keywords.reject { |kw| kw == '* ' }[0] if keyword.nil?
  keyword = Cucumber::Gherkin::I18n.code_keyword_for(keyword)
  keyword
end

#step_source(test_step) ⇒ Object


28
29
30
31
32
# File 'lib/cucumber/formatter/ast_lookup.rb', line 28

def step_source(test_step)
  uri = test_step.location.file
  @test_step_lookups[uri] ||= TestStepLookupBuilder.new(gherkin_document(uri)).lookup_hash
  @test_step_lookups[uri][test_step.location.lines.min]
end