     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
+  test "source" do
+    src = "foo"
+    assert Regex.source(Regex.compile!(src)) == src
+    assert Regex.source(~r/#{src}/) == src
+
+    src = "\a\b\d\e\f\n\r\s\t\v"
+    assert Regex.source(Regex.compile!(src)) == src
+    assert Regex.source(~r/#{src}/) == src
+
+    src = "\a\\b\\d\\e\f\n\r\\s\t\v"
+    assert Regex.source(Regex.compile!(src)) == src
+    assert Regex.source(~r/#{src}/) == src
+  end
+
+  test "literal source" do
+    assert Regex.source(Regex.compile!("foo")) == "foo"
+    assert Regex.source(~r"foo") == "foo"
+    assert Regex.re_pattern(Regex.compile!("foo"))
+           == Regex.re_pattern(~r"foo")
+
+    assert Regex.source(Regex.compile!("\a\b\d\e\f\n\r\s\t\v")) == "\a\b\d\e\f\n\r\s\t\v"
+    assert Regex.source(~r<\a\b\d\e\f\n\r\s\t\v>) == "\a\\b\\d\\e\f\n\r\\s\t\v"
+    assert Regex.re_pattern(Regex.compile!("\a\b\d\e\f\n\r\s\t\v"))
+           == Regex.re_pattern(~r"\a\010\177\033\f\n\r \t\v")
+
+    assert Regex.source(Regex.compile!("\a\\b\\d\e\f\n\r\\s\t\v")) == "\a\\b\\d\e\f\n\r\\s\t\v"
+    assert Regex.source(~r<\a\\b\\d\\e\f\n\r\\s\t\v>) == "\a\\\\b\\\\d\\\\e\f\n\r\\\\s\t\v"
+    assert Regex.re_pattern(Regex.compile!("\a\\b\\d\e\f\n\r\\s\t\v"))
+           == Regex.re_pattern(~r"\a\b\d\e\f\n\r\s\t\v")
+  end
+
+  test "Unicode" do
+    assert "olá" =~ ~r"\p{Latin}$"u
+    refute "£" =~ ~r/\p{Lu}/u
+
+    # Non breaking space matches [[:space:]] with Unicode
+    assert <<0xA0::utf8>> =~ ~r/[[:space:]]/u
+    assert <<0xA0::utf8>> =~ ~r/\s/u
+
+    assert <<?<, 255, ?>>> =~ ~r/<.>/
+    refute <<?<, 255, ?>>> =~ ~r/<.>/u
+  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 "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 Regex.split(~r" ", " foo bar baz ", trim: true, parts: 2) == ["foo", "bar baz "]
  end

  test "split on" do
    assert Regex.split(~r/()abc()/, "xabcxabcx", on: :none) ==
           ["xabcxabcx"]
    assert Regex.split(~r/()abc()/, "xabcxabcx", on: :all_but_first) ==

    assert Regex.match?(~R/f#{1,3}o/, "f#o")
  end

  test "run/2" do
    assert Regex.run(~r"c(d)", "abcd") == ["cd", "d"]
    assert Regex.run(~r"e", "abcd") == nil
  end

  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/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/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/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/2,3" do
    assert Regex.split(~r",", "") == [""]
    assert Regex.split(~r",", "", trim: true) == []
    assert Regex.split(~r",", "", trim: true, parts: 2) == []
