From 1ea6f9bea243039e8e8261a95746714314c9e4ac Mon Sep 17 00:00:00 2001 From: Daniel Spofford Date: Tue, 28 May 2019 20:40:24 -0500 Subject: [PATCH] DRY out symmetric serialization testing - use `Serializable.assert_symmetry/2` everywhere to reduce repetition --- lib/harald/serializable.ex | 12 +++++++--- .../manufacturer_data/apple_test.exs | 17 ++++---------- .../data_type/manufacturer_data_test.exs | 17 ++++---------- test/harald/data_type/service_data_test.exs | 17 ++++---------- .../hci/event/inquiry_complete_test.exs | 23 +++++-------------- .../event/le_meta/advertising_report_test.exs | 21 ++++------------- test/harald/hci/event/le_meta_test.exs | 10 ++++---- test/harald/hci/event_test.exs | 17 ++++---------- test/harald/hci_test.exs | 15 +++--------- 9 files changed, 48 insertions(+), 101 deletions(-) diff --git a/lib/harald/serializable.ex b/lib/harald/serializable.ex index ad4bc39..ac55272 100644 --- a/lib/harald/serializable.ex +++ b/lib/harald/serializable.ex @@ -17,9 +17,15 @@ defmodule Harald.Serializable do import ExUnit.Assertions, only: [assert: 1, assert: 2] case mod.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == mod.serialize(data) - {:error, data} when not is_binary(data) -> true - {:error, bin2} -> assert bin == bin2 + {:ok, data} -> + assert {:ok, bin2} = mod.serialize(data) + assert :binary.bin_to_list(bin) == :binary.bin_to_list(bin2) + + {:error, data} when not is_binary(data) -> + true + + {:error, bin2} -> + assert :binary.bin_to_list(bin) == :binary.bin_to_list(bin2) end end end diff --git a/test/harald/data_type/manufacturer_data/apple_test.exs b/test/harald/data_type/manufacturer_data/apple_test.exs index 88ce49b..9045752 100644 --- a/test/harald/data_type/manufacturer_data/apple_test.exs +++ b/test/harald/data_type/manufacturer_data/apple_test.exs @@ -3,22 +3,15 @@ defmodule Harald.DataType.ManufacturerData.AppleTest do use ExUnitProperties alias Harald.Generators.DataType.ManufacturerData.Apple, as: AppleGen alias Harald.DataType.ManufacturerData.Apple + require Harald.Serializable, as: Serializable doctest Apple, import: true property "symmetric (de)serialization" do - check all bin <- AppleGen.binary() do - case Apple.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == Apple.serialize(data) - {:error, _} -> :ok - end - end - - check all bin <- StreamData.binary() do - case Apple.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == Apple.serialize(data) - {:error, _} -> :ok - end + check all bin <- AppleGen.binary(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(Apple, bin) + Serializable.assert_symmetry(Apple, rand_bin) end end end diff --git a/test/harald/data_type/manufacturer_data_test.exs b/test/harald/data_type/manufacturer_data_test.exs index 13995c4..4b5e9be 100644 --- a/test/harald/data_type/manufacturer_data_test.exs +++ b/test/harald/data_type/manufacturer_data_test.exs @@ -3,22 +3,15 @@ defmodule Harald.DataType.ManufacturerDataTest do use ExUnitProperties alias Harald.Generators.DataType.ManufacturerData, as: ManufacturerDataGen alias Harald.DataType.ManufacturerData + require Harald.Serializable, as: Serializable doctest ManufacturerData, import: true property "symmetric (de)serialization" do - check all bin <- ManufacturerDataGen.binary() do - case ManufacturerData.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == ManufacturerData.serialize(data) - {:error, _} -> :ok - end - end - - check all bin <- StreamData.binary() do - case ManufacturerData.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == ManufacturerData.serialize(data) - {:error, _} -> :ok - end + check all bin <- ManufacturerDataGen.binary(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(ManufacturerData, bin) + Serializable.assert_symmetry(ManufacturerData, rand_bin) end end end diff --git a/test/harald/data_type/service_data_test.exs b/test/harald/data_type/service_data_test.exs index 1f48a54..4ebce7c 100644 --- a/test/harald/data_type/service_data_test.exs +++ b/test/harald/data_type/service_data_test.exs @@ -3,22 +3,15 @@ defmodule Harald.DataType.ServiceDataTest do use ExUnitProperties alias Harald.Generators.DataType.ServiceData, as: ServiceDataGen alias Harald.DataType.ServiceData + require Harald.Serializable, as: Serializable doctest ServiceData, import: true property "symmetric (de)serialization" do - check all bin <- ServiceDataGen.binary() do - case ServiceData.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == ServiceData.serialize(data) - {:error, _} -> :ok - end - end - - check all bin <- StreamData.binary() do - case ServiceData.deserialize(bin) do - {:ok, data} -> assert {:ok, bin} == ServiceData.serialize(data) - {:error, _} -> :ok - end + check all bin <- ServiceDataGen.binary(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(ServiceData, bin) + Serializable.assert_symmetry(ServiceData, rand_bin) end end end diff --git a/test/harald/hci/event/inquiry_complete_test.exs b/test/harald/hci/event/inquiry_complete_test.exs index 3c7d7c7..2590ba5 100644 --- a/test/harald/hci/event/inquiry_complete_test.exs +++ b/test/harald/hci/event/inquiry_complete_test.exs @@ -3,26 +3,15 @@ defmodule Harald.HCI.Event.InquiryCompleteTest do use ExUnitProperties alias Harald.Generators.HCI.Event.InquiryComplete, as: InquiryCompleteGen alias Harald.HCI.Event.InquiryComplete + require Harald.Serializable, as: Serializable doctest InquiryComplete, import: true - property "symmetric serialization" do - check all parameters <- InquiryCompleteGen.parameters() do - case InquiryComplete.deserialize(parameters) do - {:ok, data} -> - assert {:ok, bin} = InquiryComplete.serialize(data) - assert :binary.bin_to_list(parameters) == :binary.bin_to_list(bin) - - {:error, _} -> - :ok - end - end - - check all parameters <- StreamData.binary() do - case InquiryComplete.deserialize(parameters) do - {:ok, data} -> assert {:ok, parameters} == InquiryComplete.serialize(data) - {:error, _} -> :ok - end + property "symmetric (de)serialization" do + check all bin <- InquiryCompleteGen.parameters(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(InquiryComplete, bin) + Serializable.assert_symmetry(InquiryComplete, rand_bin) end end end diff --git a/test/harald/hci/event/le_meta/advertising_report_test.exs b/test/harald/hci/event/le_meta/advertising_report_test.exs index b7b98fd..5e40f8e 100644 --- a/test/harald/hci/event/le_meta/advertising_report_test.exs +++ b/test/harald/hci/event/le_meta/advertising_report_test.exs @@ -3,26 +3,15 @@ defmodule Harald.HCI.Event.LEMeta.AdvertisingReportTest do use ExUnitProperties alias Harald.Generators.HCI.Event.LEMeta.AdvertisingReport, as: AdvertisingReportGen alias Harald.HCI.Event.LEMeta.AdvertisingReport + require Harald.Serializable, as: Serializable doctest AdvertisingReport, import: true property "symmetric (de)serialization" do - check all parameters <- AdvertisingReportGen.parameters() do - case AdvertisingReport.deserialize(parameters) do - {:ok, data} -> - assert {:ok, bin} = AdvertisingReport.serialize(data) - assert :binary.bin_to_list(parameters) == :binary.bin_to_list(bin) - - {:error, _} -> - :ok - end - end - - check all parameters <- StreamData.binary() do - case AdvertisingReport.deserialize(parameters) do - {:ok, data} -> assert {:ok, parameters} == AdvertisingReport.serialize(data) - {:error, _} -> :ok - end + check all bin <- AdvertisingReportGen.parameters(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(AdvertisingReport, bin) + Serializable.assert_symmetry(AdvertisingReport, rand_bin) end end end diff --git a/test/harald/hci/event/le_meta_test.exs b/test/harald/hci/event/le_meta_test.exs index d2f6845..6d1151d 100644 --- a/test/harald/hci/event/le_meta_test.exs +++ b/test/harald/hci/event/le_meta_test.exs @@ -3,15 +3,15 @@ defmodule Harald.HCI.Event.LEMetaTest do use ExUnitProperties alias Harald.Generators.HCI.Event.LEMeta, as: LEMetaGen alias Harald.HCI.Event.LEMeta + require Harald.Serializable, as: Serializable doctest LEMeta, import: true property "symmetric (de)serialization" do - check all parameters <- LEMetaGen.parameters() do - case LEMeta.deserialize(parameters) do - {:ok, data} -> assert {:ok, parameters} == LEMeta.serialize(data) - {:error, _} -> :ok - end + check all bin <- LEMetaGen.parameters(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(LEMeta, bin) + Serializable.assert_symmetry(LEMeta, rand_bin) end end end diff --git a/test/harald/hci/event_test.exs b/test/harald/hci/event_test.exs index 3c0b662..ab75470 100644 --- a/test/harald/hci/event_test.exs +++ b/test/harald/hci/event_test.exs @@ -3,22 +3,15 @@ defmodule Harald.HCI.EventTest do use ExUnitProperties alias Harald.Generators.HCI.Event, as: EventGen alias Harald.HCI.Event + require Harald.Serializable, as: Serializable doctest Event, import: true property "symmetric (de)serialization" do - check all parameters <- EventGen.binary() do - case Event.deserialize(parameters) do - {:ok, data} -> assert {:ok, parameters} == Event.serialize(data) - {:error, _} -> :ok - end - end - - check all parameters <- StreamData.binary() do - case Event.deserialize(parameters) do - {:ok, data} -> assert {:ok, parameters} == Event.serialize(data) - {:error, _} -> :ok - end + check all bin <- EventGen.binary(), + rand_bin <- StreamData.binary() do + Serializable.assert_symmetry(Event, bin) + Serializable.assert_symmetry(Event, rand_bin) end end end diff --git a/test/harald/hci_test.exs b/test/harald/hci_test.exs index 0d10f9e..5344428 100644 --- a/test/harald/hci_test.exs +++ b/test/harald/hci_test.exs @@ -51,19 +51,10 @@ defmodule Harald.HCITest do end property "symmetric (de)serialization" do - check all parameters <- HCIGen.packet() do - case HCI.deserialize(parameters) do - {:ok, data} -> - assert {:ok, bin} = HCI.serialize(data) - assert :binary.bin_to_list(parameters) == :binary.bin_to_list(bin) - - {:error, _} -> - :ok - end - end - - check all bin <- StreamData.binary() do + check all bin <- HCIGen.packet(), + rand_bin <- StreamData.binary() do Serializable.assert_symmetry(HCI, bin) + Serializable.assert_symmetry(HCI, rand_bin) end end end