Class: Gauge::CodeParser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/code_parser.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.process_node(node, param_positions, new_param_values, new_step_text) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def self.process_node(node, param_positions, new_param_values, new_step_text)
  new_params = []
  args = step_args_from_code node
  param_positions.sort_by!(&:newPosition).each.with_index {|e, i|
    if e.oldPosition == -1
      param = Util.remove_special_chars new_param_values[e.newPosition].downcase.split.join('_')
      if param == ''
        param = i
      end
      new_params[e.newPosition] = "arg_#{param}"
    else
      new_params[e.newPosition] = args[e.oldPosition].children[0]
    end
  }
  args = new_params.map {|v| Parser::AST::Node.new(:arg, [v])}
  step = [node.children[0].children[0], node.children[0].children[1], Parser::AST::Node.new(:str, [new_step_text])]
  c1 = Parser::AST::Node.new(:send, step)
  c2 = Parser::AST::Node.new(:args, args)
  Parser::AST::Node.new(:block, [c1, c2, node.children[2]])
end

.refactor(step_info, param_positions, new_step) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
# File 'lib/code_parser.rb', line 81

def self.refactor(step_info, param_positions, new_step)
  ast = code_to_ast File.read(step_info[:locations][0][:file])
  refactor_args(step_info[:step_text], ast, param_positions, new_step.parameters, new_step.parameterizedStepValue)
end

.refactor_args(step_text, ast, param_positions, new_param_values, new_step_text) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
77
78
79
# File 'lib/code_parser.rb', line 70

def self.refactor_args(step_text, ast, param_positions, new_param_values, new_step_text)
  new_ast = replace ast do |node|
    if node.children[0].children[2].children[0] == step_text
      process_node(node, param_positions, new_param_values, new_step_text)
    else
      node
    end
  end
  Unparser.unparse new_ast
end

.replace(ast, &visitor) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/code_parser.rb', line 54

def self.replace(ast, &visitor)
  return ast if ast.class != Parser::AST::Node
  if ast && step_node?(ast)
    visitor.call(ast)
  else
    children = ast.children.map {|node|
      replace(node, &visitor)
    }
    return ast.updated(nil, children, nil)
  end
end

.step_args_from_code(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
31
# File 'lib/code_parser.rb', line 28

def self.step_args_from_code(ast)
  arg_node = ast.children[1]
  arg_node.children
end

.step_node?(node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


66
67
68
# File 'lib/code_parser.rb', line 66

def self.step_node?(node)
  node.type == :block && node.children[0].children.size > 2 && node.children[0].children[1] == :step
end