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

https://github.com/oleganza/GRMustache · Objective C · 400 lines · 347 code · 32 blank · 21 comment · 26 complexity · 494c24e5989d8d187c327a2242d79520 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 GRMustacheTemplateFromMethodsTest : GRMustachePublicAPITest
  25. @end
  26. @interface GRMustacheTemplateFromMethodsTestSupport: NSObject {
  27. NSString *_stringProperty;
  28. }
  29. @property (nonatomic, retain) NSString *stringProperty;
  30. @end
  31. @implementation GRMustacheTemplateFromMethodsTestSupport
  32. @synthesize stringProperty=_stringProperty;
  33. @end
  34. @interface GRMustacheTemplateFromMethodsTest()
  35. @property (nonatomic, readonly) NSString *templateName;
  36. @property (nonatomic, readonly) NSURL *templateURL;
  37. @property (nonatomic, readonly) NSString *templatePath;
  38. @property (nonatomic, readonly) NSString *templateString;
  39. @property (nonatomic, readonly) NSString *parserErrorTemplateName;
  40. @property (nonatomic, readonly) NSURL *parserErrorTemplateURL;
  41. @property (nonatomic, readonly) NSString *parserErrorTemplatePath;
  42. @property (nonatomic, readonly) NSString *parserErrorTemplateString;
  43. @property (nonatomic, readonly) NSString *parserErrorTemplateWrapperName;
  44. @property (nonatomic, readonly) NSURL *parserErrorTemplateWrapperURL;
  45. @property (nonatomic, readonly) NSString *parserErrorTemplateWrapperPath;
  46. @property (nonatomic, readonly) NSString *parserErrorTemplateWrapperString;
  47. @property (nonatomic, readonly) NSString *compilerErrorTemplateName;
  48. @property (nonatomic, readonly) NSURL *compilerErrorTemplateURL;
  49. @property (nonatomic, readonly) NSString *compilerErrorTemplatePath;
  50. @property (nonatomic, readonly) NSString *compilerErrorTemplateString;
  51. @property (nonatomic, readonly) NSString *compilerErrorTemplateWrapperName;
  52. @property (nonatomic, readonly) NSURL *compilerErrorTemplateWrapperURL;
  53. @property (nonatomic, readonly) NSString *compilerErrorTemplateWrapperPath;
  54. @property (nonatomic, readonly) NSString *compilerErrorTemplateWrapperString;
  55. @end
  56. @implementation GRMustacheTemplateFromMethodsTest
  57. - (NSString *)templateName { return @"GRMustacheTemplateFromMethodsTest"; }
  58. - (NSURL *)templateURL { return [self.testBundle URLForResource:self.templateName withExtension:@"mustache"]; }
  59. - (NSString *)templatePath { return [self.templateURL path]; }
  60. - (NSString *)templateString { return [NSString stringWithContentsOfFile:self.templatePath encoding:NSUTF8StringEncoding error:NULL]; }
  61. - (NSString *)parserErrorTemplateName { return @"GRMustacheTemplateFromMethodsTest_parserError"; }
  62. - (NSURL *)parserErrorTemplateURL { return [self.testBundle URLForResource:self.parserErrorTemplateName withExtension:@"mustache"]; }
  63. - (NSString *)parserErrorTemplatePath { return [self.parserErrorTemplateURL path]; }
  64. - (NSString *)parserErrorTemplateString { return [NSString stringWithContentsOfFile:self.parserErrorTemplatePath encoding:NSUTF8StringEncoding error:NULL]; }
  65. - (NSString *)parserErrorTemplateWrapperName { return @"GRMustacheTemplateFromMethodsTest_parserErrorWrapper"; }
  66. - (NSURL *)parserErrorTemplateWrapperURL { return [self.testBundle URLForResource:self.parserErrorTemplateWrapperName withExtension:@"mustache"]; }
  67. - (NSString *)parserErrorTemplateWrapperPath { return [self.parserErrorTemplateWrapperURL path]; }
  68. - (NSString *)parserErrorTemplateWrapperString { return [NSString stringWithContentsOfFile:self.parserErrorTemplateWrapperPath encoding:NSUTF8StringEncoding error:NULL]; }
  69. - (NSString *)compilerErrorTemplateName { return @"GRMustacheTemplateFromMethodsTest_compilerError"; }
  70. - (NSURL *)compilerErrorTemplateURL { return [self.testBundle URLForResource:self.compilerErrorTemplateName withExtension:@"mustache"]; }
  71. - (NSString *)compilerErrorTemplatePath { return [self.compilerErrorTemplateURL path]; }
  72. - (NSString *)compilerErrorTemplateString { return [NSString stringWithContentsOfFile:self.compilerErrorTemplatePath encoding:NSUTF8StringEncoding error:NULL]; }
  73. - (NSString *)compilerErrorTemplateWrapperName { return @"GRMustacheTemplateFromMethodsTest_compilerErrorWrapper"; }
  74. - (NSURL *)compilerErrorTemplateWrapperURL { return [self.testBundle URLForResource:self.compilerErrorTemplateWrapperName withExtension:@"mustache"]; }
  75. - (NSString *)compilerErrorTemplateWrapperPath { return [self.compilerErrorTemplateWrapperURL path]; }
  76. - (NSString *)compilerErrorTemplateWrapperString { return [NSString stringWithContentsOfFile:self.compilerErrorTemplateWrapperPath encoding:NSUTF8StringEncoding error:NULL]; }
  77. - (id)valueForKey:(NSString *)key inRendering:(NSString *)rendering
  78. {
  79. NSError *error;
  80. NSData *data = [rendering dataUsingEncoding:NSUTF8StringEncoding];
  81. id object = [self JSONObjectWithData:data error:&error];
  82. STAssertNotNil(object, @"%@", error);
  83. return [object valueForKey:key];
  84. }
  85. - (NSString *)valueForStringPropertyInRendering:(NSString *)rendering
  86. {
  87. return [self valueForKey:@"stringProperty" inRendering:rendering];
  88. }
  89. - (NSString *)extensionOfTemplateFileInRendering:(NSString *)rendering
  90. {
  91. NSString *fileName = [self valueForKey:@"fileName" inRendering:rendering];
  92. return [fileName pathExtension];
  93. }
  94. - (void)test_templateFromString_error
  95. {
  96. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:self.templateString
  97. error:NULL];
  98. GRMustacheTemplateFromMethodsTestSupport *context = [[[GRMustacheTemplateFromMethodsTestSupport alloc] init] autorelease];
  99. context.stringProperty = @"foo";
  100. NSString *rendering = [template renderObject:context error:NULL];
  101. STAssertEqualObjects(@"foo", [self valueForStringPropertyInRendering:rendering], nil);
  102. }
  103. - (void)test_templateFromContentsOfFile_error
  104. {
  105. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfFile:self.templatePath
  106. error:NULL];
  107. GRMustacheTemplateFromMethodsTestSupport *context = [[[GRMustacheTemplateFromMethodsTestSupport alloc] init] autorelease];
  108. context.stringProperty = @"foo";
  109. NSString *rendering = [template renderObject:context error:NULL];
  110. STAssertEqualObjects(@"foo", [self valueForStringPropertyInRendering:rendering], nil);
  111. }
  112. - (void)test_templateFromContentsOfURL_error
  113. {
  114. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfURL:self.templateURL
  115. error:NULL];
  116. GRMustacheTemplateFromMethodsTestSupport *context = [[[GRMustacheTemplateFromMethodsTestSupport alloc] init] autorelease];
  117. context.stringProperty = @"foo";
  118. NSString *rendering = [template renderObject:context error:NULL];
  119. STAssertEqualObjects(@"foo", [self valueForStringPropertyInRendering:rendering], nil);
  120. }
  121. - (void)test_templateFromResource_bundle_error
  122. {
  123. GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:self.templateName
  124. bundle:self.testBundle
  125. error:NULL];
  126. GRMustacheTemplateFromMethodsTestSupport *context = [[[GRMustacheTemplateFromMethodsTestSupport alloc] init] autorelease];
  127. context.stringProperty = @"foo";
  128. NSString *rendering = [template renderObject:context error:NULL];
  129. STAssertEqualObjects(@"foo", [self valueForStringPropertyInRendering:rendering], nil);
  130. STAssertEqualObjects(@"mustache", [self extensionOfTemplateFileInRendering:rendering], nil);
  131. }
  132. - (void)testParserError_templateFromString_error
  133. {
  134. NSError *error;
  135. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:self.parserErrorTemplateString
  136. error:&error];
  137. STAssertNil(template, @"");
  138. STAssertNotNil(error, @"");
  139. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  140. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  141. STAssertTrue(range.location != NSNotFound, @"");
  142. }
  143. - (void)testParserError_templateFromContentsOfFile_error
  144. {
  145. {
  146. NSError *error;
  147. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfFile:self.parserErrorTemplatePath
  148. error:&error];
  149. STAssertNil(template, @"");
  150. STAssertNotNil(error, @"");
  151. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  152. {
  153. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  154. STAssertTrue(range.location != NSNotFound, @"");
  155. }
  156. {
  157. NSRange range = [error.localizedDescription rangeOfString:self.parserErrorTemplatePath];
  158. STAssertTrue(range.location != NSNotFound, @"");
  159. }
  160. }
  161. {
  162. NSError *error;
  163. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfFile:self.parserErrorTemplateWrapperPath
  164. error:&error];
  165. STAssertNil(template, @"");
  166. STAssertNotNil(error, @"");
  167. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  168. {
  169. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  170. STAssertTrue(range.location != NSNotFound, @"");
  171. }
  172. {
  173. NSRange range = [error.localizedDescription rangeOfString:self.parserErrorTemplatePath];
  174. STAssertTrue(range.location != NSNotFound, @"");
  175. }
  176. }
  177. }
  178. - (void)testParserError_templateFromContentsOfURL_error
  179. {
  180. {
  181. NSError *error;
  182. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfURL:self.parserErrorTemplateURL
  183. error:&error];
  184. STAssertNil(template, @"");
  185. STAssertNotNil(error, @"");
  186. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  187. {
  188. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  189. STAssertTrue(range.location != NSNotFound, @"");
  190. }
  191. {
  192. NSRange range = [error.localizedDescription rangeOfString:self.parserErrorTemplatePath];
  193. STAssertTrue(range.location != NSNotFound, @"");
  194. }
  195. }
  196. {
  197. NSError *error;
  198. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfURL:self.parserErrorTemplateWrapperURL
  199. error:&error];
  200. STAssertNil(template, @"");
  201. STAssertNotNil(error, @"");
  202. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  203. {
  204. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  205. STAssertTrue(range.location != NSNotFound, @"");
  206. }
  207. {
  208. NSRange range = [error.localizedDescription rangeOfString:self.parserErrorTemplatePath];
  209. STAssertTrue(range.location != NSNotFound, @"");
  210. }
  211. }
  212. }
  213. - (void)testParserError_templateFromResource_bundle_error
  214. {
  215. {
  216. NSError *error;
  217. GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:self.parserErrorTemplateName
  218. bundle:self.testBundle
  219. error:&error];
  220. STAssertNil(template, @"");
  221. STAssertNotNil(error, @"");
  222. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  223. {
  224. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  225. STAssertTrue(range.location != NSNotFound, @"");
  226. }
  227. {
  228. NSRange range = [error.localizedDescription rangeOfString:self.parserErrorTemplatePath];
  229. STAssertTrue(range.location != NSNotFound, @"");
  230. }
  231. }
  232. {
  233. NSError *error;
  234. GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:self.parserErrorTemplateWrapperName
  235. bundle:self.testBundle
  236. error:&error];
  237. STAssertNil(template, @"");
  238. STAssertNotNil(error, @"");
  239. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  240. {
  241. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  242. STAssertTrue(range.location != NSNotFound, @"");
  243. }
  244. {
  245. NSRange range = [error.localizedDescription rangeOfString:self.parserErrorTemplatePath];
  246. STAssertTrue(range.location != NSNotFound, @"");
  247. }
  248. }
  249. }
  250. - (void)testCompilerError_templateFromString_error
  251. {
  252. NSError *error;
  253. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:self.compilerErrorTemplateString
  254. error:&error];
  255. STAssertNil(template, @"");
  256. STAssertNotNil(error, @"");
  257. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  258. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  259. STAssertTrue(range.location != NSNotFound, @"");
  260. }
  261. - (void)testCompilerError_templateFromContentsOfFile_error
  262. {
  263. {
  264. NSError *error;
  265. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfFile:self.compilerErrorTemplatePath
  266. error:&error];
  267. STAssertNil(template, @"");
  268. STAssertNotNil(error, @"");
  269. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  270. {
  271. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  272. STAssertTrue(range.location != NSNotFound, @"");
  273. }
  274. {
  275. NSRange range = [error.localizedDescription rangeOfString:self.compilerErrorTemplatePath];
  276. STAssertTrue(range.location != NSNotFound, @"");
  277. }
  278. }
  279. {
  280. NSError *error;
  281. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfFile:self.compilerErrorTemplateWrapperPath
  282. error:&error];
  283. STAssertNil(template, @"");
  284. STAssertNotNil(error, @"");
  285. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  286. {
  287. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  288. STAssertTrue(range.location != NSNotFound, @"");
  289. }
  290. {
  291. NSRange range = [error.localizedDescription rangeOfString:self.compilerErrorTemplatePath];
  292. STAssertTrue(range.location != NSNotFound, @"");
  293. }
  294. }
  295. }
  296. - (void)testCompilerError_templateFromContentsOfURL_error
  297. {
  298. {
  299. NSError *error;
  300. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfURL:self.compilerErrorTemplateURL
  301. error:&error];
  302. STAssertNil(template, @"");
  303. STAssertNotNil(error, @"");
  304. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  305. {
  306. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  307. STAssertTrue(range.location != NSNotFound, @"");
  308. }
  309. {
  310. NSRange range = [error.localizedDescription rangeOfString:self.compilerErrorTemplatePath];
  311. STAssertTrue(range.location != NSNotFound, @"");
  312. }
  313. }
  314. {
  315. NSError *error;
  316. GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfURL:self.compilerErrorTemplateWrapperURL
  317. error:&error];
  318. STAssertNil(template, @"");
  319. STAssertNotNil(error, @"");
  320. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  321. {
  322. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  323. STAssertTrue(range.location != NSNotFound, @"");
  324. }
  325. {
  326. NSRange range = [error.localizedDescription rangeOfString:self.compilerErrorTemplatePath];
  327. STAssertTrue(range.location != NSNotFound, @"");
  328. }
  329. }
  330. }
  331. - (void)testCompilerError_templateFromResource_bundle_error
  332. {
  333. {
  334. NSError *error;
  335. GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:self.compilerErrorTemplateName
  336. bundle:self.testBundle
  337. error:&error];
  338. STAssertNil(template, @"");
  339. STAssertNotNil(error, @"");
  340. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  341. {
  342. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  343. STAssertTrue(range.location != NSNotFound, @"");
  344. }
  345. {
  346. NSRange range = [error.localizedDescription rangeOfString:self.compilerErrorTemplatePath];
  347. STAssertTrue(range.location != NSNotFound, @"");
  348. }
  349. }
  350. {
  351. NSError *error;
  352. GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:self.compilerErrorTemplateWrapperName
  353. bundle:self.testBundle
  354. error:&error];
  355. STAssertNil(template, @"");
  356. STAssertNotNil(error, @"");
  357. STAssertEquals((NSInteger)GRMustacheErrorCodeParseError, error.code, @"");
  358. {
  359. NSRange range = [error.localizedDescription rangeOfString:@"line 2"];
  360. STAssertTrue(range.location != NSNotFound, @"");
  361. }
  362. {
  363. NSRange range = [error.localizedDescription rangeOfString:self.compilerErrorTemplatePath];
  364. STAssertTrue(range.location != NSNotFound, @"");
  365. }
  366. }
  367. }
  368. @end