Class: Assert

Inherits:
Gloo::Core::Verb
  • Object
show all
Defined in:
lib/assert.rb

Overview

Assert that [it] is true.

Constant Summary collapse

KEYWORD =
'assert'.freeze
KEYWORD_SHORT =
'expect'.freeze
DEFAULT_MESSAGE =
'Assertion failed'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doc_dataObject

Get the verb's documentation data.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/assert.rb', line 64

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'Assert an expectation about the state or ' \
      'results. The verb looks at the value of it. If it is true, ' \
      'then the assertion passes, otherwise it fails.',
    :syntax => [ 'assert {optional expectation message}' ],
    :parameters => [
      "optional expectation message — A textual statement about " \
        "what was expected. Optional. If none provided the default " \
        "\"#{DEFAULT_MESSAGE}\" will be shown."
    ],
    :result => 'Validates an assumption and reports failures (or success).',
    :examples => <<~EXAMPLES.strip
      assert_noop [test] :
        description [string] : Assert no operation
        on_test [script] :
          noop
          assert 'noop should be true'
    EXAMPLES
  }
end

.keywordObject

Get the Verb's keyword.



13
14
15
# File 'lib/assert.rb', line 13

def self.keyword
  return KEYWORD
end

.keyword_shortcutObject

Get the Verb's keyword shortcut.



20
21
22
# File 'lib/assert.rb', line 20

def self.keyword_shortcut
  return KEYWORD_SHORT
end

Instance Method Details

#get_messageObject

Get the assertion message.



48
49
50
51
52
53
54
55
# File 'lib/assert.rb', line 48

def get_message
  if @tokens.token_count > 1
    expr = Gloo::Expr::Expression.new( @engine, @tokens.params )
    result = expr.evaluate
    return result
  end
  return DEFAULT_MESSAGE
end

#runObject

Run the verb.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/assert.rb', line 27

def run
  begin
    @engine.context_object.assert_count += 1
    if @engine.heap.it.is_true?
      # Assertion passes
      @engine.context_object.passed = true
      return true
    else
      # Assertion fails
      @engine.context_object.passed = false
      @engine.context_object.add_message get_message
      return false
    end
  rescue => ex
    @engine.log_exception ex
  end
end