Path: blob/trunk/rb/spec/unit/selenium/webdriver/guard_spec.rb
1864 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require_relative 'spec_helper'20require 'selenium/webdriver/support/guards'2122module Selenium23module WebDriver24module Support25describe Guards do26describe '#new' do27it 'collects guards from example only for known guard types',28except: {}, exclude: {}, exclusive: {}, flaky: {}, ignored: {}, only: {} do |example|29guards = described_class.new(example)30types = guards.instance_variable_get(:@guards).map { |g| g.instance_variable_get(:@type) }31expect(types).to include :except, :only, :exclusive, :exclude, :flaky32expect(types).not_to include :ignored33end3435it 'accepts bug tracker value' do |example|36guards = described_class.new(example, bug_tracker: 'https://example.com/bugs')37expect(guards.instance_variable_get(:@bug_tracker)).to eq 'https://example.com/bugs'38end3940it 'accepts conditions' do |example|41condition1 = WebDriver::Support::Guards::GuardCondition.new(:foo)42condition2 = WebDriver::Support::Guards::GuardCondition.new(:bar)4344guards = described_class.new(example, conditions: [condition1, condition2])45expect(guards.instance_variable_get(:@guard_conditions)).to include condition1, condition246end47end4849describe '#add_conditions' do50it 'sets multiple' do |example|51guards = described_class.new(example)52guards.add_condition :foo, true53guards.add_condition :bar, false5455expect(guards.instance_variable_get(:@guard_conditions).map(&:name)).to include :foo, :bar56end57end5859describe '#add_message' do60it 'sets multiple custom messages' do |example|61guards = described_class.new(example)62guards.add_message(:foo, 'The problem is foo')63guards.add_message(:bar, 'The problem is bar')6465expect(guards.messages).to include({foo: 'The problem is foo'}, {bar: 'The problem is bar'})66end67end6869describe '#disposition' do70it 'returns nothing' do |example|71guards = described_class.new(example)72expect(guards.disposition).to be_nil73end7475it 'is pending without provided reason', except: {foo: false} do |example|76guards = described_class.new(example)77guards.add_condition(:foo, false)7879expect(guards.disposition.size).to eq(2)80expect(guards.disposition[0]).to eq :pending81message = /Test guarded;/82guarded_by = /Guarded by {:?foo[:=][ >]false, :?reason[:=][ >]"No reason given"};/83expect(guards.disposition[1]).to match(/#{message} #{guarded_by}/)84end8586it 'is skipped without provided reason', exclusive: {foo: true} do |example|87guards = described_class.new(example)88guards.add_condition(:foo, false)8990expect(guards.disposition.size).to eq(2)91expect(guards.disposition[0]).to eq :skip92message = /Test does not apply to this configuration;/93guarded_by = /Guarded by {:?foo[:=][ >]true, :?reason[:=][ >]"No reason given"};/94expect(guards.disposition[1]).to match(/#{message} #{guarded_by}/)95end96end9798describe '#satisfied?' do99it 'evaluates guard' do |example|100guards = described_class.new(example)101guards.add_condition(:foo, true)102guards.add_condition(:bar, false)103104guard = Guards::Guard.new({foo: true, bar: false}, :only)105106expect(guards.satisfied?(guard)).to be true107end108end109end110111describe Guards::GuardCondition do112describe '#new' do113it 'accepts condition' do114condition = described_class.new(:foo, true)115expect(condition.name).to eq :foo116expect(condition.execution).to be_a Proc117expect(condition.execution.call([true])).to be true118end119120it 'accepts block' do121condition = described_class.new(:foo) { |guarded| guarded.include?(7) }122expect(condition.name).to eq :foo123expect(condition.execution).to be_a Proc124expect(condition.execution.call([7])).to be true125end126end127128describe '#satisfied' do129it 'returns true with corresponding guard' do130condition = described_class.new(:foo) { |guarded| guarded.include?(7) }131guard = Guards::Guard.new({foo: 7}, :only)132expect(condition.satisfied?(guard)).to be true133end134135it 'returns false with corresponding guard' do136condition = described_class.new(:foo) { |guarded| guarded.include?(7) }137guard = Guards::Guard.new({foo: 8}, :except)138expect(condition.satisfied?(guard)).to be false139end140end141end142143describe Guards::Guard do144describe '#new' do145it 'requires guarded Hash and type' do146guard = described_class.new({foo: 7}, :only)147expect(guard.guarded).to eq(foo: 7, reason: 'No reason given')148expect(guard.type).to eq :only149end150151it 'creates unknown message by default' do152guard = described_class.new({foo: 7}, :only)153expect(guard.messages).to include(unknown: 'TODO: Investigate why this is failing and file a bug report')154end155156it 'accepts a reason in guarded' do157guard = described_class.new({foo: 7, reason: 'because'}, :only)158expect(guard.reason).to eq 'because'159end160end161162describe '#message' do163it 'defaults to no reason given' do164guard = described_class.new({}, :only)165166expect(guard.message).to match(/Test guarded; Guarded by {:?reason[:=][ >]"No reason given"};/)167end168169it 'accepts integer' do |example|170guards = WebDriver::Support::Guards.new(example, bug_tracker: 'http://example.com/bugs')171guard = described_class.new({reason: 1}, :only, guards)172173expect(guard.message).to eq('Test guarded; Bug Filed: http://example.com/bugs/1')174end175176it 'accepts String' do177guard = described_class.new({reason: 'because'}, :only)178179expect(guard.message).to match(/Test guarded; Guarded by {:?reason[:=][ >]"because"};/)180end181182it 'accepts Symbol of known message' do183guard = described_class.new({reason: :unknown}, :only)184185expect(guard.message).to eq('Test guarded; TODO: Investigate why this is failing and file a bug report')186end187188it 'accepts Symbol of new message' do |example|189guards = WebDriver::Support::Guards.new(example)190guards.add_message(:foo, 'all due to foo')191guard = described_class.new({reason: :foo}, :only, guards)192193expect(guard.message).to eq('Test guarded; all due to foo')194end195196it 'has special message for exclude' do197guard = described_class.new({reason: 'because'}, :exclude)198199message = /Test skipped because it breaks test run;/200guarded_by = /Guarded by {:?reason[:=][ >]"because"};/201expect(guard.message).to match(/#{message} #{guarded_by}/)202end203204it 'has special message for flaky' do205guard = described_class.new({reason: 'because'}, :flaky)206207message = /Test skipped because it is unreliable in this configuration;/208guarded_by = /Guarded by {:?reason[:=][ >]"because"};/209expect(guard.message).to match(/#{message} #{guarded_by}/)210end211212it 'has special message for exclusive' do213guard = described_class.new({reason: 'because'}, :exclusive)214215message = /Test does not apply to this configuration;/216guarded_by = /Guarded by {:?reason[:=][ >]"because"};/217expect(guard.message).to match(/#{message} #{guarded_by}/)218end219end220end221end # Support222end # WebDriver223end # Selenium224225226