class RSpecParser

Attributes

file[R]
test_cases[R]
verbose[RW]

Public Class Methods

new(file_name) click to toggle source
# File lib/files/RSpecParser.rb, line 20
def initialize(file_name)
  file = file_name
  @top_node = Parser::CurrentRuby.parse_file(file)
  @ids = []
  @metadata = {}
  @skips = []
  test_cases = []
  @skips_in_before = []
  @condition_set = false
  @indent_amount = "     "
  @in_testrail_id = false
end

Public Instance Methods

dump_ast() click to toggle source

Prints to standard out the full, unvarnished abstract syntax tree in all it's ugliness.

# File lib/files/RSpecParser.rb, line 53
def dump_ast
  print_ast(@top_node, "")
  puts ""
end
inspect() click to toggle source

Prints to standard out the test cases that were generated by parsing the file

# File lib/files/RSpecParser.rb, line 46
def inspect
  test_cases.each do |tc|
    puts tc.print
  end
end
parse(verbosity:false) click to toggle source

Performs the parsing of the rspec file and generates an array of TestCase instances

# File lib/files/RSpecParser.rb, line 34
def parse(verbosity:false)
  verbose = verbosity
  traverse(@top_node, "")
  # get the last one added to the array
  test_cases << @current_test_case.clone
  test_cases.each do |tc|
    tc.skip += @skips_in_before
  end
  puts "" if verbose
end

Private Instance Methods

check_node(node) click to toggle source
# File lib/files/RSpecParser.rb, line 59
def check_node(node)
  case node
    when :before
      @state = :example_body
      @in_before = true
    when :example
      @in_before = false
      if @state != :example
        test_cases << @current_test_case.clone if @current_test_case
        @ids.clear
        @metadata.clear
        @skips.clear
        @condition_set = false
        @title = nil
      end
      @current_test_case = TestRailOperations::TestCase.new(nil,   # test ID, don't know it yet
                                                            nil,   # title, don't know it yet
                                                            nil,   # priority, don't know it yet
                                                            true,  # automated, of course this is automated
                                                            nil,   # screen size, don't know it yet
                                                            true,  # is automatable if it is automated
                                                            nil,   # no Jira references
                                                            nil)   # no runonce references
      # get the title
      @state = :example_title
    when :skip
      if @condition_set
        # do nothing, the browsers have already been set.
      else
        @skips << "allbrowsers" # a skip call with no conditions. Skips on all browsers
      end

      if @in_before
        @skips_in_before += @skips.clone
      else
        @current_test_case.skip = @skips.clone
      end
      @state = :example_body
    when :testrail_id
      @state = :testrail_id
    when :priority
      @state = :priority
    when :ENV
      @state = :example_body
  end
end
check_node_string(node) click to toggle source
# File lib/files/RSpecParser.rb, line 106
def check_node_string(node)
  if @state == :example_title
    @title = node.children[0]
    @current_test_case.title = @title.clone
    @state = :example_args
  elsif @state == :example_body
    if (node.children[0] == "SELENIUM_BROWSER")
      @state = :selenium_browser
    end
  elsif @state == :selenium_browser
    browser_skip = node.children[0]
    @skips << browser_skip
    @state == :example_body
  elsif @state == :skip
    @state = :example_body
  elsif @state == :testrail_id
    id = node.children[0]
    @ids << id
  end
end
check_node_type(node) click to toggle source
# File lib/files/RSpecParser.rb, line 127
def check_node_type(node)
  case node.type
    when :if
      @condition_set = true
    when :str
      check_node_string(node)
    when :args
      @state == :example_body
    when :begin
      @state = :example_body
      if @ids.count > 0
        @metadata[:testrail_id] = @ids.clone
        @current_test_case.metadata = @metadata.clone
        @current_test_case.id = @ids.clone
      end
    when :int
      if @state == :priority
        @metadata[:priority] = node.children[0].to_i
        @current_test_case.priority = node.children[0].to_i
        @state = :example_args
      end
  end
end
print_ast(node, indent) click to toggle source

recursive function to dump the entire abstract syntax tree, and NOT the pretty print version which hides child elements

traverse(node, indent) click to toggle source

Recursive function to iterate through the direct acyclic graph of the abstract syntax tree (AST).

# File lib/files/RSpecParser.rb, line 152
def traverse(node, indent)
  check_node(node)
  check_node_type(node) if node.respond_to?(:type)
  return unless node.respond_to?(:children)
  node.children.each do |child|
    traverse(child, indent + @indent_amount)
  end
end