diff --git a/CHANGELOG.md b/CHANGELOG.md index 5717dc9..ee05eeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ _None_ [David Jennes](https://github.com/djbe) [#10](https://github.com/SwiftGen/SwiftGenKit/issues/10) [#28](https://github.com/SwiftGen/SwiftGenKit/issues/28) +* We can now re-generate the contexts used for testing by using the "Generate Contexts" Xcode scheme. + [David Jennes](https://github.com/djbe) + [#14](https://github.com/SwiftGen/SwiftGenKit/issues/14) * Documented the input & output of each parser. [David Jennes](https://github.com/djbe) [#24](https://github.com/SwiftGen/SwiftGenKit/issues/24) diff --git a/README.md b/README.md index e47899f..5173b4b 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,11 @@ Each parser provided by this framework has a corresponding documentation file ex * [Fonts](Documentation/Fonts.md) * [Storyboards](Documentation/Storyboards.md) * [Strings](Documentation/Strings.md) + +## Contributing + +Please check the [CONTRIBUTING file](https://github.com/SwiftGen/SwiftGen/blob/master/CONTRIBUTING.md) for guidelines on how to contribute to this repository. + +During development, should you make changes to the code generating context, you can re-generate all the context files instead of modifying them manually. Use either of these methods: +- In Xcode, select the "Generate Contexts" scheme and run the tests +- From Terminal, execute `rake generate_contexts` diff --git a/Rakefile b/Rakefile index 49d6345..07db7f1 100644 --- a/Rakefile +++ b/Rakefile @@ -8,4 +8,10 @@ CONFIGURATION = 'Debug' POD_NAME = 'SwiftGenKit' +desc 'Generate Test Contexts' +task :generate_contexts => "xcode:build" do |task| + Utils.print_header 'Generating contexts...' + Utils.run(%Q(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "Generate Contexts" -configuration "#{CONFIGURATION}" test-without-building), task, xcrun: true, formatter: :xcpretty) +end + task :default => 'xcode:test' diff --git a/Sources/Parsers/AssetsCatalogParser.swift b/Sources/Parsers/AssetsCatalogParser.swift index 7ca1cb3..48a5217 100644 --- a/Sources/Parsers/AssetsCatalogParser.swift +++ b/Sources/Parsers/AssetsCatalogParser.swift @@ -152,7 +152,7 @@ extension AssetsCatalogParser { - Returns: An array of dictionaries, representing the tree of nodes in the catalog. */ fileprivate func loadAssetCatalog(at path: Path) -> [[String: Any]]? { - let command = Command("xcrun", arguments: "actool", "--print-contents", path.description) + let command = Command("xcrun", arguments: "actool", "--print-contents", path.string) let output = command.execute() as Data // try to parse plist diff --git a/Sources/Parsers/ColorsFileParser.swift b/Sources/Parsers/ColorsFileParser.swift index ebc303b..feb41a9 100644 --- a/Sources/Parsers/ColorsFileParser.swift +++ b/Sources/Parsers/ColorsFileParser.swift @@ -137,7 +137,7 @@ public final class ColorsCLRFileParser: ColorsFileParser { public init() {} public func parseFile(at path: Path) throws { - if let colorsList = NSColorList(name: "UserColors", fromFile: path.description) { + if let colorsList = NSColorList(name: "UserColors", fromFile: path.string) { for colorName in colorsList.allKeys { colors[colorName] = colorsList.color(withKey: colorName)?.rgbColor?.hexValue } diff --git a/Sources/Parsers/StringsFileParser.swift b/Sources/Parsers/StringsFileParser.swift index 13989e1..99cf520 100644 --- a/Sources/Parsers/StringsFileParser.swift +++ b/Sources/Parsers/StringsFileParser.swift @@ -8,14 +8,14 @@ import Foundation import PathKit public enum StringsFileParserError: Error, CustomStringConvertible { - case FailureOnLoading(path: String) - case InvalidFormat + case failureOnLoading(path: String) + case invalidFormat public var description: String { switch self { - case .FailureOnLoading(let path): + case .failureOnLoading(let path): return "Failed to load a file at \"\(path)\"" - case .InvalidFormat: + case .invalidFormat: return "Invalid strings file" } } @@ -33,14 +33,14 @@ public final class StringsFileParser { // Localizable.strings files are generally UTF16, not UTF8! public func parseFile(at path: Path) throws { guard let data = try? path.read() else { - throw StringsFileParserError.FailureOnLoading(path: path.description) + throw StringsFileParserError.failureOnLoading(path: path.string) } let plist = try PropertyListSerialization .propertyList(from: data, format: nil) guard let dict = plist as? [String: String] else { - throw StringsFileParserError.InvalidFormat + throw StringsFileParserError.invalidFormat } for (key, translation) in dict { diff --git a/SwiftGenKit.xcodeproj/xcshareddata/xcschemes/Generate Contexts.xcscheme b/SwiftGenKit.xcodeproj/xcshareddata/xcschemes/Generate Contexts.xcscheme new file mode 100644 index 0000000..62672cc --- /dev/null +++ b/SwiftGenKit.xcodeproj/xcshareddata/xcschemes/Generate Contexts.xcscheme @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/SwiftGenKitTests/ColorsCLRFileTests.swift b/Tests/SwiftGenKitTests/ColorsCLRFileTests.swift index c1153e1..7138ba6 100644 --- a/Tests/SwiftGenKitTests/ColorsCLRFileTests.swift +++ b/Tests/SwiftGenKitTests/ColorsCLRFileTests.swift @@ -13,9 +13,7 @@ class ColorsCLRFileTests: XCTestCase { let parser = ColorsCLRFileParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .colors) } func testFileWithDefaults() throws { @@ -23,9 +21,7 @@ class ColorsCLRFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.clr", sub: .colors)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "defaults.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "defaults.plist", sub: .colors) } func testFileWithCustomName() throws { @@ -33,9 +29,7 @@ class ColorsCLRFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.clr", sub: .colors)) let result = parser.stencilContext(enumName: "XCTColors") - let expected = Fixtures.context(for: "customname.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .colors) } func testFileWithBadFile() { diff --git a/Tests/SwiftGenKitTests/ColorsJSONFileTests.swift b/Tests/SwiftGenKitTests/ColorsJSONFileTests.swift index cc27002..0af26b1 100644 --- a/Tests/SwiftGenKitTests/ColorsJSONFileTests.swift +++ b/Tests/SwiftGenKitTests/ColorsJSONFileTests.swift @@ -13,9 +13,7 @@ class ColorsJSONFileTests: XCTestCase { let parser = ColorsJSONFileParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .colors) } func testFileWithDefaults() throws { @@ -23,9 +21,7 @@ class ColorsJSONFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.json", sub: .colors)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "defaults.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "defaults.plist", sub: .colors) } func testFileWithCustomName() throws { @@ -33,9 +29,7 @@ class ColorsJSONFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.json", sub: .colors)) let result = parser.stencilContext(enumName: "XCTColors") - let expected = Fixtures.context(for: "customname.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .colors) } func testFileWithBadSyntax() { diff --git a/Tests/SwiftGenKitTests/ColorsTextFileTests.swift b/Tests/SwiftGenKitTests/ColorsTextFileTests.swift index e28fb2c..c616207 100644 --- a/Tests/SwiftGenKitTests/ColorsTextFileTests.swift +++ b/Tests/SwiftGenKitTests/ColorsTextFileTests.swift @@ -13,9 +13,7 @@ class ColorsTextFileTests: XCTestCase { let parser = ColorsTextFileParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .colors) } func testListWithDefaults() throws { @@ -26,9 +24,7 @@ class ColorsTextFileTests: XCTestCase { try parser.addColor(named: "ArticleBackground", value: "#ffcc0099") let result = parser.stencilContext() - let expected = Fixtures.context(for: "entries.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "entries.plist", sub: .colors) } func testFileWithDefaults() throws { @@ -36,9 +32,7 @@ class ColorsTextFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.txt", sub: .colors)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "text-defaults.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "text-defaults.plist", sub: .colors) } func testFileWithCustomName() throws { @@ -46,9 +40,7 @@ class ColorsTextFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.txt", sub: .colors)) let result = parser.stencilContext(enumName: "XCTColors") - let expected = Fixtures.context(for: "text-customname.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "text-customname.plist", sub: .colors) } func testFileWithBadSyntax() { diff --git a/Tests/SwiftGenKitTests/ColorsXMLFileTests.swift b/Tests/SwiftGenKitTests/ColorsXMLFileTests.swift index ce20473..0c06b39 100644 --- a/Tests/SwiftGenKitTests/ColorsXMLFileTests.swift +++ b/Tests/SwiftGenKitTests/ColorsXMLFileTests.swift @@ -13,9 +13,7 @@ class ColorsXMLFileTests: XCTestCase { let parser = ColorsXMLFileParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .colors) } func testFileWithDefaults() throws { @@ -23,9 +21,7 @@ class ColorsXMLFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.xml", sub: .colors)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "defaults.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "defaults.plist", sub: .colors) } func testFileWithCustomName() throws { @@ -33,9 +29,7 @@ class ColorsXMLFileTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "colors.xml", sub: .colors)) let result = parser.stencilContext(enumName: "XCTColors") - let expected = Fixtures.context(for: "customname.plist", sub: .colors) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .colors) } func testFileWithBadSyntax() { diff --git a/Tests/SwiftGenKitTests/FontsTests.swift b/Tests/SwiftGenKitTests/FontsTests.swift index 2cab041..0faebd2 100644 --- a/Tests/SwiftGenKitTests/FontsTests.swift +++ b/Tests/SwiftGenKitTests/FontsTests.swift @@ -15,9 +15,7 @@ class FontsTests: XCTestCase { let parser = FontsFileParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .fonts) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .fonts) } func testDefaults() { @@ -25,9 +23,7 @@ class FontsTests: XCTestCase { parser.parseFile(at: Fixtures.directory(sub: .fonts)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "defaults.plist", sub: .fonts) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "defaults.plist", sub: .fonts) } func testCustomName() { @@ -35,8 +31,6 @@ class FontsTests: XCTestCase { parser.parseFile(at: Fixtures.directory(sub: .fonts)) let result = parser.stencilContext(enumName: "CustomFamily") - let expected = Fixtures.context(for: "customname.plist", sub: .fonts) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .fonts) } } diff --git a/Tests/SwiftGenKitTests/ImagesTests.swift b/Tests/SwiftGenKitTests/ImagesTests.swift index 2632c9b..aed0b0b 100644 --- a/Tests/SwiftGenKitTests/ImagesTests.swift +++ b/Tests/SwiftGenKitTests/ImagesTests.swift @@ -19,9 +19,7 @@ class ImagesTests: XCTestCase { let parser = AssetsCatalogParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .images) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .images) } func testFileWithDefaults() { @@ -29,9 +27,7 @@ class ImagesTests: XCTestCase { parser.parseCatalog(at: Fixtures.path(for: "Images.xcassets", sub: .images)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "defaults.plist", sub: .images) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "defaults.plist", sub: .images) } func testFileWithCustomName() { @@ -39,8 +35,6 @@ class ImagesTests: XCTestCase { parser.parseCatalog(at: Fixtures.path(for: "Images.xcassets", sub: .images)) let result = parser.stencilContext(enumName: "XCTImages") - let expected = Fixtures.context(for: "customname.plist", sub: .images) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .images) } } diff --git a/Tests/SwiftGenKitTests/StoryboardsMacOSTests.swift b/Tests/SwiftGenKitTests/StoryboardsMacOSTests.swift index 9e75db4..bb49f38 100644 --- a/Tests/SwiftGenKitTests/StoryboardsMacOSTests.swift +++ b/Tests/SwiftGenKitTests/StoryboardsMacOSTests.swift @@ -18,9 +18,7 @@ class StoryboardsMacOSTests: XCTestCase { let parser = StoryboardParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .storyboardsMacOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .storyboardsMacOS) } func testMessageStoryboardWithDefaults() { @@ -32,9 +30,7 @@ class StoryboardsMacOSTests: XCTestCase { } let result = parser.stencilContext() - let expected = Fixtures.context(for: "messages.plist", sub: .storyboardsMacOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "messages.plist", sub: .storyboardsMacOS) } func testAnonymousStoryboardWithDefaults() { @@ -46,9 +42,7 @@ class StoryboardsMacOSTests: XCTestCase { } let result = parser.stencilContext() - let expected = Fixtures.context(for: "anonymous.plist", sub: .storyboardsMacOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "anonymous.plist", sub: .storyboardsMacOS) } func testAllStoryboardsWithDefaults() { @@ -60,9 +54,7 @@ class StoryboardsMacOSTests: XCTestCase { } let result = parser.stencilContext() - let expected = Fixtures.context(for: "all.plist", sub: .storyboardsMacOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "all.plist", sub: .storyboardsMacOS) } func testAllStoryboardsWithCustomName() { @@ -74,8 +66,6 @@ class StoryboardsMacOSTests: XCTestCase { } let result = parser.stencilContext(sceneEnumName: "XCTStoryboardsScene", segueEnumName: "XCTStoryboardsSegue") - let expected = Fixtures.context(for: "customname.plist", sub: .storyboardsMacOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .storyboardsMacOS) } } diff --git a/Tests/SwiftGenKitTests/StoryboardsiOSTests.swift b/Tests/SwiftGenKitTests/StoryboardsiOSTests.swift index 43f3336..6d4f234 100644 --- a/Tests/SwiftGenKitTests/StoryboardsiOSTests.swift +++ b/Tests/SwiftGenKitTests/StoryboardsiOSTests.swift @@ -19,9 +19,7 @@ class StoryboardsiOSTests: XCTestCase { let parser = StoryboardParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .storyboardsiOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .storyboardsiOS) } func testMessageStoryboardWithDefaults() { @@ -33,9 +31,7 @@ class StoryboardsiOSTests: XCTestCase { } let result = parser.stencilContext() - let expected = Fixtures.context(for: "messages.plist", sub: .storyboardsiOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "messages.plist", sub: .storyboardsiOS) } func testAnonymousStoryboardWithDefaults() { @@ -47,9 +43,7 @@ class StoryboardsiOSTests: XCTestCase { } let result = parser.stencilContext() - let expected = Fixtures.context(for: "anonymous.plist", sub: .storyboardsiOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "anonymous.plist", sub: .storyboardsiOS) } func testAllStoryboardsWithDefaults() { @@ -61,9 +55,7 @@ class StoryboardsiOSTests: XCTestCase { } let result = parser.stencilContext() - let expected = Fixtures.context(for: "all.plist", sub: .storyboardsiOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "all.plist", sub: .storyboardsiOS) } func testAllStoryboardsWithCustomName() { @@ -75,8 +67,6 @@ class StoryboardsiOSTests: XCTestCase { } let result = parser.stencilContext(sceneEnumName: "XCTStoryboardsScene", segueEnumName: "XCTStoryboardsSegue") - let expected = Fixtures.context(for: "customname.plist", sub: .storyboardsiOS) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .storyboardsiOS) } } diff --git a/Tests/SwiftGenKitTests/StringsTests.swift b/Tests/SwiftGenKitTests/StringsTests.swift index 96450a1..d651706 100644 --- a/Tests/SwiftGenKitTests/StringsTests.swift +++ b/Tests/SwiftGenKitTests/StringsTests.swift @@ -19,9 +19,7 @@ class StringsTests: XCTestCase { let parser = StringsFileParser() let result = parser.stencilContext() - let expected = Fixtures.context(for: "empty.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "empty.plist", sub: .strings) } func testEntriesWithDefaults() { @@ -33,9 +31,7 @@ class StringsTests: XCTestCase { types: .Object, .Int)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "entries.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "entries.plist", sub: .strings) } func testFileWithDefaults() throws { @@ -43,9 +39,7 @@ class StringsTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "defaults.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "defaults.plist", sub: .strings) } func testMultiline() throws { @@ -53,9 +47,7 @@ class StringsTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "LocMultiline.strings", sub: .strings)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "multiline.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "multiline.plist", sub: .strings) } func testUTF8FileWithDefaults() throws { @@ -63,9 +55,7 @@ class StringsTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "LocUTF8.strings", sub: .strings)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "utf8.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "utf8.plist", sub: .strings) } func testFileWithCustomName() throws { @@ -73,9 +63,7 @@ class StringsTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings)) let result = parser.stencilContext(enumName: "XCTLoc") - let expected = Fixtures.context(for: "customname.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "customname.plist", sub: .strings) } func testFileWithStructuredOnly() throws { @@ -83,9 +71,7 @@ class StringsTests: XCTestCase { try parser.parseFile(at: Fixtures.path(for: "LocStructuredOnly.strings", sub: .strings)) let result = parser.stencilContext() - let expected = Fixtures.context(for: "structuredonly.plist", sub: .strings) - - XCTDiffContexts(result, expected) + XCTDiffContexts(result, expected: "structuredonly.plist", sub: .strings) } //////////////////////////////////////////////////////////////////////// diff --git a/Tests/SwiftGenKitTests/TestsHelper.swift b/Tests/SwiftGenKitTests/TestsHelper.swift index 918ad34..dbb5cd8 100644 --- a/Tests/SwiftGenKitTests/TestsHelper.swift +++ b/Tests/SwiftGenKitTests/TestsHelper.swift @@ -71,10 +71,21 @@ func compare(_ lhs: Any, _ rhs: Any, key: String, path: String) -> String? { return nil } -func XCTDiffContexts(_ result: [String: Any], _ expected: [String: Any], - file: StaticString = #file, line: UInt = #line) { - guard let error = diff(result, expected) else { return } - XCTFail(error, file: file, line: line) +func XCTDiffContexts(_ result: [String: Any], + expected name: String, + sub directory: Fixtures.Directory, + file: StaticString = #file, + line: UInt = #line) { + if ProcessInfo().environment["GENERATE_CONTEXTS"] == "YES" { + let target = Path(#file).parent().parent() + "Resources/Contexts" + directory.rawValue + name + guard (result as NSDictionary).write(to: target.url, atomically: true) else { + fatalError("Unable to write context file \(target)") + } + } else { + let expected = Fixtures.context(for: name, sub: directory) + guard let error = diff(result, expected) else { return } + XCTFail(error, file: file, line: line) + } } class Fixtures { @@ -114,7 +125,7 @@ class Fixtures { static func context(for name: String, sub: Directory) -> [String: Any] { let path = self.path(for: name, subDirectory: "Contexts/\(sub.rawValue)") - guard let data = NSDictionary(contentsOfFile: path.description) as? [String: Any] else { + guard let data = NSDictionary(contentsOf: path.url) as? [String: Any] else { fatalError("Unable to load fixture content") }