Skip to content

Commit

Permalink
Fix test environment to run old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Jul 31, 2015
1 parent 9b24d42 commit 22943d3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 103 deletions.
26 changes: 1 addition & 25 deletions Swifternalization.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
6D5004541B3EF91600A54B36 /* Swifternalization.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D5004481B3EF91600A54B36 /* Swifternalization.framework */; };
6D50045B1B3EF91600A54B36 /* SwifternalizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D50045A1B3EF91600A54B36 /* SwifternalizationTests.swift */; };
6D5004661B3EF92600A54B36 /* Swifternalization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5004641B3EF92600A54B36 /* Swifternalization.swift */; };
6D5004921B3EFF6D00A54B36 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6D5004941B3EFF6D00A54B36 /* Localizable.strings */; };
6D5004961B3EFFC100A54B36 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 6D5004591B3EF91600A54B36 /* Info.plist */; };
6D5BA5F01B651796000D7E49 /* TranslationsLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5EF1B651796000D7E49 /* TranslationsLoaderTests.swift */; };
6D5BA5F11B6517A6000D7E49 /* TranslationsLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC731B5EBDA600A1220F /* TranslationsLoader.swift */; };
Expand Down Expand Up @@ -78,7 +77,6 @@
6DB3CC901B5EC29600A1220F /* ExpressionPatternType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC8F1B5EC29600A1220F /* ExpressionPatternType.swift */; };
6DB3CC911B5EC29E00A1220F /* ExpressionPatternType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC8F1B5EC29600A1220F /* ExpressionPatternType.swift */; };
6DBB6C521B401B8A002F39A3 /* Swifternalization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5004641B3EF92600A54B36 /* Swifternalization.swift */; };
6DBB6C591B4026B8002F39A3 /* Expressions.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6DBB6C5B1B4026B8002F39A3 /* Expressions.strings */; };
6DBB6C691B4040F0002F39A3 /* InternalPattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C681B4040F0002F39A3 /* InternalPattern.swift */; };
6DBB6C6A1B40412D002F39A3 /* InternalPattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C681B4040F0002F39A3 /* InternalPattern.swift */; };
6DBB6C871B40718F002F39A3 /* Expressions.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6DBB6C891B40718F002F39A3 /* Expressions.strings */; };
Expand Down Expand Up @@ -121,7 +119,6 @@
6D5004591B3EF91600A54B36 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6D50045A1B3EF91600A54B36 /* SwifternalizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwifternalizationTests.swift; sourceTree = "<group>"; };
6D5004641B3EF92600A54B36 /* Swifternalization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Swifternalization.swift; sourceTree = "<group>"; };
6D5004931B3EFF6D00A54B36 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
6D5BA5EF1B651796000D7E49 /* TranslationsLoaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationsLoaderTests.swift; sourceTree = "<group>"; };
6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpressionsProcessor.swift; sourceTree = "<group>"; };
6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SharedExpression.swift; path = ../SharedExpression.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -163,7 +160,6 @@
6DB3CC6A1B5EBDA600A1220F /* LengthVariation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LengthVariation.swift; sourceTree = "<group>"; };
6DB3CC731B5EBDA600A1220F /* TranslationsLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationsLoader.swift; sourceTree = "<group>"; };
6DB3CC8F1B5EC29600A1220F /* ExpressionPatternType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionPatternType.swift; sourceTree = "<group>"; };
6DBB6C5A1B4026B8002F39A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Expressions.strings; sourceTree = "<group>"; };
6DBB6C681B4040F0002F39A3 /* InternalPattern.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InternalPattern.swift; sourceTree = "<group>"; };
6DBB6C881B40718F002F39A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Expressions.strings; sourceTree = "<group>"; };
6DBB6C8A1B407190002F39A3 /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/Expressions.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -283,8 +279,6 @@
6D6282931B3F052B00E65FCD /* TranslatablePairTests.swift */,
6D140F451B56D04300359143 /* Helpers */,
6D5004581B3EF91600A54B36 /* Supporting Files */,
6D5004941B3EFF6D00A54B36 /* Localizable.strings */,
6DBB6C5B1B4026B8002F39A3 /* Expressions.strings */,
6DD3B93D1B5ED35600C79EAC /* Localizable Files */,
);
path = SwifternalizationTests;
Expand Down Expand Up @@ -360,8 +354,8 @@
6DD3B93D1B5ED35600C79EAC /* Localizable Files */ = {
isa = PBXGroup;
children = (
6DD3B9371B5ED35200C79EAC /* base.json */,
6DD3B9381B5ED35200C79EAC /* expressions.json */,
6DD3B9371B5ED35200C79EAC /* base.json */,
6DD3B9391B5ED35200C79EAC /* pl.json */,
);
name = "Localizable Files";
Expand Down Expand Up @@ -506,8 +500,6 @@
6DD3B93A1B5ED35200C79EAC /* base.json in Resources */,
6D5004961B3EFFC100A54B36 /* Info.plist in Resources */,
6DD3B93C1B5ED35200C79EAC /* pl.json in Resources */,
6DBB6C591B4026B8002F39A3 /* Expressions.strings in Resources */,
6D5004921B3EFF6D00A54B36 /* Localizable.strings in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -634,14 +626,6 @@
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
6D5004941B3EFF6D00A54B36 /* Localizable.strings */ = {
isa = PBXVariantGroup;
children = (
6D5004931B3EFF6D00A54B36 /* Base */,
);
name = Localizable.strings;
sourceTree = "<group>";
};
6D6283491B3F622A00E65FCD /* Localizable.strings */ = {
isa = PBXVariantGroup;
children = (
Expand All @@ -651,14 +635,6 @@
name = Localizable.strings;
sourceTree = "<group>";
};
6DBB6C5B1B4026B8002F39A3 /* Expressions.strings */ = {
isa = PBXVariantGroup;
children = (
6DBB6C5A1B4026B8002F39A3 /* Base */,
);
name = Expressions.strings;
sourceTree = "<group>";
};
6DBB6C891B40718F002F39A3 /* Expressions.strings */ = {
isa = PBXVariantGroup;
children = (
Expand Down
8 changes: 4 additions & 4 deletions Swifternalization/JSONFileLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class JSONFileLoader {
:param: bundle A bundle when file is located.
:returns: Returns json of file or nil if cannot load a file.
*/
class func loadTranslations(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> JSONDictionary? {
return self.load(countryCode, bundle: bundle)
class func loadTranslations(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> JSONDictionary {
return self.load(countryCode, bundle: bundle) ?? [:]
}

/**
Expand All @@ -28,8 +28,8 @@ final class JSONFileLoader {
:param: bundle A bundle when file is located.
:returns: dictionary with expressions or nil.
*/
class func loadExpressions(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> Dictionary<String, String>? {
return self.load("expressions", bundle: bundle)?[countryCode] as? Dictionary<String, String>
class func loadExpressions(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> Dictionary<String, String> {
return self.load("expressions", bundle: bundle)?[countryCode] as? Dictionary<String, String> ?? [:]
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/LengthVariation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Foundation
Length variation representation.
*/
struct LengthVariation {
/// Length - width of a screen.
let length: Int
/// width of a screen.
let width: Int

/// localized string.
let value: String
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/LoadedTranslationsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class LoadedTranslationsProcessor {
// Translation contains length expressions like @100, @200, etc.
var lengthVariations = [LengthVariation]()
for (key, value) in $0.content as! Dictionary<String, String> {
lengthVariations.append(LengthVariation(length: self.parseNumberFromLengthVariation(key), value: value))
lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(key), value: value))
}
return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, localizedValue: lengthVariations.last!.value, lengthVariations: lengthVariations)])

Expand All @@ -85,7 +85,7 @@ class LoadedTranslationsProcessor {
if value is Dictionary<String, String> {
var lengthVariations = [LengthVariation]()
for (lvKey, lvValue) in value as! Dictionary<String, String> {
lengthVariations.append(LengthVariation(length: self.parseNumberFromLengthVariation(lvKey), value: lvValue))
lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(lvKey), value: lvValue))
}
expressions.append(Expression(pattern: pattern, localizedValue: lengthVariations.last!.value, lengthVariations: lengthVariations))
} else if value is String {
Expand Down
21 changes: 16 additions & 5 deletions Swifternalization/Swifternalization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ public class Swifternalization {
} else if matchingExpression.lengthVariations.count == 0 {
return matchingExpression.localizedValue
} else if matchingExpression.lengthVariations.count > 0 {
/// GET PROPER VARIANT
let sortedVariations = matchingExpression.lengthVariations.sorted({$0.width < $1.width})

var selectedValue = matchingExpression.localizedValue
for variation in sortedVariations {
if variation.width <= fittingWidth {
selectedValue = variation.value
} else {
break
}
}

return selectedValue
}
}
}
Expand All @@ -53,12 +64,12 @@ public class Swifternalization {
let base = "base"
let language = getPreferredLanguage(bundle)

let baseExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(base, bundle: bundle) ?? [:])
let languageExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(language, bundle: bundle) ?? [:])
let baseExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(base, bundle: bundle))
let languageExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(language, bundle: bundle))
let expressions = SharedExpressionsProcessor.processSharedExpression(language, preferedLanguageExpressions: languageExpressions, baseLanguageExpressions: baseExpressions)

let baseTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(base, bundle: bundle) ?? [:])
let languageTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(language, bundle: bundle) ?? [:])
let baseTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(base, bundle: bundle))
let languageTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(language, bundle: bundle))

translations = LoadedTranslationsProcessor.processTranslations(baseTranslations, preferedLanguageTranslations: languageTranslations, sharedExpressions: expressions)
}
Expand Down
12 changes: 0 additions & 12 deletions SwifternalizationTests/Base.lproj/Expressions.strings

This file was deleted.

21 changes: 0 additions & 21 deletions SwifternalizationTests/Base.lproj/Localizable.strings

This file was deleted.

16 changes: 8 additions & 8 deletions SwifternalizationTests/JSONFileLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@ import XCTest
class JSONFileLoaderTests: XCTestCase {

// Expressions
func loadExpressions(cc: CountryCode) -> Dictionary<String, String>? {
func loadExpressions(cc: CountryCode) -> Dictionary<String, String> {
return JSONFileLoader.loadExpressions(cc, bundle: NSBundle.testBundle())
}

func testShouldLoadBaseExpressions() {
XCTAssertNotNil(loadExpressions("base"), "")
XCTAssertFalse(loadExpressions("base").isEmpty, "")
}

func testShouldLoadPLExpressions() {
XCTAssertNotNil(loadExpressions("pl"), "")
XCTAssertFalse(loadExpressions("pl").isEmpty, "")
}

func testShouldNotLoadDEExpressions() {
XCTAssertNil(loadExpressions("de"), "")
XCTAssertTrue(loadExpressions("de").isEmpty, "")
}

// Translations
func loadTranslations(cc: CountryCode) -> JSONDictionary? {
func loadTranslations(cc: CountryCode) -> JSONDictionary {
return JSONFileLoader.loadTranslations(cc, bundle: NSBundle.testBundle())
}

func testShouldLoadBaseTranslations() {
XCTAssertNotNil(loadTranslations("base"), "")
XCTAssertFalse(loadTranslations("base").isEmpty, "")
}

func testShouldLoadPLTranslations() {
XCTAssertNotNil(loadTranslations("pl"), "")
XCTAssertFalse(loadTranslations("pl").isEmpty, "")
}

func testShouldNotLoadDETranslations() {
XCTAssertNil(loadTranslations("de"), "")
XCTAssertTrue(loadTranslations("de").isEmpty, "")
}
}
42 changes: 21 additions & 21 deletions SwifternalizationTests/base.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"welcome": "welcome",
"welcome-key": "welcome",

"only-in-base": "this is from base",

"cars": {
"one": "1 car",
"ie:x=2": "2 cars",
"more": "%d cars"
"one": "one car",
"ie:x>=2": "%d cars",
"ie:x<=-2": "minus %d cars"
},

"forgot-password": {
"@100": "Forgot Password? Help.",
"@200": "Forgot Password? Get password Help.",
"@300": "Forgotten Your Password? Get password Help."
"pl-cars": {
"one": "jeden samochód",
"iex:2<=x<=4": "%d samochody",
"iex:-4<=x<=-2": "-2 - -4 samochody"
},

"car-sentence": {
"one": {
"@100": "one car",
"@200": "just one car",
"@300": "you've got just one car"
},

"more": {
"@100": "%d cars",
"@300": "you've got %d cars"
}

"pl-police-cars": {
"ie:x=1": "1 samochód policyjny",
"exp:(((?!1).[2-4]{1})$)|(^[2-4]$)": "%d samochody policyjne",
"custom-pl-few": "%d samochodów policyjnych"
},

"things": {
"ten": "10 things",
">20": ">20 things"
}
}
6 changes: 3 additions & 3 deletions SwifternalizationTests/expressions.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"base": {
"one": "ie:x=1",
"two": "ie:x=2",
"more": "exp:(^[^1])|(^\\d{2,})"
"ten": "ie:x=10",
">20": "ie:x>20",
"custom-pl-few": "exp:(.*(?=1).[0-9]$)|(^[05-9]$)|(.*(?!1).[0156789])"
},

"pl": {
Expand Down

0 comments on commit 22943d3

Please sign in to comment.