Module: TestProf::RSpecStamp::Parser

Defined in:
lib/test_prof/rspec_stamp/parser.rb

Overview

Parse examples headers

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.parse(code) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/test_prof/rspec_stamp/parser.rb', line 33

def parse(code)
  sexp = Ripper.sexp(code)
  return unless sexp

  # sexp has the following format:
  # [:program,
  #   [
  #     [
  #       :command,
  #       [:@ident, "it", [1, 0]],
  #       [:args_add_block, [ ... ]]
  #     ]
  #   ]
  # ]
  #
  # or
  #
  # [:program,
  #   [
  #     [
  #       :vcall,
  #       [:@ident, "it", [1, 0]]
  #     ]
  #   ]
  # ]
  res = Result.new

  fcall = sexp[1][0][1]
  args_block = sexp[1][0][2]

  if fcall.first == :fcall
    fcall = fcall[1]
  elsif fcall.first == :var_ref
    res.fname = [parse_const(fcall), sexp[1][0][3][1]].join(".")
    args_block = sexp[1][0][4]
  end

  res.fname ||= fcall[1]

  return res if args_block.nil?

  args_block = args_block[1] if args_block.first == :arg_paren

  args = args_block[1]

  if args.first.first == :string_literal
    res.desc = parse_literal(args.shift)
  elsif args.first.first == :var_ref || args.first.first == :const_path_ref
    res.desc_const = parse_const(args.shift)
  end

  parse_arg(res, args.shift) until args.empty?

  res
end