/ZincTests/ZincManifestTests.m

http://github.com/mindsnacks/Zinc-ObjC · Objective C · 405 lines · 333 code · 65 blank · 7 comment · 37 complexity · 68c3d2bedb5e7843653f0240a606c765 MD5 · raw file

  1. //
  2. // ZincManifestTests.m
  3. // Zinc-ObjC
  4. //
  5. // Created by Andy Mroczkowski on 1/17/12.
  6. // Copyright (c) 2012 MindSnacks. All rights reserved.
  7. //
  8. #import "ZincManifest.h"
  9. #import "ZincJSONSerialization.h"
  10. @interface ZincManifestTests : SenTestCase
  11. @end
  12. @implementation ZincManifestTests
  13. - (void) testReadFromJson1
  14. {
  15. NSError* error = nil;
  16. NSString* path = TEST_RESOURCE_PATH(@"meep-1.json");
  17. NSData* jsonData = [[[NSData alloc] initWithContentsOfFile:path options:0 error:&error] autorelease];
  18. if (jsonData == nil) {
  19. STFail(@"%@", error);
  20. }
  21. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  22. if (dict == nil) {
  23. STFail(@"%@", error);
  24. }
  25. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  26. if (manifest == nil) {
  27. STFail(@"manifest didn't load");
  28. }
  29. NSString* sha = [manifest shaForFile:@"tmp9GuVWu"];
  30. STAssertEqualObjects(sha, @"697948fc09f23a83e9755b4ed42ddd1ad489d408", @"sha is wrong");
  31. NSArray* allSHAs = [manifest allSHAs];
  32. STAssertTrue([allSHAs count] == 1, @"count wrong");
  33. NSString* firstSHA = [allSHAs objectAtIndex:0];
  34. STAssertEqualObjects(firstSHA, @"697948fc09f23a83e9755b4ed42ddd1ad489d408", @"sha is wrong");
  35. NSArray* allFormats = [manifest formatsForFile:@"tmp9GuVWu"];
  36. STAssertTrue([allFormats count] == 1, @"count wrong");
  37. NSString* firstFormat = [allFormats objectAtIndex:0];
  38. STAssertEqualObjects(firstFormat, ZincFileFormatGZ, @"format is wrong");
  39. }
  40. - (void) testBestFormatForFileRawOnly
  41. {
  42. NSString* jsonString =
  43. @"{ \
  44. \"files\": { \
  45. \"meep.jpg\": { \
  46. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  47. \"formats\": { \
  48. \"raw\": { \
  49. \"size\": 3578 \
  50. } \
  51. } \
  52. } \
  53. }, \
  54. \"version\": 1, \
  55. \"bundle\": \"Test\" \
  56. }";
  57. NSError* error = nil;
  58. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  59. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  60. if (dict == nil) {
  61. STFail(@"%@", error);
  62. }
  63. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  64. if (manifest == nil) {
  65. STFail(@"manifest didn't load");
  66. }
  67. NSString* bestFormat = [manifest bestFormatForFile:@"meep.jpg"];
  68. STAssertTrue([bestFormat isEqualToString:ZincFileFormatRaw], @"should be raw");
  69. }
  70. - (void) testBestFormatForFileGZOnly
  71. {
  72. NSString* jsonString =
  73. @"{ \
  74. \"files\": { \
  75. \"meep.jpg\": { \
  76. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  77. \"formats\": { \
  78. \"gz\": { \
  79. \"size\": 3578 \
  80. } \
  81. } \
  82. } \
  83. }, \
  84. \"version\": 1, \
  85. \"bundle\": \"Test\" \
  86. }";
  87. NSError* error = nil;
  88. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  89. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  90. if (dict == nil) {
  91. STFail(@"%@", error);
  92. }
  93. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  94. if (manifest == nil) {
  95. STFail(@"manifest didn't load");
  96. }
  97. NSString* bestFormat = [manifest bestFormatForFile:@"meep.jpg" withPreferredFormats:[NSArray arrayWithObjects:ZincFileFormatGZ, ZincFileFormatRaw, nil]];
  98. STAssertTrue([bestFormat isEqualToString:ZincFileFormatGZ], @"should be gz");
  99. }
  100. - (void) testBestFormatForFileGZAndRaw
  101. {
  102. NSString* jsonString =
  103. @"{ \
  104. \"files\": { \
  105. \"meep.jpg\": { \
  106. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  107. \"formats\": { \
  108. \"raw\": { \
  109. \"size\": 3578 \
  110. }, \
  111. \"gz\": { \
  112. \"size\": 123 \
  113. } \
  114. } \
  115. } \
  116. }, \
  117. \"version\": 1, \
  118. \"bundle\": \"Test\" \
  119. }";
  120. NSError* error = nil;
  121. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  122. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  123. if (dict == nil) {
  124. STFail(@"%@", error);
  125. }
  126. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  127. if (manifest == nil) {
  128. STFail(@"manifest didn't load");
  129. }
  130. NSString* bestFormat = [manifest bestFormatForFile:@"meep.jpg" withPreferredFormats:[NSArray arrayWithObjects:ZincFileFormatGZ, ZincFileFormatRaw, nil]];
  131. STAssertTrue([bestFormat isEqualToString:ZincFileFormatGZ], @"should be gz");
  132. }
  133. - (void) testFileSize
  134. {
  135. NSString* jsonString =
  136. @"{ \
  137. \"files\": { \
  138. \"meep.jpg\": { \
  139. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  140. \"formats\": { \
  141. \"raw\": { \
  142. \"size\": 3578 \
  143. }, \
  144. \"gz\": { \
  145. \"size\": 123 \
  146. } \
  147. } \
  148. } \
  149. }, \
  150. \"version\": 1, \
  151. \"bundle\": \"Test\" \
  152. }";
  153. NSError* error = nil;
  154. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  155. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  156. if (dict == nil) {
  157. STFail(@"%@", error);
  158. }
  159. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  160. if (manifest == nil) {
  161. STFail(@"manifest didn't load");
  162. }
  163. NSUInteger size = [manifest sizeForFile:@"meep.jpg" format:ZincFileFormatGZ];
  164. STAssertTrue(size == 123, @"size is wrong");
  165. }
  166. - (void) testReadGlobalFlavors
  167. {
  168. NSString* jsonString =
  169. @"{ \
  170. \"files\": { \
  171. \"meep.jpg\": { \
  172. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  173. \"formats\": { \
  174. \"raw\": { \
  175. \"size\": 3578 \
  176. }, \
  177. \"gz\": { \
  178. \"size\": 123 \
  179. } \
  180. } \
  181. } \
  182. }, \
  183. \"version\": 1, \
  184. \"bundle\": \"Test\", \
  185. \"flavors\": [\"small\", \"large\"] \
  186. }";
  187. NSError* error = nil;
  188. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  189. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  190. if (dict == nil) {
  191. STFail(@"%@", error);
  192. }
  193. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  194. if (manifest == nil) {
  195. STFail(@"manifest didn't load");
  196. }
  197. NSArray* expectedFlavors = [NSArray arrayWithObjects:@"small", @"large", nil];
  198. STAssertEqualObjects(manifest.flavors, expectedFlavors, @"fail");
  199. }
  200. - (void) testReadFileFlavors
  201. {
  202. NSString* jsonString =
  203. @"{ \
  204. \"files\": { \
  205. \"meep.jpg\": { \
  206. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  207. \"flavors\": [\"small\"], \
  208. \"formats\": { \
  209. \"raw\": { \
  210. \"size\": 3578 \
  211. }, \
  212. \"gz\": { \
  213. \"size\": 123 \
  214. } \
  215. } \
  216. } \
  217. }, \
  218. \"version\": 1, \
  219. \"bundle\": \"Test\", \
  220. \"flavors\": [\"small\", \"large\"] \
  221. }";
  222. NSError* error = nil;
  223. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  224. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  225. if (dict == nil) {
  226. STFail(@"%@", error);
  227. }
  228. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  229. if (manifest == nil) {
  230. STFail(@"manifest didn't load");
  231. }
  232. NSArray* expectedFlavors = [NSArray arrayWithObjects:@"small", nil];
  233. STAssertEqualObjects([manifest flavorsForFile:@"meep.jpg"], expectedFlavors, @"fail");
  234. }
  235. - (void) testFilesForFlavor
  236. {
  237. NSString* jsonString =
  238. @"{ \
  239. \"files\": { \
  240. \"meep.jpg\": { \
  241. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f888\", \
  242. \"flavors\": [\"small\"], \
  243. \"formats\": { \
  244. \"raw\": { \
  245. \"size\": 3578 \
  246. }, \
  247. \"gz\": { \
  248. \"size\": 123 \
  249. } \
  250. } \
  251. }, \
  252. \"meep2.jpg\": { \
  253. \"sha\": \"e9185889564c9af0968ee60a7d7771dcfc19f889\", \
  254. \"flavors\": [], \
  255. \"formats\": { \
  256. \"raw\": { \
  257. \"size\": 3578 \
  258. }, \
  259. \"gz\": { \
  260. \"size\": 123 \
  261. } \
  262. } \
  263. } \
  264. }, \
  265. \"version\": 1, \
  266. \"bundle\": \"Test\", \
  267. \"flavors\": [\"small\", \"large\"] \
  268. }";
  269. NSError* error = nil;
  270. NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  271. NSDictionary* dict = [ZincJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  272. if (dict == nil) {
  273. STFail(@"%@", error);
  274. }
  275. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:dict] autorelease];
  276. if (manifest == nil) {
  277. STFail(@"manifest didn't load");
  278. }
  279. NSArray* smallFiles = [manifest filesForFlavor:@"small"];
  280. STAssertTrue([smallFiles count]==1, @"2 small files");
  281. NSArray* largeFiles = [manifest filesForFlavor:@"large"];
  282. STAssertTrue([largeFiles count]==0, @"0 large files");
  283. NSArray* allFiles = [manifest filesForFlavor:nil];
  284. STAssertTrue([allFiles count]==2, @"2 files");
  285. }
  286. - (ZincManifest*) _manifestForDictionaryRepresentationTest
  287. {
  288. ZincManifest* manifest = [[[ZincManifest alloc] init] autorelease];
  289. manifest.catalogID = @"com.mindsnacks.food";
  290. manifest.bundleName = @"pork";
  291. manifest.version = 5;
  292. return manifest;
  293. }
  294. - (void) testDictionaryRepresentation_bundleName
  295. {
  296. ZincManifest* manifest = [self _manifestForDictionaryRepresentationTest];
  297. NSDictionary* dict = [manifest dictionaryRepresentation];
  298. STAssertEqualObjects([dict objectForKey:@"bundle"], manifest.bundleName, @"bundle name doesn't match");
  299. }
  300. - (void) testDictionaryRepresentation_catalogID
  301. {
  302. ZincManifest* manifest = [self _manifestForDictionaryRepresentationTest];
  303. NSDictionary* dict = [manifest dictionaryRepresentation];
  304. STAssertEqualObjects([dict objectForKey:@"catalog"], manifest.catalogID, @"catalog id doesn't match");
  305. }
  306. - (void) testDictionaryRepresentation_version
  307. {
  308. ZincManifest* manifest = [self _manifestForDictionaryRepresentationTest];
  309. NSDictionary* dict = [manifest dictionaryRepresentation];
  310. STAssertEquals((ZincVersion)[[dict objectForKey:@"version"] integerValue], manifest.version, @"version doesn't match");
  311. }
  312. - (void) testDictionaryRepresentation_flavors_nil
  313. {
  314. ZincManifest* manifest = [self _manifestForDictionaryRepresentationTest];
  315. NSDictionary* dict = [manifest dictionaryRepresentation];
  316. STAssertEquals((id)[dict objectForKey:@"flavors"], manifest.flavors, @"flavors don't match");
  317. }
  318. - (void) testDictionaryRepresentation_flavors_notNil
  319. {
  320. ZincManifest* manifest = [self _manifestForDictionaryRepresentationTest];
  321. manifest.flavors = @[@"chop", @"bacon"];
  322. NSDictionary* dict = [manifest dictionaryRepresentation];
  323. STAssertEquals((id)[dict objectForKey:@"flavors"], manifest.flavors, @"flavors don't match");
  324. }
  325. - (void) testRebuildFlavorsFromFiles
  326. {
  327. NSDictionary* manifestDict = @{
  328. @"catalog": @"com.mindsnacks.food",
  329. @"bundle" : @"pork",
  330. @"version": @5,
  331. @"files":
  332. @{ @"1.png":
  333. @{ @"flavors": @[@"pork"]
  334. }
  335. }
  336. };
  337. ZincManifest* manifest = [[[ZincManifest alloc] initWithDictionary:manifestDict] autorelease];
  338. STAssertEqualObjects(manifest.flavors, @[@"pork"], @"should have built flavors from files");
  339. }
  340. @end