-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5839c2
commit 97ec18d
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Tool.swift | ||
// OctoPhone | ||
// | ||
// Created by Josef Dolezal on 09/05/2017. | ||
// Copyright © 2017 Josef Dolezal. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import RealmSwift | ||
|
||
/// Print tool object | ||
final class Tool { | ||
|
||
// MARK: - Stored properties | ||
|
||
/// Actual temperature of hotend | ||
var actualTemperature = 0.0 | ||
|
||
/// Offset temperature | ||
var offsetTemperature = 0.0 | ||
|
||
/// Target temperature | ||
var targetTemperature = 0.0 | ||
|
||
// MARK: - Computed properties | ||
|
||
// MARK: - Public API | ||
|
||
init(actual: Double, offset: Double, target: Double) { | ||
self.actualTemperature = actual | ||
self.offsetTemperature = offset | ||
self.targetTemperature = target | ||
} | ||
|
||
// MARK: - Realm API | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Tool+JSONAble.swift | ||
// OctoPhone | ||
// | ||
// Created by Josef Dolezal on 09/05/2017. | ||
// Copyright © 2017 Josef Dolezal. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - JSONAble | ||
extension Tool: JSONAble { | ||
static func fromJSON(json: [String : Any]) throws -> Tool { | ||
guard | ||
let actual = json["actual"] as? Double, | ||
let offset = json["offset"] as? Double, | ||
let target = json["target"] as? Double | ||
else { | ||
throw JSONAbleError.errorMappingJSONToObject(json: json) | ||
} | ||
|
||
return Tool(actual: actual, offset: offset, target: target) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// ToolTests.swift | ||
// OctoPhone | ||
// | ||
// Created by Josef Dolezal on 09/05/2017. | ||
// Copyright © 2017 Josef Dolezal. All rights reserved. | ||
// | ||
|
||
import Nimble | ||
import Quick | ||
@testable import OctoPhone | ||
|
||
class ToolTests: QuickSpec { | ||
override func spec() { | ||
describe("Tool") { | ||
describe("converts from JSON") { | ||
var data: [String: Any]! | ||
|
||
afterEach { | ||
data = nil | ||
} | ||
|
||
context("valid JSON") { | ||
beforeEach { | ||
data = ["target": 200.0, "offset": 5.0, "actual": 99.1] | ||
} | ||
|
||
it("converts") { | ||
expect { try Tool.fromJSON(json: data) }.notTo(throwError()) | ||
if let subject = try? Tool.fromJSON(json: data) { | ||
expect(subject.actualTemperature) == 99.1 | ||
expect(subject.offsetTemperature) == 5.0 | ||
expect(subject.targetTemperature) == 200.0 | ||
} | ||
} | ||
} | ||
|
||
context("invalid JSON") { | ||
beforeEach { | ||
data = [:] | ||
} | ||
|
||
it("throws an error") { | ||
expect { try Tool.fromJSON(json: data) }.to(throwError()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |