TDD的iOS开发初步以及Kiwi使用入门
|
//Product Code
add(float num1, float num 2) {...}
//Test code
let a = 3;
let b = 5;
let c = a + b;
if (c == 8) {
// Yeah, it works!
} else {
//Something wrong!
}
|
|
- (void)testStackExist {
XCTAssertNotNil([VVStack class], @"VVStack class should exist.");
}
- (void)testStackObjectCanBeCreated {
VVStack *stack = [VVStack new];
XCTAssertNotNil(stack, @"VVStack object can be created.");
}
|
|
- (void)testPushANumberAndGetIt {
VVStack *stack = [VVStack new];
[stack push:2.3];
double topNumber = [stack top];
XCTAssertEqual(topNumber, 2.3, @"VVStack should can be pushed and has that top value.");
}
|
|
//VVStack.h
@interface VVStack : NSObject
- (void)push:(double)num;
- (double)top;
@end
//VVStack.m
@implementation VVStack
- (void)push:(double)num {
}
- (double)top {
return 2.3;
}
@end
|
|
- (void)testPushANumberAndGetIt {
//...
[stack push:4.6];
topNumber = [stack top];
XCTAssertEqual(topNumber, 4.6, @"Top value of VVStack should be the last num pushed into it");
}
|
|
//VVStack.m
@interface VVStack()
@property (nonatomic, strong) NSMutableArray *numbers;
@end
@implementation VVStack
- (id)init {
if (self = [super init]) {
_numbers = [NSMutableArray new];
}
return self;
}
- (void)push:(double)num {
[self.numbers addObject:@(num)];
}
- (double)top {
return [[self.numbers lastObject] doubleValue];
}
@end
|
|
describe(@"Team", ^{
context(@"when newly created", ^{
it(@"should have a name", ^{
id team = [Team team];
[[team.name should] equal:@"Black Hawks"];
});
it(@"should have 11 players", ^{
id team = [Team team];
[[[team should] have:11] players];
});
});
});
|
|
describe(@"SimpleString", ^{
context(@"when assigned to 'Hello world'", ^{
NSString *greeting = @"Hello world";
it(@"should exist", ^{
[[greeting shouldNot] beNil];
});
it(@"should equal to 'Hello world'", ^{
[[greeting should] equal:@"Hello world"];
});
});
});
|
|
describe(@"VVStack", ^{
context(@"when created", ^{
__block VVStack *stack = nil;
beforeEach(^{
stack = [VVStack new];
});
afterEach(^{
stack = nil;
});
it(@"should have the class VVStack", ^{
[[[VVStack class] shouldNot] beNil];
});
it(@"should exist", ^{
[[stack shouldNot] beNil];
});
it(@"should be able to push and get top", ^{
[stack push:2.3];
[[theValue([stack top]) should] equal:theValue(2.3)];
[stack push:4.6];
[[theValue([stack top]) should] equal:4.6 withDelta:0.001];
});
});
});
|
|
//VVStack.h
//...
- (NSUInteger)count;
//...
//VVStack.m
//...
- (NSUInteger)count {
return [self.numbers count];
}
//...
it(@"should equal contains 0 element", ^{
[[theValue([stack count]) should] beZero];
});
|
|
context(@"when new created and pushed 4.6", ^{
__block VVStack *stack = nil;
beforeEach(^{
stack = [VVStack new];
[stack push:4.6];
});
afterEach(^{
stack = nil;
});
it(@"can be poped and the value equals 4.6", ^{
[[theValue([stack pop]) should] equal:theValue(4.6)];
});
it(@"should contains 0 element after pop", ^{
[stack pop];
[[stack should] beEmpty];
});
});
|
|
- (double)pop {
if ([self count] == 0) {
[NSException raise:@"VVStackPopEmptyException" format:@"Can not pop an empty stack."];
}
double result = [self top];
[self.numbers removeLastObject];
return result;
}
|


