Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #8

Merged
merged 12 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Sources/MagickWand/MagickWand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Sources/MagickWand/Wand/ImageWand/ImageWand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class ImageWand: Wand {
}

deinit {
self.clear()
self.destroy()
}

Expand All @@ -82,6 +83,7 @@ public class ImageWand: Wand {
}

public func destroy() {
guard MagickWand.isInstantiated else { return }
DestroyMagickWand(self.pointer)
}
}
2 changes: 2 additions & 0 deletions Sources/MagickWand/Wand/PixelWand/PixelWand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class PixelWand: Wand {
}

deinit {
self.clear()
self.destroy()
}

Expand All @@ -59,6 +60,7 @@ public class PixelWand: Wand {
}

public func destroy() {
guard MagickWand.isInstantiated else { return }
DestroyPixelWand(self.pointer)
}
}
2 changes: 1 addition & 1 deletion Sources/MagickWand/WandTypes/MagickWand+Boolean.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
extension MagickBooleanType {

var bool: Bool {
return self.rawValue == 1
return self.rawValue != 0
}
}
153 changes: 23 additions & 130 deletions Tests/MagickWandTests/MagickWandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
}
}
16 changes: 7 additions & 9 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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