Test Regex with new line string \n via PHPUnit - phpunit

I created a php library that parse content via regex. One of this regex is '#\n-{3,}#' to parse --- only with an break, a new line, before.
I have also tests written in PHPUnit for all methods and always I get a failure for tests with the new line regex. I test always with assertSame()
I tried to set as input the follow strings:
$input = PHP_EOL . '---';
$input = '<br>---';
$input = '
---'; // with break in code
As expected I set:
'<hr/>'
However always it fail and get an error. If I send this variables to the assert check it will fail and parse not the new line. Only without the \n inside the Regex, like '#-{3,}#', it works fine without error for the tests.
Also if I use as input for the test a new line with a string before, it works also, like
$input = "test\n---";
But I would like also to test without string, only start with a new line.
The parse for front end works fine, it replace via this regex from my markdown file if the content is include a break and followed by the 3 -.
How is it possible to set as input for the assertSame() function in PHPUnit a new line before the string?

The problem is you are using single quotes in your pattern.
In PHP \n means "new line" in a string only if double quotes are used.
$input = PHP_EOL . '---';
preg_match("#\n-{3,}#", $input); // this will match
See https://3v4l.org/EpEGf

Related

Character in a string formation

I use Rselenium and I use javascript queries.
The query in javascript is this:
document.querySelectorAll('ul#test div.mytext')[1].innerText.split('\n').filter(x => x).join('???')
When I try to run it in RSelenium code I use this:
remDr$executeScript('return document.querySelectorAll(\'ul#test div.mytext\')[ 1 ].innerText.split(\'//\n\').filter(x => x).join(\'???\')', args = list("dummy"))
However I receive an error and I belive it is due to \n character
How can I write it properly?
You are using single quotes for delimiting the code you want to run when it also contains single quotes. Since there are no double quotes in the expression, try:
remDr$executeScript("return document.querySelectorAll(\'ul#test div.mytext\')[ 1 ].innerText.split(\'//\n\').filter(x => x).join(\'???\')", args = list("dummy"))

How to make spaces in URI readable by SPARQL queries in R?

As we write SPARQL in Virtuoso, it is easy to escape a space within an URI by coding the space with %20. However, when I install the package SPARQL in R, the escape fails. There is an argument curl_args in command SPARQL, which should work around this issue. But it is not successful. Here is my R scripts:
###Step 1: Building up the query
query <-"select ?instance {
?form a <URI name> .
?instance a <http://StemAddress/Where My Question Is> .
}"
###Step 2: Executing the query
qd <- SPARQL(endpoint,queryC,curl_args = curlPercentEncode("http://StemAddress/Where My Question Is", amp = TRUE, codes = " ", post.amp = TRUE))
####In Step 1, what it works in Virtuoso is
select ?instance {
?form a <URI name> .
?instance a <http://StemAddress/Where%20My%20Question%20Is> .
}
####But this just threw me an error in R environment.
Writing %20 is not an escape for a space. It really is the three characters %-2-0 in the URI. Encode != escape. Spaces in URIs are illegal.

Escape Spaces in QT

I want to use the QString.split(' ') method to split a input command into the command
QStringList commandList = command.split(' ');
however command has a UNIX path at the end of it. i.e. it looks something like
QString command = new QString("caommand -a -b /path/to\ specific/file");
the path command is specified by the user at runtime(the user escapes any spaces in the path). For some reason command.split(' '); does not escape the spaces.
I am new to QT, how does it escape spaces?
Thanks for any help
You can use QDir::toNativeSeparators() to convert it to unix style. And split received result by spaces, though you have got to figure out where are the spaces between commands and where are the possible spaces in filename
For example:
QString myUnixPath = QDir::toNativeSeparators("/home/path with spaces/");
will return unix style path, while
QString qtPath = QDir::fromNativeSeparators("/path/with\ spaces/");
will return /path with spaces/

QRegExp: individual quantifiers can't be non-greedy, but what good alternatives then?

I'm trying to write code that appends ending _my_ending to the filename, and does not change file extension.
Examples of what I need to get:
"test.bmp" -> "test_my_ending.bmp"
"test.foo.bar.bmp" -> "test.foo.bar_my_ending.bmp"
"test" -> "test_my_ending"
I have some experience in PCRE, and that's trivial task using it. Because of the lack of experience in Qt, initially I wrote the following code:
QString new_string = old_string.replace(
QRegExp("^(.+?)(\\.[^.]+)?$"),
"\\1_my_ending\\2"
);
This code does not work (no match at all), and then I found in the docs that
Non-greedy matching cannot be applied to individual quantifiers, but can be applied to all the quantifiers in the pattern
As you see, in my regexp I tried to reduce greediness of the first quantifier + by adding ? after it. This isn't supported in QRegExp.
This is really disappointing for me, and so, I have to write the following ugly but working code:
//-- write regexp that matches only filenames with extension
QRegExp r = QRegExp("^(.+)(\\.[^.]+)$");
r.setMinimal(true);
QString new_string;
if (old_string.contains(r)){
//-- filename contains extension, so, insert ending just before it
new_string = old_string.replace(r, "\\1_my_ending\\2");
} else {
//-- filename does not contain extension, so, just append ending
new_string = old_string + time_add;
}
But is there some better solution? I like Qt, but some things that I see in it seem to be discouraging.
How about using QFileInfo? This is shorter than your 'ugly' code:
QFileInfo fi(old_string);
QString new_string = fi.completeBaseName() + "_my_ending"
+ (fi.suffix().isEmpty() ? "" : ".") + fi.suffix();

xQuery substring problem

I now have a full path for a file as a string like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
However, now I need to take out only the folder path, so it will be the above string without the last back slash content like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"
But it seems that the substring() function in xQuery only has substring(string,start,len) or substring(string,start), I am trying to figure out a way to specify the last occurence of the backslash, but no luck.
Could experts help? Thanks!
Try out the tokenize() function (for splitting a string into its component parts) and then re-assembling it, using everything but the last part.
let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
$segments := tokenize($full-path,"/")[position() ne last()]
return
concat(string-join($segments,'/'),'/')
For more details on these functions, check out their reference pages:
fn:tokenize()
fn:string-join()
fn:replace can do the job with a regular expression:
replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"[^/]+$",
"")
This can be done even with a single XPath 2.0 (subset of XQuery) expression:
substring($fullPath,
1,
string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
)
where $fullPath should be substituted with the actual string, such as:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
The following code tokenizes, removes the last token, replaces it with an empty string, and joins back.
string-join(
(
tokenize(
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"/"
)[position() ne last()],
""
),
"/"
)
It seems to return the desired result on try.zorba-xquery.com. Does this help?

Resources