//
//  ATMApplicationTests.m
//  ATMApplicationTests
//
//  David J. Malan
//  Harvard University
//  malan@harvard.edu
//
//  Simulates an ATM (that only accepts deposits).
//

#import "ATMApplicationTests.h"

#import "AppDelegate.h"
#import "ViewController.h"


@interface ATMApplicationTests ()

// private properties
@property (nonatomic, readwrite, weak) AppDelegate *appDelegate;
@property (nonatomic, readwrite, weak) ViewController *viewController;
@property (nonatomic, readwrite, weak) UIView *view;

@end


@implementation ATMApplicationTests

@synthesize appDelegate=_appDelegate;
@synthesize viewController=_viewController;
@synthesize view=_view;

- (void)setUp
{
    [super setUp];
    
    self.appDelegate = [[UIApplication sharedApplication] delegate];
    self.viewController = self.appDelegate.viewController;
    self.view = self.viewController.view;
}

- (void)testAppDelegate
{
    STAssertNotNil(self.appDelegate, @"Cannot find the application delegate");
}

- (void)testClear
{
    // input $1
    [self.viewController digit:[self.view viewWithTag:1]];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$1"], @"Deposit should be $1");
    
    // clearing should yield $0
    [self.viewController clear:nil];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$0"], @"Deposit should be $0");
}

- (void)testDeposit
{
    // balance should be $0 at first
    STAssertTrue([[self.viewController.balanceLabel text] isEqualToString:@"$0"], @"Balance should be $0");
    
    // deposit $1
    [self.viewController digit:[self.view viewWithTag:1]];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$1"], @"Deposit should be $1");
    [self.viewController deposit:nil];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$0"], @"Deposit should be $0");
    STAssertTrue([[self.viewController.balanceLabel text] isEqualToString:@"$1"], @"Balance should be $1");
    
    // deposit $12
    [self.viewController digit:[self.view viewWithTag:1]];
    [self.viewController digit:[self.view viewWithTag:2]];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$12"], @"Deposit should be $12");
    [self.viewController deposit:nil];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$0"], @"Deposit should be $0");
    STAssertTrue([[self.viewController.balanceLabel text] isEqualToString:@"$13"], @"Balance should be $13");
}

- (void)testDigit
{
    // default value should be $0
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$0"], @"Deposit should be $0");

    // inputting a leading zero should still yield $0
    [self.viewController digit:[self.view viewWithTag:0]];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$0"], @"Deposit should be $0");

    // inputting 1 should yield $1
    [self.viewController digit:[self.view viewWithTag:1]];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$1"], @"Deposit should be $1");

    // inputting 2 should now yield $12
    [self.viewController digit:[self.view viewWithTag:2]];
    STAssertTrue([[self.viewController.depositLabel text] isEqualToString:@"$12"], @"Deposit should be $12");
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

- (void)testExample
{
    //STFail(@"Unit tests are not implemented yet in ATMApplicationTests");
}

@end
