Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 8f574a44c901f6f974780629ca3fd7bc74b719a9 @nschum committed Sep 8, 2014
Showing with 4,566 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +665 −0 Hamcrest.xcodeproj/project.pbxproj
  3. +7 −0 Hamcrest.xcodeproj/project.xcworkspace/contents.xcworkspacedata
  4. +96 −0 Hamcrest.xcodeproj/xcshareddata/xcschemes/Hamcrest OSX.xcscheme
  5. +77 −0 Hamcrest.xcodeproj/xcshareddata/xcschemes/Hamcrest iOS.xcscheme
  6. +10 −0 Hamcrest.xcworkspace/contents.xcworkspacedata
  7. +32 −0 Hamcrest/ArithmeticMatchers.swift
  8. +70 −0 Hamcrest/BasicMatchers.swift
  9. +51 −0 Hamcrest/Descriptions.swift
  10. +38 −0 Hamcrest/DictionaryMatchers.swift
  11. +65 −0 Hamcrest/Hamcrest.swift
  12. +26 −0 Hamcrest/Info.plist
  13. +26 −0 Hamcrest/MatchResult.swift
  14. +19 −0 Hamcrest/Matcher.swift
  15. +47 −0 Hamcrest/MetaMatchers.swift
  16. +55 −0 Hamcrest/OperatorMatchers.swift
  17. +6 −0 Hamcrest/Playground.playground/contents.xcplayground
  18. +7 −0 Hamcrest/Playground.playground/section-1.swift
  19. +55 −0 Hamcrest/README.playground/Documentation/playground-style.css
  20. +37 −0 Hamcrest/README.playground/Documentation/section-1.html
  21. +24 −0 Hamcrest/README.playground/Documentation/section-11.html
  22. +24 −0 Hamcrest/README.playground/Documentation/section-13.html
  23. +24 −0 Hamcrest/README.playground/Documentation/section-15.html
  24. +24 −0 Hamcrest/README.playground/Documentation/section-17.html
  25. +23 −0 Hamcrest/README.playground/Documentation/section-19.html
  26. +23 −0 Hamcrest/README.playground/Documentation/section-21.html
  27. +23 −0 Hamcrest/README.playground/Documentation/section-23.html
  28. +23 −0 Hamcrest/README.playground/Documentation/section-25.html
  29. +24 −0 Hamcrest/README.playground/Documentation/section-27.html
  30. +24 −0 Hamcrest/README.playground/Documentation/section-29.html
  31. +24 −0 Hamcrest/README.playground/Documentation/section-3.html
  32. +24 −0 Hamcrest/README.playground/Documentation/section-5.html
  33. +23 −0 Hamcrest/README.playground/Documentation/section-7.html
  34. +24 −0 Hamcrest/README.playground/Documentation/section-9.html
  35. +35 −0 Hamcrest/README.playground/contents.xcplayground
  36. +14 −0 Hamcrest/README.playground/section-10.swift
  37. +33 −0 Hamcrest/README.playground/section-12.swift
  38. +7 −0 Hamcrest/README.playground/section-14.swift
  39. +8 −0 Hamcrest/README.playground/section-16.swift
  40. +7 −0 Hamcrest/README.playground/section-18.swift
  41. +1 −0 Hamcrest/README.playground/section-2.swift
  42. +8 −0 Hamcrest/README.playground/section-20.swift
  43. +6 −0 Hamcrest/README.playground/section-22.swift
  44. +13 −0 Hamcrest/README.playground/section-24.swift
  45. +11 −0 Hamcrest/README.playground/section-26.swift
  46. +11 −0 Hamcrest/README.playground/section-28.swift
  47. +11 −0 Hamcrest/README.playground/section-30.swift
  48. +21 −0 Hamcrest/README.playground/section-4.swift
  49. +17 −0 Hamcrest/README.playground/section-6.swift
  50. +19 −0 Hamcrest/README.playground/section-8.swift
  51. +6 −0 Hamcrest/README.playground/timeline.xctimeline
  52. +35 −0 Hamcrest/ReflectionMatchers.swift
  53. +192 −0 Hamcrest/SequenceMatchers.swift
  54. +26 −0 Hamcrest/StringMatchers.swift
  55. +416 −0 HamcrestDemo.xcodeproj/project.pbxproj
  56. +110 −0 HamcrestDemo.xcodeproj/xcshareddata/xcschemes/HamcrestDemo.xcscheme
  57. +26 −0 HamcrestDemo/AppDelegate.swift
  58. +667 −0 HamcrestDemo/Base.lproj/MainMenu.xib
  59. +58 −0 HamcrestDemo/Images.xcassets/AppIcon.appiconset/Contents.json
  60. +34 −0 HamcrestDemo/Info.plist
  61. +7 −0 HamcrestDemo/Playground.playground/contents.xcplayground
  62. +6 −0 HamcrestDemo/Playground.playground/section-1.swift
  63. +6 −0 HamcrestDemo/Playground.playground/timeline.xctimeline
  64. +11 −0 HamcrestDemo/main.swift
  65. +18 −0 HamcrestDemoTests/HamcrestDemoTests.swift
  66. +24 −0 HamcrestDemoTests/Info.plist
  67. +40 −0 HamcrestTests/ArithmeticMatcherTests.swift
  68. +45 −0 HamcrestTests/AssertThatTests.swift
  69. +88 −0 HamcrestTests/BaseTestCase.swift
  70. +58 −0 HamcrestTests/BasicMatcherTests.swift
  71. +34 −0 HamcrestTests/DictionaryMatcherTests.swift
  72. +24 −0 HamcrestTests/Info.plist
  73. +29 −0 HamcrestTests/MatcherMocks.swift
  74. +105 −0 HamcrestTests/MetaMatcherTests.swift
  75. +46 −0 HamcrestTests/ReflectionMatcherTests.swift
  76. +147 −0 HamcrestTests/SequenceMatcherTests.swift
  77. +45 −0 HamcrestTests/StringMatcherTests.swift
  78. +27 −0 LICENSE
  79. +287 −0 README.md
@@ -0,0 +1,7 @@
+*.xcodeproj/*.mode*
+*.xcodeproj/*.pbxuser
+*.xcodeproj/*.perspective*
+*.xcworkspace/*.mode*
+*.xcworkspace/*.pbxuser
+*.xcworkspace/*.perspective*
+xcuserdata
Oops, something went wrong.
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace
+ version = "1.0">
+ <FileRef
+ location = "self:Hamcrest.xcodeproj">
+ </FileRef>
+</Workspace>
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+ LastUpgradeVersion = "0600"
+ version = "1.3">
+ <BuildAction
+ parallelizeBuildables = "YES"
+ buildImplicitDependencies = "YES">
+ <BuildActionEntries>
+ <BuildActionEntry
+ buildForTesting = "YES"
+ buildForRunning = "YES"
+ buildForProfiling = "YES"
+ buildForArchiving = "YES"
+ buildForAnalyzing = "YES">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2496DAF019922407008C270E"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "HamcrestOSX"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </BuildActionEntry>
+ </BuildActionEntries>
+ </BuildAction>
+ <TestAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ buildConfiguration = "Debug">
+ <Testables>
+ <TestableReference
+ skipped = "NO">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2496DAFB19922407008C270E"
+ BuildableName = "HamcrestTests.xctest"
+ BlueprintName = "HamcrestTests"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </TestableReference>
+ </Testables>
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2496DAF019922407008C270E"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "HamcrestOSX"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ </TestAction>
+ <LaunchAction
+ selectedDebuggerIdentifier = ""
+ selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
+ launchStyle = "0"
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Debug"
+ ignoresPersistentStateOnLaunch = "NO"
+ debugDocumentVersioning = "YES"
+ allowLocationSimulation = "YES">
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2496DAF019922407008C270E"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "HamcrestOSX"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ <AdditionalOptions>
+ </AdditionalOptions>
+ </LaunchAction>
+ <ProfileAction
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ savedToolIdentifier = ""
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Release"
+ debugDocumentVersioning = "YES">
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2496DAF019922407008C270E"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "HamcrestOSX"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ </ProfileAction>
+ <AnalyzeAction
+ buildConfiguration = "Debug">
+ </AnalyzeAction>
+ <ArchiveAction
+ buildConfiguration = "Release"
+ revealArchiveInOrganizer = "YES">
+ </ArchiveAction>
+</Scheme>
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+ LastUpgradeVersion = "0600"
+ version = "1.3">
+ <BuildAction
+ parallelizeBuildables = "YES"
+ buildImplicitDependencies = "YES">
+ <BuildActionEntries>
+ <BuildActionEntry
+ buildForTesting = "YES"
+ buildForRunning = "YES"
+ buildForProfiling = "YES"
+ buildForArchiving = "YES"
+ buildForAnalyzing = "YES">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2494844719B60A56007EEA3D"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "Hamcrest"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </BuildActionEntry>
+ </BuildActionEntries>
+ </BuildAction>
+ <TestAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ buildConfiguration = "Debug">
+ <Testables>
+ </Testables>
+ </TestAction>
+ <LaunchAction
+ selectedDebuggerIdentifier = ""
+ selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
+ launchStyle = "0"
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Debug"
+ ignoresPersistentStateOnLaunch = "NO"
+ debugDocumentVersioning = "YES"
+ allowLocationSimulation = "YES">
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2494844719B60A56007EEA3D"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "Hamcrest"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ <AdditionalOptions>
+ </AdditionalOptions>
+ </LaunchAction>
+ <ProfileAction
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ savedToolIdentifier = ""
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Release"
+ debugDocumentVersioning = "YES">
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "2494844719B60A56007EEA3D"
+ BuildableName = "Hamcrest.framework"
+ BlueprintName = "Hamcrest"
+ ReferencedContainer = "container:Hamcrest.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ </ProfileAction>
+ <AnalyzeAction
+ buildConfiguration = "Debug">
+ </AnalyzeAction>
+ <ArchiveAction
+ buildConfiguration = "Release"
+ revealArchiveInOrganizer = "YES">
+ </ArchiveAction>
+</Scheme>
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace
+ version = "1.0">
+ <FileRef
+ location = "group:Hamcrest.xcodeproj">
+ </FileRef>
+ <FileRef
+ location = "group:HamcrestDemo.xcodeproj">
+ </FileRef>
+</Workspace>
@@ -0,0 +1,32 @@
+public func equalTo<T: Equatable>(expectedValue: T) -> Matcher<T> {
+ return Matcher("equal to \(expectedValue)") {$0 == expectedValue}
+}
+
+public func closeTo(expectedValue: Double, delta: Double) -> Matcher<Double> {
+ return Matcher("within \(delta) of \(expectedValue)") {
+ (value: Double) -> MatchResult in
+ let actual = abs(value - expectedValue)
+ return MatchResult(actual < delta, "difference of \(actual)")
+ }
+}
+
+public func closeTo(expectedValue: Float, delta: Double) -> Matcher<Float> {
+ let matcher = closeTo(Double(expectedValue), Double(delta))
+ return Matcher(matcher.description) {matcher.matches(Double($0))}
+}
+
+public func greaterThan<T: Comparable>(expectedValue: T) -> Matcher<T> {
+ return Matcher("greater than \(expectedValue)") {$0 > expectedValue}
+}
+
+public func greaterThanOrEqualTo<T: Comparable>(expectedValue: T) -> Matcher<T> {
+ return Matcher("greater than or equal to \(expectedValue)") {$0 >= expectedValue}
+}
+
+public func lessThan<T: Comparable>(expectedValue: T) -> Matcher<T> {
+ return Matcher("greater than \(expectedValue)") {$0 < expectedValue}
+}
+
+public func lessThanOrEqualTo<T: Comparable>(expectedValue: T) -> Matcher<T> {
+ return Matcher("greater than or equal to \(expectedValue)") {$0 <= expectedValue}
+}
@@ -0,0 +1,70 @@
+public func anything<T>() -> Matcher<T> {
+ return Matcher("anything") {value in true}
+}
+
+public func sameInstance<T: AnyObject>(expectedValue: T) -> Matcher<T> {
+ return Matcher("same instance as \(describeAddress(expectedValue))") {
+ (value: T) -> MatchResult in
+ value === expectedValue ? .Match : .Mismatch(describeAddress(value))
+ }
+}
+
+// MARK: is
+
+public func `is`<T>(matcher: Matcher<T>) -> Matcher<T> {
+ return Matcher("is " + matcher.description) {
+ (value: T) -> MatchResult in
+ return matcher.matches(value)
+ }
+}
+
+public func isA<T: Any>(expectedType: T.Type) -> Matcher<Any> {
+ return `is`(instanceOf(expectedType))
+}
+
+public func `is`<T: Equatable>(expectedValue: T) -> Matcher<T> {
+ return `is`(equalTo(expectedValue))
+}
+
+// MARK: optionals
+
+public func nilValue<T>() -> Matcher<Optional<T>> {
+ return Matcher("nil") {$0 == nil}
+}
+
+public func present<T>() -> Matcher<Optional<T>> {
+ return Matcher("present") {$0 != nil}
+}
+
+public func presentAnd<T>(matcher: Matcher<T>) -> Matcher<Optional<T>> {
+ return Matcher("present and \(matcher.description)") {
+ (value: T?) -> MatchResult in
+ if let unwrappedValue = value {
+ return matcher.matches(unwrappedValue)
+ } else {
+ return .Mismatch(nil)
+ }
+ }
+}
+
+// MARK: casting
+
+public func instanceOf<T: Any>(expectedType: T.Type) -> Matcher<Any> {
+ // There seems to be no way to get the type name.
+ return Matcher("instance of expected type") {$0 is T}
+}
+
+public func instanceOf<T: Any>(expectedType: T.Type, and matcher: Matcher<T>) -> Matcher<Any> {
+ return instanceOfAnd(matcher)
+}
+
+public func instanceOfAnd<T: Any>(matcher: Matcher<T>) -> Matcher<Any> {
+ return Matcher("instance of and \(matcher.description)") {
+ (value: Any) -> MatchResult in
+ if let value = value as? T {
+ return matcher.matches(value)
+ } else {
+ return .Mismatch("mismatched type")
+ }
+ }
+}
@@ -0,0 +1,51 @@
+import Foundation
+
+func describe<T>(value: T) -> String {
+ if let stringArray = value as? [String] {
+ return joinDescriptions(stringArray.map {describe($0)})
+ }
+ if let string = value as? String {
+ return "\"\(string)\""
+ }
+ return toString(value)
+}
+
+func describeAddress<T: AnyObject>(object: T) -> String {
+ return NSString(format: "%p", unsafeBitCast(object, Int.self))
+}
+
+func describeMismatch<T>(value: T, description: String, mismatchDescription: String?) -> String {
+ return "GOT: " + describeActualValue(value, mismatchDescription) + ", EXPECTED: \(description)"
+}
+
+func describeActualValue<T>(value: T, mismatchDescription: String?) -> String {
+ return describe(value) + (mismatchDescription != nil ? " (\(mismatchDescription!))" : "")
+}
+
+func joinDescriptions(descriptions: [String]) -> String {
+ return joinStrings(descriptions)
+}
+
+func joinDescriptions(descriptions: [String?]) -> String? {
+ let notNil = filterNotNil(descriptions)
+ return notNil.isEmpty ? nil : joinStrings(notNil)
+}
+
+func joinMatcherDescriptions<T>(matchers: [Matcher<T>], prefix: String = "all of") -> String {
+ if matchers.count == 1 {
+ return matchers[0].description
+ } else {
+ return prefix + " " + joinDescriptions(matchers.map({$0.description}))
+ }
+}
+
+private func joinStrings(strings: [String]) -> String {
+ switch (strings.count) {
+ case 0:
+ return "none"
+ case 1:
+ return strings[0]
+ default:
+ return "[" + join(", ", strings) + "]"
+ }
+}
@@ -0,0 +1,38 @@
+public func hasEntry<K: Hashable, V>(keyMatcher: Matcher<K>, valueMatcher: Matcher<V>)
+ -> Matcher<Dictionary<K, V>> {
+
+ return Matcher("a dictionary containing [\(keyMatcher.description) -> \(valueMatcher.description)]") {
+ (dictionary: Dictionary<K, V>) -> Bool in
+
+ for (key, value) in dictionary {
+ if keyMatcher.matches(key) && valueMatcher.matches(value) {
+ return true
+ }
+ }
+ return false
+ }
+}
+
+public func hasEntry<K: Equatable, V: Equatable where K: Hashable>(expectedKey: K, expectedValue: V)
+ -> Matcher<Dictionary<K, V>> {
+
+ return hasEntry(equalToWithoutDescription(expectedKey), equalToWithoutDescription(expectedValue))
+}
+
+public func hasKey<K: Hashable, V>(matcher: Matcher<K>) -> Matcher<Dictionary<K, V>> {
+ return hasEntry(matcher, anything())
+}
+
+public func hasKey<K, V where K: Equatable, K: Hashable>(expectedKey: K)
+ -> Matcher<Dictionary<K, V>> {
+
+ return hasKey(equalToWithoutDescription(expectedKey))
+}
+
+public func hasValue<K: Hashable, V>(matcher: Matcher<V>) -> Matcher<Dictionary<K, V>> {
+ return hasEntry(anything(), matcher)
+}
+
+public func hasValue<K: Hashable, V: Equatable>(expectedValue: V) -> Matcher<Dictionary<K, V>> {
+ return hasValue(equalToWithoutDescription(expectedValue))
+}
Oops, something went wrong.

0 comments on commit 8f574a4

Please sign in to comment.