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

Allow Escaped % in Format String Matches #2914

Merged
merged 3 commits into from
Dec 11, 2017

Conversation

mkasberg
Copy link
Contributor

@mkasberg mkasberg commented Dec 10, 2017

When using the StringMatchesFormatDescription constraint, define the '%%' format specifier to be interpreted as a literal '%', which allows for things like the following:

$this->assertStringMatchesFormat('This %%d is escaped.', 'This %d is escaped.');

This addresses #2907.

Add a test file for the StringMatchesFormatDescription constraint, and
add a simple test case for each format specifier.
For example, the format '%%d' should match the string literal '%d'.

We achieve this by using negative lookbehind
(http://php.net/manual/en/regexp.reference.assertions.php) when
constructing the regex pattern that we will match against.
On Windows-based machines, the DIRECTORY_SEPARATOR is '\'. Make sure %e
can handle either a backslash or a forward slash in that position.

We ran into problems here because preg_replace allows its own
replacement strings to use \0, \1, etc to replace certain matches. Thus,
the string '\\\\' in code turns into a single '\' in our output string.
The fix is simple - just str_replace('\\', '\\\\', ...).

This problem does not affect any of our other arguments to preg_replace
because none of those contain backslashes followed by a number or
another backslash.
@mkasberg
Copy link
Contributor Author

I'm certainly open to thoughts, criticism, or suggestions 😄

@codecov-io
Copy link

codecov-io commented Dec 10, 2017

Codecov Report

Merging #2914 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2914      +/-   ##
============================================
+ Coverage     79.62%   79.62%   +<.01%     
  Complexity     2849     2849              
============================================
  Files           105      105              
  Lines          7519     7520       +1     
============================================
+ Hits           5987     5988       +1     
  Misses         1532     1532
Impacted Files Coverage Δ Complexity Δ
...work/Constraint/StringMatchesFormatDescription.php 100% <100%> (ø) 8 <0> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e9a2d7f...46a3de0. Read the comment docs.

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.

3 participants