Skip to content

Commit

Permalink
Add specs for regex literal expansion (#13253)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Apr 5, 2023
1 parent 578646b commit 706075e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
31 changes: 31 additions & 0 deletions spec/compiler/normalize/regex_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "../../spec_helper"

describe "Normalize: regex literal" do
describe "options" do
it "empty" do
assert_expand %q(/#{"".to_s}/), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(0))
CRYSTAL
end
it "i" do
assert_expand %q(/#{"".to_s}/i), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(1))
CRYSTAL
end
it "x" do
assert_expand %q(/#{"".to_s}/x), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(8))
CRYSTAL
end
it "im" do
assert_expand %q(/#{"".to_s}/im), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(7))
CRYSTAL
end
it "imx" do
assert_expand %q(/#{"".to_s}/imx), <<-'CRYSTAL'
::Regex.new("#{"".to_s}", ::Regex::Options.new(15))
CRYSTAL
end
end
end
16 changes: 8 additions & 8 deletions src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ module Crystal
end
end

private def regex_new_call(node, value)
Call.new(Path.global("Regex").at(node), "new", value, regex_options(node)).at(node)
end

private def regex_options(node)
Call.new(Path.global(["Regex", "Options"]).at(node), "new", NumberLiteral.new(node.options.value.to_s).at(node)).at(node)
end

# Convert and to if:
#
# From:
Expand Down Expand Up @@ -1019,14 +1027,6 @@ module Crystal
end
end

private def regex_new_call(node, value)
Call.new(Path.global("Regex").at(node), "new", value, regex_options(node)).at(node)
end

private def regex_options(node)
Call.new(Path.global(["Regex", "Options"]).at(node), "new", NumberLiteral.new(node.options.value).at(node)).at(node)
end

def expand(node)
raise "#{node} (#{node.class}) can't be expanded"
end
Expand Down

0 comments on commit 706075e

Please sign in to comment.