Why there are some forward slash in the error string? - jestjs

Here is my stdout after I ran my testings.
expect(received).toBe(expected) // Object.is equality
Expected: "child 'path1' fails because ['path1' is not allowed to be empty]"
Received: "child \"path1\" fails because [\"path1\" is not allowed to be empty]"
39 | } catch (error) {
40 | expect(error.name).toBe('ValidationError');
> 41 | expect(error.message).toBe("child 'path1' fails because ['path1' is not allowed to be empty]");
| ^
42 | }
43 | });
44 | });
at Object.<anonymous> (src/__tests__/models/adChannel/googleadwords/AdGroupAd.spec.ts:41:29)
As you can see, the Received value has forward slash \. It doesn't match the Expected value.
I think maybe the string is escaped? I expect the string doesn't have \

Short answer
Change your expect to this:
expect(error.message).toBe('child "path1" fails because ["path1" is not allowed to be empty]');
...and it will work.
Details
JavaScript allows strings to be defined using either single quotes: 'a string' or double quotes: "a string".
From the MDN doc:
Unlike some other languages, JavaScript makes no distinction between single-quoted strings and double-quoted strings
...so it doesn't matter which approach you use.
Single quotes work fine in a string defined with double quotes:
const singleQuotes = "has 'single quotes' in it";
...and the same is true for double quotes in a string defined with single quotes:
const doubleQuotes = 'has "double quotes" in it';
...but single quotes need to be escaped if they are in a string defined with single quotes:
const singleQuotes = 'has \'single quotes\' in it';
...and the same is true for double quotes in a string defined with double quotes:
const doubleQuotes = "has \"double quotes\" in it";
You are seeing escape characters in the Jest output because Jest is formatting the output string with double quotes around it so the double quotes within it need to be escaped.

Related

POSIX Shell Escape Quotes in Dynamic String

Say I have a function that accepts a string and interpolates it inside quotes.
print_str() (
printf '"%s"\n' "$1"
)
Since the function encloses the input in double quotes, I want to escape all double quotes, if any, in the argument. But, critically, double quotes that are already escaped must be preceded by a backslash. In short, the function must add one layer of escaping to all double quotes in the argument, be that the first layer or an additional layer.
Examples:
'abc"def' -> 'abc\"def'
'{ "json": "{ \"key\": \"value\" }" }' -> '{ \"json\": \"{ \\\"key\\\": \\\"value\\\" }\" }'
The tricky part for me is determining whether a backslash preceding a double quote is actually escaping the double quote, because, for example, in '\\"' the backslash does not escape the double quote even though it immediately precedes the double quote.
I've thought about using sed and checking whether the number of backslashes before a double quote is odd or even, but that seems pretty convoluted. I managed to get the solution I wanted by piping the input through jq -R ., but I am wondering if there is another way to do it. In particular, I am looking for something that works in POSIX shell.
EDIT:
For further clarification, I am seeking some function that will add an additional layer of escaping to double quotes in a given string.
some_function() {
# IDK what the implementation would be
}
Then, some_function in conjunction with print_str would producing the following output.
string='{ "json": "{ \"key\": \"value\" }" }'
print_str "$(some_function "$string")"
# -> prints "{ \"json\": \"{ \\\"key\\\": \\\"value\\\" }\" }"
You don't need to count anything
sed 's/["\]/\\&/g; s/.*/"&"/'

Are there any more "quote" symbols to use as quotes?

Are there any "quote" symbols that you can use if you run out of them?
I know one can use " and ' (and, perhaps a combination of them) like "''" (somehow)
A example of a "additional" "quote" symbol would be
\" inside quotes.
If I make a python script, and "used up" both " and ', is there any more chars that I can use to indicate a quote?
No. From what I can tell, ' and " are the only quotes than can be used in a string literal. The Lexical Analysis page contains this information:
shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
In plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).
Which suggests that those are the only two options.
No, " and ' are the only quote characters in Python.
If you run out of quote symbols you can "escape" quotes with a backslash(\), which will ignore the quotes and just make them part of the string.
For example, this would be a valid string:
print("hello \"bob\"")
This would return 'hello "bob"'
No, their are no other quote chracters in Python for right now
As far as I know ' and " are tho only quote characters in python
but if you count a multi line string then it would look like this:
multiline = """
string
string
string
"""
and to make a list of them you would use:
multiline.splitlines()

Movlet Failing if double quotes exists in string

I have a string with double quotes for example:
"Diagnosed with "Covid19 -Ve"
while rendering in MEL variable it's failing.
My string may contains multiple double quotes in future.
How to escape if string contains all the double quotes in entire string value at the end in MEL
you can escape the double quotes character using a backslash and then the double quotes. Just like this:
myVariable = "Diagnosed with \"Covid19 -Ve" ;

ANTLRv4: How to read double quote escaped double quotes in string?

In ANTLR v4, how do we parse this kind of string with double quote escaped double quotes like in VBA?
for text:
"some string with ""john doe"" in it"
the goal would be to identify the string: some string with "john doe" in it
And is it possible to rewrite it to turn double double quotes in single double quotes? "" -> "?
Like this:
STRING
: '"' (~[\r\n"] | '""')* '"'
;
where ~[\r\n"] | '""' means:
~[\r\n"] # any char other than '\r', '\n' and double quotes
| # OR
'""' # two successive double quotes
And is it possible to rewrite it to turn double double quotes in single double quotes?
Not without embedding custom code. In Java that could look like:
STRING
: '"' (~[\r\n"] | '""')* '"'
{
String s = getText();
s = s.substring(1, s.length() - 1); // strip the leading and trailing quotes
s = s.replace("\"\"", "\""); // replace all double quotes with single quotes
setText(s);
}
;

How to use backslash escape char for new line in JavaCC?

I have an assignment to create a lexical analyser and I've got everything working except for one bit.
I need to create a string that will accept a new line, and the string is delimited by double quotes.
The string accepts any number, letter, some specified punctuation, backslashes and double quotes within the delimiters.
I can't seem to figure out how to escape a new line character.
Is there a certain way of escaping characters like new line and tab?
Here's some of my code that might help
< STRING : ( < QUOTE> (< QUOTE > | < BACKSLASH > | < ID > | < NUM > | " " )* <QUOTE>) >
< #QUOTE : "\"" >
< #BACKSLASH : "\\" >
So my string should allow for a quote, then any of the following characters like a backslash, a whitespace, a number etc, and then followed by another quote.
The newline char like "\n" is what's not working.
Thanks in advance!
For string literals, JavaCC borrows the syntax of Java. So, a single-character literal comprising a carriage return is escaped as "\r", and a single-character literal comprising a line feed is escaped as "\n".
However, the processed string value is just a single character; it is not the escape itself. So, suppose you define a token for line feed:
< LF : "\n" >
A match of the token <LF> will be a single line-feed character. When substituting the token in the definition of another token, the single character is effectively substituted. So, suppose you have the higher-level definition:
< STRING : "\"" ( <LF> ) "\"" >
A match of the token <STRING> will be three characters: a quotation mark, followed by a line feed, followed by a quotation mark. What you seem to want instead is for the escape sequence to be recognized:
< STRING : "\"" ( "\\n" ) "\"" >
Now a match of the token <STRING> will be four characters: a quotation mark, followed by an escape sequence representing a line feed, followed by a quotation mark.
In your current definition, I see that other often-escaped metacharacters like quotation mark and backslash are also being recognized literally, rather than as escape sequences.

Resources