Class: Transpec::Syntax::Have::SourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/transpec/syntax/have/source_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(have, size_source) ⇒ SourceBuilder

Returns a new instance of SourceBuilder.



11
12
13
14
# File 'lib/transpec/syntax/have/source_builder.rb', line 11

def initialize(have, size_source)
  @have = have
  @size_source = size_source
end

Instance Attribute Details

#haveObject (readonly)

Returns the value of attribute have.



9
10
11
# File 'lib/transpec/syntax/have/source_builder.rb', line 9

def have
  @have
end

#size_sourceObject (readonly)

Returns the value of attribute size_source.



9
10
11
# File 'lib/transpec/syntax/have/source_builder.rb', line 9

def size_source
  @size_source
end

Instance Method Details

#base_subject_source(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/transpec/syntax/have/source_builder.rb', line 38

def base_subject_source(node)
  if node.send_type? && (arg_node = node.children[2])
    left_of_arg_source = node.loc.selector.end.join(arg_node.loc.expression.begin).source

    if left_of_arg_source.match(/\A\s*\Z/)
      source = node.loc.expression.begin.join(node.loc.selector.end).source
      source << '('
      source << arg_node.loc.expression.begin.join(node.loc.expression.end).source
      source << ')'
      return source
    end
  end

  node.loc.expression.source
end

#collection_accessor_args_body_sourceObject



92
93
94
95
96
# File 'lib/transpec/syntax/have/source_builder.rb', line 92

def collection_accessor_args_body_source
  arg_nodes = have.items_node.children[2..-1]
  range = arg_nodes.first.loc.expression.begin.join(arg_nodes.last.loc.expression.end)
  range.source
end

#collection_accessor_args_parentheses_sourceObject



86
87
88
89
90
# File 'lib/transpec/syntax/have/source_builder.rb', line 86

def collection_accessor_args_parentheses_source
  map = have.items_node.loc
  range = map.selector.end.join(map.expression.end)
  range.source
end

#replacement_matcher_source(parenthesize_arg = true) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/transpec/syntax/have/source_builder.rb', line 54

def replacement_matcher_source(parenthesize_arg = true)
  case have.expectation.current_syntax_type
  when :should
    replacement_matcher_source_for_should
  when :expect
    replacement_matcher_source_for_expect(parenthesize_arg)
  end
end

#replacement_matcher_source_for_expect(parenthesize_arg) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/transpec/syntax/have/source_builder.rb', line 71

def replacement_matcher_source_for_expect(parenthesize_arg)
  case have.method_name
  when :have, :have_exactly
    if parenthesize_arg
      "eq(#{size_source})"
    else
      "eq #{size_source}"
    end
  when :have_at_least
    "be >= #{size_source}"
  when :have_at_most
    "be <= #{size_source}"
  end
end

#replacement_matcher_source_for_shouldObject



63
64
65
66
67
68
69
# File 'lib/transpec/syntax/have/source_builder.rb', line 63

def replacement_matcher_source_for_should
  case have.method_name
  when :have, :have_exactly then "== #{size_source}"
  when :have_at_least       then ">= #{size_source}"
  when :have_at_most        then "<= #{size_source}"
  end
end

#replacement_subject_source(base_subject) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/transpec/syntax/have/source_builder.rb', line 16

def replacement_subject_source(base_subject)
  source = case base_subject
           when String    then base_subject
           when AST::Node then base_subject_source(base_subject)
           else fail "Invalid base subject #{base_subject}"
           end

  if have.subject_is_owner_of_collection?
    if have.collection_accessor_is_private?
      source << ".send(#{have.collection_accessor.inspect}"
      if have.items_method_has_arguments?
        source << ", #{collection_accessor_args_body_source}"
      end
      source << ')'
    else
      source << ".#{have.collection_accessor}#{collection_accessor_args_parentheses_source}"
    end
  end

  source << ".#{have.query_method}"
end