Skip to content

Use IsType assertions in test #1570

Closed

Description

@tzdybal

Using IsType is more expressive.

	node := initAndStartNodeWithCleanup(ctx, t, "full")
	require.IsType(t, new(FullNode), node)

Originally posted by @tzdybal in #1544 (comment)
See also: #1544 (comment)

Activity

  1. added
    good first issue
    T:testing
    T:code-hygiene
    and removed on Feb 27, 2024
  2. PrathyushaLakkireddy commented on Feb 28, 2024

    @PrathyushaLakkireddy
    Contributor

    @tzdybal I would like to work on this issue, Thank you.

  3. moved this to In Progress in Evolveon Feb 28, 2024
  4. AryanGodara commented on Feb 28, 2024

    @AryanGodara
    Contributor

    @tzdybal I was thinking that initially,
    I had put assertions like assert.NotNill(fn) to verify the returned object from the helper function.
    But then discussed it with Matthew and came to the conclusion that helper functions (in this case, initAndStartNodeWithCleanup) need to be robust, and in the current state, instead of propagating an error, or returning nil, they simply panic.

    So, is there a need to check the type of the return value, if the program is going to panic beforehand anways, if something goes wrong?

    I'm just curious about this one 😅

  5. tzdybal commented on Feb 28, 2024

    @tzdybal
    ContributorAuthor

    The only reason why I suggested this change is readability. When I first saw this code I was really confused - why type assertion is used when result is discarded anyways? Why there is no error check (second variable)? It was not obvious that this code ensures that returned object is of expected type.

    Switching to (not adding 😉) require.IsType make the intention of the code obvious. This code is way more expressive and still holds all the same assumptions (it will stop if something goes wrong).

  6. added a commit that references this issue on Feb 28, 2024
    a5c1173
  7. moved this from In Progress to Done in Evolveon Feb 28, 2024
  8. AryanGodara commented on Feb 29, 2024

    @AryanGodara
    Contributor

    The only reason why I suggested this change is readability. When I first saw this code I was really confused - why type assertion is used when result is discarded anyways? Why there is no error check (second variable)? It was not obvious that this code ensures that returned object is of expected type.

    Switching to (not adding 😉) require.IsType make the intention of the code obvious. This code is way more expressive and still holds all the same assumptions (it will stop if something goes wrong).

    Aha, I see! You're right, the code did look confusing in the previous state 😅
    Learning new practices everyday :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

T:code-hygieneT:testinggood first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions