From 866342871446c830794ed8c3de3bb6c6e84a73ca Mon Sep 17 00:00:00 2001 From: kean Date: Fri, 28 Jun 2024 09:34:46 -0400 Subject: [PATCH 1/2] Add Package.swift --- .gitignore | 1 + .../Extensions/NSBundle+AztecBundle.swift | 6 ++- .../Extensions/UIImageResizeTests.swift | 24 +++++++++--- AztecTests/TestingSupport/TextViewStub.swift | 34 ++++++++++------- Package.swift | 37 +++++++++++++++++++ .../GutenpackAttachmentRendererTests.swift | 8 +++- 6 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 Package.swift diff --git a/.gitignore b/.gitignore index 15268ca39..003ad203a 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ playground.xcworkspace # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. # Packages/ .build/ +.swiftpm # CocoaPods # diff --git a/Aztec/Classes/Extensions/NSBundle+AztecBundle.swift b/Aztec/Classes/Extensions/NSBundle+AztecBundle.swift index ae7105590..e865e107f 100644 --- a/Aztec/Classes/Extensions/NSBundle+AztecBundle.swift +++ b/Aztec/Classes/Extensions/NSBundle+AztecBundle.swift @@ -2,13 +2,17 @@ import Foundation extension Bundle { @objc public class var aztecBundle: Bundle { +#if SWIFT_PACKAGE + return Bundle.module +#else let defaultBundle = Bundle(for: EditorView.self) // If installed with CocoaPods, resources will be in WordPress-Aztec-iOS.bundle if let bundleURL = defaultBundle.resourceURL, - let resourceBundle = Bundle(url: bundleURL.appendingPathComponent("WordPress-Aztec-iOS.bundle")) { + let resourceBundle = Bundle(url: bundleURL.appendingPathComponent("WordPress-Aztec-iOS.bundle")) { return resourceBundle } // Otherwise, the default bundle is used for resources return defaultBundle +#endif } } diff --git a/AztecTests/Extensions/UIImageResizeTests.swift b/AztecTests/Extensions/UIImageResizeTests.swift index 1f7ee53f4..bbf87ac2d 100644 --- a/AztecTests/Extensions/UIImageResizeTests.swift +++ b/AztecTests/Extensions/UIImageResizeTests.swift @@ -14,8 +14,12 @@ class UIImageResizeTests: XCTestCase { } func testResizingImageWorks() { - let bundle = Bundle(for: type(of: self)) - +#if SPM + let bundle = Bundle.module + #else + let bundle = Bundle(for: type(of: self)) + #endif + guard let image = UIImage(named: "aztec", in: bundle, compatibleWith: nil) else { XCTFail() return @@ -52,8 +56,12 @@ class UIImageResizeTests: XCTestCase { } func testResizingImageWorks2() { - let bundle = Bundle(for: type(of: self)) - +#if SPM + let bundle = Bundle.module + #else + let bundle = Bundle(for: type(of: self)) + #endif + guard let image = UIImage(named: "aztec", in: bundle, compatibleWith: nil) else { XCTFail() return @@ -90,8 +98,12 @@ class UIImageResizeTests: XCTestCase { } func testResizingImageWithoutSizeChangeReturnsSameImage() { - let bundle = Bundle(for: type(of: self)) - +#if SPM + let bundle = Bundle.module + #else + let bundle = Bundle(for: type(of: self)) + #endif + guard let image = UIImage(named: "aztec", in: bundle, compatibleWith: nil) else { XCTFail() return diff --git a/AztecTests/TestingSupport/TextViewStub.swift b/AztecTests/TestingSupport/TextViewStub.swift index 5db3ddc92..722ba49c5 100644 --- a/AztecTests/TestingSupport/TextViewStub.swift +++ b/AztecTests/TestingSupport/TextViewStub.swift @@ -2,42 +2,48 @@ import UIKit @testable import Aztec class TextViewStub: Aztec.TextView { - + let attachmentDelegate = TextViewStubAttachmentDelegate() - + // MARK: - Sample HTML Retrieval - + static func loadSampleHTML() -> String { - guard let path = Bundle(for: self).path(forResource: "content", ofType: "html"), - let sample = try? String(contentsOfFile: path) - else { - fatalError() +#if SWIFT_PACKAGE + let bundle = Bundle.module +#else + let bundle = Bundle(for: self) +#endif + + guard let path = bundle.path(forResource: "content", ofType: "html"), + let sample = try? String(contentsOfFile: path) + else { + fatalError() } - + return sample } - + init(withHTML html: String? = nil, font: UIFont = .systemFont(ofSize: 14), pasteboard: UIPasteboard = .forTesting) { super.init( defaultFont: font, defaultMissingImage: UIImage()) - + textAttachmentDelegate = attachmentDelegate registerAttachmentImageProvider(attachmentDelegate) - + if let html = html { setHTML(html) } self.pasteboard = pasteboard } - + convenience init(withSampleHTML: Bool) { let html = withSampleHTML ? TextViewStub.loadSampleHTML() : nil - + self.init(withHTML: html) } - + required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } diff --git a/Package.swift b/Package.swift new file mode 100644 index 000000000..010c3a21d --- /dev/null +++ b/Package.swift @@ -0,0 +1,37 @@ +// swift-tools-version:5.10 + +import PackageDescription + +let package = Package( + name: "WordPressAztecEditor", + platforms: [.iOS(.v11)], + products: [ + .library(name: "Aztec", targets: ["Aztec"]), + .library(name: "WordPressEditor", targets: ["WordPressEditor"]), + ], + targets: [ + .target( + name: "Aztec", + path: "Aztec", + resources: [.process("Assets")] + ), + .testTarget( + name: "AztecTests", + dependencies: ["Aztec"], + path: "AztecTests", + resources: [.process("Resources")] + ), + .target( + name: "WordPressEditor", + dependencies: ["Aztec"], + path: "WordPressEditor/WordPressEditor" + ), + .testTarget( + name: "WordPressEditorTests", + dependencies: ["Aztec", "WordPressEditor"], + path: "WordPressEditor/WordPressEditorTests", + resources: [.process("Resources")] + ) + ], + swiftLanguageVersions: [.v5] +) diff --git a/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift b/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift index 5217e0c73..440dcd138 100644 --- a/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift +++ b/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift @@ -66,8 +66,12 @@ class GutenpackAttachmentRendererTests: XCTestCase { // We no longer support 1x fatalError() }() - - let bundle = Bundle(for: type(of: self)) + +#if SPM + let bundle = Bundle.module + #else + let bundle = Bundle(for: type(of: self)) + #endif guard let url = bundle.url(forResource: fileName, withExtension: "png", subdirectory: nil), let expectedPNGRepresentation = try? Data(contentsOf: url, options: []) else { XCTFail() From ae01e027950f8ed6f4a78882f91820373569651e Mon Sep 17 00:00:00 2001 From: kean Date: Fri, 28 Jun 2024 09:44:28 -0400 Subject: [PATCH 2/2] Remove obsolete tests (were not part of the main project) --- .../CLinkedListToArrayConverterTests.swift | 56 ---------- .../Extensions/UIImageResizeTests.swift | 6 +- Package.swift | 10 +- .../GutenbergPreProcessorTests.swift | 104 ------------------ .../GutenpackAttachmentRendererTests.swift | 2 +- 5 files changed, 9 insertions(+), 169 deletions(-) delete mode 100644 AztecTests/BaseConverters/CLinkedListToArrayConverterTests.swift delete mode 100644 WordPressEditor/WordPressEditorTests/GutenbergPreProcessorTests.swift diff --git a/AztecTests/BaseConverters/CLinkedListToArrayConverterTests.swift b/AztecTests/BaseConverters/CLinkedListToArrayConverterTests.swift deleted file mode 100644 index e0603b8aa..000000000 --- a/AztecTests/BaseConverters/CLinkedListToArrayConverterTests.swift +++ /dev/null @@ -1,56 +0,0 @@ -import XCTest -@testable import Aztec - -class CLinkedListToArrayConverterTests: XCTestCase { - - override func setUp() { - super.setUp() - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } - - struct TestStruct { - var name: String - var next: UnsafeMutablePointer - } - - class TestClass { - let name: String - - init(name: String) { - self.name = name - } - } - - class TestStructToClassConverter: SafeConverter { - func convert(input: TestStruct) -> TestClass { - let testClass = TestClass(name: input.name) - return testClass - } - } - - func testConversion() { - var struct4 = TestStruct(name: "Struct 4", next: nil) - var struct3 = TestStruct(name: "Struct 3", next: &struct4) - var struct2 = TestStruct(name: "Struct 2", next: &struct3) - var struct1 = TestStruct(name: "Struct 1", next: &struct2) - - // Useful for the final comparison. - let structArray = [struct1, struct2, struct3, struct4] - - let elementConverter = TestStructToClassConverter() - let listToArrayConverter = SafeCLinkedListToArrayConverter(elementConverter: elementConverter, next: { return $0.next }) - - let array = listToArrayConverter.convert(&struct1) - - XCTAssertEqual(array.count, structArray.count) - - for (index, element) in array.enumerate() { - XCTAssertEqual(element.name, structArray[index].name) - } - } -} \ No newline at end of file diff --git a/AztecTests/Extensions/UIImageResizeTests.swift b/AztecTests/Extensions/UIImageResizeTests.swift index bbf87ac2d..b9766fe77 100644 --- a/AztecTests/Extensions/UIImageResizeTests.swift +++ b/AztecTests/Extensions/UIImageResizeTests.swift @@ -14,7 +14,7 @@ class UIImageResizeTests: XCTestCase { } func testResizingImageWorks() { -#if SPM +#if SWIFT_PACKAGE let bundle = Bundle.module #else let bundle = Bundle(for: type(of: self)) @@ -56,7 +56,7 @@ class UIImageResizeTests: XCTestCase { } func testResizingImageWorks2() { -#if SPM +#if SWIFT_PACKAGE let bundle = Bundle.module #else let bundle = Bundle(for: type(of: self)) @@ -98,7 +98,7 @@ class UIImageResizeTests: XCTestCase { } func testResizingImageWithoutSizeChangeReturnsSameImage() { -#if SPM +#if SWIFT_PACKAGE let bundle = Bundle.module #else let bundle = Bundle(for: type(of: self)) diff --git a/Package.swift b/Package.swift index 010c3a21d..b4524fe41 100644 --- a/Package.swift +++ b/Package.swift @@ -15,17 +15,17 @@ let package = Package( path: "Aztec", resources: [.process("Assets")] ), + .target( + name: "WordPressEditor", + dependencies: ["Aztec"], + path: "WordPressEditor/WordPressEditor" + ), .testTarget( name: "AztecTests", dependencies: ["Aztec"], path: "AztecTests", resources: [.process("Resources")] ), - .target( - name: "WordPressEditor", - dependencies: ["Aztec"], - path: "WordPressEditor/WordPressEditor" - ), .testTarget( name: "WordPressEditorTests", dependencies: ["Aztec", "WordPressEditor"], diff --git a/WordPressEditor/WordPressEditorTests/GutenbergPreProcessorTests.swift b/WordPressEditor/WordPressEditorTests/GutenbergPreProcessorTests.swift deleted file mode 100644 index 025240df5..000000000 --- a/WordPressEditor/WordPressEditorTests/GutenbergPreProcessorTests.swift +++ /dev/null @@ -1,104 +0,0 @@ -import XCTest -@testable import WordPressEditor - -class GutenbergPreProcessorTests: XCTestCase { - - let processor = GutenbergPreProcessor() - - // MARK: Regular Gutenberg Tags - - private func encode(blockString: String) -> String { - let data = blockString.data(using: .utf16)! - let base64String = data.base64EncodedString() - - return base64String - } - - /// Verifies that a Gutenberg paragraph block is properly encoded. - /// - func testParagraphBlock() { - let openingGutentag = "" - let input = "\(openingGutentag)\n

Hello there!

\n" - - let encodedOpeningComment = encode(blockString: openingGutentag) - let expected = "\n

Hello there!

\n
" - - let output = processor.process(input) - - XCTAssertEqual(output, expected) - } - - /// Verifies that a Gutenberg paragraph block with attributes is properly encoded. - /// - func testParagraphBlockWithAttributes() { - let openingGutentag = "" - let input = "\(openingGutentag)\n

Hello there!

\n" - - let encodedOpeningComment = encode(blockString: openingGutentag) - let expected = "\n

Hello there!

\n
" - - let output = processor.process(input) - - XCTAssertEqual(output, expected) - } - - /// Verifies that multiple Gutenberg paragraph blocks with attributes are properly encoded. - /// - func testMultipleParagraphBlocksWithAttributes() { - let openingGutentag = "" - let singleInputParagraph = "\(openingGutentag)\n

Hello there!

\n" - let input = String(format: "%@\n%@\n%@", singleInputParagraph, singleInputParagraph, singleInputParagraph) - - let encodedOpeningComment = encode(blockString: openingGutentag) - let singleExpectedParagraph = "\n

Hello there!

\n
" - let expected = String(format: "%@\n%@\n%@", singleExpectedParagraph, singleExpectedParagraph, singleExpectedParagraph) - - let output = processor.process(input) - - XCTAssertEqual(output, expected) - } - - // MARK: - Self-Closing Gutenberg Tags - - /// Verifies that a self closing block is properly processed - /// - func testSelfClosedBlock() { - let input = "" - - let encodedGutentag = encode(blockString: input) - let expected = "" - - let output = processor.process(input) - - XCTAssertEqual(output, expected) - } - - /// Verifies that a self closing block with attributes is properly processed - /// - func testSelfClosedBlockWithAttributes() { - let input = "" - - let encodedGutentag = encode(blockString: input) - let expected = "" - - let output = processor.process(input) - - XCTAssertEqual(output, expected) - } - - - /// Verifies that multiple self closing blocks with attributes are properly processed - /// - func testMultipleSelfClosedBlockWithAttributes() { - let singleGutentag = "" - let input = String(format: "%@\n%@\n%@", singleGutentag, singleGutentag, singleGutentag) - - let encodedGutentag = encode(blockString: singleGutentag) - let singleExpectedElement = "" - let expected = String(format: "%@\n%@\n%@", singleExpectedElement, singleExpectedElement, singleExpectedElement) - - let output = processor.process(input) - - XCTAssertEqual(output, expected) - } -} diff --git a/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift b/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift index 440dcd138..3af8d67a8 100644 --- a/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift +++ b/WordPressEditor/WordPressEditorTests/WordPressPlugin/Gutenberg/GutenpackAttachmentRendererTests.swift @@ -67,7 +67,7 @@ class GutenpackAttachmentRendererTests: XCTestCase { fatalError() }() -#if SPM +#if SWIFT_PACKAGE let bundle = Bundle.module #else let bundle = Bundle(for: type(of: self))