Skip to content

Commit

Permalink
Add test helper for normalize/regex_spec (#14545)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored May 8, 2024
1 parent a3bbcf9 commit 3eab3e4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
50 changes: 40 additions & 10 deletions spec/compiler/normalize/regex_spec.cr
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
require "../../spec_helper"

private def assert_expand_regex_const(from : String, to, *, flags = nil, file = __FILE__, line = __LINE__)
from_nodes = Parser.parse(from)
assert_expand(from_nodes, flags: flags, file: file, line: line) do |to_nodes, program|
const = program.types[to_nodes.to_s].should be_a(Crystal::Const), file: file, line: line
const.value.to_s.should eq(to.strip), file: file, line: line
end
end

describe "Normalize: regex literal" do
describe "StringLiteral" do
it "expands to const" do
assert_expand Parser.parse(%q(/foo/)) do |to_nodes, program|
to_nodes.to_s.should eq "$Regex:0"
end
end

it "simple" do
assert_expand_regex_const %q(/foo/), <<-'CRYSTAL'
::Regex.new("foo", ::Regex::Options.new(0))
CRYSTAL
end
end

describe "StringInterpolation" do
it "simple" do
assert_expand %q(/#{"foo".to_s}/), <<-'CRYSTAL'
::Regex.new("#{"foo".to_s}", ::Regex::Options.new(0))
CRYSTAL
end
end

describe "options" do
it "empty" do
assert_expand %q(/#{"".to_s}/), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(0))
assert_expand_regex_const %q(//), <<-'CRYSTAL'
::Regex.new("", ::Regex::Options.new(0))
CRYSTAL
end
it "i" do
assert_expand %q(/#{"".to_s}/i), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(1))
assert_expand_regex_const %q(//i), <<-'CRYSTAL'
::Regex.new("", ::Regex::Options.new(1))
CRYSTAL
end
it "x" do
assert_expand %q(/#{"".to_s}/x), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(8))
assert_expand_regex_const %q(//x), <<-'CRYSTAL'
::Regex.new("", ::Regex::Options.new(8))
CRYSTAL
end
it "im" do
assert_expand %q(/#{"".to_s}/im), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(7))
assert_expand_regex_const %q(//im), <<-'CRYSTAL'
::Regex.new("", ::Regex::Options.new(7))
CRYSTAL
end
it "imx" do
assert_expand %q(/#{"".to_s}/imx), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(15))
assert_expand_regex_const %q(//imx), <<-'CRYSTAL'
::Regex.new("", ::Regex::Options.new(15))
CRYSTAL
end
end
Expand Down
8 changes: 7 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ def assert_expand(from : String, to, *, flags = nil, file = __FILE__, line = __L
end

def assert_expand(from_nodes : ASTNode, to, *, flags = nil, file = __FILE__, line = __LINE__)
assert_expand(from_nodes, flags: flags, file: file, line: line) do |to_nodes|
to_nodes.to_s.strip.should eq(to.strip), file: file, line: line
end
end

def assert_expand(from_nodes : ASTNode, *, flags = nil, file = __FILE__, line = __LINE__, &)
program = new_program
program.flags.concat(flags.split) if flags
to_nodes = LiteralExpander.new(program).expand(from_nodes)
to_nodes.to_s.strip.should eq(to.strip), file: file, line: line
yield to_nodes, program
end

def assert_expand_second(from : String, to, *, flags = nil, file = __FILE__, line = __LINE__)
Expand Down

0 comments on commit 3eab3e4

Please sign in to comment.