-
Notifications
You must be signed in to change notification settings - Fork 3
/
NORLabelNodeTest.m
129 lines (97 loc) · 5.14 KB
/
NORLabelNodeTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// NORLabelNodeTest.m
// TotalGreed
//
// Created by Benjamin Larsen on 14.03.14.
// Copyright (c) 2014 Benjamin Larsen. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "NORLabelNode.h"
#import "TargetConditionals.h"
@interface NORLabelNode ()
@property (nonatomic, strong) NSArray *subNodes;
@property (nonatomic, strong) SKLabelNode *propertyStateholderNode;
- (NSArray *)labelNodesFromText:(NSString *)text;
@end
@interface NORLabelNodeTest : XCTestCase
@end
@implementation NORLabelNodeTest
- (void)setUp{
[super setUp];
}
- (void)testInitWithFontCreatesTheAppropriateControllerNode{
NORLabelNode *labelNode = [NORLabelNode labelNodeWithFontNamed:@"Avenir"];
XCTAssertEqual(labelNode.fontName, labelNode.propertyStateholderNode.fontName, @"The controllerNode should have the same fontName as the node itself");
XCTAssertNotNil(labelNode.propertyStateholderNode, @"The node should have a controllerNode after initWithFontName");
}
- (void)testThatLabelNodesFromTextReturnsTheCorrectNumberOfLabelNodes{
NORLabelNode *labelNode = [NORLabelNode node];
NSString *stringWithBreaks = @"There was an old lady\nwho swallowed a fly\nI don't know why...";
NSArray *labelNodes = [labelNode labelNodesFromText:stringWithBreaks];
XCTAssertEqual(3, [labelNodes count], @"There should be 3 nodes in the returned array.");
}
- (void)testThatSettingTextWithLinebreaksCreatesSubNodes{
NORLabelNode *labelNode = [NORLabelNode node];
labelNode.text = @"There was an old lady\nwho swallowed a fly\nI don't know why...";
XCTAssertEqual([labelNode.subNodes count], 3, @"The subNodes array of the labelNode should have three nodes after setting the text");
}
- (void)testThatTextMethodReturnsTheOriginalText{
NORLabelNode *labelNode = [NORLabelNode labelNodeWithFontNamed:@"Avenir"];
NSString *textString = @"There was an old lady\nwho swallowed a fly\nI don't know why...";
labelNode.text = textString;
XCTAssertEqualObjects(labelNode.text, textString, @"The text returned from the text method should be the same as the string passed into the property.");
}
- (void)testThatSubnodesInheritsPropertiesFromParent{
NORLabelNode *threeLineNode = [self nodeWithThreeSubNodes];
threeLineNode.fontColor = [SKColor greenColor];
threeLineNode.fontName = @"Avenir";
threeLineNode.fontSize = 100;
threeLineNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
threeLineNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeTop;
threeLineNode.position = CGPointMake(42, 0);
for (SKLabelNode *subNode in threeLineNode.subNodes) {
#if TARGET_OS_PHONE
XCTAssertEqualObjects(threeLineNode.fontColor, subNode.fontColor, @"The subnodes should have the same fontColor as the parent.");
#endif
XCTAssertEqualObjects(threeLineNode.fontName, subNode.fontName, @"The subnodes should have the same fontName as the parent.");
XCTAssertEqual(threeLineNode.fontSize, subNode.fontSize, @"The subnodes should have teh same fontSize as the parent.");
XCTAssertEqual(threeLineNode.horizontalAlignmentMode, subNode.horizontalAlignmentMode, @"The subnodes should have the same horizontalAligmentMode as the parent.");
XCTAssertEqual(threeLineNode.verticalAlignmentMode, subNode.verticalAlignmentMode, @"The subnodes should have the same verticalAlignmentMode as the parent.");
}
}
- (void)testThatChangingLineSpacingHasEffect{
SKScene *scene = nil;
NORLabelNode *threeLineNode = [self nodeWithThreeSubNodes];
[scene addChild:threeLineNode];
SKLabelNode *lastNode = [threeLineNode.subNodes lastObject];
CGFloat originalPosition = lastNode.position.y;
threeLineNode.lineSpacing = 3.0;
CGFloat changedPosition = lastNode.position.y;
XCTAssertNotEqual(originalPosition, changedPosition, @"The position of the node should have changed after altering the linespacing");
}
- (void) testThatDefaultFontSizeIsDefined{
NORLabelNode *newNode = [NORLabelNode labelNodeWithFontNamed:@"Helvetica"];
XCTAssertNotEqual(0, newNode.fontSize, @"The fontsize should never be 0");
}
- (void)testThatVerticallyCenteredNodesCentersOnMiddleOfFrame{
CGPoint position = CGPointMake(0, 100);
SKScene *scene = [SKScene sceneWithSize:CGSizeMake(1024, 768)];
NORLabelNode *threeLineNode = [self nodeWithThreeSubNodes];
threeLineNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
threeLineNode.position = position;
[scene addChild:threeLineNode];
SKLabelNode *middleNode = [threeLineNode.subNodes objectAtIndex:1];
CGPoint middleConvertedPosition = [threeLineNode convertPoint:middleNode.position toNode:threeLineNode.parent];
SKLabelNode *singleLineNode = [SKLabelNode node];
singleLineNode.text = @"who swallowed a fly";
singleLineNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
singleLineNode.position = position;
[scene addChild:singleLineNode];
XCTAssertEqual(singleLineNode.position.y, middleConvertedPosition.y, @"The nodes should have the same y-position");
}
- (NORLabelNode *)nodeWithThreeSubNodes{
NORLabelNode *labelNode = [[NORLabelNode alloc] init];
labelNode.text = @"There was an old lady\nwho swallowed a fly\nI don't know why...";
return labelNode;
}
@end