Skip to content

pkg/reflectutil: add unit tests for UnknownNonEmptyFields#4919

Open
varsha-0007 wants to merge 2 commits intolima-vm:masterfrom
varsha-0007:add-reflectutil-tests
Open

pkg/reflectutil: add unit tests for UnknownNonEmptyFields#4919
varsha-0007 wants to merge 2 commits intolima-vm:masterfrom
varsha-0007:add-reflectutil-tests

Conversation

@varsha-0007
Copy link
Copy Markdown

pkg/reflectutil has no test file, unlike other utility packages
such as pkg/ptr, pkg/textutil, and pkg/osutil.

This PR adds reflectutil_test.go covering the following cases:

  • Known fields are not reported
  • Unknown non-empty fields are reported
  • Zero/empty values are ignored
  • Pointer-to-struct input works correctly
  • Zero-length slice and map are treated as empty
  • Non-struct input panics as expected

Copy link
Copy Markdown
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

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

Please also address linter errors.

Bonus suggestions from Claude Code for additional test coverage:

  • Multiple unknown fields: every unknown-field assertion uses cmp.Contains(result, "Unknown"), which doesn't check that only Unknown is reported, nor the result's full contents/order. A case with two unknown non-empty fields and an assert.DeepEqual against a sorted/expected slice would catch regressions cmp.Contains won't.
  • Mixed known + unknown: no case where some non-empty fields are known and others aren't (the realistic usage pattern).
  • Nil pointer: UnknownNonEmptyFields((*testStruct)(nil)) calls origVal.Elem() on a nil pointer, then val.NumField() panics. If the documented contract is "panics on nil", add a test; if not, this is a latent bug worth surfacing to the maintainers rather than silently leaving uncovered.

Comment thread pkg/reflectutil/reflectutil_test.go Outdated
Comment thread pkg/reflectutil/reflectutil_test.go Outdated
@varsha-0007
Copy link
Copy Markdown
Author

@jandubois Sure, will look into it!

Thanks for the detailed review! 🙂

Copy link
Copy Markdown
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

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

Please fix linter errors!

Comment thread pkg/reflectutil/reflectutil_test.go Outdated
},
{
// Unknown is non-empty and not in known list — must be reported.
name: "one unknown field",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is actually a mix of one known and one unknown field, and an exact duplicate of the mixed case later in this file.

Comment thread pkg/reflectutil/reflectutil_test.go Outdated
expected: []string{"Meta"},
},
{
// Reviewer suggestion: multiple unknown fields at once.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please don't include "history" comments. We have git blame and GitHub issue and PR comments for that.

Comments in the source code should explain the WHAT and WHY, but not the history behind changes.

defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic but did not panic")
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe add a check on the panic message, so you are not silently accepting a different panic than the one you are expecting?

Comment thread pkg/reflectutil/reflectutil_test.go Outdated
)

// testStruct is used as the input type for all test cases.
// Count field removed — it was unused in any test case (reviewer feedback).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't explain the history.

Suggested change
// Count field removed — it was unused in any test case (reviewer feedback).

@varsha-0007 varsha-0007 requested a review from jandubois May 3, 2026 04:57
@jandubois
Copy link
Copy Markdown
Member

You have fixed the linter errors but have not addressed my feedback.

@varsha-0007
Copy link
Copy Markdown
Author

@jandubois, will do it now!!

Copy link
Copy Markdown
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

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

There still is a linter error, and please squash your commits!

@varsha-0007 varsha-0007 force-pushed the add-reflectutil-tests branch from 3dd6d28 to c37de27 Compare May 5, 2026 14:02
Comment thread pkg/reflectutil/reflectutil.go Outdated
@@ -1,18 +1,31 @@
// SPDX-FileCopyrightText: Copyright The Lima Authors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's up with all the random blank lines you have inserted?

If you create/update your changes with AI, then you MUST review them before submitting them to the PR. Please don't waste reviewer time; otherwise it will be faster if we use AI ourselves to create these kind of PRs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right, I should have reviewed the changes more carefully before pushing. I apologize for wasting your time. Fixing now.

@varsha-0007 varsha-0007 force-pushed the add-reflectutil-tests branch 2 times, most recently from 740f1c0 to d4c5cdd Compare May 6, 2026 16:01
@varsha-0007 varsha-0007 closed this May 6, 2026
@varsha-0007 varsha-0007 force-pushed the add-reflectutil-tests branch from d4c5cdd to 580866d Compare May 6, 2026 16:05
Signed-off-by: varsha <varsha.narvi.07@gmail.com>
@varsha-0007 varsha-0007 reopened this May 6, 2026
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 this pull request may close these issues.

2 participants