forked from navellabs/objective-mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLObjectiveMustacheTest.m
175 lines (132 loc) · 5.25 KB
/
NLObjectiveMustacheTest.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#import <SenTestingKit/SenTestingKit.h>
#import "NLObjectiveMustache.h"
#define DICT(...) [NSDictionary dictionaryWithObjectsAndKeys:__VA_ARGS__, nil]
@interface NLObjectiveMustacheTest : SenTestCase {
}
@end
@interface TestObjectiveMustacheKVCObject : NSObject {
}
// This should be accessible via Key Value Coding
// i.e. [obj valueForKey:@"replace_me"];
- (NSString *)replace_me;
@end
@implementation TestObjectiveMustacheKVCObject
- (NSString *)replace_me
{
return @"replaced by KVC result!";
}
@end
@implementation NLObjectiveMustacheTest
- (void)testTemplateWithNoInterpolationsRendersAsIs
{
NSString *template = @"this is same";
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:nil];
STAssertEqualObjects(result, @"this is same", nil);
}
- (void)testSubstitutingAVariableOnASingleLine
{
NSString *template = @"this is {{variable}}";
NSDictionary *view = DICT(@"good!", @"variable", nil);
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"this is good!", nil);
}
- (void)testSubstitutingMultipleVariables
{
NSString *template = @"this is {{variable}}\n and {{more}}";
NSDictionary *view = DICT(
@"good!", @"variable",
@"great!", @"more",
nil);
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"this is good!\n and great!", nil);
}
- (void)testHandlesEmptySigils
{
NSString *template = @"this {{}}";
NSDictionary *view = DICT(@"good!", @"variable", nil);
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"this ", nil);
}
- (void)testWhenTheTemplateStartsWithAnInterpolation
{
NSString *template = @"{{escape_me}} else";
NSDictionary *view = DICT(@"something", @"escape_me");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"something else", nil);
}
- (void)testEscapingInterpolatedValues
{
NSString *template = @"Please {{escape_me}}";
NSDictionary *view = DICT(@"<>&\"", @"escape_me");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Please <>&"", nil);
}
- (void)testMissingVariablesAreJustIgnored
{
NSString *template = @"Is this {{missing}}?";
NSDictionary *view = DICT(@"another", @"value");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Is this ?", nil);
}
- (void)testWorksWithANilView
{
NSString *template = @"Is this {{missing}}?";
NSDictionary *view = nil;
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Is this ?", nil);
}
- (void)testBooleanSectionIgnoredWhenFalse
{
NSString *template = @"Here {{#missing}}this{{/missing}}";
NSDictionary *view = DICT(@"another", @"value");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Here ", nil);
}
- (void)testBooleanSectionRenderedWhenTrue
{
NSString *template = @"Here {{#not_missing}}this{{/not_missing}}";
NSDictionary *view = DICT([NSNumber numberWithBool:YES], @"not_missing");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Here this", nil);
}
- (void)testBooleanSectionWithInnerInterpolations
{
NSString *template = @"Here {{#not_missing}}this {{value}}{{/not_missing}}";
NSDictionary *view = DICT(
[NSNumber numberWithBool:YES], @"not_missing",
@"one", @"value");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Here this one", nil);
}
- (void)testNonEscapedInterpolation
{
NSString *template = @"{{{no_escape}}}";
NSDictionary *view = DICT(@"<>&", @"no_escape");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"<>&", nil);
}
- (void)testViewObjectThatIsNotADictionaryButSupportsKeyValueCoding
{
NSString *template = @"{{replace_me}}";
TestObjectiveMustacheKVCObject *view = [[TestObjectiveMustacheKVCObject alloc] init];
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"replaced by KVC result!", nil);
[view release];
}
- (void)testSectionIteratesOverAnEnumerable
{
NSString *template = @"Here is{{#items}} this {{item}}{{/items}}";
NSArray *items = [NSArray arrayWithObjects:DICT(@"first", @"item"), DICT(@"second", @"item"), nil];
NSDictionary *view = DICT(items, @"items");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Here is this first this second", nil);
}
- (void)testSectionDoesNotShowAnEmptyEnumerable
{
NSString *template = @"Here is{{#items}} this {{item}}{{/items}}";
NSArray *items = [NSArray array];
NSDictionary *view = DICT(items, @"items");
NSString *result = [NLObjectiveMustache stringFromTemplate:template view:view];
STAssertEqualObjects(result, @"Here is", nil);
}
@end