checkers 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/checkers.rb +215 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0f748a3a1883fe5465789e56d4a3bc896a61bfe
|
4
|
+
data.tar.gz: 28640d62f2a67fb4bd521b8aa61f7f1222f29686
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab35aeb2b54390a69eb6027a2b5e154f38346e0a92af6ed158470f2c6cd418c9e4f552301bcece07fe434b28e58d8297007dc7c1363db925551d0879c6119546
|
7
|
+
data.tar.gz: 504658c5617633e1fb06da29996457debc3cabefb1a384e1ce162ad414af53311b9892c231dc60db137e354f6827348525141cc466c20fc14075405889458af6
|
data/lib/checkers.rb
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
module Checkers
|
2
|
+
VERSION = '0.0.0'
|
3
|
+
|
4
|
+
module Sugar
|
5
|
+
def not; @inverted = !inverted; self; end
|
6
|
+
def is; self; end
|
7
|
+
def are; self; end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Checks
|
11
|
+
def empty
|
12
|
+
check subject.empty?, "#{subject.inspect} should be empty, but isn't"
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(o)
|
16
|
+
check subject == o, "#{subject.inspect} should be equal to #{o.inspect}, but isn't"
|
17
|
+
end
|
18
|
+
alias :equals :==
|
19
|
+
|
20
|
+
def matches(pattern)
|
21
|
+
check subject.match(pattern), "#{subject.inspect} should match with #{pattern.inspect}, but doesn't"
|
22
|
+
end
|
23
|
+
|
24
|
+
def includes(o)
|
25
|
+
check subject.include?(o), "#{subject.inspect} should include #{o.inspect}, but doesn't"
|
26
|
+
end
|
27
|
+
|
28
|
+
def raises(e)
|
29
|
+
subject.call
|
30
|
+
check false, "#{subject.inspect} should raise #{e.inspect}, doesn't"
|
31
|
+
rescue e
|
32
|
+
check true, "#{subject.inspect} should raise #{e.inspect}, doesn't"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class Check
|
38
|
+
include Checks, Sugar
|
39
|
+
attr_reader :subject, :caller, :message, :inverted, :run
|
40
|
+
|
41
|
+
def initialize(subject, caller, run = Checkers.run)
|
42
|
+
@subject = subject
|
43
|
+
@caller = caller
|
44
|
+
@message = ''
|
45
|
+
@inverted = false
|
46
|
+
@run = run
|
47
|
+
end
|
48
|
+
|
49
|
+
def check(condition, msg = 'Check failed.')
|
50
|
+
self.message = msg
|
51
|
+
condition = !condition if inverted
|
52
|
+
condition ? run.pass(self) : run.fail(self)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
attr_writer :message
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
class Checklist
|
61
|
+
attr_reader :name, :caller, :block, :error, :printer, :run
|
62
|
+
|
63
|
+
def initialize(name, caller, printer = Checkers.printer, run = Checkers.run, &block)
|
64
|
+
@name = name
|
65
|
+
@caller = caller
|
66
|
+
@block = block
|
67
|
+
@printer = printer
|
68
|
+
@run = run
|
69
|
+
end
|
70
|
+
|
71
|
+
def verify
|
72
|
+
printer.checklist self
|
73
|
+
instance_eval(&block)
|
74
|
+
rescue StandardError => e
|
75
|
+
@error = e
|
76
|
+
run.fail(self)
|
77
|
+
end
|
78
|
+
|
79
|
+
def check(subject)
|
80
|
+
Check.new(subject, caller)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
class Printer
|
86
|
+
COLORS = { red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, gray: 90 }
|
87
|
+
|
88
|
+
def color(color, string)
|
89
|
+
"\e[#{COLORS[color]}m#{string}\e[0m"
|
90
|
+
end
|
91
|
+
|
92
|
+
COLORS.each do |name, _|
|
93
|
+
define_method name, ->(string) { color(name, string) }
|
94
|
+
end
|
95
|
+
|
96
|
+
def pass(check)
|
97
|
+
STDOUT.print green '+'
|
98
|
+
end
|
99
|
+
|
100
|
+
def fail(check)
|
101
|
+
STDOUT.print red "F"
|
102
|
+
end
|
103
|
+
|
104
|
+
def checklist(checklist)
|
105
|
+
name = checklist.name ? blue(checklist.name) : blue(checklist.caller.first.split(":in").first)
|
106
|
+
STDOUT.print "\nChecking #{name}: "
|
107
|
+
end
|
108
|
+
|
109
|
+
def summary passes, failures
|
110
|
+
failed = failures.count
|
111
|
+
passed = passes.count
|
112
|
+
total = failed + passed
|
113
|
+
|
114
|
+
STDOUT.puts "\n\n\n\n#{blue total} checks verified: #{green passed} Passed #{red failed} Failed\n\n"
|
115
|
+
|
116
|
+
failures.each.with_index do |check, i|
|
117
|
+
STDOUT.print red("#{i}) ")
|
118
|
+
|
119
|
+
if check.is_a? Check
|
120
|
+
STDOUT.puts "#{check.message}" << gray(" (#{check.caller.first})")
|
121
|
+
elsif check.is_a? Checklist
|
122
|
+
name = check.name ? "'#{check.name}'" : ""
|
123
|
+
trace = format_trace(check.error.backtrace)
|
124
|
+
STDOUT.puts "In checklist #{ blue "#{name}" } #{ gray "(#{check.caller.first})" }: #{red check.error.message} \n\n#{trace}\n\n"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
STDOUT.puts green "All good!" if failures.empty? && total > 0
|
128
|
+
end
|
129
|
+
|
130
|
+
def format_trace(trace)
|
131
|
+
trace.join("\n")
|
132
|
+
.gsub(":in", " ")
|
133
|
+
.gsub(/`(.*)'/, "\\1") # method name
|
134
|
+
.gsub(/:(\d+)/, gray(":\\1")) # line number
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
class Run
|
140
|
+
attr_reader :failures, :passes, :printer
|
141
|
+
|
142
|
+
def initialize(printer)
|
143
|
+
@failures = []
|
144
|
+
@passes = []
|
145
|
+
@printer = printer
|
146
|
+
end
|
147
|
+
|
148
|
+
def pass(check)
|
149
|
+
passes << check
|
150
|
+
printer.pass check
|
151
|
+
end
|
152
|
+
|
153
|
+
def fail(check)
|
154
|
+
failures << check
|
155
|
+
printer.fail check
|
156
|
+
end
|
157
|
+
|
158
|
+
def failed?
|
159
|
+
!failures.empty?
|
160
|
+
end
|
161
|
+
|
162
|
+
def status
|
163
|
+
failed? ? 0 : 1
|
164
|
+
end
|
165
|
+
|
166
|
+
def summary
|
167
|
+
printer.summary passes, failures
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
## module methods
|
172
|
+
def self.before_blocks
|
173
|
+
@@before_blocks ||= []
|
174
|
+
end
|
175
|
+
|
176
|
+
def self.after_blocks
|
177
|
+
@@after_blocks ||= []
|
178
|
+
end
|
179
|
+
|
180
|
+
def self.before_checking(&block)
|
181
|
+
before_blocks << block
|
182
|
+
end
|
183
|
+
|
184
|
+
def self.after_checking(&block)
|
185
|
+
after_blocks << block
|
186
|
+
end
|
187
|
+
|
188
|
+
def self.run
|
189
|
+
@@run ||= Run.new printer
|
190
|
+
end
|
191
|
+
|
192
|
+
def self.printer=(printer)
|
193
|
+
@@printer = printer
|
194
|
+
end
|
195
|
+
|
196
|
+
def self.printer
|
197
|
+
@@printer ||= Printer.new
|
198
|
+
end
|
199
|
+
|
200
|
+
# extension
|
201
|
+
module Helper
|
202
|
+
def checklist(name = nil, &block)
|
203
|
+
Checkers.before_blocks.each(&:call)
|
204
|
+
Checklist.new(name, caller, &block).verify
|
205
|
+
Checkers.after_blocks.each(&:call)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
extend Helper
|
210
|
+
|
211
|
+
at_exit {
|
212
|
+
Checkers.run.summary
|
213
|
+
exit Checkers.run.status
|
214
|
+
}
|
215
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: checkers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fernando Martínez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A mini assertion libray + helpers for grouping the assertions + helpers
|
14
|
+
for printing results. With a bit of sugar on top.
|
15
|
+
email: fernando@templ.co
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/checkers.rb
|
21
|
+
homepage: http://rubygems.org/gems/checkers
|
22
|
+
licenses:
|
23
|
+
- Unlicense
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.5.1
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: A mini assertion libray
|
45
|
+
test_files: []
|