Skip to content

Commit

Permalink
Support and, or, &&, ||
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tsj committed Jan 24, 2017
1 parent fc89d8e commit ec8f043
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/power_assert/parser.rb
Expand Up @@ -54,6 +54,8 @@ def slice_expression(str)
class Branch < Array
end

AND_OR_OPS = %i(and or && ||)

#
# Returns idents as graph structure.
#
Expand Down Expand Up @@ -81,7 +83,12 @@ def extract_idents(sexp)
when :unary
handle_columnless_ident([], sexp[1], extract_idents(sexp[2]))
when :binary
handle_columnless_ident(extract_idents(sexp[1]), sexp[2], extract_idents(sexp[3]))
op = sexp[2]
if AND_OR_OPS.include?(op)
extract_idents(sexp[1]) + [Branch[extract_idents(sexp[3]), []]]
else
handle_columnless_ident(extract_idents(sexp[1]), op, extract_idents(sexp[3]))
end
when :call
with_safe_op = sexp[2] == :"&."
if sexp[3] == :call
Expand Down
8 changes: 8 additions & 0 deletions test/parser_test.rb
Expand Up @@ -149,6 +149,14 @@ class TestParser < Test::Unit::TestCase
['a ? 0 : 0',
[[:method, "a", 0], [[], []]],
[["a"]]],

['a && b || c',
[[:method, "a", 0], [[[:method, "b", 5]], []], [[[:method, "c", 10]], []]],
[["a", "b", "c"], ["a", "c"], ["a", "b"], ["a"]]],

['a and b or c',
[[:method, "a", 0], [[[:method, "b", 6]], []], [[[:method, "c", 11]], []]],
[["a", "b", "c"], ["a", "c"], ["a", "b"], ["a"]]],
].each_with_object({}) {|(source, expected_idents, expected_paths), h| h[source] = [expected_idents, expected_paths, source] }
end
def test_valid_syntax(*args)
Expand Down

0 comments on commit ec8f043

Please sign in to comment.