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

Refactor assertString: Faster, less nested and more consistent. #2372

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

Conversation

EmersonRabelo
Copy link

@profnandaa,
I have a possible improvement for the assertString.
I am reducing the number of necessary validations and also a more assertive approach, in my point of view (if this approach doesn't make sense, I would appreciate feedback on it, if possible).
The performance has also improved, it wasn't something incredible, but it was considerable.

image

Unit tests:
image

Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

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

I think the tests are failing because we currently do not throw an error for '' (an empty string).
Also; can you commit the unit tests as well?

Copy link

codecov bot commented Feb 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b958bd7) 99.95% compared to head (f8e62de) 99.95%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2372      +/-   ##
==========================================
- Coverage   99.95%   99.95%   -0.01%     
==========================================
  Files         107      107              
  Lines        2454     2445       -9     
  Branches      619      617       -2     
==========================================
- Hits         2453     2444       -9     
  Partials        1        1              

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

@EmersonRabelo
Copy link
Author

EmersonRabelo commented Feb 9, 2024

Hi, @WikiRik!
I made a small change in the code and also committed the unit tests.
But it failed in one of the tests.

image

@WikiRik
Copy link
Member

WikiRik commented Feb 9, 2024

codecov/project will be fixed with #2341

});

it('Should not throw an error if the argument is a string', () => {
assert.doesNotThrow(() => { assertString('testing'); });
Copy link
Member

Choose a reason for hiding this comment

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

Can we keep the empty string unit test? Then we will not forget that we accept that on purpose

Copy link
Author

Choose a reason for hiding this comment

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

Yes, we can. I'll create and commit it.

Copy link
Author

Choose a reason for hiding this comment

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

Now we've this unit teste:
image

Copy link
Contributor

Choose a reason for hiding this comment

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

while we are at it, we should likely also test cases for null and undefined.

Maybe NaN too? And I guess true and false should also be tested against, for full coverage of all types :-)

Copy link
Author

Choose a reason for hiding this comment

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

I created the null input validation and also added the suggested tests. I tried to add the following test:

it('Should not throw an error if the argument is a String instance', () => { assert.doesNotThrow(() => { assertString(new String('antidisestablishmentarianism')); }); });
But some ESLint rules prevent this approach, ESLint "no-new-wrappers.

WikiRik
WikiRik previously approved these changes Feb 9, 2024
Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

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

This looks good to me

rubiin
rubiin previously approved these changes Feb 9, 2024
Copy link
Member

@rubiin rubiin left a comment

Choose a reason for hiding this comment

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

LGTM

@pano9000
Copy link
Contributor

pano9000 commented Feb 9, 2024

when you pass literally null, it gives

Uncaught TypeError: Cannot read properties of null (reading 'constructor')

because it is not caught by the first check.

Should we explicitly check for null there as well?


throw new TypeError(`Expected a string but received a ${invalidType}`);
}
if (input === undefined) throw new TypeError(`Expected a string but received a ${input}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

should probably check for null as well, see #2372 (comment)

Copy link
Author

Choose a reason for hiding this comment

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

I will add null validation and create tests for both cases.

});

it('Should not throw an error if the argument is a string', () => {
assert.doesNotThrow(() => { assertString('testing'); });
Copy link
Contributor

Choose a reason for hiding this comment

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

while we are at it, we should likely also test cases for null and undefined.

Maybe NaN too? And I guess true and false should also be tested against, for full coverage of all types :-)

@EmersonRabelo
Copy link
Author

Hello, @pano9000!
Do you have any further comments on this or is everything okay?
If necessary, I will make the alterations!

Copy link
Contributor

@pano9000 pano9000 left a comment

Choose a reason for hiding this comment

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

should be ok now :-)
(however my approval is rather useless, as I am not a maintainer)

@EmersonRabelo EmersonRabelo dismissed stale reviews from rubiin and WikiRik via f8e62de June 1, 2024 14:35
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.

4 participants