go-testdeep icon indicating copy to clipboard operation
go-testdeep copied to clipboard

Add support for quotation marks in JSON operators

Open r6q opened this issue 2 years ago • 3 comments

Problem: We use different operators in JSONs which are then used for comparison. If we add regular operators to json string, e.g. Len, HasPrefix, etc, the JSON becomes invalid and formatters, validators cannot parse the string.

Proposal: Add support for quotation marks for operators in JSON strings, similarly to currently it is being used for shortcut operators "$^NotEmpty". Ideally it could look similar "$^Len(10)"

r6q avatar May 12 '22 04:05 r6q

Interesting idea, but it is not as easy as it seems as you probably want the following works too:

"$^Bag(SuperMapOf({\"foo\":123}))"

for example, so the JSON parser has to be reentrant as when we get "$^" we are in the lexer https://github.com/maxatome/go-testdeep/blob/0da89a913b09cdd9e316c658015c5536472883f7/internal/json/lex.go#L459 and we should not loose the location, in case the user makes a mistake to help him/her to find the origin...

maxatome avatar May 12 '22 07:05 maxatome

Ah, I see, seems relatively complicated. I might try implementing it, whenever I have free time. Will start off by trying to branch of the logic in a file you mentioned. Thanks!

r6q avatar May 17 '22 12:05 r6q

Hi @r6q, if you still need this feature could you review #217 please?

I also added the possibility to have raw string in JSON, useful to avoid escaping double-quotes for strings-in-strings:

// with raw string:
td.Cmp(t, got, td.JSON(`{"name": "$^Re(r<^Bob \\w+$>)"}`))
// without raw string:
td.Cmp(t, got, td.JSON(`{"name": "$^Re(\"^Bob \\\\w+$>\""}`))

Until I write the documentation, all raw strings cases are listed here.

maxatome avatar Aug 31 '22 21:08 maxatome