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
|
# frozen_string_literal: true
RSpec.shared_examples 'syntax' do |opts|
opts[:implements].each do |type, tokens|
tokens.each do |token|
it("implements #{token} #{type}") do
expect(described_class.implements?(type, token)).to be true
end
end
end
opts[:excludes] && opts[:excludes].each do |type, tokens|
tokens.each do |token|
it("does not implement #{token} #{type}") do
expect(described_class.implements?(type, token)).to be false
end
end
end
end
RSpec.shared_examples 'scan' do |pattern, checks|
context "given the pattern #{pattern}" do
before(:all) { @tokens = Regexp::Scanner.scan(pattern) }
checks.each do |index, (type, token, text, ts, te)|
it "scans token #{index} as #{token} #{type} at #{ts}..#{te}" do
result = @tokens.at(index)
result || fail("no token at index #{index}, max is #{@tokens.size - 1}")
expect(result[0]).to eq type
expect(result[1]).to eq token
expect(result[2]).to eq text
expect(result[3]).to eq ts
expect(result[4]).to eq te
end
end
end
end
RSpec.shared_examples 'lex' do |pattern, checks|
context "given the pattern #{pattern}" do
before(:all) { @tokens = Regexp::Lexer.lex(pattern) }
checks.each do |index, (type, token, text, ts, te, lvl, set_lvl, cond_lvl)|
it "lexes token #{index} as #{token} #{type} at #{lvl}, #{set_lvl}, #{cond_lvl}" do
struct = @tokens.at(index)
expect(struct.type).to eq type
expect(struct.token).to eq token
expect(struct.text).to eq text
expect(struct.ts).to eq ts
expect(struct.te).to eq te
expect(struct.level).to eq lvl
expect(struct.set_level).to eq set_lvl
expect(struct.conditional_level).to eq cond_lvl
end
end
end
end
RSpec.shared_examples 'parse' do |pattern, checks|
context "given the pattern #{pattern}" do
before(:all) { @root = Regexp::Parser.parse(pattern, '*') }
checks.each do |path, expectations|
path = Array(path)
inspect_quantifier = path.last == :q && path.pop
attributes = expectations.pop if expectations.last.is_a?(Hash)
klass = expectations.pop if expectations.last.is_a?(Class)
token = expectations.pop
type = expectations.pop
description = klass || token || type || 'Expression'
it "parses expression at #{path} as #{description}" do
exp = @root.dig(*path)
exp = exp.quantifier if inspect_quantifier
klass && expect(exp).to(be_instance_of(klass))
type && expect(exp.type).to(eq(type))
token && expect(exp.token).to(eq(token))
attributes && attributes.each do |method, value|
actual = exp.send(method)
expect(actual).to eq(value),
"expected #{description} at #{path} to "\
"have #{method} #{value.inspect}, got #{actual.inspect}"
end
end
end
end
end
|