Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/guard_spec.rb
1864 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require_relative 'spec_helper'
21
require 'selenium/webdriver/support/guards'
22
23
module Selenium
24
module WebDriver
25
module Support
26
describe Guards do
27
describe '#new' do
28
it 'collects guards from example only for known guard types',
29
except: {}, exclude: {}, exclusive: {}, flaky: {}, ignored: {}, only: {} do |example|
30
guards = described_class.new(example)
31
types = guards.instance_variable_get(:@guards).map { |g| g.instance_variable_get(:@type) }
32
expect(types).to include :except, :only, :exclusive, :exclude, :flaky
33
expect(types).not_to include :ignored
34
end
35
36
it 'accepts bug tracker value' do |example|
37
guards = described_class.new(example, bug_tracker: 'https://example.com/bugs')
38
expect(guards.instance_variable_get(:@bug_tracker)).to eq 'https://example.com/bugs'
39
end
40
41
it 'accepts conditions' do |example|
42
condition1 = WebDriver::Support::Guards::GuardCondition.new(:foo)
43
condition2 = WebDriver::Support::Guards::GuardCondition.new(:bar)
44
45
guards = described_class.new(example, conditions: [condition1, condition2])
46
expect(guards.instance_variable_get(:@guard_conditions)).to include condition1, condition2
47
end
48
end
49
50
describe '#add_conditions' do
51
it 'sets multiple' do |example|
52
guards = described_class.new(example)
53
guards.add_condition :foo, true
54
guards.add_condition :bar, false
55
56
expect(guards.instance_variable_get(:@guard_conditions).map(&:name)).to include :foo, :bar
57
end
58
end
59
60
describe '#add_message' do
61
it 'sets multiple custom messages' do |example|
62
guards = described_class.new(example)
63
guards.add_message(:foo, 'The problem is foo')
64
guards.add_message(:bar, 'The problem is bar')
65
66
expect(guards.messages).to include({foo: 'The problem is foo'}, {bar: 'The problem is bar'})
67
end
68
end
69
70
describe '#disposition' do
71
it 'returns nothing' do |example|
72
guards = described_class.new(example)
73
expect(guards.disposition).to be_nil
74
end
75
76
it 'is pending without provided reason', except: {foo: false} do |example|
77
guards = described_class.new(example)
78
guards.add_condition(:foo, false)
79
80
expect(guards.disposition.size).to eq(2)
81
expect(guards.disposition[0]).to eq :pending
82
message = /Test guarded;/
83
guarded_by = /Guarded by {:?foo[:=][ >]false, :?reason[:=][ >]"No reason given"};/
84
expect(guards.disposition[1]).to match(/#{message} #{guarded_by}/)
85
end
86
87
it 'is skipped without provided reason', exclusive: {foo: true} do |example|
88
guards = described_class.new(example)
89
guards.add_condition(:foo, false)
90
91
expect(guards.disposition.size).to eq(2)
92
expect(guards.disposition[0]).to eq :skip
93
message = /Test does not apply to this configuration;/
94
guarded_by = /Guarded by {:?foo[:=][ >]true, :?reason[:=][ >]"No reason given"};/
95
expect(guards.disposition[1]).to match(/#{message} #{guarded_by}/)
96
end
97
end
98
99
describe '#satisfied?' do
100
it 'evaluates guard' do |example|
101
guards = described_class.new(example)
102
guards.add_condition(:foo, true)
103
guards.add_condition(:bar, false)
104
105
guard = Guards::Guard.new({foo: true, bar: false}, :only)
106
107
expect(guards.satisfied?(guard)).to be true
108
end
109
end
110
end
111
112
describe Guards::GuardCondition do
113
describe '#new' do
114
it 'accepts condition' do
115
condition = described_class.new(:foo, true)
116
expect(condition.name).to eq :foo
117
expect(condition.execution).to be_a Proc
118
expect(condition.execution.call([true])).to be true
119
end
120
121
it 'accepts block' do
122
condition = described_class.new(:foo) { |guarded| guarded.include?(7) }
123
expect(condition.name).to eq :foo
124
expect(condition.execution).to be_a Proc
125
expect(condition.execution.call([7])).to be true
126
end
127
end
128
129
describe '#satisfied' do
130
it 'returns true with corresponding guard' do
131
condition = described_class.new(:foo) { |guarded| guarded.include?(7) }
132
guard = Guards::Guard.new({foo: 7}, :only)
133
expect(condition.satisfied?(guard)).to be true
134
end
135
136
it 'returns false with corresponding guard' do
137
condition = described_class.new(:foo) { |guarded| guarded.include?(7) }
138
guard = Guards::Guard.new({foo: 8}, :except)
139
expect(condition.satisfied?(guard)).to be false
140
end
141
end
142
end
143
144
describe Guards::Guard do
145
describe '#new' do
146
it 'requires guarded Hash and type' do
147
guard = described_class.new({foo: 7}, :only)
148
expect(guard.guarded).to eq(foo: 7, reason: 'No reason given')
149
expect(guard.type).to eq :only
150
end
151
152
it 'creates unknown message by default' do
153
guard = described_class.new({foo: 7}, :only)
154
expect(guard.messages).to include(unknown: 'TODO: Investigate why this is failing and file a bug report')
155
end
156
157
it 'accepts a reason in guarded' do
158
guard = described_class.new({foo: 7, reason: 'because'}, :only)
159
expect(guard.reason).to eq 'because'
160
end
161
end
162
163
describe '#message' do
164
it 'defaults to no reason given' do
165
guard = described_class.new({}, :only)
166
167
expect(guard.message).to match(/Test guarded; Guarded by {:?reason[:=][ >]"No reason given"};/)
168
end
169
170
it 'accepts integer' do |example|
171
guards = WebDriver::Support::Guards.new(example, bug_tracker: 'http://example.com/bugs')
172
guard = described_class.new({reason: 1}, :only, guards)
173
174
expect(guard.message).to eq('Test guarded; Bug Filed: http://example.com/bugs/1')
175
end
176
177
it 'accepts String' do
178
guard = described_class.new({reason: 'because'}, :only)
179
180
expect(guard.message).to match(/Test guarded; Guarded by {:?reason[:=][ >]"because"};/)
181
end
182
183
it 'accepts Symbol of known message' do
184
guard = described_class.new({reason: :unknown}, :only)
185
186
expect(guard.message).to eq('Test guarded; TODO: Investigate why this is failing and file a bug report')
187
end
188
189
it 'accepts Symbol of new message' do |example|
190
guards = WebDriver::Support::Guards.new(example)
191
guards.add_message(:foo, 'all due to foo')
192
guard = described_class.new({reason: :foo}, :only, guards)
193
194
expect(guard.message).to eq('Test guarded; all due to foo')
195
end
196
197
it 'has special message for exclude' do
198
guard = described_class.new({reason: 'because'}, :exclude)
199
200
message = /Test skipped because it breaks test run;/
201
guarded_by = /Guarded by {:?reason[:=][ >]"because"};/
202
expect(guard.message).to match(/#{message} #{guarded_by}/)
203
end
204
205
it 'has special message for flaky' do
206
guard = described_class.new({reason: 'because'}, :flaky)
207
208
message = /Test skipped because it is unreliable in this configuration;/
209
guarded_by = /Guarded by {:?reason[:=][ >]"because"};/
210
expect(guard.message).to match(/#{message} #{guarded_by}/)
211
end
212
213
it 'has special message for exclusive' do
214
guard = described_class.new({reason: 'because'}, :exclusive)
215
216
message = /Test does not apply to this configuration;/
217
guarded_by = /Guarded by {:?reason[:=][ >]"because"};/
218
expect(guard.message).to match(/#{message} #{guarded_by}/)
219
end
220
end
221
end
222
end # Support
223
end # WebDriver
224
end # Selenium
225
226