smarty / assertions

Fluent assertion-style functions used by goconvey and gunit. Can also be used in any test or application.
Other
100 stars 34 forks source link

ShouldEqualJSON does not work with json arrays #37

Closed xamenyap closed 4 years ago

xamenyap commented 4 years ago

Hi, thanks for the awesome lib :)

I just found a small issue with ShouldEqualJSON. The sample test below will fail

func TestSmartyStreetsJsonEqual(t *testing.T) {
    Convey("Test json equal", t, func() {
        actual := `
        {
          "items": [
            {
              "id": 1,
              "sku": "sku-123"
            },
            {
              "id": 2,
              "sku": "sku-456"
            }
          ]
        }
        `
        expected := `
        {
          "items": [
            {
              "id": 2,
              "sku": "sku-456"
            },
            {
              "id": 1,
              "sku": "sku-123"
            }
          ]
        }
        `
        So(actual, assertions.ShouldEqualJSON, expected)
    })
}

It looks like under the hood it's comparing two string [{"id": 1, "sku": "sku-123"}, {"id": 2, "sku": "sku-456"}] and [{"id": 2, "sku": "sku-456"}, {"id": 1, "sku": "sku-123"}], hence the failed test. Is this the expected behavior of ShouldEqualJSON? Right now I can bypass this by unmarshalling the json array and compare their elements, but it would be nice if we can have some assertions function to do this. What do you think?

mdwhatcott commented 4 years ago

From json.org:

An array is an ordered collection of values.

In order for two JSON arrays to be deemed equal they must have the same length and their items at each index should also be deemed equal (ordering is part of the assertion). So, the assertion above must fail.

mdwhatcott commented 4 years ago

@xamenyap - Glad you're enjoying the libraries!

mdwhatcott commented 4 years ago

https://github.com/smartystreets/assertions/issues/35

xamenyap commented 4 years ago

thanks for the explanation 👍