Skip to content

Commit

Permalink
Swift 3 Xcode 8 Beta 6.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCoordinator authored and tomkowz committed Oct 21, 2016
1 parent 26d0787 commit 7576259
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 70 deletions.
4 changes: 4 additions & 0 deletions Swifternalization.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,14 @@
TargetAttributes = {
6D5004471B3EF91600A54B36 = {
CreatedOnToolsVersion = 6.3.2;
DevelopmentTeam = NPGLBT5EYF;
DevelopmentTeamName = "Snupps Limited";
LastSwiftMigration = 0800;
};
6D5004521B3EF91600A54B36 = {
CreatedOnToolsVersion = 6.3.2;
DevelopmentTeam = NPGLBT5EYF;
DevelopmentTeamName = "Snupps Limited";
LastSwiftMigration = 0800;
};
};
Expand Down
9 changes: 5 additions & 4 deletions Swifternalization/JSONFileLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
/**
Represents json content.
*/
internal typealias JSONDictionary = Dictionary<String, AnyObject>
internal typealias JSONDictionary = [String : Any]

/**
Simple JSON file loader.
Expand All @@ -28,8 +28,8 @@ final class JSONFileLoader {
:param: bundle A bundle when file is located.
:returns: dictionary with expressions or empty dictionary if cannot load a file.
*/
class func loadExpressions(_ countryCode: CountryCode, bundle: Bundle) -> Dictionary<String, String> {
return self.load("expressions", bundle: bundle)?[countryCode] as? Dictionary<String, String> ?? [:]
class func loadExpressions(_ countryCode: CountryCode, bundle: Bundle) -> [String : String] {
return self.load("expressions", bundle: bundle)?[countryCode] as? [String : String] ?? [:]
}

/**
Expand All @@ -41,9 +41,10 @@ final class JSONFileLoader {
:returns: JSON or nil if file cannot be loaded.
*/
private class func load(_ fileName: String, bundle: Bundle) -> JSONDictionary? {
if let fileURL = bundle.urlForResource(fileName, withExtension: "json") {
if let fileURL = bundle.url(forResource: fileName, withExtension: "json") {
return load(fileURL)
}

print("Cannot find file \(fileName).json.")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/LoadedTranslation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ struct LoadedTranslation {
/**
A content of translation just loaded from a file user in future processing.
*/
let content: Dictionary<String, AnyObject>
let content: [String : Any]
}
10 changes: 5 additions & 5 deletions Swifternalization/LoadedTranslationsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LoadedTranslationsProcessor {
if there is a key it is replaced with real expression pattern.
*/
var expressions = [Expression]()
for (key, value) in $0.content as! Dictionary<String, String> {
for (key, value) in $0.content as! [String : String] {
let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key
expressions.append(Expression(pattern: pattern, value: value))
}
Expand All @@ -74,7 +74,7 @@ class LoadedTranslationsProcessor {
case .withLengthVariations:
// Translation contains length expressions like @100, @200, etc.
var lengthVariations = [LengthVariation]()
for (key, value) in $0.content as! Dictionary<String, String> {
for (key, value) in $0.content as! [String : String] {
lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(key), value: value))
}
return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, value: lengthVariations.last!.value, lengthVariations: lengthVariations)])
Expand All @@ -91,9 +91,9 @@ class LoadedTranslationsProcessor {
var expressions = [Expression]()
for (key, value) in $0.content {
let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key
if value is Dictionary<String, String> {
if value is [String : String] {
var lengthVariations = [LengthVariation]()
for (lvKey, lvValue) in value as! Dictionary<String, String> {
for (lvKey, lvValue) in value as! [String : String] {
lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(lvKey), value: lvValue))
}
expressions.append(Expression(pattern: pattern, value: lengthVariations.last!.value, lengthVariations: lengthVariations))
Expand All @@ -112,7 +112,7 @@ class LoadedTranslationsProcessor {
:param: string A string that contains length variation string like @100.
:returns: A number parsed from the string.
*/
private class func parseNumberFromLengthVariation(_ string: String) -> Int {
fileprivate class func parseNumberFromLengthVariation(_ string: String) -> Int {
return (Regex.matchInString(string, pattern: "@(\\d+)", capturingGroupIdx: 1)! as NSString).integerValue
}
}
10 changes: 5 additions & 5 deletions Swifternalization/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ final class Regex {
var resultString: String?

let range = NSMakeRange(0, str.characters.distance(from: str.startIndex, to: str.endIndex))
regexp(pattern)?.enumerateMatches(in: str, options: RegularExpression.MatchingOptions.reportCompletion, range: range, using: { result, flags, stop in
regexp(pattern)?.enumerateMatches(in: str, options: NSRegularExpression.MatchingOptions.reportCompletion, range: range, using: { result, flags, stop in
if let result = result {
if let capturingGroupIdx = capturingGroupIdx where result.numberOfRanges > capturingGroupIdx {
resultString = self.substring(str, range: result.range(at: capturingGroupIdx))
if let capturingGroupIdx = capturingGroupIdx, result.numberOfRanges > capturingGroupIdx {
resultString = self.substring(str, range: result.rangeAt(capturingGroupIdx))
} else {
resultString = self.substring(str, range: result.range)
}
Expand Down Expand Up @@ -76,9 +76,9 @@ final class Regex {
:param: pattern A regexp pattern.
:returns: `NSRegularExpression` object or nil if it cannot be created.
*/
private class func regexp(_ pattern: String) -> RegularExpression? {
private class func regexp(_ pattern: String) -> NSRegularExpression? {
do {
return try RegularExpression(pattern: pattern, options: RegularExpression.Options.caseInsensitive)
return try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
} catch let error as NSError {
print(error)
}
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/RegexExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class RegexExpressionParser: ExpressionParser {
:returns: `RegexPattern` or nil when there is no regular expression
in the pattern.
*/
private func regexPattern() -> RegexPattern? {
fileprivate func regexPattern() -> RegexPattern? {
if let regex = Regex.firstMatchInString(pattern, pattern: "(?<=^\(ExpressionPatternType.Regex.rawValue):).*") {
return regex
} else {
print("Cannot find any regular expression, pattern: \(pattern)")
return nil
}
}
}
}
2 changes: 1 addition & 1 deletion Swifternalization/SharedExpressionLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class SharedExpressionsLoader {
:param: countryCode A country code
:returns: array of loaded expressions.
*/
class func loadExpressions(_ json: Dictionary<String, String>) -> [SharedExpression] {
class func loadExpressions(_ json: [String : String]) -> [SharedExpression] {
var expressions = [SharedExpression]()
for (identifier, pattern) in json {
expressions.append(SharedExpression(identifier: identifier, pattern: pattern))
Expand Down
6 changes: 5 additions & 1 deletion Swifternalization/SharedExpressionsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ class SharedExpressionsProcessor {
}
}

infix operator <! {}
precedencegroup SetPrecedence {
higherThan: DefaultPrecedence
}

infix operator <! : SetPrecedence
/**
"Get Unique" operator. It helps in getting unique shared expressions from two arrays.
Content of `lhs` array will be checked in terms on uniqueness. The operator does
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/Translation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Translation {
If variation width is shorter or equal `fittingWidth`
take associated value.
*/
for variation in expression.lengthVariations.sorted(isOrderedBefore: {$0.width < $1.width}) {
for variation in expression.lengthVariations.sorted(by: {$0.width < $1.width}) {
if variation.width <= fittingWidth! {
localizedValue = variation.value
}
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/TranslationsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class TranslationsLoader {
translations.
:returns: Array of `LoadedTranslation` objects from specified file.
*/
class func loadTranslations(_ json: Dictionary<String, AnyObject>) -> [LoadedTranslation] {
class func loadTranslations(_ json: [String : Any]) -> [LoadedTranslation] {
var loadedTranslations = [LoadedTranslation]()
for (key, value) in json {
if value is String {
Expand Down Expand Up @@ -45,7 +45,7 @@ final class TranslationsLoader {
for (_, value) in element {
if value is String {
strings += 1
} else if value is Dictionary<String, AnyObject> {
} else if value is [String : Any] {
dicts += 1
}
}
Expand Down
6 changes: 3 additions & 3 deletions SwifternalizationTests/ExpressionJSONs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import Foundation

class ExpressionJSONs {

class func base() -> Dictionary<String, String> {
class func base() -> [String : String] {
return [
"key1": "val1",
"key2": "val2",
"key3": "val3"
]
}

class func en() -> Dictionary<String, String> {
class func en() -> [String : String] {
return [
"key3": "val3",
"key4": "val4",
"key5": "val5"
]
}
}
}
26 changes: 13 additions & 13 deletions SwifternalizationTests/InequalityExpressionMatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,43 @@ class InequalityExpressionMatcherTests: XCTestCase {
let m = createMatcher("ie:x=3")
XCTAssertTrue(m.validate("3"), "")
XCTAssertTrue(m.validate("3.0"), "")
for n in Float.randomNumbersStrings(lower: -100.33, upper: 100.99, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( -100.33, upper: 100.99, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIE2() {
let m = createMatcher("ie:x<=3")
for n in Float.randomNumbersStrings(lower: -9999, upper: 3.0, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 3.00001, upper: 1000000, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( -9999, upper: 3.0, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings( 3.00001, upper: 1000000, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIE3() {
let m = createMatcher("ie:x>=3")
for n in Float.randomNumbersStrings(lower: 3.0, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -999999, upper: 2.9999999, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( 3.0, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings( -999999, upper: 2.9999999, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIE4() {
let m = createMatcher("ie:x>3")
for n in Float.randomNumbersStrings(lower: 3.0001, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -999999, upper: 3.000000, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( 3.0001, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings( -999999, upper: 3.000000, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIE5() {
let m = createMatcher("ie:x<3")
for n in Float.randomNumbersStrings(lower: -99999, upper: 2.9999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 3.0000, upper: 999999, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( -99999, upper: 2.9999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings( 3.0000, upper: 999999, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

// MARK: Float
func testIE6() {
let m = createMatcher("ie:x<=4.3")
for n in Float.randomNumbersStrings(lower: -99999, upper: 4.3, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 4.31, upper: 10000000, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( -99999, upper: 4.3, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings( 4.31, upper: 10000000, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIE7() {
let m = createMatcher("ie:x>-199")
for n in Float.randomNumbersStrings(lower: -198, upper: 99999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -99999, upper: -199, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings( -198, upper: 99999, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings( -99999, upper: -199, count: steps) { XCTAssertFalse(m.validate(n), "") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ class InequalityExtendedExpressionMatcherTests: XCTestCase {
// MARK: - Int
func testIEX1() {
let m = createMatcher("iex:4<x<6")
for n in Float.randomNumbersStrings(lower: 4.01, upper: 5.99, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -9999, upper: 4.0, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 6.0, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(4.01, upper: 5.99, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(-9999, upper: 4.0, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(6.0, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIEX2() {
let m = createMatcher("iex:4<=x<10")
for n in Float.randomNumbersStrings(lower: 4.00, upper: 9.99, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -9999, upper: 3.99, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 10.0, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(4.00, upper: 9.99, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(-9999, upper: 3.99, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(10.0, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

// MARK: - Float
func testIEX3() {
let m = createMatcher("iex:4.99<x<9.99")
for n in Float.randomNumbersStrings(lower: 5.00, upper: 9.98, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -9999, upper: 4.99, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 10.0, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(5.00, upper: 9.98, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(-9999, upper: 4.99, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(10.0, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
}

func testIEX4() {
let m = createMatcher("iex:-10.33<=x<10.4")
for n in Float.randomNumbersStrings(lower: -10.33, upper: 10.4, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: -9999, upper: -10.34, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(lower: 10.41, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(-10.33, upper: 10.4, count: steps) { XCTAssertTrue(m.validate(n), "") }
for n in Float.randomNumbersStrings(-9999, upper: -10.34, count: steps) { XCTAssertFalse(m.validate(n), "") }
for n in Float.randomNumbersStrings(10.41, upper: 9999, count: steps) { XCTAssertFalse(m.validate(n), "") }
}
}
2 changes: 1 addition & 1 deletion SwifternalizationTests/JSONFileLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTest
class JSONFileLoaderTests: XCTestCase {

// Expressions
func loadExpressions(_ cc: CountryCode) -> Dictionary<String, String> {
func loadExpressions(_ cc: CountryCode) -> [String : String] {
return JSONFileLoader.loadExpressions(cc, bundle: Bundle.testBundle())
}

Expand Down
18 changes: 9 additions & 9 deletions SwifternalizationTests/LoadedTranslationsProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import XCTest

class LoadedTranslationsProcessorTests: XCTestCase {

private func _baseExpressions() -> Dictionary<String, String> {
private func _baseExpressions() -> [String : String] {
return [
"e1": "ie:x=1",
"e2": "ie:x=2"
]
}

private func _enExpressions() -> Dictionary<String, String> {
private func _enExpressions() -> [String : String] {
return [
"e1": "ie:x=1",
"e2": "ie:x=2",
Expand All @@ -27,7 +27,7 @@ class LoadedTranslationsProcessorTests: XCTestCase {
]
}

private func _baseTranslations() -> Dictionary<String, AnyObject> {
private func _baseTranslations() -> [String : Any] {
return [
"k1": "v1",
"k2": "v2",
Expand All @@ -36,7 +36,7 @@ class LoadedTranslationsProcessorTests: XCTestCase {
]
}

private func _enTranslations() -> Dictionary<String, AnyObject> {
private func _enTranslations() -> [String : Any] {
return [
"k1": "v1",
"k2": [
Expand Down Expand Up @@ -72,7 +72,7 @@ class LoadedTranslationsProcessorTests: XCTestCase {
let enTranslations = TranslationsLoader.loadTranslations(_enTranslations())
var translations = LoadedTranslationsProcessor.processTranslations(baseTranslations, preferedLanguageTranslations: enTranslations, sharedExpressions: expressions)

translations.sort(isOrderedBefore: {$0.key < $1.key})
translations.sort(by: {$0.key < $1.key})

XCTAssertEqual(translations.count, 4, "")

Expand All @@ -99,10 +99,10 @@ class LoadedTranslationsProcessorTests: XCTestCase {

// Get patterns of expressions
var k3ExpressionPatterns: [String] = k3Translation.expressions.map({ $0.pattern })
k3ExpressionPatterns.sort(isOrderedBefore: {$0 < $1})
k3ExpressionPatterns.sort(by: {$0 < $1})

var k3ExpressionsToMatch: [String] = [_enExpressions()["e1"]!, _enExpressions()["e2"]!]
k3ExpressionsToMatch.sort(isOrderedBefore: {$0 < $1})
k3ExpressionsToMatch.sort(by: {$0 < $1})

XCTAssertEqual(k3ExpressionPatterns, k3ExpressionsToMatch, "")

Expand All @@ -115,10 +115,10 @@ class LoadedTranslationsProcessorTests: XCTestCase {

// Get patterns of expressions
var k4ExpressionPatterns: [String] = k4Translation.expressions.map({ $0.pattern })
k4ExpressionPatterns.sort(isOrderedBefore: {$0 < $1})
k4ExpressionPatterns.sort(by: {$0 < $1})

var k4ExpressionsToMatch: [String] = [_enExpressions()["e2"]!, _enExpressions()["e3"]!, _enExpressions()["e4"]!]
k4ExpressionsToMatch.sort(isOrderedBefore: {$0 < $1})
k4ExpressionsToMatch.sort(by: {$0 < $1})

XCTAssertEqual(k4ExpressionPatterns, k4ExpressionsToMatch, "")
}
Expand Down
Loading

0 comments on commit 7576259

Please sign in to comment.