Skip to content

Commit

Permalink
evaluate '&' as AND
Browse files Browse the repository at this point in the history
  • Loading branch information
tyok committed Sep 19, 2011
1 parent ac54c83 commit 94c00a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/textquery/textquery_grammar.treetop
Expand Up @@ -27,7 +27,7 @@ grammar TextQueryGrammar
end

rule binary
'AND' {
('&' /'AND') {
def eval(a,b)
a && b
end
Expand Down
30 changes: 17 additions & 13 deletions spec/textquery_spec.rb
Expand Up @@ -31,12 +31,14 @@ def parse(input)
end

it "should accept logical AND" do
parse("a AND b").eval("c").should be_false
parse("a AND b").eval("a").should be_false
parse("a AND b").eval("b").should be_false
%w[& AND].each do |_and|
parse("a #{_and} b").eval("c").should be_false
parse("a #{_and} b").eval("a").should be_false
parse("a #{_and} b").eval("b").should be_false

parse("a AND b").eval("a b").should be_true
parse("a AND b").eval("a c b").should be_true
parse("a #{_and} b").eval("a b").should be_true
parse("a #{_and} b").eval("a c b").should be_true
end
end

it "should accept logical OR" do
Expand All @@ -52,14 +54,16 @@ def parse(input)

it "should give precedence to AND" do
# a AND (b OR c) == a AND b OR c
%w[/ OR].each do |_or|
parse("a AND b #{_or} c").eval("a b c").should be_true
parse("a AND b #{_or} c").eval("a b").should be_true
parse("a AND b #{_or} c").eval("a c").should be_true

parse("a AND b #{_or} c").eval("b c").should be_false
parse("a AND b #{_or} c").eval("c").should be_false
parse("a AND b #{_or} c").eval("b").should be_false
%w[& AND].each do |_and|
%w[/ OR].each do |_or|
parse("a #{_and} b #{_or} c").eval("a b c").should be_true
parse("a #{_and} b #{_or} c").eval("a b").should be_true
parse("a #{_and} b #{_or} c").eval("a c").should be_true

parse("a #{_and} b #{_or} c").eval("b c").should be_false
parse("a #{_and} b #{_or} c").eval("c").should be_false
parse("a #{_and} b #{_or} c").eval("b").should be_false
end
end
end

Expand Down

0 comments on commit 94c00a9

Please sign in to comment.