//
//  OAExpressionTests.m
//  drachma
//
//  Created by Niko Matsakis on 11/25/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "OAExpressionTests.h"
#import "OAExpressionEvaluator.h"

@implementation OAExpressionTests

- (void) expr:(NSString*)expr numberResult:(id)value ops:(OAExpressionOperations*)ops
{
    id result = evaluateExpression(expr, ops);
    STAssertTrue([result class] == [value class], @"Result is of class %@, not %@", [result class], [value class]);
    STAssertEqualObjects(result, value,
                         @"Evaluating '%@' with %@ yielded %@, should have yielded %@.",
                         expr, [value class], result, value);
}
    
- (void) expr:(NSString*)expr result:(NSString*)result
{
    [self expr:expr 
  numberResult:[NSDecimalNumber decimalNumberWithString:result] 
           ops:[OAExpressionOperations operationsOnNSDecimalNumber]];
    [self expr:expr 
  numberResult:[NSNumber numberWithDouble:[result doubleValue]]
           ops:[OAExpressionOperations operationsOnNSNumber]];
}

- (void) testSimpleAdd
{
    [self expr:@"1+1" result:@"2"];
}

- (void) testSimpleSub
{
    [self expr:@"2-1" result:@"1"];
}

- (void) testSimpleMul
{
    [self expr:@"2*3" result:@"6"];
}

- (void) testSimpleDiv
{
    [self expr:@"6/2" result:@"3"];
}

- (void) testLeadingDecimal
{
    [self expr:@".5+1" result:@"1.5"];
}

- (void) testDecimalPoint
{
    [self expr:@"1.5+1.5" result:@"3"];
}

- (void) testPrecedence
{
    [self expr:@"1+2*3" result:@"7"];
    [self expr:@"1-2*3" result:@"-5"];
    [self expr:@"1+6/3" result:@"3"];
    [self expr:@"10-9/3" result:@"7"];
}

- (void) testUnaryMinus
{
    [self expr:@"-13" result:@"-13"];
}

@end
