//
//  IngredientTests.m
//  MealBuilder
//
//  Created by Phil Horowitz on 2/17/13.
//  Copyright (c) 2013 Phil Horowitz. All rights reserved.
//

#import "IngredientTests.h"
#import "Ingredient.h"
#import "Measurements.h"

@implementation IngredientTests

-(void) testIngredientsCanCombine{
    Ingredient *firstIngredient = [[Ingredient alloc] init];
    Ingredient *secondIngredient = [[Ingredient alloc] init];
    
    NSString *name = @"SomeIngredient";
    firstIngredient.name = name;
    secondIngredient.name = name;
    
    float initAmount = 6;
    Unit *amount = [[Gram alloc] initWithAmount:initAmount];
    UnitContainer *container = [[UnitContainer alloc] initWithUnit:amount];
    firstIngredient.amount = container;
    secondIngredient.amount = container;
    
    [firstIngredient combineWithIngredient:secondIngredient];
    
    STAssertEqualsWithAccuracy((initAmount * 2), firstIngredient.amount.amount, 0.1f, @"combining ingrediens should change the amount of the first ingredient");
}

-(void)testDifferentIngredientsCantCombine{
    Ingredient *firstIngredient = [[Ingredient alloc] init];
    firstIngredient.name = @"A thing you eat";
    Ingredient *secondIngredient = [[Ingredient alloc] init];
    secondIngredient.name = @"A thing you drink";
    
    float ingredientAmount = 10;
    Unit *amount = [[Gram alloc] initWithAmount:ingredientAmount];
    firstIngredient.amount.unit = amount;
    secondIngredient.amount.unit = amount;
    
    [firstIngredient combineWithIngredient:secondIngredient];
    STAssertEqualsWithAccuracy(firstIngredient.amount.amount, ingredientAmount, 0.1f, @"Ingrediens of different types should not combine");
}

-(void) testAliasedIngredientsCanCombine{
    float ingredientAmount = 10;
    Unit *amount = [[Gram alloc] initWithAmount:ingredientAmount];
    
    Ingredient *firstIngredient = [[Ingredient alloc] init];
    firstIngredient.name = @"thing1";
    [firstIngredient.aliases addObject:@"thing2"];
    [firstIngredient.aliases addObject:@"thing3"];
    firstIngredient.amount.unit = amount;
    
    Ingredient *secondIngredient = [[Ingredient alloc] init];
    secondIngredient.name = @"thing3";
    secondIngredient.amount.unit = amount;
    
    [firstIngredient combineWithIngredient:secondIngredient];
    STAssertEqualsWithAccuracy(firstIngredient.amount.amount, ingredientAmount * 2, 0.1, @"Ingredients that are aliases should combine");

}

-(void) testInitWithName{
    NSString *name = @"success";
    Ingredient *testIngredient = [[Ingredient alloc] initWithName:name];
    STAssertNotNil(testIngredient, @"initWithName should not return nil");
    STAssertEquals(testIngredient.name, name, @"initWithName should set the ingredient's name");
}
-(void) testInitWithNameAndAmount{
    NSString *name = @"success";
    Unit *amount = [[Gram alloc] initWithAmount:10];
    Ingredient *testIngredient = [[Ingredient alloc] initWithName:name andAmount:amount];
    
    STAssertNotNil(testIngredient, @"initWithName:AndAmount: should not return nil");
    STAssertEquals(testIngredient.name, name, @"initWithName:AndAmount: should propertly set the object's name");
    STAssertEqualObjects(amount, testIngredient.amount.unit, @"initWithName:AndAmount: should set the unit");
}

-(void) testYieldFrom{
    Ingredient *testIngredient = [[Ingredient alloc] init];
    testIngredient.name = @"chopped onions";
    testIngredient.amount.unit = [[Cup alloc] initWithAmount:3];
    
    Ingredient *testYield = [testIngredient yieldsFrom];
    STAssertEquals(testYield, testIngredient,@"Ingredien with no yield ingredient should return itself");
    
    Ingredient *yieldIngredient = [[Ingredient alloc] initWithName:@"Whole Onion" andAmount:[[Unit alloc] initWithAmount:2.0f]];
    testIngredient.yieldIngredient = yieldIngredient;
    testYield = [testIngredient yieldsFrom];
    STAssertTrue([testYield.name isEqualToString:yieldIngredient.name], @"yieldsFrom should return the same kind of ingredient as the yieldIngredient");
    STAssertEqualsWithAccuracy(testYield.amount.amount, 6.0f, 0.1, @"the ingredient returned from yieldsFrom should be converted");
}

@end
