Commit d5fd0469 authored by Drew's avatar Drew

Compare dictionaries

parent 02cc12d3
Pipeline #2404 passed with stages
in 2 minutes and 20 seconds
......@@ -40,6 +40,14 @@ private func compare<T: SequenceType where T.Generator.Element: Equatable>(_ a:
return true
}
#endif
private func compareDict<K: Equatable, V: Equatable> (_ a: [K:V], _ b: [K:V]) -> Bool {
if a.count != b.count { return false }
for k in a.keys {
if a[k] != b[k] { return false }
}
return true
}
extension CarolineTest {
///Fail the test.
......@@ -105,6 +113,23 @@ extension CarolineTest {
self.fail("\(message()) - \(cdump(a)) == \(cdump(b))", file: file, line: line)
}
}
///Assert that two dictionaries are equal
///- parameter mesage: A reason for failure
public final func assert<K,V where K: Equatable, V: Equatable>(_ a: [K:V], equals b: [K:V], _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
if !compareDict(a,b) {
self.fail("\(message()) - \(cdump(a)) != \(cdump(b))", file: file, line: line)
}
}
///Assert that two dictionaries aren't equal
///- parameter mesage: A reason for failure
public final func assert<K,V where K: Equatable, V: Equatable>(_ a: [K:V], notEqual b: [K:V], _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
if compareDict(a,b) {
self.fail("\(message()) - \(cdump(a)) == \(cdump(b))", file: file, line: line)
}
}
#else
///- noindex: true
public final func assert<T: Equatable>(_ a: T, equals b: T, @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
......@@ -130,6 +155,21 @@ extension CarolineTest {
self.fail("\(message()) - \(cdump(a)) == \(cdump(b))", file: file, line: line)
}
}
///- noindex: true
public final func assert<K,V where K: Equatable, V: Equatable>(_ a: [K:V], equals b: [K:V], @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
if !compareDict(a,b) {
self.fail("\(message()) - \(cdump(a)) != \(cdump(b))", file: file, line: line)
}
}
///- noindex: true
public final func assert<K,V where K: Equatable, V: Equatable>(_ a: [K:V], notEqual b: [K:V], @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
if compareDict(a,b) {
self.fail("\(message()) - \(cdump(a)) == \(cdump(b))", file: file, line: line)
}
}
#endif
#if swift(>=3.0)
......
......@@ -100,11 +100,11 @@
;;coretest tests the full core API
:coretest-gen {
:tool "shell"
:script "bin/caroline-static-tool --core tests/CoreTests/assert.swift tests/CoreTests/fail.swift tests/CoreTests/SimpleEqual.swift tests/CoreTests/SequenceEqual.swift > tests/CoreTests/main.swift"
:script "bin/caroline-static-tool --core tests/CoreTests/assert.swift tests/CoreTests/fail.swift tests/CoreTests/SimpleEqual.swift tests/CoreTests/SequenceEqual.swift tests/CoreTests/DictEqual.swift > tests/CoreTests/main.swift"
:dependencies ["statictool"]
}
:coretest-build {
:dependencies ["core" "coretest-gen"]
:dependencies ["core"]
:tool "atllbuild"
:output-type "executable"
:name "CoreTest"
......@@ -129,11 +129,6 @@
:link-with-product ["CarolineCore.a"]
:publish-product true
}
:coretest {
:dependencies ["coretest-build-xcode"]
:tool "shell"
:script "bin/CoreTest"
}
;;we can run this via standard toolchain since we only depend on statictool
......
// Copyright (c) 2016 Drew Crawford.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import CarolineCore
class DictEqual: CarolineTest {
func test() {
self.assert(["k":"v"], equals: ["k":"v"])
}
}
class DictEqualInverse: CarolineTest {
func test() {
self.assert(["k":"v"], equals: ["k":"y"])
}
let expectFailure = true
}
class DictEqualLengthMismatch: CarolineTest {
func test() {
self.assert(["k":"v","kk":"vv"], equals: ["k":"v"])
}
let expectFailure = true
}
class DictNotEqual: CarolineTest {
func test() {
self.assert(["k":"v"], notEqual: ["k":"y"])
}
}
class DictNotEqualInverse: CarolineTest {
func test() {
self.assert(["k":"v"], notEqual: ["k":"v"])
}
let expectFailure = true
}
class DictNotEqualLengthMismatch: CarolineTest {
func test() {
self.assert(["k":"v","kk":"vv"], notEqual: ["k":"v"])
}
}
\ No newline at end of file
......@@ -35,7 +35,13 @@ let allTests: [CarolineTest] = [
SequenceNotEqualLengthA(),
SequenceNotEqualLengthB(),
SequenceNotEqualLengthC(),
SequenceNotEqualLengthD()
SequenceNotEqualLengthD(),
DictEqual(),
DictEqualInverse(),
DictEqualLengthMismatch(),
DictNotEqual(),
DictNotEqualInverse(),
DictNotEqualLengthMismatch()
]
let engine = CarolineCoreEngine()
if !engine.testAll(allTests) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment