BeEmpty.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Foundation
  2. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  3. /// means the are no items in that collection. For strings, it is an empty string.
  4. public func beEmpty<S: Sequence>() -> Predicate<S> {
  5. return Predicate.simple("be empty") { actualExpression in
  6. guard let actual = try actualExpression.evaluate() else {
  7. return .fail
  8. }
  9. var generator = actual.makeIterator()
  10. return PredicateStatus(bool: generator.next() == nil)
  11. }
  12. }
  13. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  14. /// means the are no items in that collection. For strings, it is an empty string.
  15. public func beEmpty<S: SetAlgebra>() -> Predicate<S> {
  16. return Predicate.simple("be empty") { actualExpression in
  17. guard let actual = try actualExpression.evaluate() else {
  18. return .fail
  19. }
  20. return PredicateStatus(bool: actual.isEmpty)
  21. }
  22. }
  23. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  24. /// means the are no items in that collection. For strings, it is an empty string.
  25. public func beEmpty<S: Sequence & SetAlgebra>() -> Predicate<S> {
  26. return Predicate.simple("be empty") { actualExpression in
  27. guard let actual = try actualExpression.evaluate() else {
  28. return .fail
  29. }
  30. return PredicateStatus(bool: actual.isEmpty)
  31. }
  32. }
  33. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  34. /// means the are no items in that collection. For strings, it is an empty string.
  35. public func beEmpty() -> Predicate<String> {
  36. return Predicate.simple("be empty") { actualExpression in
  37. let actualString = try actualExpression.evaluate()
  38. return PredicateStatus(bool: actualString == nil || NSString(string: actualString!).length == 0)
  39. }
  40. }
  41. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  42. /// means the are no items in that collection. For NSString instances, it is an empty string.
  43. public func beEmpty() -> Predicate<NSString> {
  44. return Predicate.simple("be empty") { actualExpression in
  45. let actualString = try actualExpression.evaluate()
  46. return PredicateStatus(bool: actualString == nil || actualString!.length == 0)
  47. }
  48. }
  49. // Without specific overrides, beEmpty() is ambiguous for NSDictionary, NSArray,
  50. // etc, since they conform to Sequence as well as NMBCollection.
  51. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  52. /// means the are no items in that collection. For strings, it is an empty string.
  53. public func beEmpty() -> Predicate<NSDictionary> {
  54. return Predicate.simple("be empty") { actualExpression in
  55. let actualDictionary = try actualExpression.evaluate()
  56. return PredicateStatus(bool: actualDictionary == nil || actualDictionary!.count == 0)
  57. }
  58. }
  59. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  60. /// means the are no items in that collection. For strings, it is an empty string.
  61. public func beEmpty() -> Predicate<NSArray> {
  62. return Predicate.simple("be empty") { actualExpression in
  63. let actualArray = try actualExpression.evaluate()
  64. return PredicateStatus(bool: actualArray == nil || actualArray!.count == 0)
  65. }
  66. }
  67. /// A Nimble matcher that succeeds when a value is "empty". For collections, this
  68. /// means the are no items in that collection. For strings, it is an empty string.
  69. public func beEmpty() -> Predicate<NMBCollection> {
  70. return Predicate.simple("be empty") { actualExpression in
  71. let actual = try actualExpression.evaluate()
  72. return PredicateStatus(bool: actual == nil || actual!.count == 0)
  73. }
  74. }
  75. #if canImport(Darwin)
  76. extension NMBObjCMatcher {
  77. @objc public class func beEmptyMatcher() -> NMBPredicate {
  78. return NMBPredicate { actualExpression in
  79. let location = actualExpression.location
  80. let actualValue = try actualExpression.evaluate()
  81. if let value = actualValue as? NMBCollection {
  82. let expr = Expression(expression: ({ value as NMBCollection }), location: location)
  83. return try beEmpty().satisfies(expr).toObjectiveC()
  84. } else if let value = actualValue as? NSString {
  85. let expr = Expression(expression: ({ value as String }), location: location)
  86. return try beEmpty().satisfies(expr).toObjectiveC()
  87. } else if let actualValue = actualValue {
  88. // swiftlint:disable:next line_length
  89. let badTypeErrorMsg = "be empty (only works for NSArrays, NSSets, NSIndexSets, NSDictionaries, NSHashTables, and NSStrings)"
  90. return NMBPredicateResult(
  91. status: NMBPredicateStatus.fail,
  92. message: NMBExpectationMessage(
  93. expectedActualValueTo: badTypeErrorMsg,
  94. customActualValue: "\(String(describing: type(of: actualValue))) type"
  95. )
  96. )
  97. }
  98. return NMBPredicateResult(
  99. status: NMBPredicateStatus.fail,
  100. message: NMBExpectationMessage(expectedActualValueTo: "be empty").appendedBeNilHint()
  101. )
  102. }
  103. }
  104. }
  105. #endif