Skip to content

Commit 7644bd8

Browse files
josevalimJosé Valim
authored andcommitted
Merge pull request #3198 from eksperimental/pattern_operator
fix Kernel.=~/2 for "" =~ "" Signed-off-by: José Valim <[email protected]>
1 parent 9e2c5a2 commit 7644bd8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,14 @@ defmodule Kernel do
13861386
iex> "abcd" =~ "ad"
13871387
false
13881388
1389+
iex> "abcd" =~ ""
1390+
true
1391+
13891392
"""
1393+
1394+
@spec (String.t =~ (String.t | Regex.t)) :: boolean
1395+
def left =~ "" when is_binary(left), do: true
1396+
13901397
def left =~ right when is_binary(left) and is_binary(right) do
13911398
:binary.match(left, right) != :nomatch
13921399
end

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,60 @@ defmodule KernelTest do
66
test "=~/2" do
77
assert ("abcd" =~ ~r/c(d)/) == true
88
assert ("abcd" =~ ~r/e/) == false
9+
assert ("abcd" =~ ~R/c(d)/) == true
10+
assert ("abcd" =~ ~R/e/) == false
911

1012
string = "^ab+cd*$"
1113
assert (string =~ "ab+") == true
1214
assert (string =~ "bb") == false
1315

16+
assert ("abcd" =~ ~r//) == true
17+
assert ("abcd" =~ ~R//) == true
18+
assert ("abcd" =~ "") == true
19+
20+
assert ("" =~ ~r//) == true
21+
assert ("" =~ ~R//) == true
22+
assert ("" =~ "") == true
23+
24+
assert ("" =~ "abcd") == false
25+
assert ("" =~ ~r/abcd/) == false
26+
assert ("" =~ ~R/abcd/) == false
27+
1428
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
1529
1234 =~ "hello"
1630
end
1731

1832
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
1933
1234 =~ ~r"hello"
2034
end
35+
36+
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
37+
1234 =~ ~R"hello"
38+
end
39+
40+
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
41+
~r"hello" =~ "hello"
42+
end
43+
44+
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
45+
~r"hello" =~ ~r"hello"
46+
end
47+
48+
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
49+
:abcd =~ ~r//
50+
end
51+
52+
assert_raise FunctionClauseError, "no function clause matching in Kernel.=~/2", fn ->
53+
:abcd =~ ""
54+
end
55+
56+
assert_raise FunctionClauseError, "no function clause matching in Regex.match?/2", fn ->
57+
"abcd" =~ nil
58+
end
59+
60+
assert_raise FunctionClauseError, "no function clause matching in Regex.match?/2", fn ->
61+
"abcd" =~ :abcd
62+
end
2163
end
2264

2365
test "^" do

0 commit comments

Comments
 (0)