Class: MrubycTestCase

Inherits:
Object show all
Defined in:
lib/mrubyc-ext/mrubyc_test_case.rb,
lib/mrubyc_test_case/mrubyc_test_case.rb

Constant Summary collapse

@@description_locations =
{}
@@added_method_names =
{}
@@method_locations =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(information) ⇒ MrubycTestCase

Returns a new instance of MrubycTestCase.



2
3
4
5
6
7
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 2

def initialize(information)
  @information = information
  $mock ||= Mock.new
  @puts_success_message = true
  @puts_failure_message = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/mrubyc_test_case/mrubyc_test_case.rb', line 71

def method_missing(method_name, *args)
  case method_name
  when :stub, :mock
    location = caller_locations(1, 1)[0]
    Mrubyc::Test::Generator::Double.new(method_name, args[0], location)
  end
end

Class Method Details

.added_method_names ⇒ Object



40
41
42
# File 'lib/mrubyc_test_case/mrubyc_test_case.rb', line 40

def added_method_names
  (@@added_method_names[self] ||= {}).keys
end

.description(text) ⇒ Object Also known as: desc



49
50
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 49

def self.description(text)
end

.method_added(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mrubyc_test_case/mrubyc_test_case.rb', line 23

def method_added(name)
  return false if %i(method_missing setup teardown).include?(name)
  # puts "method '#{self}' '#{name}' '#{name.class}' was added"
  added_method_names = (@@added_method_names[self] ||= {})
  stringified_name = name.to_s
  location = caller_locations(1, 1)[0]
  path = location.absolute_path || location.path
  line = location.lineno
  location = {
    method_name: stringified_name,
    path: File.expand_path(path),
    line: line,
  }
  add_method_location(location)
  added_method_names[stringified_name] = true
end

Instance Method Details

#assert_equal(expected, actual, message = nil) ⇒ Object



37
38
39
40
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 37

def assert_equal(expected, actual, message = nil)
  assertion = :assert_equal
  actual == expected ? success(assertion, expected, actual) : failure(assertion, expected, actual, message)
end

#assert_not_equal(expected, actual, message = nil) ⇒ Object



41
42
43
44
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 41

def assert_not_equal(expected, actual, message = nil)
  assertion = :assert_not_equal
  actual != expected ? success(assertion, expected, actual) : failure(assertion, expected, actual, message)
end

#assert_not_nil(expression, message = nil) ⇒ Object



45
46
47
48
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 45

def assert_not_nil(expression, message = nil)
  assertion = :assert_not_nil
  expression != nil ? success(assertion, nil, expression) : failure(assertion, nil, expression, message)
end

#check_mock ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 60

def check_mock
  $mock.expected.keys.each do |key|
    $mock.actual[key] = 0 unless $mock.actual[key]
    if $mock.expected[key] > $mock.actual[key]
      failure(:mock, $mock.expected[key], $mock.actual[key], key.to_s + ' shoud have been called at least expected times')
    else
      success(:mock, $mock.expected[key], $mock.actual[key])
    end
  end
end

#failure(assertion, expected, actual, message) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 22

def failure(assertion, expected, actual, message)
  puts_information
  $failure_count += 1
  if @puts_failure_message
    puts $colors[:failure] + '  path       : ' + @information[:path].to_s
    puts $colors[:failure] + '  line       : ' + @information[:line].to_s
    puts $colors[:failure] + '  description: ' + @information[:description].to_s
    puts $colors[:failure] + '  ' + message if message
    puts $colors[:failure] + '  assertion  : ' + assertion.to_s + $colors[:normal]
    puts $colors[:failure] + '  expected   : ' + expected.to_ss + $colors[:normal]
    puts $colors[:failure] + '  actual     : ' + actual.to_ss + $colors[:normal]
  else
    print $colors[:failure] + '.' + $colors[:normal]
  end
end

#puts_information ⇒ Object



8
9
10
11
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 8

def puts_information
  puts
  puts @information[:test_class_name] + '#' + @information[:method_name]
end

#setup ⇒ Object



53
54
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 53

def setup
end

#stub(object) ⇒ Object



57
58
59
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 57

def stub(object)
  object
end

#success(assertion, expected, actual) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 12

def success(assertion, expected, actual)
  puts_information
  $success_count += 1
  if @puts_success_message
    puts $colors[:success] + '  assertion : ' + assertion.to_s + $colors[:normal]
    puts $colors[:success] + '  result    : ' + actual.to_ss + $colors[:normal]
  else
    print $colors[:success] + '.' + $colors[:normal]
  end
end

#teardown ⇒ Object



55
56
# File 'lib/mrubyc-ext/mrubyc_test_case.rb', line 55

def teardown
end