Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make percent() a bit more lenient

  • Loading branch information...
commit bfd75c2c631db7b2e04cc2e357a0df23547a574d 1 parent dc6f838
@davedelong authored
View
34 DDMathParser/_DDFunctionUtilities.m
@@ -519,39 +519,21 @@ + (DDMathFunction) percentFunction {
DDExpression *percentExpression = [percentArgument parentExpression];
DDExpression *percentContext = [percentExpression parentExpression];
- if (percentContext == nil || [percentExpression expressionType] != DDExpressionTypeFunction) {
- if (error) {
- *error = ERR(DDErrorCodeInvalidFormat, @"unable to determine context for percent");
- }
- return nil;
- }
-
NSString *parentFunction = [percentContext function];
_DDOperatorInfo *operatorInfo = [[_DDOperatorInfo infosForOperatorFunction:parentFunction] lastObject];
- if (operatorInfo == nil) {
- if (error) {
- *error = ERR(DDErrorCodeInvalidFormat, @"unable to determine context for percent");
- }
- return nil;
- }
- if ([operatorInfo arity] != DDOperatorArityBinary) {
- if (error) {
- *error = ERR(DDErrorCodeInvalidFormat, @"unable to determine context for percent");
- }
- return nil;
- }
+ NSNumber *context = [NSNumber numberWithInt:1];
- BOOL percentIsRightArgument = ([[percentContext arguments] objectAtIndex:1] == percentExpression);
- if (!percentIsRightArgument) {
- if (error) {
- *error = ERR(DDErrorCodeInvalidFormat, @"unable to determine context for percent");
+ if ([operatorInfo arity] == DDOperatorArityBinary) {
+ if ([parentFunction isEqualToString:DDOperatorAdd] || [parentFunction isEqualToString:DDOperatorMinus]) {
+ BOOL percentIsRightArgument = ([[percentContext arguments] objectAtIndex:1] == percentExpression);
+ if (percentIsRightArgument) {
+ DDExpression *baseExpression = [[percentContext arguments] objectAtIndex:0];
+ context = [baseExpression evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ }
}
- return nil;
}
- DDExpression *baseExpression = [[percentContext arguments] objectAtIndex:0];
- NSNumber *context = [baseExpression evaluateWithSubstitutions:variables evaluator:evaluator error:error];
NSNumber *percent = [percentArgument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(context);
View
7 Unit Tests/EvaluationTests.m
@@ -65,4 +65,11 @@ - (void) testFactorial {
TEST(@"2!!!!!!!!!", 2);
}
+- (void)testPercent {
+ TEST(@"100+percent(5)", 105);
+ TEST(@"100-percent(5)", 95);
+ TEST(@"100*percent(5)", 5);
+ TEST(@"100/percent(5)", 2000);
+}
+
@end
View
8 Unit Tests/UnitTestMacros.h
@@ -10,8 +10,12 @@
extern NSNumber* _UnitTestNumber(const void* value, const char* type);
-#define TEST(_s, _v) STAssertEqualObjects(EVAL(_s), NUM(_v), @"")
+#define TEST(_s, _v) { \
+NSNumber *_eval = EVAL(_s); \
+NSNumber *_val = NUM(_v); \
+STAssertEqualObjects(_eval, _val, @"%@ should be equal to %@", (_s), _val); \
+}
-#define EVAL(_s) [(_s) numberByEvaluatingString]
+#define EVAL(_s) ([(_s) numberByEvaluatingString])
#define NUM(_f) ({typeof(_f) _Y_ = (_f); _UnitTestNumber(&_Y_, @encode(typeof(_f)));})
Please sign in to comment.
Something went wrong with that request. Please try again.