Class: Yadriggy::Assert::Reason

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

Overview

Reason that an assertion fails.

Instance Method Summary collapse

Instance Method Details

#astASTnode

Gets the AST of the block given to #assertion.

Returns:

  • (ASTnode)

    an abstract syntax tree.



63
# File 'lib/yadriggy/assert.rb', line 63

def ast() @ast end

#resultsHash<ASTnode,Pair<String,Object>>

Gets the detailed results.

Returns:

  • (Hash<ASTnode,Pair<String,Object>>)

    a map from sub-expressions to their source and resulting vales. The sub-expressions are Yadriggy::ASTnode objects.



69
# File 'lib/yadriggy/assert.rb', line 69

def results() @results end

#setup(ast, results) ⇒ Object



56
57
58
59
# File 'lib/yadriggy/assert.rb', line 56

def setup(ast, results)
  @ast = ast
  @results = results
end

#showArray<String>

Gets the text showing the values of the sub-expressions.

Returns:

  • (Array<String>)

    an array of lines.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yadriggy/assert.rb', line 73

def show
  output = []
  header = show2(@ast, '', output)
  output << header
  src, value = @results[ast]
  if src.nil?
    pp = PrettyPrinter.new(Printer.new(2))
    pp.print(ast)
    output << pp.printer.output
  else
    output << src
  end
  output.reverse!
end

#show2(ast, header, output) ⇒ String

Returns the new header.

Returns:

  • (String)

    the new header.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/yadriggy/assert.rb', line 90

def show2(ast, header, output)
  if ast.is_a?(Paren)
    show2(ast.expression, header + ' ', output) + ' '
  elsif ast.is_a?(Call) && ast.block_arg.nil? && ast.block.nil?
    header2 = show2(ast.receiver, header, output)
    src2, value2 = @results[ast.receiver]
    src, value = @results[ast]
    if src.nil?
      src = PrettyPrinter.ast_to_s(ast)
      if src2.nil?
        "#{header}#{' ' * src.size}"
      else
        "#{header2}#{' ' * (src.size - src2.size)}"
      end
    else
      output << "#{header2} #{str_rep(value)}"
      "#{header2} |#{' ' * (src.size - src2.size - 2)}"
    end
  elsif ast.is_a?(Binary)
    header = show2(ast.left, header, output)
    src, value = @results[ast]
    if src.nil?
      header = header + '   '
    else
      output << "#{header} #{str_rep(value)}"
      header = "#{header} | #{' ' * (ast.op.to_s.size - 1)}"
    end
    show2(ast.right, header, output)
  elsif ast.is_a?(Unary)
    src, value = @results[ast]
    if src.nil?
      header = header + ' '
    else
      output << "#{header}#{str_rep(value)}"
      header = "#{header}|"
    end
    show2(ast.operand, header, output)
  else
    src, value = @results[ast]
    if src.nil?
      src = PrettyPrinter.ast_to_s(ast)
      return "#{header}#{' ' * src.size}"
    else
      output << header + str_rep(value)
      "#{header}|#{' ' * (src.size - 1)}"
    end
  end
end

#str_rep(v) ⇒ Object

Obtains the text representation of the given value.



141
142
143
144
145
146
147
148
149
# File 'lib/yadriggy/assert.rb', line 141

def str_rep(v)
  max = 70
  str = v.inspect
  if str.length < max
    str
  else
    str[0, max] + '...'
  end
end