blob: 754fb205e26682b284483b56bf897c7602778f16 (
plain)
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
|
require 'rspec/expectations'
def comment_lines(file_path)
File.open(file_path, 'r').each_line.select { |l| l.chars.first == '#' }
end
def data_lines(file_path)
File.open(file_path, 'r').each_line.select { |l| l.chars.first != '#' }
end
RSpec::Matchers.define :be_a_regular_file do
match do |actual|
File.file?(actual)
end
end
RSpec::Matchers.define :be_an_empty_directory do
match do |actual|
Dir.empty?(actual)
end
end
RSpec::Matchers.define :be_a_valid_results_file do
match do |actual|
begin
comment_lines = comment_lines(actual)
data_lines = data_lines(actual)
# It must have 3 lines: 2 comments and >= 1 data line
return false if comment_lines.size != 2 || data_lines.size < 1
# Data line must consist of non-negative numbers and array 1 and 0
data_lines.each do |l|
l.scan(/\[[^\]]*\]/).first.tr('[]', '').split(',').map { |n| n.to_i }.each do |i|
return false if i != 0 && i != 1
end
l.scan(/[0-9\.]+/).map { |n| n.to_f }.each do |i|
return false if i < 0
end
end
true
rescue
false
end
end
end
RSpec::Matchers.define :be_a_equal_to_results_file do |good|
match do |actual|
begin
lines = data_lines(actual)
good_lines = data_lines(good)
return false if lines.size != good_lines.size
lines.each_with_index do |l, i|
# Check price and configuration
return false if l.split(']').first != good_lines[i].split(']').first
# Check relative error
return false if l.split().last != good_lines[i].split().last
return false if l.split().size != good_lines[i].split().size
end
true
rescue
false
end
end
end
RSpec::Matchers.define :be_a_equal_to_stats_file do |good|
match do |actual|
begin
lines = data_lines(actual)
good_lines = data_lines(good)
return false if lines.size != good_lines.size
lines.each_with_index do |l, i|
# Check average price
return false if l.split().first != good_lines[i].split().first
# Check average relative error
return false if l.split().last != good_lines[i].split().last
return false if l.split().size != good_lines[i].split().size
end
true
rescue
false
end
end
end
RSpec::Matchers.define :be_a_valid_stats_file do
match do |actual|
begin
comment_lines = comment_lines(actual)
data_lines = data_lines(actual)
# It must have 3 lines: 2 comments and 1 data line
return false if comment_lines.size != 2 || data_lines.size != 1
# Data line must consist of non-negative numbers
data_lines.first.split.map { |i| i.to_f }.each do |n|
return false if n < 0
end
true
rescue
false
end
end
end
|