-
Notifications
You must be signed in to change notification settings - Fork 0
/
marsh_grass_spec.rb
131 lines (110 loc) · 4.75 KB
/
marsh_grass_spec.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
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MarshGrass do
it 'has a version number' do
expect(MarshGrass::VERSION).not_to be nil
end
context 'test purity' do
# Should always pass
it 'forgets instance variables between tests with repetitions', :repetitions do
expect(@forgettable_thing).to be nil
@forgettable_thing = 'something'
end
# # Should always pass
it 'forgets instance variables between tests with time_of_day', :time_of_day do
expect(@forgettable_thing).to be nil
@forgettable_thing = 'something'
end
# Should always pass
it 'forgets instance variables between tests with surrounding_time', surrounding_time: { hour: 0, minute: 0, second: 0 } do
expect(@forgettable_thing).to be nil
@forgettable_thing = 'something'
end
# Should always pass
it 'forgets instance variables between tests with elapsed_time', :elapsed_time do
expect(@forgettable_thing).to be nil
@forgettable_thing = 'something'
end
# Should always pass
it 'forgets instance variables between with time_zones', :time_zones do
expect(@forgettable_thing).to be nil
@forgettable_thing = 'something'
end
end
context 'running tests a certain number of times' do
# Should run 20x and fail ~14x
it 'allows specifying a number of repetitions', repetitions: 20 do
expect(rand(1..3)).to eq 1
end
# Should run 1000x and fail 66%
it 'allows running a default number of repetitions', :repetitions do
expect(rand(1..3)).to eq 1
end
end
context 'running tests at a certain time of day' do
# Should run 24x and fail 1x
it 'allows testing against an iteration of hours', time_of_day: :hours do
expect(Time.now.hour).not_to eq(10)
end
# Should run 60x and fail 1x
it 'allows testing against an iteration of minutes', time_of_day: :minutes do
expect(Time.now.min).not_to eq(25)
end
# Should run 60x and fail 1x
it 'allows testing against an iteration of seconds', time_of_day: :seconds do
expect(Time.now.sec).not_to eq(44)
end
# Should run (24 * 60) = 1440x and fail 1x
it 'allows testing against an iteration of hours and minutes', time_of_day: [:hours, :minutes] do
expect(Time.now.strftime('%H:%M')).not_to eq('10:25')
end
# Should run (60 * 60) = 3600x and fail 1x
it 'allows testing against an iteration of minutes and seconds', time_of_day: [:minutes, :seconds] do
expect(Time.now.strftime('%M:%S')).not_to eq('25:44')
end
# Should run (24 * 60 * 60) = 86400x and fail 1x
it 'allows testing against an iteration of hours, minutes, and seconds', time_of_day: [:hours, :minutes, :seconds] do
expect(Time.now.strftime('%H:%M:%S')).not_to eq('10:25:44')
end
end
context 'running tests surrounding a particular time' do
# Should run 1000x before passed time and fail last ~ 50x
# Should run 1x 'on' the particular time and pass
# Should run 1000x after passed time and never fail
it 'allows testing for time surrounding midnight', surrounding_time: { hour: 0, minute: 0, second: 0 } do
now = Time.now
expect { sleep 0.05 }.not_to change { Time.now.day }.from(now.day)
end
end
context 'running tests for variable elapsed time' do
# Should run 10x and fail ?x (usually roughly half: depending on where in the
# milliseconds you run, does 0.2 scaled by elapsed push it over to next second?)
it 'allows testing for time-dependent methods across default duration multipliers', :elapsed_time do
expect { sleep 0.2 }.to change { Time.now.to_i }.by(1)
end
# Should run 2x and fail 1x
it 'allows testing for specified duration multipliers', elapsed_time: (1..2) do
expect { sleep 1 }.to change { Time.now.to_i }.by(1)
end
end
context 'running tests for variations in timezone' do
# Should run 54x and fail 2x-4x (depending on what time of day it is in reality
# because two times zones are overlapping with other zones)
it 'allows testing for all timezone variations', :time_zones do
expect(Time.now.hour).not_to eq(1)
end
end
context 'combining test scenarios' do
# Should run (10 * 24) = 240x and fail 66%
it 'runs repetitions of iterations on hours', repetitions: 10, time_of_day: :hours do
expect(rand(1..3)).to eq 1
end
# Should run (2 * 1000) = 2000x before chosen time and fail last ~ (3 * 50)x
# Should run 2x at chosen time and pass
# Should run (2 * 1000) = 2000x after chosen time and never fail
it 'runs slowly and time approaches midnight', elapsed_time: (1..2), surrounding_time: { hour: 0, minute: 0, second: 0 } do
now = Time.now
expect { sleep 0.05 }.not_to change { Time.now.day }.from(now.day)
end
end
end