Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONKeyEqual will give confusing error message when comparing JSON numbers #278

Closed
ShadowJonathan opened this issue Dec 26, 2021 · 3 comments · Fixed by #552
Closed

JSONKeyEqual will give confusing error message when comparing JSON numbers #278

ShadowJonathan opened this issue Dec 26, 2021 · 3 comments · Fixed by #552

Comments

@ShadowJonathan
Copy link
Contributor

MatchResponse key 'matrix:image:size' got '2239' want '2239'

This is when using JSONKeyEqual on a match.JSON array.

Changing the error message section to the following;

  if !reflect.DeepEqual(gotValue, wantValue) {
    return fmt.Errorf("key '%s' got '%v' (%T) want '%v' (%T)", wantKey, gotValue, gotValue, wantValue, wantValue)
  }

Yields this more useful error message;

MatchResponse key 'matrix:image:size' got '2239' (float64) want '2239' (int)

This behaviour should probably be accounted for when extracting values with JSONKeyEqual, so either JSONKeyEqual will have to coerce all numerical types into float64, or give this above more-clear error message.

@kegsay
Copy link
Member

kegsay commented Jan 6, 2022

Bleh, thanks for pointing this out. Agreed, test writers should not need to know or care about this.

@ShadowJonathan
Copy link
Contributor Author

I got tripped up by this again today:

		PLContent := map[string]interface{}{
			"invite": 100,
			"users": map[string]interface{}{
				alice.UserID: 100,
			},
		}

		eventId := alice.SendEventSynced(t, roomID, b.Event{
			Type:     "m.room.power_levels",
			StateKey: b.Ptr(""),
			Content:  PLContent,
		})

		res := alice.MustDoFunc(
			t,
			"GET",
			[]string{"_matrix", "client", "v3", "rooms", roomID, "event", eventId},
		)

		must.MatchResponse(t, res, match.HTTPResponse{
			JSON: []match.JSON{
				match.JSONKeyEqual("content", PLContent),
			},
		})

MatchResponse key 'content' got 'map[invite:100 users:map[@alice:hs1:100]]' want 'map[invite:100 users:map[@alice:hs1:100]]'

This fails because the integer inside the powerlevel event's content is converted to float64, so doing a roundtrip is not possible.

@ShadowJonathan
Copy link
Contributor Author

One thing we can do is do a canonicalisation roundtrip by Marshalling and Unmarshal the compared-to interface{}, to make sure it matches the type quirks that reflect.DeepEquals needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants