/src/tests/Public/v6.0/GRBooleanTest.m

https://github.com/oleganza/GRMustache · Objective C · 356 lines · 305 code · 24 blank · 27 comment · 6 complexity · ee9dbd3ed8b50196ecc377d165f36c8a MD5 · raw file

  1. // The MIT License
  2. //
  3. // Copyright (c) 2013 Gwendal Roué
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #define GRMUSTACHE_VERSION_MAX_ALLOWED GRMUSTACHE_VERSION_6_0
  23. #import "GRMustachePublicAPITest.h"
  24. @interface GRBooleanTest : GRMustachePublicAPITest
  25. @end
  26. @interface GRBooleanTestSupport: NSObject {
  27. BOOL _customGetterBOOLProperty;
  28. bool _customGetterboolProperty;
  29. }
  30. @property (readonly) bool boolFalseProperty;
  31. @property (readonly) bool boolTrueProperty;
  32. @property (readonly) BOOL BOOLFalseProperty;
  33. @property (readonly) BOOL BOOLTrueProperty;
  34. @property (readonly) char charFalseProperty;
  35. @property (readonly) char charTrueProperty;
  36. @property (readonly) unsigned char unsigned_charFalseProperty;
  37. @property (readonly) unsigned char unsigned_charTrueProperty;
  38. @property (readonly) int intFalseProperty;
  39. @property (readonly) int intTrueProperty;
  40. @property (getter=isCustomGetterBOOLProperty) BOOL customGetterBOOLProperty;
  41. @property (getter=isCustomGetterboolProperty) bool customGetterboolProperty;
  42. @end
  43. @implementation GRBooleanTestSupport
  44. @synthesize customGetterBOOLProperty=_customGetterBOOLProperty;
  45. @synthesize customGetterboolProperty=_customGetterboolProperty;
  46. - (bool)boolFalseProperty { return NO; }
  47. - (bool)boolTrueProperty { return YES; }
  48. - (BOOL)BOOLFalseProperty { return NO; }
  49. - (BOOL)BOOLTrueProperty { return YES; }
  50. - (char)charFalseProperty { return NO; }
  51. - (char)charTrueProperty { return YES; }
  52. - (unsigned char)unsigned_charFalseProperty { return NO; }
  53. - (unsigned char)unsigned_charTrueProperty { return YES; }
  54. - (int)intFalseProperty { return NO; }
  55. - (int)intTrueProperty { return YES; }
  56. - (bool)boolFalseMethod { return NO; }
  57. - (bool)boolTrueMethod { return YES; }
  58. - (BOOL)BOOLFalseMethod { return NO; }
  59. - (BOOL)BOOLTrueMethod { return YES; }
  60. - (char)charFalseMethod { return NO; }
  61. - (char)charTrueMethod { return YES; }
  62. - (unsigned char)unsigned_charFalseMethod { return NO; }
  63. - (unsigned char)unsigned_charTrueMethod { return YES; }
  64. - (int)intFalseMethod { return NO; }
  65. - (int)intTrueMethod { return YES; }
  66. @end
  67. @interface GRBooleanTestSupportSubClass: GRBooleanTestSupport
  68. @end
  69. @implementation GRBooleanTestSupportSubClass
  70. @end
  71. @implementation GRBooleanTest
  72. - (BOOL)booleanInterpretationForKey:(NSString *)key inObject:(id)object
  73. {
  74. NSString *templateString = [NSString stringWithFormat:@"{{#%@}}YES{{/%@}}{{^%@}}NO{{/%@}}", key, key, key, key];
  75. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
  76. NSString *result = [template renderObject:object error:NULL];
  77. if ([result isEqualToString:@"YES"]) {
  78. return YES;
  79. } else if ([result isEqualToString:@"NO"]) {
  80. return NO;
  81. } else {
  82. result = [template renderObject:object error:NULL]; // allow breakpoint
  83. STAssertTrue(NO, @"");
  84. return NO; // meaningless
  85. }
  86. }
  87. - (BOOL)booleanInterpretationOfObject:(id)object
  88. {
  89. NSDictionary *context = object ? [NSDictionary dictionaryWithObject:object forKey:@"bool"] : [NSDictionary dictionary];
  90. return [self booleanInterpretationForKey:@"bool" inObject:context];
  91. }
  92. - (BOOL)doesObjectRender:(id)object
  93. {
  94. NSDictionary *context = object ? [NSDictionary dictionaryWithObject:object forKey:@"bool"] : [NSDictionary dictionary];
  95. NSString *result = [[GRMustacheTemplate templateFromString:@"<{{bool}}>" error:NULL] renderObject:context error:NULL];
  96. if ([result isEqualToString:@"<>"]) {
  97. return NO;
  98. } else {
  99. return YES;
  100. }
  101. }
  102. - (void)test_Nil_isFalseValue
  103. {
  104. // Test boolean interpretation of values that we could not test in GRMustacheSuites/sections.json and GRMustacheSuites/inverted_sections.json
  105. STAssertEquals(NO, [self booleanInterpretationOfObject:nil], nil);
  106. }
  107. - (void)test_Nil_doesNotRender
  108. {
  109. // Test boolean interpretation of values that we could not test in GRMustacheSuites/sections.json and GRMustacheSuites/inverted_sections.json
  110. STAssertEquals(NO, [self doesObjectRender:nil], @"");
  111. }
  112. - (void)test_NSNull_isFalseValue
  113. {
  114. // Test boolean interpretation of values that we could not test in GRMustacheSuites/sections.json and GRMustacheSuites/inverted_sections.json
  115. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNull null]], (NSInteger)NO, nil);
  116. }
  117. - (void)test_NSNull_doesNotRender
  118. {
  119. // Test boolean interpretation of values that we could not test in GRMustacheSuites/sections.json and GRMustacheSuites/inverted_sections.json
  120. STAssertEquals(NO, [self doesObjectRender:[NSNull null]], @"");
  121. }
  122. - (void)test_NSNumberWithZero_isFalseValue
  123. {
  124. // Test boolean interpretation of values that we could not test in GRMustacheSuites/sections.json and GRMustacheSuites/inverted_sections.json
  125. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithChar:0]], nil);
  126. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithFloat:0]], nil);
  127. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithDouble:0]], nil);
  128. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithInt:0]], nil);
  129. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithInteger:0]], nil);
  130. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithLong:0]], nil);
  131. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithLongLong:0]], nil);
  132. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithShort:0]], nil);
  133. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithUnsignedChar:0]], nil);
  134. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithUnsignedInt:0]], nil);
  135. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithUnsignedInteger:0]], nil);
  136. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithUnsignedLong:0]], nil);
  137. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithUnsignedLongLong:0]], nil);
  138. STAssertEquals(NO, [self booleanInterpretationOfObject:[NSNumber numberWithUnsignedShort:0]], nil);
  139. }
  140. - (void)test_NSNumberWithZero_doesRender
  141. {
  142. // Test boolean interpretation of values that we could not test in GRMustacheSuites/sections.json and GRMustacheSuites/inverted_sections.json
  143. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithChar:0]], @"");
  144. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithFloat:0]], @"");
  145. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithDouble:0]], @"");
  146. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithInt:0]], @"");
  147. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithInteger:0]], @"");
  148. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithLong:0]], @"");
  149. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithLongLong:0]], @"");
  150. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithShort:0]], @"");
  151. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithUnsignedChar:0]], @"");
  152. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithUnsignedInt:0]], @"");
  153. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithUnsignedInteger:0]], @"");
  154. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithUnsignedLong:0]], @"");
  155. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithUnsignedLongLong:0]], @"");
  156. STAssertEquals(YES, [self doesObjectRender:[NSNumber numberWithUnsignedShort:0]], @"");
  157. }
  158. - (void)testCustomGetterBOOLProperty
  159. {
  160. GRBooleanTestSupport *context = [[[GRBooleanTestSupport alloc] init] autorelease];
  161. GRBooleanTestSupportSubClass *inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  162. context.customGetterBOOLProperty = NO;
  163. inheritingContext.customGetterBOOLProperty = NO;
  164. STAssertEquals(NO, [self booleanInterpretationForKey:@"customGetterBOOLProperty" inObject:context], @"");
  165. STAssertEquals(NO, [self booleanInterpretationForKey:@"isCustomGetterBOOLProperty" inObject:context], @"");
  166. STAssertEquals(NO, [self booleanInterpretationForKey:@"customGetterBOOLProperty" inObject:inheritingContext], @"");
  167. STAssertEquals(NO, [self booleanInterpretationForKey:@"isCustomGetterBOOLProperty" inObject:inheritingContext], @"");
  168. context.customGetterBOOLProperty = YES;
  169. inheritingContext.customGetterBOOLProperty = YES;
  170. STAssertEquals(YES, [self booleanInterpretationForKey:@"customGetterBOOLProperty" inObject:context], @"");
  171. STAssertEquals(YES, [self booleanInterpretationForKey:@"isCustomGetterBOOLProperty" inObject:context], @"");
  172. STAssertEquals(YES, [self booleanInterpretationForKey:@"customGetterBOOLProperty" inObject:inheritingContext], @"");
  173. STAssertEquals(YES, [self booleanInterpretationForKey:@"isCustomGetterBOOLProperty" inObject:inheritingContext], @"");
  174. }
  175. - (void)testCustomGetterboolProperty
  176. {
  177. GRBooleanTestSupport *context = [[[GRBooleanTestSupport alloc] init] autorelease];
  178. GRBooleanTestSupportSubClass *inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  179. context.customGetterboolProperty = NO;
  180. inheritingContext.customGetterboolProperty = NO;
  181. STAssertEquals(NO, [self booleanInterpretationForKey:@"customGetterboolProperty" inObject:context], @"");
  182. STAssertEquals(NO, [self booleanInterpretationForKey:@"isCustomGetterboolProperty" inObject:context], @"");
  183. STAssertEquals(NO, [self booleanInterpretationForKey:@"customGetterboolProperty" inObject:inheritingContext], @"");
  184. STAssertEquals(NO, [self booleanInterpretationForKey:@"isCustomGetterboolProperty" inObject:inheritingContext], @"");
  185. context.customGetterboolProperty = YES;
  186. inheritingContext.customGetterboolProperty = YES;
  187. STAssertEquals(YES, [self booleanInterpretationForKey:@"customGetterboolProperty" inObject:context], @"");
  188. STAssertEquals(YES, [self booleanInterpretationForKey:@"isCustomGetterboolProperty" inObject:context], @"");
  189. STAssertEquals(YES, [self booleanInterpretationForKey:@"customGetterboolProperty" inObject:inheritingContext], @"");
  190. STAssertEquals(YES, [self booleanInterpretationForKey:@"isCustomGetterboolProperty" inObject:inheritingContext], @"");
  191. }
  192. - (void)test_boolFalseProperty_isFalseValue
  193. {
  194. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  195. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  196. STAssertEquals(NO, [self booleanInterpretationForKey:@"boolFalseProperty" inObject:context], @"");
  197. STAssertEquals(NO, [self booleanInterpretationForKey:@"boolFalseProperty" inObject:inheritingContext], @"");
  198. }
  199. - (void)test_boolTrueProperty_isTrueValue
  200. {
  201. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  202. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  203. STAssertEquals(YES, [self booleanInterpretationForKey:@"boolTrueProperty" inObject:context], @"");
  204. STAssertEquals(YES, [self booleanInterpretationForKey:@"boolTrueProperty" inObject:inheritingContext], @"");
  205. }
  206. - (void)test_BOOLFalseProperty_isTrueValue
  207. {
  208. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  209. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  210. STAssertEquals(NO, [self booleanInterpretationForKey:@"BOOLFalseProperty" inObject:context], @"");
  211. STAssertEquals(NO, [self booleanInterpretationForKey:@"BOOLFalseProperty" inObject:inheritingContext], @"");
  212. }
  213. - (void)test_BOOLTrueProperty_isTrueValue
  214. {
  215. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  216. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  217. STAssertEquals(YES, [self booleanInterpretationForKey:@"BOOLTrueProperty" inObject:context], @"");
  218. STAssertEquals(YES, [self booleanInterpretationForKey:@"BOOLTrueProperty" inObject:inheritingContext], @"");
  219. }
  220. - (void)test_charFalseProperty_isTrueValue
  221. {
  222. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  223. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  224. STAssertEquals(NO, [self booleanInterpretationForKey:@"charFalseProperty" inObject:context], @"");
  225. STAssertEquals(NO, [self booleanInterpretationForKey:@"charFalseProperty" inObject:inheritingContext], @"");
  226. }
  227. - (void)test_charTrueProperty_isTrueValue
  228. {
  229. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  230. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  231. STAssertEquals(YES, [self booleanInterpretationForKey:@"charTrueProperty" inObject:context], @"");
  232. STAssertEquals(YES, [self booleanInterpretationForKey:@"charTrueProperty" inObject:inheritingContext], @"");
  233. }
  234. - (void)test_unsigned_charFalseProperty_isFalseValue
  235. {
  236. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  237. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  238. STAssertEquals(NO, [self booleanInterpretationForKey:@"unsigned_charFalseProperty" inObject:context], @"");
  239. STAssertEquals(NO, [self booleanInterpretationForKey:@"unsigned_charFalseProperty" inObject:inheritingContext], @"");
  240. }
  241. - (void)test_unsigned_charTrueProperty_isTrueValue
  242. {
  243. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  244. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  245. STAssertEquals(YES, [self booleanInterpretationForKey:@"unsigned_charTrueProperty" inObject:context], @"");
  246. STAssertEquals(YES, [self booleanInterpretationForKey:@"unsigned_charTrueProperty" inObject:inheritingContext], @"");
  247. }
  248. - (void)test_intFalseProperty_isFalseValue
  249. {
  250. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  251. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  252. STAssertEquals(NO, [self booleanInterpretationForKey:@"intFalseProperty" inObject:context], @"");
  253. STAssertEquals(NO, [self booleanInterpretationForKey:@"intFalseProperty" inObject:inheritingContext], @"");
  254. }
  255. - (void)test_intTrueProperty_isTrueValue
  256. {
  257. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  258. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  259. STAssertEquals(YES, [self booleanInterpretationForKey:@"intTrueProperty" inObject:context], @"");
  260. STAssertEquals(YES, [self booleanInterpretationForKey:@"intTrueProperty" inObject:inheritingContext], @"");
  261. }
  262. - (void)test_boolFalseMethod_isFalseValue
  263. {
  264. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  265. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  266. STAssertEquals(NO, [self booleanInterpretationForKey:@"boolFalseMethod" inObject:context], @"");
  267. STAssertEquals(NO, [self booleanInterpretationForKey:@"boolFalseMethod" inObject:inheritingContext], @"");
  268. }
  269. - (void)test_boolTrueMethod_isTrueValue
  270. {
  271. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  272. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  273. STAssertEquals(YES, [self booleanInterpretationForKey:@"boolTrueMethod" inObject:context], @"");
  274. STAssertEquals(YES, [self booleanInterpretationForKey:@"boolTrueMethod" inObject:inheritingContext], @"");
  275. }
  276. - (void)test_BOOLFalseMethod_isFalseValue
  277. {
  278. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  279. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  280. STAssertEquals(NO, [self booleanInterpretationForKey:@"BOOLFalseMethod" inObject:context], @"");
  281. STAssertEquals(NO, [self booleanInterpretationForKey:@"BOOLFalseMethod" inObject:inheritingContext], @"");
  282. }
  283. - (void)test_BOOLTrueMethod_isTrueValue
  284. {
  285. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  286. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  287. STAssertEquals(YES, [self booleanInterpretationForKey:@"BOOLTrueMethod" inObject:context], @"");
  288. STAssertEquals(YES, [self booleanInterpretationForKey:@"BOOLTrueMethod" inObject:inheritingContext], @"");
  289. }
  290. - (void)test_charFalseMethod_isFalseValue
  291. {
  292. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  293. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  294. STAssertEquals(NO, [self booleanInterpretationForKey:@"charFalseMethod" inObject:context], @"");
  295. STAssertEquals(NO, [self booleanInterpretationForKey:@"charFalseMethod" inObject:inheritingContext], @"");
  296. }
  297. - (void)test_charTrueMethod_isTrueValue
  298. {
  299. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  300. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  301. STAssertEquals(YES, [self booleanInterpretationForKey:@"charTrueMethod" inObject:context], @"");
  302. STAssertEquals(YES, [self booleanInterpretationForKey:@"charTrueMethod" inObject:inheritingContext], @"");
  303. }
  304. - (void)test_unsigned_charFalseMethod_isFalseValue
  305. {
  306. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  307. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  308. STAssertEquals(NO, [self booleanInterpretationForKey:@"unsigned_charFalseMethod" inObject:context], @"");
  309. STAssertEquals(NO, [self booleanInterpretationForKey:@"unsigned_charFalseMethod" inObject:inheritingContext], @"");
  310. }
  311. - (void)test_unsigned_charTrueMethod_isTrueValue
  312. {
  313. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  314. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  315. STAssertEquals(YES, [self booleanInterpretationForKey:@"unsigned_charTrueMethod" inObject:context], @"");
  316. STAssertEquals(YES, [self booleanInterpretationForKey:@"unsigned_charTrueMethod" inObject:inheritingContext], @"");
  317. }
  318. - (void)test_intFalseMethod_isFalseValue
  319. {
  320. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  321. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  322. STAssertEquals(NO, [self booleanInterpretationForKey:@"intFalseMethod" inObject:context], @"");
  323. STAssertEquals(NO, [self booleanInterpretationForKey:@"intFalseMethod" inObject:inheritingContext], @"");
  324. }
  325. - (void)test_intTrueMethod_isTrueValue
  326. {
  327. id context = [[[GRBooleanTestSupport alloc] init] autorelease];
  328. id inheritingContext = [[[GRBooleanTestSupportSubClass alloc] init] autorelease];
  329. STAssertEquals(YES, [self booleanInterpretationForKey:@"intTrueMethod" inObject:context], @"");
  330. STAssertEquals(YES, [self booleanInterpretationForKey:@"intTrueMethod" inObject:inheritingContext], @"");
  331. }
  332. @end