Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helper for normalize/regex_spec #14545

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 40 additions & 10 deletions spec/compiler/normalize/regex_spec.cr
@@ -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
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