-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #246
- Loading branch information
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require_relative '../function' | ||
require_relative '../../exceptions' | ||
|
||
module Dentaku | ||
module AST | ||
class Xor < Function | ||
def self.min_param_count | ||
1 | ||
end | ||
|
||
def self.max_param_count | ||
Float::INFINITY | ||
end | ||
|
||
def deferred_args | ||
[1, 2] | ||
end | ||
|
||
def value(context = {}) | ||
if @args.empty? | ||
raise Dentaku::ArgumentError.for( | ||
:too_few_arguments, | ||
function_name: 'XOR()', at_least: 1, given: 0 | ||
), 'XOR() requires at least one argument' | ||
end | ||
|
||
true_arg_count = 0 | ||
@args.each do |arg| | ||
case arg.value(context) | ||
when TrueClass | ||
true_arg_count += 1 | ||
break if true_arg_count > 1 | ||
when FalseClass, nil | ||
next | ||
else | ||
raise Dentaku::ArgumentError.for( | ||
:incompatible_type, | ||
function_name: 'XOR()', expect: :logical, actual: arg.class | ||
), 'XOR() requires arguments to be logical expressions' | ||
end | ||
end | ||
true_arg_count == 1 | ||
end | ||
end | ||
end | ||
end | ||
|
||
Dentaku::AST::Function.register_class(:xor, Dentaku::AST::Xor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'spec_helper' | ||
require 'dentaku' | ||
require 'dentaku/ast/functions/or' | ||
|
||
describe 'Dentaku::AST::Xor' do | ||
let(:calculator) { Dentaku::Calculator.new } | ||
|
||
it 'returns false if all of the arguments are false' do | ||
result = Dentaku('XOR(false, false)') | ||
expect(result).to eq(false) | ||
end | ||
|
||
it 'returns true if only one of the arguments is true' do | ||
result = Dentaku('XOR(false, true)') | ||
expect(result).to eq(true) | ||
end | ||
|
||
it 'returns false if more than one of the arguments is true' do | ||
result = Dentaku('XOR(false, true, true)') | ||
expect(result).to eq(false) | ||
end | ||
|
||
it 'supports nested expressions' do | ||
result = Dentaku('XOR(y = 1, x = 1)', x: 1, y: 2) | ||
expect(result).to eq(true) | ||
end | ||
|
||
it 'raises an error if no arguments are passed' do | ||
expect { calculator.evaluate!('XOR()') }.to raise_error(Dentaku::ParseError) | ||
end | ||
|
||
it 'raises an error if a non logical argument is passed' do | ||
expect { calculator.evaluate!('XOR("r")') }.to raise_error(Dentaku::ArgumentError) | ||
end | ||
end |