diff --git a/Sources/MagickWand/MagickWand.swift b/Sources/MagickWand/MagickWand.swift index 754ee47..7bd0695 100644 --- a/Sources/MagickWand/MagickWand.swift +++ b/Sources/MagickWand/MagickWand.swift @@ -29,18 +29,30 @@ public struct MagickWand { static let unknownVersion = "unknown" + +#if os(Linux) + static private var wandInstantiated = false +#endif public static func genesis() { MagickWandGenesis() + + #if os(Linux) + self.wandInstantiated = true + #endif } public static func terminus() { MagickWandTerminus() + + #if os(Linux) + self.wandInstantiated = false + #endif } public static var isInstantiated: Bool { #if os(Linux) - return IsMagickInstantiated().bool + return IsMagickInstantiated().bool || self.wandInstantiated #else return IsMagickWandInstantiated().bool #endif diff --git a/Sources/MagickWand/Wand/ImageWand/ImageWand.swift b/Sources/MagickWand/Wand/ImageWand/ImageWand.swift index a4ea7e0..b0aa73e 100644 --- a/Sources/MagickWand/Wand/ImageWand/ImageWand.swift +++ b/Sources/MagickWand/Wand/ImageWand/ImageWand.swift @@ -60,6 +60,7 @@ public class ImageWand: Wand { } deinit { + self.clear() self.destroy() } @@ -82,6 +83,7 @@ public class ImageWand: Wand { } public func destroy() { + guard MagickWand.isInstantiated else { return } DestroyMagickWand(self.pointer) } } diff --git a/Sources/MagickWand/Wand/PixelWand/PixelWand.swift b/Sources/MagickWand/Wand/PixelWand/PixelWand.swift index 3421bfe..ee7c9c0 100644 --- a/Sources/MagickWand/Wand/PixelWand/PixelWand.swift +++ b/Sources/MagickWand/Wand/PixelWand/PixelWand.swift @@ -37,6 +37,7 @@ public class PixelWand: Wand { } deinit { + self.clear() self.destroy() } @@ -59,6 +60,7 @@ public class PixelWand: Wand { } public func destroy() { + guard MagickWand.isInstantiated else { return } DestroyPixelWand(self.pointer) } } diff --git a/Sources/MagickWand/WandTypes/MagickWand+Boolean.swift b/Sources/MagickWand/WandTypes/MagickWand+Boolean.swift index df7e5f8..f7740ff 100644 --- a/Sources/MagickWand/WandTypes/MagickWand+Boolean.swift +++ b/Sources/MagickWand/WandTypes/MagickWand+Boolean.swift @@ -29,6 +29,6 @@ extension MagickBooleanType { var bool: Bool { - return self.rawValue == 1 + return self.rawValue != 0 } } diff --git a/Tests/MagickWandTests/MagickWandTests.swift b/Tests/MagickWandTests/MagickWandTests.swift index 6df5b74..e3432e0 100644 --- a/Tests/MagickWandTests/MagickWandTests.swift +++ b/Tests/MagickWandTests/MagickWandTests.swift @@ -2,147 +2,40 @@ import Foundation import XCTest @testable import MagickWand -class MagickWandTests: XCTestCase { - func testWand() { - - let version = MagickWand.version - print(version) +func open(file: String, ofType type: String) -> Data? { + var fileData: Data? + #if os(Linux) + fileData = try? Data(contentsOf: URL(fileURLWithPath: "\(file).\(type)")) + #else + if let path = Bundle(for: MagickWandTests.self).path(forResource: file, ofType: type) { + fileData = try? Data(contentsOf: URL(fileURLWithPath: path)) + } else { + fileData = try? Data(contentsOf: URL(fileURLWithPath: "\(file).\(type)")) + } + #endif + + return fileData +} - XCTAssert(!version.isEmpty, "version should not be empty") - XCTAssertNotEqual(version, MagickWand.unknownVersion, "version should not be unknown") +class MagickWandTests: XCTestCase { + + func testGenesisTerminus() { + XCTAssertFalse(MagickWand.isInstantiated) + MagickWand.genesis() - - //100x50 - var fileData: Data? - #if os(Linux) - fileData = try? Data(contentsOf: URL(fileURLWithPath: "rect.png")) - #else - if let path = Bundle(for: MagickWandTests.self).path(forResource: "rect", ofType: "png") { - fileData = try? Data(contentsOf: URL(fileURLWithPath: path)) - } else { - fileData = try? Data(contentsOf: URL(fileURLWithPath: "rect.png")) - } - #endif + XCTAssertTrue(MagickWand.isInstantiated) - guard let data = fileData else { - XCTFail("data should not be nil") - return - } - - do { //wand should not exist on terminus - guard let wand = ImageWand(data: data) else { - XCTFail("image should not be nil") - return - } - - XCTAssertEqual(wand.size.width, 100, "wrong image width") - XCTAssertEqual(wand.size.height, 50, "wrong image height") - - guard let cloned = wand.clone() else { - XCTFail("clone should exist") - return - } - - XCTAssertEqual(cloned.size.width, 100, "wrong cloned image width") - XCTAssertEqual(cloned.size.height, 50, "wrong cloned image height") - - wand.clear() - - XCTAssertEqual(wand.size.width, 0, "wrong image width after clear") - XCTAssertEqual(wand.size.height, 0, "wrong image height after clear") - - XCTAssertEqual(cloned.size.width, 100, "cloned image width should not change after parent clear") - XCTAssertEqual(cloned.size.height, 50, "cloned image height should not change after parent clear") - - cloned.resize(width: 40, height: 40, filter: .lanczos) - - XCTAssertEqual(cloned.size.width, 40, "cloned image width wasn't changed on resize") - XCTAssertEqual(cloned.size.height, 40, "cloned image height wasn't changed on resize") - - cloned.adaptiveResize(width: 90, height: 45) - - XCTAssertEqual(cloned.size.width, 90, "cloned image width wasn't changed on adaptive resize") - XCTAssertEqual(cloned.size.height, 45, "cloned image height wasn't changed on adaptive resize") - - cloned.scale(width: 100, height: 50) - - XCTAssertEqual(cloned.size.width, 100, "cloned image width wasn't changed on scale") - XCTAssertEqual(cloned.size.height, 50, "cloned image height wasn't changed on scale") - - wand.read(data: data) - - XCTAssertEqual(cloned.size.width, 100, "wrong image width after reading data") - XCTAssertEqual(cloned.size.height, 50, "wrong image height after reading data") - - let size = wand.size(for: 80) - - XCTAssertEqual(size.width, 80, "wrong size for dimension") - XCTAssertEqual(size.height, 40, "wrong size for dimension") - - print(wand[option: .custom("jpeg:perserve")]) - - print(wand.identity) - print(wand.format) - wand.format = "jpeg" - print(wand.format) - print(wand.filename) - print(wand.interlace) - print(wand.orientation) - print(wand.size) - print("resolution") - print(wand.resolution) - print(wand.compression) - print(wand.gravity) - print(wand.colorspace) - - - - func showColors(_ color: PixelWand?) { - print(color?.colors.rgba) - print(color?.colors.hsl) - print(color?.colors.cmy) - - print(color?.colors.info) - - print(color?.colors.string) - print(color?.colors.normalizedString) - - print(color?.colors.count) - } - - print("background") - print(wand.background) - showColors(wand.background) - - print("border") - print(wand.border) - showColors(wand.border) - - print("matte") - print(wand.matte) - showColors(wand.matte) - - print("pixel") - showColors(wand.pixel(x: 0, y: 0)) - - } - - let isInstantiatedBefore = MagickWand.isInstantiated - XCTAssertEqual(isInstantiatedBefore, true, "value of 'isInstantiated' value before 'terminus()' is wrong") - MagickWand.terminus() - - let isInstantiatedAfter = MagickWand.isInstantiated - - XCTAssertEqual(isInstantiatedAfter, false, "value of 'isInstantiated' value after 'terminus()' is wrong") + + XCTAssertFalse(MagickWand.isInstantiated) } static var allTests : [(String, (MagickWandTests) -> () throws -> Void)] { return [ - ("testWand", testWand), + ("Test Genesis - Terminus", testGenesisTerminus), ] } } diff --git a/codecov.yml b/codecov.yml index b6a46a9..99b107d 100644 --- a/codecov.yml +++ b/codecov.yml @@ -9,21 +9,19 @@ coverage: status: project: - default: - target: auto - threshold: null - branches: null + default: true patch: - default: - target: auto - branches: null + default: true changes: - default: - branches: null + default: true ignore: - "Tests/*" - "Sources/CMagickWand/*" +comment: + layout: "header" + branches: null + behavior: default