Clean up and reorder tests for the Regex module
This commit is contained in:
@@ -18,32 +18,6 @@ defmodule RegexTest do
|
||||
assert "aa" =~ ~r/(a)\1/
|
||||
end
|
||||
|
||||
test "compile!" do
|
||||
assert Regex.regex?(Regex.compile!("foo"))
|
||||
|
||||
assert_raise Regex.CompileError, ~r/position 0$/, fn ->
|
||||
Regex.compile!("*foo")
|
||||
end
|
||||
end
|
||||
|
||||
test "compile" do
|
||||
{:ok, regex} = Regex.compile("foo")
|
||||
assert Regex.regex?(regex)
|
||||
assert {:error, _} = Regex.compile("*foo")
|
||||
assert {:error, _} = Regex.compile("foo", "y")
|
||||
assert {:error, _} = Regex.compile("foo", "uy")
|
||||
end
|
||||
|
||||
test "compile with erl opts" do
|
||||
{:ok, regex} = Regex.compile("foo\\sbar", [:dotall, {:newline, :anycrlf}])
|
||||
assert "foo\nbar" =~ regex
|
||||
end
|
||||
|
||||
test "regex?" do
|
||||
assert Regex.regex?(~r/foo/)
|
||||
refute Regex.regex?(0)
|
||||
end
|
||||
|
||||
test "source" do
|
||||
src = "foo"
|
||||
assert Regex.source(Regex.compile!(src)) == src
|
||||
@@ -75,10 +49,6 @@ defmodule RegexTest do
|
||||
== Regex.re_pattern(~r"\a\b\d\e\f\n\r\s\t\v")
|
||||
end
|
||||
|
||||
test "opts" do
|
||||
assert Regex.opts(Regex.compile!("foo", "i")) == "i"
|
||||
end
|
||||
|
||||
test "Unicode" do
|
||||
assert "olá" =~ ~r"\p{Latin}$"u
|
||||
refute "£" =~ ~r/\p{Lu}/u
|
||||
@@ -91,11 +61,47 @@ defmodule RegexTest do
|
||||
refute <<?<, 255, ?>>> =~ ~r/<.>/u
|
||||
end
|
||||
|
||||
test "names" do
|
||||
test "ungreedy" do
|
||||
assert Regex.run(~r/[\d ]+/, "1 2 3 4 5"), ["1 2 3 4 5"]
|
||||
assert Regex.run(~r/[\d ]?+/, "1 2 3 4 5"), ["1"]
|
||||
assert Regex.run(~r/[\d ]+/U, "1 2 3 4 5"), ["1"]
|
||||
end
|
||||
|
||||
test "regex?/1" do
|
||||
assert Regex.regex?(~r/foo/)
|
||||
refute Regex.regex?(0)
|
||||
end
|
||||
|
||||
test "compile/1" do
|
||||
{:ok, regex} = Regex.compile("foo")
|
||||
assert Regex.regex?(regex)
|
||||
assert {:error, _} = Regex.compile("*foo")
|
||||
assert {:error, _} = Regex.compile("foo", "y")
|
||||
assert {:error, _} = Regex.compile("foo", "uy")
|
||||
end
|
||||
|
||||
test "compile/1 with Erlang options" do
|
||||
{:ok, regex} = Regex.compile("foo\\sbar", [:dotall, {:newline, :anycrlf}])
|
||||
assert "foo\nbar" =~ regex
|
||||
end
|
||||
|
||||
test "compile!/1" do
|
||||
assert Regex.regex?(Regex.compile!("foo"))
|
||||
|
||||
assert_raise Regex.CompileError, ~r/position 0$/, fn ->
|
||||
Regex.compile!("*foo")
|
||||
end
|
||||
end
|
||||
|
||||
test "opts/1" do
|
||||
assert Regex.opts(Regex.compile!("foo", "i")) == "i"
|
||||
end
|
||||
|
||||
test "names/1" do
|
||||
assert Regex.names(~r/(?<FOO>foo)/) == ["FOO"]
|
||||
end
|
||||
|
||||
test "match?" do
|
||||
test "match?/2" do
|
||||
assert Regex.match?(~r/foo/, "foo")
|
||||
refute Regex.match?(~r/foo/, "FOO")
|
||||
assert Regex.match?(~r/foo/i, "FOO")
|
||||
@@ -108,7 +114,7 @@ defmodule RegexTest do
|
||||
assert Regex.match?(~r/foo$/, "afoo")
|
||||
end
|
||||
|
||||
test "named captures" do
|
||||
test "named_captures/2" do
|
||||
assert Regex.named_captures(~r/(?<foo>c)(?<bar>d)/, "abcd") == %{"bar" => "d", "foo" => "c"}
|
||||
assert Regex.named_captures(~r/c(?<foo>d)/, "abcd") == %{"foo" => "d"}
|
||||
assert Regex.named_captures(~r/c(?<foo>d)/, "no_match") == nil
|
||||
@@ -120,36 +126,36 @@ defmodule RegexTest do
|
||||
assert Regex.match?(~R/f#{1,3}o/, "f#o")
|
||||
end
|
||||
|
||||
test "run" do
|
||||
test "run/2" do
|
||||
assert Regex.run(~r"c(d)", "abcd") == ["cd", "d"]
|
||||
assert Regex.run(~r"e", "abcd") == nil
|
||||
end
|
||||
|
||||
test "run with all names" do
|
||||
test "run/3 with :all_names as the value of the :capture option" do
|
||||
assert Regex.run(~r/c(?<foo>d)/, "abcd", capture: :all_names) == ["d"]
|
||||
assert Regex.run(~r/c(?<foo>d)/, "no_match", capture: :all_names) == nil
|
||||
assert Regex.run(~r/c(?<foo>d|e)/, "abcd abce", capture: :all_names) == ["d"]
|
||||
end
|
||||
|
||||
test "run with indexes" do
|
||||
test "run/3 with :index as the value of the :return option" do
|
||||
assert Regex.run(~r"c(d)", "abcd", return: :index) == [{2, 2}, {3, 1}]
|
||||
assert Regex.run(~r"e", "abcd", return: :index) == nil
|
||||
end
|
||||
|
||||
test "scan" do
|
||||
test "scan/2" do
|
||||
assert Regex.scan(~r"c(d|e)", "abcd abce") == [["cd", "d"], ["ce", "e"]]
|
||||
assert Regex.scan(~r"c(?:d|e)", "abcd abce") == [["cd"], ["ce"]]
|
||||
assert Regex.scan(~r"e", "abcd") == []
|
||||
end
|
||||
|
||||
test "scan with all names" do
|
||||
test "scan/2 with :all_names as the value of the :capture option" do
|
||||
assert Regex.scan(~r/cd/, "abcd", capture: :all_names) == []
|
||||
assert Regex.scan(~r/c(?<foo>d)/, "abcd", capture: :all_names) == [["d"]]
|
||||
assert Regex.scan(~r/c(?<foo>d)/, "no_match", capture: :all_names) == []
|
||||
assert Regex.scan(~r/c(?<foo>d|e)/, "abcd abce", capture: :all_names) == [["d"], ["e"]]
|
||||
end
|
||||
|
||||
test "split" do
|
||||
test "split/2,3" do
|
||||
assert Regex.split(~r",", "") == [""]
|
||||
assert Regex.split(~r",", "", trim: true) == []
|
||||
assert Regex.split(~r",", "", trim: true, parts: 2) == []
|
||||
@@ -168,7 +174,7 @@ defmodule RegexTest do
|
||||
assert Regex.split(~r" ", " foo bar baz ", trim: true, parts: 2) == ["foo", "bar baz "]
|
||||
end
|
||||
|
||||
test "split on" do
|
||||
test "split/3 with the :on option" do
|
||||
assert Regex.split(~r/()abc()/, "xabcxabcx", on: :none) ==
|
||||
["xabcxabcx"]
|
||||
assert Regex.split(~r/()abc()/, "xabcxabcx", on: :all_but_first) ==
|
||||
@@ -187,14 +193,14 @@ defmodule RegexTest do
|
||||
["a", "c a", "c a", "c"]
|
||||
end
|
||||
|
||||
test "split include_captures" do
|
||||
test "split/3 with the :include_captures option" do
|
||||
assert Regex.split(~r/([ln])/, "Erlang", include_captures: true) == ["Er", "l", "a", "n", "g"]
|
||||
assert Regex.split(~r/([kw])/, "Elixir", include_captures: true) == ["Elixir"]
|
||||
assert Regex.split(~r/([Ee]lixir)/, "Elixir", include_captures: true, trim: true) == ["Elixir"]
|
||||
assert Regex.split(~r/([Ee]lixir)/, "Elixir", include_captures: true, trim: false) == ["", "Elixir", ""]
|
||||
end
|
||||
|
||||
test "replace" do
|
||||
test "replace/3,4" do
|
||||
assert Regex.replace(~r(d), "abc", "d") == "abc"
|
||||
assert Regex.replace(~r(b), "abc", "d") == "adc"
|
||||
assert Regex.replace(~r(b), "abc", "[\\0]") == "a[b]c"
|
||||
@@ -219,12 +225,6 @@ defmodule RegexTest do
|
||||
assert Regex.replace(~r[a(b)c], "abcabc", fn "abc", "b" -> "ac" end, global: false) == "acabc"
|
||||
end
|
||||
|
||||
test "ungreedy" do
|
||||
assert Regex.run(~r/[\d ]+/, "1 2 3 4 5"), ["1 2 3 4 5"]
|
||||
assert Regex.run(~r/[\d ]?+/, "1 2 3 4 5"), ["1"]
|
||||
assert Regex.run(~r/[\d ]+/U, "1 2 3 4 5"), ["1"]
|
||||
end
|
||||
|
||||
test "escape" do
|
||||
assert matches_escaped?(".")
|
||||
refute matches_escaped?(".", "x")
|
||||
|
||||
Reference in New Issue
Block a user