     end
 
     test "forces variable to exist" do
-      assert expand(quote do: (var!(a) = 1; var!(a)))
-
-      message = ~r"expected variable \"a\" to expand to an existing variable or be part of a match"
-      assert_raise CompileError, message, fn -> expand(quote do: var!(a)) end
-
-      message = ~r"expected variable \"a\" \(context Unknown\) to expand to an existing variable or be part of a match"
-      assert_raise CompileError, message, fn -> expand(quote do: var!(a, Unknown)) end
+      code =
+        quote do
+          var!(a) = 1
+          var!(a)
+        end
+
+      assert expand(code)
+
+      message =
+        ~r"expected variable \"a\" to expand to an existing variable or be part of a match"
+
+      assert_raise CompileError, message, fn ->
+        expand(quote(do: var!(a)))
+      end
+
+      message =
+        ~r"expected variable \"a\" \(context Unknown\) to expand to an existing variable or be part of a match"
+
+      assert_raise CompileError, message, fn ->
+        expand(quote(do: var!(a, Unknown)))
+      end
     end
 
     test "raises for _ used outside of a match" do
       assert_raise CompileError, ~r"unbound variable _", fn ->
-        expand(quote do: {1, 2, _})
+        expand(quote(do: {1, 2, _}))
       end
     end
   end
 
   describe "^" do
     test "expands args" do
-      assert expand(quote do: (a = 1; ^a = 1)) == quote do: (a = 1; ^a = 1)
+      before_expansion =
+        quote do
+          after_expansion = 1
+          ^after_expansion = 1
+        end
+
+      after_expansion =
+        quote do
+          after_expansion = 1
+          ^after_expansion = 1
+        end
+
+      assert expand(before_expansion) == after_expansion
     end
 
     test "raises outside match" do
       assert_raise CompileError, ~r"cannot use \^a outside of match clauses", fn ->
-        expand(quote do: ^a)
+        expand(quote(do: ^a))
       end
     end
 
     test "raises without var" do
-      assert_raise CompileError, ~r"invalid argument for unary operator \^, expected an existing variable, got: \^1", fn ->
-        expand(quote do: ^1 = 1)
+      message =
+        ~r"invalid argument for unary operator \^, expected an existing variable, got: \^1"
+
+      assert_raise CompileError, message, fn ->
+        expand(quote(do: ^1 = 1))
       end
     end
 
     test "raises when the var is undefined" do
       assert_raise CompileError, ~r"unbound variable \^foo", fn ->
-        expand(quote do: ^foo = :foo)
+        expand(quote(do: ^foo = :foo))
       end
     end
   end
 
   describe "locals" do
     test "expands to remote calls" do
-      assert {{:., _, [Kernel, :=~]}, _, [{:a, _, []}, {:b, _, []}]} =
-            expand(quote do: a =~ b)
+      assert {{:., _, [Kernel, :=~]}, _, [{:a, _, []}, {:b, _, []}]} = expand(quote(do: a =~ b))
     end
 
     test "in matches" do
-      assert_raise CompileError, ~r"cannot invoke local foo/1 inside match, called as: foo\(:bar\)", fn ->
-        expand(quote do: foo(:bar) = :bar)
+      message = ~r"cannot invoke local foo/1 inside match, called as: foo\(:bar\)"
+
+      assert_raise CompileError, message, fn ->
+        expand(quote(do: foo(:bar) = :bar))
       end
     end
 
     test "in guards" do
-      assert expand(quote(do: fn pid when :erlang.==(pid, self) -> pid end)) |> clean_meta([:import, :context]) ==
-             quote(do: fn pid when :erlang.==(pid, :erlang.self()) -> pid end)
-
-      assert_raise CompileError, ~r"cannot invoke local foo/1 inside guard, called as: foo\(arg\)", fn ->
-        expand(quote do: fn arg when foo(arg) -> arg end)
+      code = quote(do: fn pid when :erlang.==(pid, self) -> pid end)
+      expanded_code = quote(do: fn pid when :erlang.==(pid, :erlang.self()) -> pid end)
+      assert clean_meta(expand(code), [:import, :context]) == expanded_code
+
+      message = ~r"cannot invoke local foo/1 inside guard, called as: foo\(arg\)"
+
+      assert_raise CompileError, message, fn ->
+        expand(quote(do: fn arg when foo(arg) -> arg end))
       end
     end
 
     test "custom imports" do
-      assert expand(quote do: (import Kernel.ExpansionTarget; seventeen)) ==
-             quote do: (:"Elixir.Kernel.ExpansionTarget"; 17)
+      before_expansion =
+        quote do
+          import Kernel.ExpansionTarget
+          seventeen()
+        end
+
+      after_expansion =
+        quote do
+          :"Elixir.Kernel.ExpansionTarget"
+          17
+        end
+
+      assert expand(before_expansion) == after_expansion
     end
   end
 
   describe "tuples" do
     test "expanded as arguments" do
-      assert expand(quote(do: {a = 1, a})) == quote do: {a = 1, a()}
-      assert expand(quote(do: {b, a = 1, a})) == quote do: {b(), a = 1, a()}
+      assert expand(quote(do: {after_expansion = 1, a})) == quote(do: {after_expansion = 1, a()})
+
+      assert expand(quote(do: {b, after_expansion = 1, a})) ==
+               quote(do: {b(), after_expansion = 1, a()})
     end
   end
 
   describe "maps" do
     test "expanded as arguments" do
-      assert expand(quote(do: %{a: a = 1, b: a})) == quote do: %{a: a = 1, b: a()}
+      assert expand(quote(do: %{a: after_expansion = 1, b: a})) ==
+               quote(do: %{a: after_expansion = 1, b: a()})
     end
 
     test "with variables on keys" do
-      assert expand(quote(do: %{x = 1 => 1})) ==
-             quote(do: %{x = 1 => 1})
-
-      assert_raise CompileError,
-        ~r"illegal use of variable x inside map key match,",
-        fn -> expand(quote do: (%{x => 1} = %{})) end
-
-      assert_raise CompileError,
-        ~r"unbound variable \^x",
-        fn -> expand(quote do: ({x, %{^x => 1}} = %{})) end
+      assert expand(quote(do: %{x = 1 => 1})) == quote(do: %{x = 1 => 1})
+
+      assert_raise CompileError, ~r"illegal use of variable x inside map key match,", fn ->
+        expand(quote(do: %{x => 1} = %{}))
+      end
+
+      assert_raise CompileError, ~r"unbound variable \^x", fn ->
+        expand(quote(do: {x, %{^x => 1}} = %{}))
+      end
     end
 
     test "expects key-value pairs" do
       assert_raise CompileError, ~r"expected key-value pairs in a map, got: :foo", fn ->
-        expand(quote do: unquote({:%{}, [], [:foo]}))
+        expand(quote(do: unquote({:%{}, [], [:foo]})))
       end
     end
   end

Code.require_file "../test_helper.exs", __DIR__

defmodule Kernel.ExpansionTarget do
  defmacro seventeen, do: 17


  describe "import" do
    test "raises on invalid macro" do
      message = ~r"cannot import Kernel.invalid/1 because it is undefined or private"

      assert_raise CompileError, message, fn ->
        expand(quote(do: import(Kernel, only: [invalid: 1])))
      end
    end

    test "raises on invalid options" do
      message = ~r"invalid :only option for import, expected a keyword list with integer values"

      assert_raise CompileError, message, fn ->
        expand(quote(do: import(Kernel, only: [invalid: nil])))
      end

      message = ~r"invalid :except option for import, expected a keyword list with integer values"

      assert_raise CompileError, message, fn ->
        expand(quote(do: import(Kernel, except: [invalid: nil])))
      end

      message = ~r/invalid options for import, expected a keyword list, got: "invalid_options"/

      assert_raise CompileError, message, fn ->
        expand(quote(do: import(Kernel, "invalid_options")))
      end
    end

    test "raises on conflicting options" do
      message =
        ~r":only and :except can only be given together to import when :only is either :functions or :macros"

      assert_raise CompileError, message, fn ->
        expand(quote(do: import(Kernel, only: [], except: [])))
      end
    end

    test "invalid import option" do
      assert_raise CompileError, ~r"unsupported option :ops given to import", fn ->
        expand(quote(do: import(:lists, ops: 1)))
      end
    end

    test "raises for non-compile-time module" do
      assert_raise CompileError, ~r"invalid argument for import, .*, got: {:a, :tuple}", fn ->
        expand(quote(do: import({:a, :tuple})))
      end
    end
  end
