/
tryouts.rb
168 lines (145 loc) · 4.06 KB
/
tryouts.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#require 'pathname'
#p Pathname(caller.last.split(':').first)
require 'ostruct'
class Tryouts
@debug = false
class Container; end
class Section < OpenStruct; end
class Expectation
attr_accessor :path, :line, :body, :expected_values, :desc, :container
attr_reader :results, :tests
attr_accessor :ret, :exp
def initialize(path, line, body, *expected_values)
@path, @line = path, line
@body, @expected_values = body, expected_values
@tests = []
end
def run_all
@results = expected_values.collect do |expectation|
sself = self
@container.class.module_eval do
sself.ret = eval(sself.body, binding, sself.path, sself.line)
sself.exp = eval(expectation, binding, sself.path, sself.line)
end
@tests << [ret, exp]
ret == exp
end
end
def success?
!@results.nil? && @results.uniq == [true]
end
end
class << self
attr_accessor :debug
def try path
lines = preparse(path)
source = parse(lines)
#puts $/, "Passed #{results.select { |obj| obj == true}.size} of #{tests.size}"
end
def preparse path
debug "Loading #{path}"
lines = File.readlines(path)
lines
end
def parse lines
lines.size.times do |idx|
line = lines[idx]
#debug('%-4d %s' % [idx, line])
if expectation? line
offset = 0
# TODO: grab all expectations
buffer, test, desc = [], [], []
while (idx-offset >= 0)
offset += 1
this_line = lines[idx-offset]
next if ignore?(this_line)
buffer.unshift this_line if comment?(this_line)
if test_content?(this_line)
test.unshift(*buffer) && buffer.clear
test.unshift this_line
end
if expectation?(this_line) || idx-offset == 0
desc.unshift *buffer
break
end
end
debug('---------------------------')
debug(*desc)
debug(*test)
debug(line)
end
end
end
private
def expectation? str
!ignore?(str) && str.strip.match(/^\#\s*=>/)
end
def comment? str
!str.strip.match(/^\#/).nil? && !expectation?(str)
end
def test_content? str
!ignore?(str) && !expectation?(str) && !comment?(str)
end
def ignore? str
str.strip.chomp.empty?
end
def msg *msg
STDOUT.puts *msg
end
def debug *msg
STDERR.puts *msg if @debug
end
end
module Console
# ANSI escape sequence numbers for text attributes
ATTRIBUTES = {
:normal => 0,
:bright => 1,
:dim => 2,
:underline => 4,
:blink => 5,
:reverse => 7,
:hidden => 8,
:default => 0,
}.freeze unless defined? ATTRIBUTES
# ANSI escape sequence numbers for text colours
COLOURS = {
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:default => 39,
:random => 30 + rand(10).to_i
}.freeze unless defined? COLOURS
# ANSI escape sequence numbers for background colours
BGCOLOURS = {
:black => 40,
:red => 41,
:green => 42,
:yellow => 43,
:blue => 44,
:magenta => 45,
:cyan => 46,
:white => 47,
:default => 49,
:random => 40 + rand(10).to_i
}.freeze unless defined? BGCOLOURS
def self.color(col, str)
'%s%s%s' % [style(col), str, default_style]
end
def self.style(col, bgcol=nil, att=nil)
valdor = []
valdor << COLOURS[col] if COLOURS.has_key?(col)
valdor << BGCOLOURS[bgcol] if BGCOLOURS.has_key?(bgcol)
valdor << ATTRIBUTES[att] if ATTRIBUTES.has_key?(att)
"\e[#{valdor.join(";")}m" # => \e[8;34;42m
end
def self.default_style
style(:default, :default, :default)
end
end
end