|
| 1 | +// |
| 2 | +// CCFileUtilsDatabaseTests.m |
| 3 | +// cocos2d-tests |
| 4 | +// |
| 5 | +// Created by Nicky Weber on 13.01.15. |
| 6 | +// Copyright (c) 2015 Cocos2d. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import "CCFileUtilsDatabase.h" |
| 11 | +#import "FileSystemTestCase.h" |
| 12 | +#import "CCUnitTestHelperMacros.h" |
| 13 | +#import "CCFileMetaData.h" |
| 14 | + |
| 15 | +@interface CCFileUtilsDatabaseTests : FileSystemTestCase |
| 16 | + |
| 17 | +@property (nonatomic, strong) CCFileUtilsDatabase *fileUtilsDB; |
| 18 | +@property (nonatomic, copy) NSString *searchPath; |
| 19 | +@property (nonatomic, copy) NSString *dbPath; |
| 20 | + |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation CCFileUtilsDatabaseTests |
| 24 | + |
| 25 | +- (void)setUp |
| 26 | +{ |
| 27 | + [super setUp]; |
| 28 | + |
| 29 | + NSString *json = MULTILINESTRING( |
| 30 | + { |
| 31 | + "version" : 1, |
| 32 | + "data" : { |
| 33 | + "images/foo.png" : { |
| 34 | + "UIScale" : true, |
| 35 | + "filename" : "images/foo.jpg", |
| 36 | + "localizations" : { |
| 37 | + "en" : "images/foo-en.jpg" |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + ); |
| 43 | + |
| 44 | + self.searchPath = [self fullPathForFile:@"Resources"]; |
| 45 | + self.dbPath = @"config/filedb.json"; |
| 46 | + |
| 47 | + [self createFilesWithContents:@{@"Resources/config/filedb.json" : [json dataUsingEncoding:NSUTF8StringEncoding]}]; |
| 48 | + |
| 49 | + self.fileUtilsDB = [[CCFileUtilsDatabase alloc] init]; |
| 50 | +} |
| 51 | + |
| 52 | +- (void)testAddDatabaseWithFilePathInSearchPath |
| 53 | +{ |
| 54 | + NSError *error; |
| 55 | + XCTAssertTrue([_fileUtilsDB addJSONWithFilePath:_dbPath forSearchPath:_searchPath error:&error]); |
| 56 | + |
| 57 | + CCFileMetaData *metaData = [_fileUtilsDB metaDataForFileNamed:@"images/foo.png" inSearchPath:_searchPath]; |
| 58 | + |
| 59 | + XCTAssertNotNil(metaData); |
| 60 | + XCTAssertNil(error); |
| 61 | + XCTAssertEqualObjects(metaData.filename, @"images/foo.jpg"); |
| 62 | + XCTAssertEqualObjects(metaData.localizations, @{@"en" : @"images/foo-en.jpg"}); |
| 63 | +}; |
| 64 | + |
| 65 | +- (void)testRemoveDatabaseWithFilePathInSearchPath |
| 66 | +{ |
| 67 | + XCTAssertTrue([_fileUtilsDB addJSONWithFilePath:_dbPath forSearchPath:_searchPath error:NULL]); |
| 68 | + |
| 69 | + [_fileUtilsDB removeEntriesForSearchPath:[self fullPathForFile:@"Resources"]]; |
| 70 | + |
| 71 | + CCFileMetaData *metaData = [_fileUtilsDB metaDataForFileNamed:@"images/foo.png" inSearchPath:[self fullPathForFile:@"Resources"]]; |
| 72 | + |
| 73 | + XCTAssertNil(metaData); |
| 74 | +} |
| 75 | + |
| 76 | +- (void)testAddDataBaseWithCorruptJSON |
| 77 | +{ |
| 78 | + NSString *json = MULTILINESTRING( |
| 79 | + { |
| 80 | + [] asdasdasd {} |
| 81 | + }; |
| 82 | + ); |
| 83 | + |
| 84 | + [self createFilesWithContents:@{@"Resources/config/filedb.json" : [json dataUsingEncoding:NSUTF8StringEncoding]}]; |
| 85 | + |
| 86 | + NSError *error; |
| 87 | + XCTAssertFalse([_fileUtilsDB addJSONWithFilePath:_dbPath forSearchPath:_searchPath error:&error]); |
| 88 | + XCTAssertNotNil(error); |
| 89 | +} |
| 90 | + |
| 91 | +- (void)testAddDataBaseWithNonExistingDatabaseFile |
| 92 | +{ |
| 93 | + NSError *error; |
| 94 | + XCTAssertFalse([_fileUtilsDB addJSONWithFilePath:@"/adasdasd/ddddd.json" forSearchPath:_searchPath error:&error]); |
| 95 | + XCTAssertNotNil(error); |
| 96 | +} |
| 97 | + |
| 98 | +@end |
0 commit comments