Skip to content

Commit

Permalink
Attempt to write a regression test for issue #87
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Dec 10, 2014
1 parent c53a21a commit fc0a62a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/GRMustache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@
56DEC19C15262FC80031E8DC /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0600;
LastUpgradeCheck = 0610;
};
buildConfigurationList = 56DEC19F15262FC80031E8DC /* Build configuration list for PBXProject "GRMustache" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -1823,7 +1823,6 @@
56DEC265152631040031E8DC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
DSTROOT = "/tmp/$(TARGET_NAME).dst";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand All @@ -1834,7 +1833,6 @@
56DEC266152631040031E8DC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
DSTROOT = "/tmp/$(TARGET_NAME).dst";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand All @@ -1846,7 +1844,6 @@
56DEC268152631040031E8DC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -1863,7 +1860,6 @@
56DEC269152631040031E8DC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,95 @@ - (void)testTemplateRepositoryWithBundle_templateExtension_encoding
}
}

- (void)testTemplateRepositoryWithBundleLocalization
{
// Check that our custom bundles works as expected:
// - NSLocalizedString
// - resources files
// - localized resource files
// - resources directories
// - localized resources directories
NSDictionary *resources = @{ @"nonLocalized.txt": @"nonLocalized",
@"nonLocalizedResources/a.txt": @"nonLocalized a",
@"en.lproj/localized.txt": @"localized",
@"en.lproj/localizedResources/a.txt": @"localized a",
@"en.lproj/Localizable.strings": @"\"key\"=\"value\";" };
[self runTestsWithBundleResources:resources usingBlock:^(NSBundle *bundle) {
NSString *localizedString = [bundle localizedStringForKey:@"key" value:@"" table:nil];
XCTAssertEqualObjects(localizedString, @"value");

NSString *nonLocalizedResourcePath = [bundle pathForResource:@"nonLocalized" ofType:@"txt"];
NSString *nonLocalizedResourceContent = [NSString stringWithContentsOfFile:nonLocalizedResourcePath encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(nonLocalizedResourceContent, @"nonLocalized");

NSURL *nonLocalizedResourceURL = [bundle URLForResource:@"nonLocalized" withExtension:@"txt"];
nonLocalizedResourceContent = [NSString stringWithContentsOfURL:nonLocalizedResourceURL encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(nonLocalizedResourceContent, @"nonLocalized");

nonLocalizedResourceURL = [[bundle URLForResource:@"nonLocalizedResources" withExtension:nil] URLByAppendingPathComponent:@"a.txt"];
nonLocalizedResourceContent = [NSString stringWithContentsOfURL:nonLocalizedResourceURL encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(nonLocalizedResourceContent, @"nonLocalized a");

nonLocalizedResourceURL = [bundle URLForResource:@"a" withExtension:@"txt" subdirectory:@"nonLocalizedResources"];
nonLocalizedResourceContent = [NSString stringWithContentsOfURL:nonLocalizedResourceURL encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(nonLocalizedResourceContent, @"nonLocalized a");

NSURL *localizedResourceURL = [bundle URLForResource:@"localized" withExtension:@"txt"];
NSString *localizedResourceContent = [NSString stringWithContentsOfURL:localizedResourceURL encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(localizedResourceContent, @"localized");

localizedResourceURL = [[bundle URLForResource:@"localizedResources" withExtension:nil] URLByAppendingPathComponent:@"a.txt"];
localizedResourceContent = [NSString stringWithContentsOfURL:localizedResourceURL encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(localizedResourceContent, @"localized a");

localizedResourceURL = [bundle URLForResource:@"a" withExtension:@"txt" subdirectory:@"localizedResources"];
localizedResourceContent = [NSString stringWithContentsOfURL:localizedResourceURL encoding:NSUTF8StringEncoding error:NULL];
XCTAssertEqualObjects(localizedResourceContent, @"localized a");
}];


// Regression test for https://github.com/groue/GRMustache/issues/87

resources = @{ @"en.lproj/Main.mustache": @"{{> Footer }}",
@"en.lproj/Footer.mustache": @"Footer" };
[self runTestsWithBundleResources:resources usingBlock:^(NSBundle *bundle) {
GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:@"Main" bundle:bundle error:NULL];
NSString *rendering = [template renderObject:nil error:NULL];
XCTAssertEqualObjects(rendering, @"Footer");
}];


// Non-localized siblings

resources = @{ @"a.mustache": @"{{>b}}",
@"b.mustache": @"b" };
[self runTestsWithBundleResources:resources usingBlock:^(NSBundle *bundle) {
GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:@"a" bundle:bundle error:NULL];
NSString *rendering = [template renderObject:nil error:NULL];
XCTAssertEqualObjects(rendering, @"b");
}];
}

- (void)runTestsWithBundleResources:(NSDictionary *)resources usingBlock:(void(^)(NSBundle *bundle))block
{
static NSUInteger count = 0; // counter makes sure we do never generate two bundles at the same path, and suffer from NSBundle cache.
NSFileManager *fm = [NSFileManager defaultManager];
NSString *bundlePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"GRMustacheTest_%lu", (unsigned long)count++]];
[fm removeItemAtPath:bundlePath error:nil];

for (NSString *name in resources) {
NSString *string = [resources objectForKey:name];
NSString *path = [bundlePath stringByAppendingPathComponent:name];
[fm createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL];
[fm createFileAtPath:path contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
}

NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
block(bundle);

// clean up

[fm removeItemAtPath:bundlePath error:NULL];
}

@end

0 comments on commit fc0a62a

Please sign in to comment.