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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/squcumber-postgres/support/matchers.rb', line 84
def convert_mock_value(value)
value_parser_regexp = /\s*((?<modifier>(beginning|end))\s+of\s+(?<modifier_base>day|month|year))?\s*(?<placeholder>[^\(\)]+)\s*(\(as (?<format>day|month|year|(custom '[^']+'))\))?\s*/
parsed_value = value.match(value_parser_regexp)
placeholder = parsed_value[:placeholder]
format = parsed_value[:format]
modifier = parsed_value[:modifier]
modifier_base = parsed_value[:modifier_base]
new_value = case placeholder
when /today/
Date.today
when /yesterday/
Date.today.prev_day
when /tomorrow/
Date.today.next_day
when /last month/
Date.today.prev_month
when /next month/
Date.today.next_month
when /last year/
Date.today.prev_year
when /next year/
Date.today.next_year
when /\s*\d+\s+minute(s)?\s+ago\s*?/
number_of_minutes = value.match(/\d+/)[0].to_i
DateTime.now - (number_of_minutes/24.0/60.0)
when /\s*\d+\s+hours(s)?\s+ago\s*?/
number_of_hours = value.match(/\d+/)[0].to_i
DateTime.now - (number_of_hours/24.0)
when /\s*\d+\s+month(s)?\s+ago\s*?/
number_of_months = value.match(/\d+/)[0].to_i
Date.today.prev_month(number_of_months)
when /\s*\d+\s+day(s)?\s+ago\s*/
number_of_days = value.match(/\d+/)[0].to_i
Date.today.prev_day(number_of_days)
when /\s*\d+\s+year(s)?\s+ago\s*/
number_of_years = value.match(/\d+/)[0].to_i
Date.today.prev_year(number_of_years)
when /\s*\d+\s+minute(s)?\s+from now\s*?/
number_of_minutes = value.match(/\d+/)[0].to_i
DateTime.now + (number_of_minutes/24.0/60.0)
when /\s*\d+\s+hours(s)?\s+from now\s*?/
number_of_hours = value.match(/\d+/)[0].to_i
DateTime.now + (number_of_hours/24.0)
when /\s*\d+\s+month(s)?\s+from now\s*?/
number_of_months = value.match(/\d+/)[0].to_i
Date.today.next_month(number_of_months)
when /\s*\d+\s+day(s)?\s+from now\s*/
number_of_days = value.match(/\d+/)[0].to_i
Date.today.next_day(number_of_days)
when /\s*\d+\s+year(s)?\s+from now\s*/
number_of_years = value.match(/\d+/)[0].to_i
Date.today.next_year(number_of_years)
else
placeholder
end
if new_value.is_a?(Date)
modified_new_value = case modifier
when nil
new_value
when 'beginning'
case modifier_base
when 'day'
new_value
when 'month'
Date.new(new_value.year, new_value.month, 1)
when 'year'
Date.new(new_value.year, 1, 1)
else
raise "Invalid date modifier provided: #{modifier} #{modifier_base}"
end
when 'end'
case modifier_base
when 'day'
new_value
when 'month'
Date.new(new_value.next_month.year, new_value.next_month.month, 1).prev_day
when 'year'
Date.new(new_value.next_year.year, 1, 1).prev_day
else
raise "Invalid date modifier provided: #{modifier} #{modifier_base}"
end
else
raise "Invalid date modifier provided: #{modifier} #{modifier_base}"
end
formatted_new_value = case format
when nil
modified_new_value.to_s
when 'day', 'month', 'year'
modified_new_value.send(format.to_sym)
when /custom '[^']+'/
parsed_format = format.match(/custom '(?<format_string>[^']+)'/)
modified_new_value.strftime(parsed_format[:format_string])
else
raise "Invalid date format provided: #{format}"
end
formatted_new_value.to_s
else
value
end
end
|