Bug #19033
closedOne-liner pattern match as Boolean arg syntax error
Description
I was chatting earlier with Seb Wilgosz about pattern matching in tests, and suggested that he might consider the following:
expect result in pattern
...but he reported back this will syntax error, including with parens:
res = [:not_found, 999]
expect(res in [:not_found, *payload])
# => SyntaxError:
# /spec/app/interactors/articles/publish_spec.rb:13: syntax error, unexpected `in', expecting ')'
Interestingly though the following work:
res = [:not_found, 999]
expect(res) in([:not_found, *payload])
# 1 example, 0 failures
expect(res) in([:not_found, 1, 3])
# 1 example, 0 failures
While this appears like an RSpec issue I would contend that it is reproducible with any other method that takes a boolean-like argument.
For me this feels like a syntax bug, but could see a case where it may be interpreted as ambiguous depending on the precedence of in
relative to method arguments much like method_name value if condition
is vague between method_name(value) if condition
and method_name(value if condition)
. That'll be especially difficult if it's method_name value in pattern if condition
, so I do not envy parser writers here.
Would be curious for thoughts on that, or if we're looking at that wrong.