-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
V2: WIP: Tests for Replacer #2675
Conversation
Just noticed that I based the test on the old implementation. Updated it now to the new one (just used string(phOpen) instead of phOpen) and... Seems that it always missed one, because my test string has no spaces between the } and {. This worked with your previous implementation. Fix is in this branch https://github.com/aligator/caddy/tree/v2-fix-replacer but there seems to be at least one other bug because the second test fails even with the fix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice start! I like that you're using table-driven tests.
Question: Should I test only through the Interface or the replacer type directly?
I would test the concrete replacer
type, since in the tests we actually care about the implementation.
Here are a few ideas for things we need to test:
-
NewReplacer()
shouldmake
thestatic
map and set two initial providers: https://sourcegraph.com/github.com/caddyserver/caddy@b44a22a9d45443aa736a04495dfe716b62b58adc/-/blob/replacer.go#L33 -
Map()
should add the given function to the providers list. https://sourcegraph.com/github.com/caddyserver/caddy@b44a22a9d45443aa736a04495dfe716b62b58adc/-/blob/replacer.go#L51 -
Set()
should add the key and value to the static map. https://sourcegraph.com/github.com/caddyserver/caddy@b44a22a9d45443aa736a04495dfe716b62b58adc/-/blob/replacer.go#L56 -
Delete()
should remove the key from the static map. https://sourcegraph.com/github.com/caddyserver/caddy@b44a22a9d45443aa736a04495dfe716b62b58adc/-/blob/replacer.go#L62 -
ReplaceAll()
should work on any and all inputs, meaning that valid placeholders should be replaced, respectingempty
if set; and invalid/unrecognized placeholders should not be replaced. https://sourcegraph.com/github.com/caddyserver/caddy@b44a22a9d45443aa736a04495dfe716b62b58adc/-/blob/replacer.go#L75
In unit tests, don't call ReplaceAll
when what you are testing is Set
or Delete
or anything other than ReplaceAll
. Instead, to test Set
, call Set()
and then check the static
map to ensure the value was set.
This should be replaced as {lTEST1REPLACEMENT
Ok, thank you for your suggestions. I will update my tests accordingly. I just added one for something like {l{test1}... |
Excellent. :) That doesn't surprise me. You can either propose a fix in this PR or I can commit a fix. I'll be happy with either option. |
5c118f3
to
6b02474
Compare
@mholt could you please review this? I rewrote the tests and added new ones. Also I included two little fixes for the replacer logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates! Looking much better.
}, | ||
{ | ||
variable: "äöü", | ||
value: "öö_äü", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thinking to test unicode
as new should return a replace with provider which defines global replacements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Are you ready for this to be merged?
Yes, you can merge this. |
Thanks for your contribution! It's nice to have that under some testing. |
name: Tests for Replacer
about: Testing the Replacer
1. What does this change do, exactly?
This PR adds tests for the Replacer.
Currently there are tests for Set and Delete. (Both use ReplaceAll to check if they work, but I also want to write an extra test for it.)
Question: Should I test only through the Interface or the replacer type directly?
2. Please link to the relevant issues.
#2673
#2674
3. Which documentation changes (if any) need to be made because of this PR?
--
4. Checklist