Skip to content

Improve testJSONMarshal #3519

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

exageraldo
Copy link
Contributor

@exageraldo exageraldo commented Mar 17, 2025

Fixes: #2699

Copy link

google-cla bot commented Mar 17, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link

codecov bot commented Mar 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.24%. Comparing base (6a7684f) to head (0ded469).
Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3519   +/-   ##
=======================================
  Coverage   91.23%   91.24%           
=======================================
  Files         183      183           
  Lines       16053    16063   +10     
=======================================
+ Hits        14646    14656   +10     
  Misses       1231     1231           
  Partials      176      176           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@exageraldo exageraldo force-pushed the improve-testJSONMarshal branch from 07dba43 to 5eb1677 Compare March 18, 2025 17:50
@gmlewis
Copy link
Collaborator

gmlewis commented Apr 7, 2025

@exageraldo - is this still a draft PR? If not, can you please remove the draft status?

@exageraldo exageraldo marked this pull request as ready for review April 23, 2025 23:54
Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @exageraldo!
LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

@stevehipwell - might you have time for a code review? Thank you!

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Apr 24, 2025
Copy link
Contributor

@stevehipwell stevehipwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @exageraldo, I'm familiar with this issue as I had to implement my own solutions (marshal & unmarshal) for this when I refactored the rules and a global fix would be great!

Based on my experience think your implementation of testJSONMarshal is an improvement but still doesn't quite keep the marshal and unmarshal concerns separate. I think a function to test both marshal and unmarshal independently would look something like this.

func testJSONMarshal(t *testing.T, v any, want string) {
	t.Helper()

	gotBytes, err := json.Marshal(v)
	if err != nil {
		t.Errorf("Unable to marshal JSON for %#v", v)
	}

	if diff := cmp.Diff(want, string(gotBytes)); diff != "" {
		t.Errorf(
			"json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v",
			gotBytes,
			want,
			diff,
		)
	}

	var gotAny any
	err = json.Unmarshal([]byte(want), gotAny)
	if err != nil {
		t.Errorf("Unable to unmarshal JSON %v: %v", want, err)
	}

	if diff := cmp.Diff(v, gotAny); diff != "" {
		t.Errorf(
			"json.Unmarshal returned:\n%#v\nwant:\n%#v\ndiff:\n%v",
			gotAny,
			v,
			diff,
		)
	}
}

If this was greenfield I'd suggest splitting the test methods up and have a seperate test method for marshal and unmarshal, but the above signature matches the current state of play.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsReview PR is awaiting a review before merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

about testJSONMarshal's behavior
4 participants