Merge pull request #2 from ryancoyne/master

Change to keyIsEmpty function.  It will now comply with optional variables.
This commit is contained in:
Cameron Perry 2017-08-30 10:26:11 -07:00 committed by GitHub
commit c1fe8ac4a7
3 changed files with 130 additions and 25 deletions

View File

@ -1,18 +1,10 @@
//
// Package.swift
// StORM
//
// Created by Jonathan Guthrie on 2016-09-23.
// Copyright (C) 2016 Jonathan Guthrie.
//
// Generated automatically by Perfect Assistant Application
// Date: 2017-08-13 18:42:55 +0000
import PackageDescription
let package = Package(
name: "StORM",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2)
],
exclude: []
.Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2),
]
)

View File

@ -103,19 +103,25 @@ open class StORM {
/// Returns a boolean that is true if the first property in the class contains a value.
public func keyIsEmpty() -> Bool {
let (_, val) = firstAsKey()
if val is Int {
if val as! Int == 0 {
return true
} else {
return false
}
} else {
if (val as! String).isEmpty {
return true
} else {
return false
}
}
// Grab the type of value:
let type = type(of: val)
// Check if we are nil, we would then of course have an empty primary key.
guard String(describing: val) != "nil" else {
return true
}
// For now we will be expecting String & Integer key types:
switch type {
case is Int.Type, is Int?.Type:
return (val as! Int == 0)
case is String.Type, is String?.Type:
return (val as! String).isEmpty
default:
print("[StORM] WARNING: [\(#function)] Unexpected \(type) for PRIMARY KEY.")
return false
}
}
/// The create method is designed to be overridden

View File

@ -10,6 +10,113 @@ class StORMTests: XCTestCase {
override func setUp() {
super.setUp()
}
func testKeyTypeIntegerNil() {
let val : Int? = nil
// Grab the type of value:
let type = type(of: val)
// Check if we are nil, we would then of course have an empty primary key.
switch type {
case is Int.Type, is Int?.Type:
guard String(describing: val) != "nil" else {
XCTAssert(true)
return
}
XCTFail("Value was supposed to be nil.")
// return (val as! Int == 0)
case is String.Type, is String?.Type:
// return (val as! String).isEmpty
XCTFail("Type was supposed to be an integer.")
default:
XCTFail("Type should either be a String or Integer.")
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
// return false
}
}
func testKeyTypeOptionalInteger() {
let val : Int? = 1
let anyVal : Any = val
// Grab the type of value:
let type = type(of: val)
// Check if we are nil, we would then of course have an empty primary key.
guard String(describing: anyVal) != "nil" else {
XCTFail("Value was not supposed to be nil.")
return
}
switch type {
case is Int.Type, is Int?.Type:
XCTAssert((anyVal as? Int) != nil, "Failed to cast optional Integer as Any to Int")
// return (val as! Int == 0)
case is String.Type, is String?.Type:
// return (val as! String).isEmpty
XCTFail("Type was supposed to be an integer.")
default:
XCTFail("Type should either be a String or Integer.")
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
// return false
}
}
func testKeyTypeStringNil() {
let val : String? = nil
// Grab the type of value:
let type = type(of: val)
switch type {
case is Int.Type, is Int?.Type:
XCTFail("Type was supposed to be a string.")
case is String.Type, is String?.Type:
guard String(describing: val) != "nil" else {
XCTAssert(true)
return
}
XCTFail("Value was supposed to be nil.")
default:
XCTFail("Type should either be a String or Integer.")
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
}
}
func testKeyTypeOptionalString() {
let val : String? = ""
let anyVal : Any = val
// Grab the type of value:
let type = type(of: val)
// Check if we are nil, we would then of course have an empty primary key.
guard String(describing: anyVal) != "nil" else {
XCTFail("Value was not supposed to be nil.")
return
}
switch type {
case is Int.Type, is Int?.Type:
XCTFail("Type was supposed to be a string.")
case is String.Type, is String?.Type:
XCTAssert((anyVal as? String) != nil, "Failed to cast optional string as Any to String")
default:
XCTFail("Type should either be a String or Integer.")
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
}
}
static var allTests : [(String, (StORMTests) -> () throws -> Void)] {
return [