-
-
Notifications
You must be signed in to change notification settings - Fork 86
TextMatcher should not match @null@ inside pattern #45
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
Conversation
Indeed matching @null@ in TextMatcher have no sense.
(typped from head) should fix issue you found. Could you check that? |
That stops the exception, but in a different way. See the output from your change:
Whereas when the
By matching with the most appropriate matcher an not attempting to match when it is guaranteed to fail, you get a more meaningful "non-match" message, and less work done to get there. I also think it's a bit more readable, but that's subjective. Your thoughts? |
I totally agree. We need make error messages as clean as possible I'm just not sure if I like the way how you exclude text patterns from being matched.
This should solve issue with not descriptive error messages. @adamquaile what do You think? |
@norzechowicz What is it you don't like about the way it's excluded? Do you think it's too specific to this bug / use-case? In my mind, the point of the If we decide to take it out of |
@adamquaile yea I just dont like the fact that text can't be matched because there is a @null@ anywhere in this text. |
I agree with you about the excluding json/xml them; if there are any use-cases for this, I'm sure they're edge-cases. Although I would give thought to other formats you might support in the future. It's not really likely to create text which is accidentally JSON or XML, but maybe other formats for example YML maybe? I think Anyway, that's probably a small case and getting a bit off topic. Just something to think about.
I see your point. How do you think matching |
Pattern: About YAML support, IMO everything that can be parsed by YAML parser is a valid YAML file so it should be matched by YAML matcher. |
Gotcha. Happy with this.
Agreed, although I wouldn't say 100% that it should be disallowed matching by another format.
All things considered, I think you're right, and we should exclude the text matcher if it's JSON or XML, but I'm not sure if that responsibility should lie with the text matcher or somewhere higher up. If and when you add a |
Because there will be at least 2 poinst (JsonMatcher and TextMatcher) where we are going to check if string is a valid json/xml/yaml I think it would be nice to extract this logic somewhere else.
But above code breaks open closed principle so I guess we could extract logic from canMatch method into something like SimpleTextValidator class, what do You think? |
I'm not sure I follow what the SimpleTextValidator would be doing in this case. Yes, it's difficult to get a nice solution without over-engineering it. Better than the static methods is injecting two validators, but I think the main part I don't like is putting any knowledge or dependency on other formats into the TextMatcher. Perhaps there is an interface to extract out, and we abstain if either of the validators implementing this interface matches. I just can't think what the name might be in that case. This would then take the logic of text being excluded if json matches back out into the builder class based on how the objects are built together. |
If there will be more than one implementation of that extracted interface I'm ok with that. Anyway it would be greate if you could provide some code that we could discuss later. |
I'll certainly play around and see if I can come up with something nice. I'm just looking quickly now, and we can't check the value is valid JSON without adding the $value parameter to canMatch globally, as we only have the pattern. I will look more into it. I'm sure there's other ways we can look at it, too. For example, we could have TextMatcher delegate to one of JsonMatcher or XmlMatcher if they claim to be able to match. Or we could selectively choose which matchers are used based on the pattern/value. Again, just thinking out-loud; I'll get some code when I've had a chance to think about it properly. |
I'm not sure if we need $value in canMatch. canMatch was created to check if specific matcher can match specific pattern against any value. |
No, I agree - we don't. But we can't test if the pattern alone is valid JSON for example, because some patterns aren't valid. We could only check if they're JSON-like. Consider pattern from original description above: $expected = <<<JSON
{
"a": @null@,
"b": 4
}
JSON; |
Right, thats why in JsonMatcher there is a method that transform type patterns into strings so inside of canMatch there is a transformation and above example is turned into: $expected = <<<JSON
{
"a": "@null@",
"b": 4
}
JSON; |
Right.. I see. And would that still match correctly? |
It should only transform type patterns (strings between @ character) so it will ignore |
@adamquaile thanks for reporting this issue 🍻 . #49 adds everything that we discussed here |
The
TextMatcher
currently reports that it matches against all strings. During matching against strings with@null@
in them anUnknownTypeException
will be thrown - after all a pattern likethis should be @null@
makes no sense in a text context.This prevents other matchers from being able to match, so I've changed it to not match in this case.
Reproduce code: