Repository: keathley/norm
Pull request #40 (2020-01-31 15:58:25)

Implement inspect for the other structs in Norm


Patch:
diff --git a/lib/norm/schema.ex b/lib/norm/schema.ex
index 295cf63..b51accd 100644
--- a/lib/norm/schema.ex
+++ b/lib/norm/schema.ex
@@ -136,4 +136,17 @@ defmodule Norm.Schema do
       end
     end
   end
+
+  defimpl Inspect do
+    import Inspect.Algebra
+
+    def inspect(schema, opts) do
+      map = if schema.struct do
+        struct(schema.struct, schema.specs)
+      else
+        schema.specs
+      end
+      concat(["#Norm.Schema<", to_doc(map, opts), ">"])
+    end
+  end
 end
diff --git a/lib/norm/spec/alt.ex b/lib/norm/spec/alt.ex
index c212427..710de0c 100644
--- a/lib/norm/spec/alt.ex
+++ b/lib/norm/spec/alt.ex
@@ -54,4 +54,12 @@ defmodule Norm.Spec.Alt do
       end
     end
   end
+
+  defimpl Inspect do
+    import Inspect.Algebra
+
+    def inspect(alt, opts) do
+      concat(["#Norm.Alt<", to_doc(alt.specs, opts), ">"])
+    end
+  end
 end
diff --git a/lib/norm/spec/collection.ex b/lib/norm/spec/collection.ex
index 09ddbdf..1fa91e4 100644
--- a/lib/norm/spec/collection.ex
+++ b/lib/norm/spec/collection.ex
@@ -118,4 +118,12 @@ defmodule Norm.Spec.Collection do
       end
     end
   end
+
+  defimpl Inspect do
+    import Inspect.Algebra
+
+    def inspect(coll_of, opts) do
+      concat(["#Norm.CollOf<", to_doc(coll_of.spec, opts), ">"])
+    end
+  end
 end
diff --git a/lib/norm/spec/selection.ex b/lib/norm/spec/selection.ex
index 252bf35..7a9062c 100644
--- a/lib/norm/spec/selection.ex
+++ b/lib/norm/spec/selection.ex
@@ -160,4 +160,16 @@ defmodule Norm.Spec.Selection do
       end
     end
   end
+
+  defimpl Inspect do
+    import Inspect.Algebra
+
+    def inspect(selection, opts) do
+      map = %{
+        schema: selection.schema,
+        required: selection.required
+      }
+      concat(["#Norm.Selection<", to_doc(map, opts), ">"])
+    end
+  end
 end
diff --git a/lib/norm/spec/union.ex b/lib/norm/spec/union.ex
index 9f15bb0..2d8407f 100644
--- a/lib/norm/spec/union.ex
+++ b/lib/norm/spec/union.ex
@@ -51,4 +51,12 @@ defmodule Norm.Spec.Union do
       end
     end
   end
+
+  defimpl Inspect do
+    import Inspect.Algebra
+
+    def inspect(union, opts) do
+      concat(["#Norm.OneOf<", to_doc(union.specs, opts), ">"])
+    end
+  end
 end


Test patch:
diff --git a/test/norm/alt_test.exs b/test/norm/alt_test.exs
index 732fb76..e5e058b 100644
--- a/test/norm/alt_test.exs
+++ b/test/norm/alt_test.exs
@@ -20,4 +20,13 @@ defmodule Norm.Spec.AltTest do
       end
     end
   end
+
+  describe "inspect" do
+    test "alts" do
+      spec = alt(s: spec(is_binary()), i: spec(is_integer()))
+
+      assert inspect(spec) ==
+               "#Norm.Alt<[s: #Norm.Spec<is_binary()>, i: #Norm.Spec<is_integer()>]>"
+    end
+  end
 end
diff --git a/test/norm/schema_test.exs b/test/norm/schema_test.exs
index 84c105e..cf37423 100644
--- a/test/norm/schema_test.exs
+++ b/test/norm/schema_test.exs
@@ -222,4 +222,15 @@ defmodule Norm.SchemaTest do
       end
     end
   end
+
+  describe "inspect" do
+    test "map schemas" do
+      s = schema(%{name: spec(is_binary()), age: spec(is_integer())})
+      assert inspect(s) == "#Norm.Schema<%{age: #Norm.Spec<is_integer()>, name: #Norm.Spec<is_binary()>}>"
+    end
+
+    test "struct schema" do
+      assert inspect(User.s()) == "#Norm.Schema<%Norm.SchemaTest.User{age: #Norm.Spec<is_integer() and &(&1 >= 0)>, email: #Norm.Spec<is_binary()>, name: #Norm.Spec<is_binary()>}>"
+    end
+  end
 end
diff --git a/test/norm/selection_test.exs b/test/norm/selection_test.exs
index 438ba51..1287c4a 100644
--- a/test/norm/selection_test.exs
+++ b/test/norm/selection_test.exs
@@ -191,4 +191,11 @@ defmodule Norm.SelectionTest do
       end
     end
   end
+
+  describe "inspect" do
+    test "single selection" do
+      assert inspect(selection(user_schema())) == "#Norm.Selection<%{required: [:name, :email, :age], schema: #Norm.Schema<%{age: #Norm.Spec<is_integer() and &(&1 > 0)>, email: #Norm.Spec<is_binary() and &(&1 =~ ~r\"@\")>, name: #Norm.Spec<is_binary()>}>}>"
+      assert inspect(selection(user_schema(), [:name])) == "#Norm.Selection<%{required: [:name], schema: #Norm.Schema<%{age: #Norm.Spec<is_integer() and &(&1 > 0)>, email: #Norm.Spec<is_binary() and &(&1 =~ ~r\"@\")>, name: #Norm.Spec<is_binary()>}>}>"
+    end
+  end
 end
diff --git a/test/norm/spec/collection_test.exs b/test/norm/spec/collection_test.exs
new file mode 100644
index 0000000..929ed81
--- /dev/null
+++ b/test/norm/spec/collection_test.exs
@@ -0,0 +1,9 @@
+defmodule Norm.Spec.CollectionTest do
+  use ExUnit.Case, async: true
+  import Norm
+
+  test "inspect" do
+    spec = coll_of(spec(is_atom()))
+    assert inspect(spec) == "#Norm.CollOf<#Norm.Spec<is_atom()>>"
+  end
+end
diff --git a/test/norm/union_test.exs b/test/norm/union_test.exs
index 658449b..c34da90 100644
--- a/test/norm/union_test.exs
+++ b/test/norm/union_test.exs
@@ -40,4 +40,9 @@ defmodule Norm.UnionTest do
       end
     end
   end
+
+  test "inspect" do
+    union = one_of([:foo, spec(is_binary())])
+    assert inspect(union) == "#Norm.OneOf<[:foo, #Norm.Spec<is_binary()>]>"
+  end
 end