Skip to content

Commit 968bd49

Browse files
authored
Support StringLiteral#split(RegexLiteral) (#16423)
1 parent 093c765 commit 968bd49

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

spec/compiler/macro/macro_methods_spec.cr

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,22 @@ module Crystal
434434
assert_macro %({{"odelay" * 3}}), "\"odelayodelayodelay\""
435435
end
436436

437-
it "executes split without arguments" do
438-
assert_macro %({{"1 2 3".split}}), %(["1", "2", "3"] of ::String)
439-
end
437+
describe "#split" do
438+
it "works without arguments" do
439+
assert_macro %({{"1 2 3".split}}), %(["1", "2", "3"] of ::String)
440+
end
440441

441-
it "executes split with argument" do
442-
assert_macro %({{"1-2-3".split('-')}}), %(["1", "2", "3"] of ::String)
443-
end
442+
it "works with string argument" do
443+
assert_macro %({{"1-2-3".split("-")}}), %(["1", "2", "3"] of ::String)
444+
end
444445

445-
it "executes split with char argument" do
446-
assert_macro %({{"1-2-3".split('-')}}), %(["1", "2", "3"] of ::String)
446+
it "works with char argument" do
447+
assert_macro %({{"1-2-3".split('-')}}), %(["1", "2", "3"] of ::String)
448+
end
449+
450+
it "works with regex argument" do
451+
assert_macro %({{"123-456-789".split(/-(.)/)}}), %(["123", "4", "56", "7", "89"] of ::String)
452+
end
447453
end
448454

449455
it "executes strip" do

src/compiler/crystal/macros/methods.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,8 @@ module Crystal
852852
splitter = arg.value
853853
when StringLiteral
854854
splitter = arg.value
855+
when RegexLiteral
856+
splitter = regex_value(arg)
855857
else
856858
splitter = arg.to_s
857859
end

0 commit comments

Comments
 (0)