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

Support Shorthand Predicates #171

Closed
jcamiel opened this issue Feb 15, 2021 · 3 comments
Closed

Support Shorthand Predicates #171

jcamiel opened this issue Feb 15, 2021 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@jcamiel
Copy link
Collaborator

jcamiel commented Feb 15, 2021

Rational

Current Hurl predicates syntax is text based and can be a little tedious (see #164):

GET https://my_api.com

[Asserts]
jsonpath "$.my_int" greaterThanOrEquals 11
jsonpath "$.my_int" lessThan 36

A more "natural" way of writting this snippet could be:

GET https://my_api.com

[Asserts]
jsonpath "$.my_int" >= 11
jsonpath "$.my_int" < 36

Shorthand Proposed Syntax

Predicate textual Predicate shorthand
equals ==
greaterThan >
greaterThanOrEquals >=
lessThan <
lessThanOrEquals <=
countEquals count ==
startsWith
contains
includes
matches =~
exists
isInteger
isFloat
isBoolean
isString
isCollection

matches shorthand is ~= based on Ruby matching operator
and Perl matching operator

And the following predicates (not present in Hurl 1.1.0 grammar)

Predicate textual Predicate shorthand
notEquals !=
countGreaterThan count >
countGreaterThanOrEquals count >=
countLessThan count <
countLessThanOrEquals count <=
countNotEquals count !=

Negate a predicate is still possible :

GET https:/sample/api
HTTP/* *
[Asserts]
jsonpath "$.name" not == "toto"
jsonpath "$.count" not > 12
jsonpath "$.id" not != "dude"

Even if there is a "better" way of writing these asserts: (somethong maybe adressable by hurlfmt?)

GET https:/sample/api
HTTP/* *
[Asserts]
jsonpath "$.name" != "toto"
jsonpath "$.count" <= 12
jsonpath "$.id" == "dude"

Comparison

An Hurl snippet using litteral predicates:

GET http://sample.com
HTTP/1.1 *
[Asserts]
status greaterThanOrEquals 200
status lessThan 300
jsonpath "$.validated" equals true
jsonpath "$.userInfo.firstName" equals "Franck"
jsonpath "$.userInfo.lastName" equals "Herbert"
jsonpath "$.links" countEquals 12
jsonpath "$.state" not equals null

An Hurl snippet using shorthand predicates:

GET http://sample.com
HTTP/1.1 *
[Asserts]
status >= 200
status < 300
jsonpath "$.validated" == true
jsonpath "$.userInfo.firstName" == "Franck"
jsonpath "$.userInfo.lastName" == "Herbert"
jsonpath "$.links" count == 12
jsonpath "$.state" != null

Questions

  • Do we keep textual predicates ? Note that certains predicates doesn't have a shorthand (like contains, exist, etc...). Maybe hurlfmt can raise a warning. in this case. Hurl syntax is not too popular and can be modified.
  • In case we keep the textual and shorthand predicates, we should act a preferred one and updates the samples accordingly.
@jcamiel jcamiel added the enhancement New feature or request label Feb 15, 2021
@lepapareil
Copy link
Collaborator

lepapareil commented Feb 15, 2021

Can we use ^ for startWith and ~ for contains

@jcamiel
Copy link
Collaborator Author

jcamiel commented Feb 17, 2021

matches proposed shorthand (~=) is based on Ruby matching operator and Perl matching operator. ^ in JavaScript is associated with binary XOR.

Anyway, if we keep both syntaxes (textual and shorthand) why not having a shorthand for startsWith, if we found something similar in other languages. If we keep only shorthand, I think that we should keep the usual ones (==, !=, >, <, >=, <=) and some textual for the other. In this case, I'm not sure bout matches vs ~=

@lepapareil lepapareil added this to the 1.4.0 milestone Jun 11, 2021
@jcamiel
Copy link
Collaborator Author

jcamiel commented Jun 22, 2021

Literal ShortHands Lint (preferred)
jsonpath "$.books" equals 12 jsonpath "$.books" == 12 jsonpath "$.books" == 12
jsonpath "$.books" not equals 12 jsonpath "$.books" not == 12 jsonpath "$.books" != 12
jsonpath "$.books" notEquals 12 jsonpath "$.books" != 12 jsonpath "$.books" != 12
jsonpath "$.books" not notEquals 12 jsonpath "$.books" not != 12 jsonpath "$.books" == 12
jsonpath "$.books" count equals 12 jsonpath "$.books" count == 12 jsonpath "$.books" count == 12
jsonpath "$.books" count not equals 12 jsonpath "$.books" count not == 12 jsonpath "$.books" count != 12
jsonpath "$.books" count notEquals 12 jsonpath "$.books" count != 12 jsonpath "$.books" count != 12
jsonpath "$.books" count lessThan 12 jsonpath "$.books" count < 12 jsonpath "$.books" count < 12
jsonpath "$.books" count not lessThan 12 jsonpath "$.books" count not < 12 jsonpath "$.books" count >= 12
jsonpath "$.books" count lessThanOrEquals 12 jsonpath "$.books" count <= 12 jsonpath "$.books" count <= 12
jsonpath "$.books" count not lessThanOrEquals 12 jsonpath "$.books" count not <= 12 jsonpath "$.books" count > 12
jsonpath "$.books" lessThan 12 jsonpath "$.books" < 12 jsonpath "$.books" < 12
jsonpath "$.books" not lessThan 12 jsonpath "$.books" not < 12 jsonpath "$.books" >= 12
jsonpath "$.books" lessThanOrEquals 12 jsonpath "$.books" <= 12 jsonpath "$.books" <= 12
jsonpath "$.books" not lessThanOrEquals 12 jsonpath "$.books" not <= 12 jsonpath "$.books" > 12
jsonpath "$.books" greaterThan 12 jsonpath "$.books" > 12 jsonpath "$.books" > 12
jsonpath "$.books" not greaterThan 12 jsonpath "$.books" not > 12 jsonpath "$.books" <= 12
jsonpath "$.books" greaterThanOrEquals 12 jsonpath "$.books" >= 12 jsonpath "$.books" >= 12
jsonpath "$.books" not greaterThanOrEquals 12 jsonpath "$.books" not >= 12 jsonpath "$.books" < 12
jsonpath "$.books" contains "12" jsonpath "$.books" contains "12" jsonpath "$.books" contains "12"
jsonpath "$.books" not contains "12" jsonpath "$.books" not contains "12" jsonpath "$.books" not contains "12"
jsonpath "$.books" startsWith "12" jsonpath "$.books" startsWith "12" jsonpath "$.books" startsWith "12"
jsonpath "$.books" not startsWith "12" jsonpath "$.books" not startsWith "12" jsonpath "$.books" not startsWith "12"
jsonpath "$.books" exists jsonpath "$.books" exists jsonpath "$.books" exists
jsonpath "$.books" not exists jsonpath "$.books" not exists jsonpath "$.books" not exists

@fabricereix fabricereix removed the 1.4.0 label Jun 23, 2021
@jcamiel jcamiel removed this from the 1.4.0 milestone Jun 24, 2021
@lepapareil lepapareil added this to the 1.1.0 milestone Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants