Use composite pattern to return page view errors

This commit is contained in:
Grzegorz Bizon 2017-12-22 15:40:46 +01:00
parent 481f461380
commit d69e4541a4
7 changed files with 64 additions and 30 deletions

View File

@ -48,8 +48,8 @@ module QA
@evaluator ||= Page::Base::DSL.new
end
def self.validator
Page::Validator.new(self)
def self.errors
@errors ||= views.map(&:errors).flatten
end
class DSL

View File

@ -8,15 +8,14 @@ module QA
@pattern = pattern
end
def expression?
@pattern.is_a?(Regexp)
end
def matches?(line)
if expression?
line =~ pattern
case @pattern
when Regexp
!!(line =~ @pattern)
when String
line.include?(@pattern)
else
line.includes?(pattern)
raise ArgumentError, 'Pattern should be either String or Regexp!'
end
end
end

View File

@ -1,18 +0,0 @@
module QA
module Page
class Validator
def initialize(page)
@page = page
@views = page.views
end
def errors
@errors ||= @views.map do |view|
end
end
def message
end
end
end
end

View File

@ -15,8 +15,8 @@ module QA
def errors
##
# Reduce required elements by streaming views and making assertions on
# elements' patterns.
# Reduce required elements by streaming view and making assertions on
# elements' existence.
#
@missing ||= @elements.dup.tap do |elements|
File.new(pathname.to_s).foreach do |line|

View File

@ -5,7 +5,7 @@ describe QA::Page::Base do
end
end
describe 'DSL for defining view partials', '.view' do
describe '.view', 'DSL for defining view partials' do
subject do
Class.new(described_class) do
view 'path/to/some/view.html.haml' do
@ -32,4 +32,19 @@ describe QA::Page::Base do
end
end
end
describe '.errors' do
let(:view) { double('view') }
before do
allow(described_class).to receive(:views)
.and_return([view])
allow(view).to receive(:errors).and_return(['some error'])
end
it 'iterates views composite and returns errors' do
expect(described_class.errors).to eq ['some error']
end
end
end

View File

@ -0,0 +1,34 @@
describe QA::Page::Element do
context 'when pattern is an expression' do
subject { described_class.new(:something, /button 'Sign in'/) }
it 'is correctly matches against a string' do
expect(subject.matches?("button 'Sign in'")).to be true
end
it 'does not match if string does not match against a pattern' do
expect(subject.matches?("button 'Sign out'")).to be false
end
end
context 'when pattern is a string' do
subject { described_class.new(:something, 'button') }
it 'is correctly matches against a string' do
expect(subject.matches?('some button in the view')).to be true
end
it 'does not match if string does not match against a pattern' do
expect(subject.matches?('text_field :name')).to be false
end
end
context 'when pattern is not supported' do
subject { described_class.new(:something, [/something/]) }
it 'raises an error' do
expect { subject.matches?('some line') }
.to raise_error ArgumentError
end
end
end

View File

@ -57,5 +57,9 @@ describe QA::Page::View do
.to match %r(Missing element `.*` in `.*/some/file.html` view)
end
end
context 'when view partial has not been found' do
pending
end
end
end