Remove EOL in variable for comparing strings - robotframework

I use Run Keyword Unless comparing a variable and a string:
Run Keyword Unless '${text}' == 'HelloWorld' My Keyword ${text}
Sometimes ${text} consists of two lines separated by "\n" (eg. "One line\ntwo lines"). If so, the tests fails with an error:
Evaluating expression ''One line
two lines' == 'HelloWorld'' failed: SyntaxError: EOL while scanning string literal (<string>, line 1)
I solved the problem removing '\n' with String.Replace String as follows:
${one_line_text}= String.Replace String ${text} \n ${SPACE}
Run Keyword Unless '${one_line_text}' == 'HelloWorld' My Keyword ${text}
Is there a way to do it without explicit removing of EOL in a separate keyword?

What about ${text.replace("\n", " ")}?

You can use python's string literals - """ or ''' - and not change the string at all:
Run Keyword Unless '''${text}''' == 'HelloWorld' My Keyword ${text}
They are designed for pretty much this purpose - to hold values having newline characters, plus quotes.

Related

How to check if a string contains a space in Robot Framework with Should Contain keyword

I am trying to just check that I got back something in a string using Should Contain
I can't figure out why there is no " " in '\r \r'
Any ideas what I am missing?
>>> x='\r \r'
>>> " " in x
True
If you use " " as an argument in Robot, it will be three chars : quotes, space, quote. If you want to use only space, then you should use ${SPACE} variable :
Should Contain ${result} ${SPACE}
See "handling spaces" section of the documentation.

"ERROR: syntax: cannot juxtapose string literal" when ending a triple-quoted string literal with a quote

I'm trying to create a string literal representing a CSV file with quoted fields. The intended CSV looks like this:
"a","b"
"1","2"
Triple quotes work if I want a newline character at the end of the string:
julia> """
"a","b"
"1","2"
"""
"\"a\",\"b\"\n\"1\",\"2\"\n"
But if I try to make a string without the newline character at the end, then I get a syntax error:
julia> """
"a","b"
"1","2""""
ERROR: syntax: cannot juxtapose string literal
Is there a simple way to get around this?
As an aside, note that there is no syntax error when you start the string-literal with a quote:
julia> """"a","b"
"1","2"
"""
"\"a\",\"b\"\n\"1\",\"2\"\n"
The issue is that this by itself is a valid string literal:
"""
"a","b"
"1","2"""
When you follow that with another " the parser thinks "woah, you can’t just follow a string with another string". You can force it to not consider the quote after 2 as part of a closing """ sequence by escaping it with \:
"""
"a","b"
"1","2\""""
At the start of the string, there's no such issue since the first three " characters are taken to start a string literal and the following " must just be a quote character inside of the string, which is what you want.
I'm not sure what you would consider the best solution to be. The options are:
Escape the " at the end;
Put the closing """ on a separate line.
The latter seems better to me, but it's your call.
See the Julia Docs for other examples on Triple-Quoted String Literals.

Warning on regex string in Python

So, I am doing a small function to strip all the weird chars from a string, eg. #$& will be replaced just for a " "
The chars I am trying to remove are the following, defined into a string:
xChars = r"#$%()'^*\;:/|+_.–°ªº"
However I kepp getting the warning:
Anomalous backslash in string: '\;'. String constant might be missing an r prefix
However, when i used the r prefix eg. r"\" python rules out some of the special chars i want to replace. It doesnt produce an error it just thinks that those chars are ok or something and it rules them out.
Any ideas on how to fix this ?
Normally backslashes escape characters, therefore the compiler isn´t sure if the backslash has to be escaped. Maybe try using a double backslash to escape the backslash itself like: xChars = r"#$%()'^*\\;:/|+_.–°ªº"

printf not printing past '.' in string

I am having a problem using printf on my unix system. It is throwing an error every time I try to print the following
printf "%-15s %-15.2s" "Total Acounts:\nChecks=$" checks
checks should be a decimal, but I have tried printing it as a float and a decimal and get the same error.
fatal: not enough arguments to satisfy format string
`%-15s %-15.2sTotal Acounts:
Checks=$2135.92'
^
I have been working at this for a while now and can't figure it out, so any help is appreciated.
That's not how you call printf in awk. You are missing the commas indicating arguments.
You've given printf only a format string (the concatenation of "%-15s %-15.2s", "Total Acounts:\nChecks=$" and the value of checks).
As you can see from the error message that shows the entire string as the format string and includes the value of checks in the string.
You probably meant:
printf "%-15s %-15.2s", "Total Acounts:\nChecks=$", checks
#---------------------^---------------------------^
though %-15s there isn't really doing anything useful for you as `"Total Acounts:\nChecks=$" is longer than 15 characters.

flex (python PLY) regex for strings

I'm using the python module PLY to write a parser, and I am implementing as I go. I have a simple rule to detect strings:
r'("|\').*("|\')'
When lexer errors are thrown I have this:
def t_error (t) :
print 'Illegal lexer input line ' + str(t.lineno) + ' ' + t.value[:16]
sys.exit(-1)
When I feed my parser the following input:
parse("preg_match('%^[\*\%]+$%', $keywords)")
I get back this in return:
Illegal lexer input line 1 %^[\*\%]+$%', $k
My questions are:
1) Why am I not parsing this string? It seems like my regex should properly handle this string.
2) How can I fix this?
edit:
I have narrowed the problem down a bit. The following strings throw illegal lexer input errors by themselves:
'%'
'^'
Even if this regex were working it isn't quite doing what you want it to, for example it would accept "this', which isn't really a string. This is also the cause of the "illegal lexer input"...
After having done it's job and found the first string in "preg_match(' the lexer is then upset when each of the next 11 characters %^[\*\%]+$% are illegal (and not in t_ignore), since they don't even start with " or '.
.
Try doing this with two cases for " and ': "Starts with quote, some things which aren't quote, ends with quote." That is:
r'("[^"]*")|(\'[^\']*\')'
Or, if you want to include escaped speech marks:
r'("(\\"|[^"])*")|(\'(\\\'|[^\'])*\')'

Resources