-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.cr
47 lines (43 loc) · 1.43 KB
/
shared.cr
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
require "colorize"
module Assertions
@@assertion_hashes = [] of UInt64
@@counts = [] of UInt64
def self.hashes
@@assertion_hashes
end
def self.counts
@@counts
end
end
macro assert(code)
hash = {{code.stringify}}.hash
file_loc = "{{code.filename.gsub(/^.+\//,"").id}}:{{code.line_number}} "
if ({{code}})
{% if code.is_a?(Call) %}
string = ("* " + file_loc).colorize(:green).to_s + {{code.stringify}} + (" value: " + ({{code.receiver}}).inspect).colorize.mode(:dim).to_s
{% else %}
string = ("* " + file_loc).colorize(:green).to_s + {{code.stringify}} + (" value: " + ({{code}}).to_s).colorize.mode(:dim).to_s
{% end %}
if index = Assertions.hashes.index(hash)
lines = Assertions.hashes.size - index
print "\033[s\033[#{lines}A\033[K",string," (#{Assertions.counts[index]} more)", "\033[u"
Assertions.counts[index] += 1
else
puts string
Assertions.hashes << hash
Assertions.counts << 1u64
end
else
{% if code.is_a?(Call) %}
string = ("* " + file_loc).colorize(:red).to_s + {{code.stringify}} + (" value: " + ({{code.receiver}}).inspect).colorize.mode(:dim).to_s
{% else %}
string = ("* " + file_loc).colorize(:red).to_s + {{code.stringify}} + (" value: " + ({{code}}).to_s).colorize.mode(:dim).to_s
{% end %}
puts string
Assertions.hashes.clear
Assertions.counts.clear
if ENV["DEBUG"]? == "1"
debugger
end
end
end