From 956b03422df96dff8dfca0af419dd17167633947 Mon Sep 17 00:00:00 2001 From: Jeff McKenzie Date: Fri, 1 Nov 2024 09:37:05 -0500 Subject: [PATCH] [DEV] Update XML tests to eliminate false failures (#206) --- test/http_test.exs | 7 ++- test/xml/encoder_test.exs | 90 +++++++++++++++++++++++++++++++++++---- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/test/http_test.exs b/test/http_test.exs index 549959f..4515353 100644 --- a/test/http_test.exs +++ b/test/http_test.exs @@ -46,8 +46,11 @@ defmodule Braintree.HTTPTest do test "encode_body/1 converts the request body to xml" do params = %{company: "Soren", first_name: "Parker"} - assert HTTP.encode_body(params) == - ~s|\nSoren\nParker| + assert [xml_tag | nodes] = params |> HTTP.encode_body() |> String.split("\n") + + assert xml_tag == ~s|| + assert ~s|Soren| in nodes + assert ~s|Parker| in nodes end test "encode_body/1 ignores empty bodies" do diff --git a/test/xml/encoder_test.exs b/test/xml/encoder_test.exs index 2c4d6c6..fd2aa0f 100644 --- a/test/xml/encoder_test.exs +++ b/test/xml/encoder_test.exs @@ -5,21 +5,95 @@ defmodule Braintree.XML.EncoderTest do import Braintree.XML.Encoder, only: [dump: 1] + @xml_tag ~s|| + describe "dump/1" do test "with content" do - assert dump(%{company: "Soren", first_name: "Parker"}) == - ~s|\nSoren\nParker| + assert [xml_tag | nodes] = + %{company: "Soren", first_name: "Parker"} + |> dump() + |> String.split("\n") + + # Order isn't guaranteed when iterating on a map. + assert xml_tag == @xml_tag + assert ~s|Soren| in nodes + assert ~s|Parker| in nodes end test "with children" do - assert dump(%{company: "Soren", nested: %{name: "Parker"}}) == - ~s|\nSoren\n\nParker\n| + assert [xml_tag | nodes] = + %{company: "Soren", nested: %{name: "Parker"}} + |> dump() + |> String.split("\n") + + assert xml_tag == @xml_tag + + assert nodes == ~w[Soren Parker ] or + nodes == ~w[ Parker Soren] + + assert [xml_tag | nodes] = + %{company: "Soren", nested: [%{name: "Parker"}, %{name: "Shannon"}]} + |> dump() + |> String.split("\n") + + assert xml_tag == @xml_tag + + assert nodes in [ + [ + "Soren", + ~s||, + "", + "Parker", + "", + "", + "Shannon", + "", + "" + ], + [ + ~s||, + "", + "Parker", + "", + "", + "Shannon", + "", + "", + "Soren" + ] + ] + + assert [xml_tag | nodes] = + %{company: "Soren", pets: ["cat", "dog"]} + |> dump() + |> String.split("\n") - assert dump(%{company: "Soren", nested: [%{name: "Parker"}, %{name: "Shannon"}]}) == - ~s|\nSoren\n\n\nParker\n\n\nShannon\n\n| + assert xml_tag == @xml_tag - assert dump(%{company: "Soren", pets: ["cat", "dog"]}) == - ~s|\nSoren\n\n\ncat\n\n\ndog\n\n| + assert nodes in [ + [ + "Soren", + ~s||, + "", + "cat", + "", + "", + "dog", + "", + "" + ], + [ + ~s||, + "", + "cat", + "", + "", + "dog", + "", + "", + "Soren" + ] + ] end end end