/src/tests/Public/v6.2/GRMustacheConfigurationTest.m

https://github.com/oleganza/GRMustache · Objective C · 426 lines · 314 code · 62 blank · 50 comment · 0 complexity · 8359dc9103d1bc56a7af741d79b3ddc7 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_2
  23. #import "GRMustachePublicAPITest.h"
  24. @interface GRMustacheConfigurationTest : GRMustachePublicAPITest
  25. @end
  26. static BOOL defaultConfigurationHasBeenTouched = NO;
  27. @implementation GRMustacheConfigurationTest
  28. - (void)tearDown
  29. {
  30. [super tearDown];
  31. // Restore default configuration
  32. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  33. // Help test1DefaultConfigurationHasHTMLContentType test the *real* default
  34. // configuration.
  35. defaultConfigurationHasBeenTouched = YES;
  36. }
  37. // The goal is to have this test run first.
  38. // It looks that alphabetical order is applied: hence the digit 1 in the method name.
  39. - (void)test1DefaultConfigurationHasHTMLContentType
  40. {
  41. STAssertFalse(defaultConfigurationHasBeenTouched, @"this test should run first.");
  42. STAssertNotNil([GRMustacheConfiguration defaultConfiguration], @"");
  43. STAssertEquals([GRMustacheConfiguration defaultConfiguration].contentType, GRMustacheContentTypeHTML, @"");
  44. }
  45. - (void)testFactoryConfigurationHasHTMLContentTypeRegardlessOfDefaultConfiguration
  46. {
  47. {
  48. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  49. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  50. STAssertNotNil(configuration, @"");
  51. STAssertEquals(configuration.contentType, GRMustacheContentTypeHTML, @"");
  52. }
  53. {
  54. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  55. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  56. STAssertNotNil(configuration, @"");
  57. STAssertEquals(configuration.contentType, GRMustacheContentTypeHTML, @"");
  58. }
  59. }
  60. - (void)testDefaultConfigurationContentTypeHTMLHasTemplateRenderEscapedInput
  61. {
  62. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  63. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:@"{{subject}}" error:NULL];
  64. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  65. STAssertEqualObjects(rendering, @"&", @"");
  66. }
  67. - (void)testDefaultConfigurationContentTypeTextHasTemplateRenderRawInput
  68. {
  69. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  70. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:@"{{subject}}" error:NULL];
  71. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  72. STAssertEqualObjects(rendering, @"&", @"");
  73. }
  74. - (void)testDefaultConfigurationContentTypeHTMLHasTemplateRenderHTMLSafeStrings
  75. {
  76. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  77. // Templates tell if they render HTML or text via the HTMLSafe output of the
  78. // -[GRMustacheTemplate renderContentWithContext:HTMLSafe:error:] method.
  79. //
  80. // There is no public way to build a context.
  81. //
  82. // Thus we'll use a rendering object that will provide us with one:
  83. GRMustacheTemplate *testedTemplate = [GRMustacheTemplate templateFromString:@"" error:NULL];
  84. __block BOOL testedHTMLSafeDefined = NO;
  85. __block BOOL testedHTMLSafe = NO;
  86. id object = [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
  87. NSString *rendering = [testedTemplate renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
  88. testedHTMLSafe = *HTMLSafe;
  89. testedHTMLSafeDefined = YES;
  90. return rendering;
  91. }];
  92. id data = @{@"object": object};
  93. [GRMustacheTemplate renderObject:data fromString:@"{{object}}" error:NULL];
  94. STAssertTrue(testedHTMLSafeDefined, @"WTF");
  95. STAssertTrue(testedHTMLSafe, @"WTF");
  96. }
  97. - (void)testDefaultConfigurationContentTypeTextHasTemplateRenderHTMLUnsafeStrings
  98. {
  99. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  100. // Templates tell if they render HTML or text via the HTMLSafe output of the
  101. // -[GRMustacheTemplate renderContentWithContext:HTMLSafe:error:] method.
  102. //
  103. // There is no public way to build a context.
  104. //
  105. // Thus we'll use a rendering object that will provide us with one:
  106. GRMustacheTemplate *testedTemplate = [GRMustacheTemplate templateFromString:@"" error:NULL];
  107. __block BOOL testedHTMLSafeDefined = NO;
  108. __block BOOL testedHTMLSafe = NO;
  109. id object = [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
  110. NSString *rendering = [testedTemplate renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
  111. testedHTMLSafe = *HTMLSafe;
  112. testedHTMLSafeDefined = YES;
  113. return rendering;
  114. }];
  115. id data = @{@"object": object};
  116. [GRMustacheTemplate renderObject:data fromString:@"{{object}}" error:NULL];
  117. STAssertTrue(testedHTMLSafeDefined, @"WTF");
  118. STAssertFalse(testedHTMLSafe, @"WTF");
  119. }
  120. - (void)testDefaultConfigurationContentTypeHTMLHasSectionTagRenderHTMLSafeStrings
  121. {
  122. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  123. __block BOOL testedHTMLSafeDefined = NO;
  124. __block BOOL testedHTMLSafe = NO;
  125. id object = [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
  126. NSString *rendering = [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
  127. testedHTMLSafe = *HTMLSafe;
  128. testedHTMLSafeDefined = YES;
  129. return rendering;
  130. }];
  131. id data = @{@"object": object};
  132. [GRMustacheTemplate renderObject:data fromString:@"{{#object}}{{/object}}" error:NULL];
  133. STAssertTrue(testedHTMLSafeDefined, @"WTF");
  134. STAssertTrue(testedHTMLSafe, @"WTF");
  135. }
  136. - (void)testDefaultConfigurationContentTypeTextHasSectionTagRenderHTMLUnsafeStrings
  137. {
  138. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  139. __block BOOL testedHTMLSafeDefined = NO;
  140. __block BOOL testedHTMLSafe = NO;
  141. id object = [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
  142. NSString *rendering = [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
  143. testedHTMLSafe = *HTMLSafe;
  144. testedHTMLSafeDefined = YES;
  145. return rendering;
  146. }];
  147. id data = @{@"object": object};
  148. [GRMustacheTemplate renderObject:data fromString:@"{{#object}}{{/object}}" error:NULL];
  149. STAssertTrue(testedHTMLSafeDefined, @"WTF");
  150. STAssertFalse(testedHTMLSafe, @"WTF");
  151. }
  152. - (void)testDefaultConfigurationContentTypeHTMLHasVariableTagRenderHTMLSafeStrings
  153. {
  154. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  155. __block BOOL testedHTMLSafeDefined = NO;
  156. __block BOOL testedHTMLSafe = NO;
  157. id object = [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
  158. NSString *rendering = [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
  159. testedHTMLSafe = *HTMLSafe;
  160. testedHTMLSafeDefined = YES;
  161. return rendering;
  162. }];
  163. id data = @{@"object": object};
  164. [GRMustacheTemplate renderObject:data fromString:@"{{object}}" error:NULL];
  165. STAssertTrue(testedHTMLSafeDefined, @"WTF");
  166. STAssertTrue(testedHTMLSafe, @"WTF");
  167. }
  168. - (void)testDefaultConfigurationContentTypeTextHasVariableTagRenderHTMLUnsafeStrings
  169. {
  170. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  171. __block BOOL testedHTMLSafeDefined = NO;
  172. __block BOOL testedHTMLSafe = NO;
  173. id object = [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
  174. NSString *rendering = [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
  175. testedHTMLSafe = *HTMLSafe;
  176. testedHTMLSafeDefined = YES;
  177. return rendering;
  178. }];
  179. id data = @{@"object": object};
  180. [GRMustacheTemplate renderObject:data fromString:@"{{object}}" error:NULL];
  181. STAssertTrue(testedHTMLSafeDefined, @"WTF");
  182. STAssertFalse(testedHTMLSafe, @"WTF");
  183. }
  184. - (void)testCONTENT_TYPE_TEXTPragmaTagOverridesDefaultConfigurationContentTypeHTML
  185. {
  186. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  187. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:@"{{%CONTENT_TYPE:TEXT}}{{subject}}" error:NULL];
  188. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  189. STAssertEqualObjects(rendering, @"&", @"");
  190. }
  191. - (void)testCONTENT_TYPE_HTMLPragmaTagOverridesDefaultConfigurationContentTypeText
  192. {
  193. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  194. GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:@"{{%CONTENT_TYPE:HTML}}{{subject}}" error:NULL];
  195. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  196. STAssertEqualObjects(rendering, @"&", @"");
  197. }
  198. - (void)testDefaultRepositoryConfigurationHasDefaultConfigurationContentType
  199. {
  200. {
  201. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  202. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  203. STAssertEquals(repo.configuration.contentType, [GRMustacheConfiguration defaultConfiguration].contentType, @"");
  204. }
  205. {
  206. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  207. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  208. STAssertEquals(repo.configuration.contentType, [GRMustacheConfiguration defaultConfiguration].contentType, @"");
  209. }
  210. }
  211. - (void)testRepositoryConfigurationContentTypeHTMLHasTemplateRenderEscapedInput
  212. {
  213. {
  214. // Setting the whole configuration
  215. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  216. configuration.contentType = GRMustacheContentTypeHTML;
  217. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  218. repo.configuration = configuration;
  219. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  220. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  221. STAssertEqualObjects(rendering, @"&", @"");
  222. }
  223. {
  224. // Setting configuration property
  225. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  226. repo.configuration.contentType = GRMustacheContentTypeHTML;
  227. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  228. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  229. STAssertEqualObjects(rendering, @"&", @"");
  230. }
  231. }
  232. - (void)testRepositoryConfigurationContentTypeTextHasTemplateRenderRawInput
  233. {
  234. {
  235. // Setting the whole configuration
  236. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  237. configuration.contentType = GRMustacheContentTypeText;
  238. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  239. repo.configuration = configuration;
  240. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  241. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  242. STAssertEqualObjects(rendering, @"&", @"");
  243. }
  244. {
  245. // Setting configuration property
  246. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  247. repo.configuration.contentType = GRMustacheContentTypeText;
  248. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  249. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  250. STAssertEqualObjects(rendering, @"&", @"");
  251. }
  252. }
  253. - (void)testRepositoryConfigurationContentTypeTextOverridesDefaultConfigurationContentTypeHTML
  254. {
  255. {
  256. // Setting the whole configuration
  257. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  258. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  259. configuration.contentType = GRMustacheContentTypeText;
  260. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  261. repo.configuration = configuration;
  262. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  263. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  264. STAssertEqualObjects(rendering, @"&", @"");
  265. }
  266. {
  267. // Setting configuration property
  268. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeHTML;
  269. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  270. repo.configuration.contentType = GRMustacheContentTypeText;
  271. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  272. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  273. STAssertEqualObjects(rendering, @"&", @"");
  274. }
  275. }
  276. - (void)testRepositoryConfigurationContentTypeHTMLOverridesDefaultConfigurationContentTypeText
  277. {
  278. {
  279. // Setting the whole configuration
  280. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  281. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  282. configuration.contentType = GRMustacheContentTypeHTML;
  283. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  284. repo.configuration = configuration;
  285. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  286. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  287. STAssertEqualObjects(rendering, @"&", @"");
  288. }
  289. {
  290. // Setting configuration property
  291. [GRMustacheConfiguration defaultConfiguration].contentType = GRMustacheContentTypeText;
  292. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  293. repo.configuration.contentType = GRMustacheContentTypeHTML;
  294. GRMustacheTemplate *template = [repo templateFromString:@"{{subject}}" error:NULL];
  295. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  296. STAssertEqualObjects(rendering, @"&", @"");
  297. }
  298. }
  299. - (void)testCONTENT_TYPE_TEXTPragmaTagOverridesRepositoryConfigurationContentTypeHTML
  300. {
  301. {
  302. // Setting the whole configuration
  303. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  304. configuration.contentType = GRMustacheContentTypeHTML;
  305. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  306. repo.configuration = configuration;
  307. GRMustacheTemplate *template = [repo templateFromString:@"{{%CONTENT_TYPE:TEXT}}{{subject}}" error:NULL];
  308. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  309. STAssertEqualObjects(rendering, @"&", @"");
  310. }
  311. {
  312. // Setting configuration property
  313. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  314. repo.configuration.contentType = GRMustacheContentTypeHTML;
  315. GRMustacheTemplate *template = [repo templateFromString:@"{{%CONTENT_TYPE:TEXT}}{{subject}}" error:NULL];
  316. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  317. STAssertEqualObjects(rendering, @"&", @"");
  318. }
  319. }
  320. - (void)testCONTENT_TYPE_HTMLPragmaTagOverridesRepositoryConfigurationContentTypeText
  321. {
  322. {
  323. // Setting the whole configuration
  324. GRMustacheConfiguration *configuration = [GRMustacheConfiguration configuration];
  325. configuration.contentType = GRMustacheContentTypeText;
  326. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  327. repo.configuration = configuration;
  328. GRMustacheTemplate *template = [repo templateFromString:@"{{%CONTENT_TYPE:HTML}}{{subject}}" error:NULL];
  329. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  330. STAssertEqualObjects(rendering, @"&", @"");
  331. }
  332. {
  333. // Setting configuration property
  334. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  335. repo.configuration.contentType = GRMustacheContentTypeText;
  336. GRMustacheTemplate *template = [repo templateFromString:@"{{%CONTENT_TYPE:HTML}}{{subject}}" error:NULL];
  337. NSString *rendering = [template renderObject:@{@"subject":@"&"} error:NULL];
  338. STAssertEqualObjects(rendering, @"&", @"");
  339. }
  340. }
  341. - (void)testRepositoryConfigurationCanBeMutatedBeforeAnyTemplateHasBeenCompiled
  342. {
  343. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  344. STAssertNoThrow([repo.configuration setContentType:GRMustacheContentTypeText], @"");
  345. STAssertNoThrow([repo.configuration setContentType:GRMustacheContentTypeHTML], @"");
  346. STAssertNoThrow([repo.configuration setContentType:GRMustacheContentTypeText], @"");
  347. }
  348. - (void)testDefaultConfigurationCanBeMutatedBeforeAnyTemplateHasBeenCompiled
  349. {
  350. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  351. [repo templateFromString:@"" error:NULL];
  352. STAssertNoThrow([[GRMustacheConfiguration defaultConfiguration] setContentType:GRMustacheContentTypeText], @"");
  353. STAssertNoThrow([[GRMustacheConfiguration defaultConfiguration] setContentType:GRMustacheContentTypeHTML], @"");
  354. STAssertNoThrow([[GRMustacheConfiguration defaultConfiguration] setContentType:GRMustacheContentTypeText], @"");
  355. }
  356. - (void)testRepositoryConfigurationCanNotBeMutatedAfterATemplateHasBeenCompiled
  357. {
  358. GRMustacheTemplateRepository *repo = [GRMustacheTemplateRepository templateRepository];
  359. [repo templateFromString:@"" error:NULL];
  360. STAssertThrows([repo.configuration setContentType:GRMustacheContentTypeText], @"");
  361. STAssertThrows([repo.configuration setContentType:GRMustacheContentTypeHTML], @"");
  362. STAssertThrows([repo setConfiguration:[GRMustacheConfiguration configuration]], @"");
  363. }
  364. @end