-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
71 lines (59 loc) · 2.1 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
require "test/unit"
require 'active_support'
# ============================ Test suite to test locally codewars
# To run type in shell i.e. `ruby prodenum/spec.rb`
# In that file require_relative ../test.rb and your solution + paste specs to Codewars module
module Codewars
def self.method_missing(method_name, *args)
[method_name, *args]
end
class SolutionKlass
include Solution
end
class Test < Test::Unit::TestCase
GREEN_COLOR = "\e[0;32m".freeze
RED_COLOR = "\e[0;31m".freeze
FINISH_COLOR = "\e[0m".freeze
alias :assert_equals :assert_equal
def initialize(*args)
@time_start = Time.now
@solution = SolutionKlass.new
super
end
TOO_LONG_SUITE_RUN_ERROR_MESSAGE = 'TESTS ARE RUNNING TOO LONG. 12 seconds is max'
def setup
if Time.now - @time_start > 12 # seconds
self.instance_variable_get(:@internal_data).interrupted
raise TOO_LONG_SUITE_RUN_ERROR_MESSAGE
end
end
def teardown
if !self.instance_variable_get(:@internal_data).interrupted? && Time.now - @time_start > 12
self.instance_variable_get(:@internal_data).interrupted
raise TOO_LONG_SUITE_RUN_ERROR_MESSAGE
end
end
def self.describe(description, &block)
@@description = description
puts 'Testing ' + description
block.call
@@description = ''
end
def self.it(description, &block)
@@it_description = description
begin
block.call
rescue => e
puts "#{RED_COLOR} Error occured: #{e} #{FINISH_COLOR}", e.backtrace[0..1]
end
@@it_description = ''
end
def self.assert_equals(test, expected, description = nil)
name = Random.urandom(10).chars.map { |c| (c.ord % 25 + 97).chr }.join
test_description = [@@description, @@it_description, description, test[1..-1]].compact.join(': ')
define_method('test_' + test[0].to_s + '_' + test[1].to_s) { assert_equals(@solution.public_send(test[0], *test[1..-1]), expected, test_description) }
rescue => e
puts "#{RED_COLOR} Error occured: #{e} #{FINISH_COLOR}", e.backtrace[0..1]
end
end
end