Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import Nimble
  2. import Quick
  3.  
  4. class ValueObjectSharedExampleConfiguration: QuickConfiguration {
  5. override class func configure(_ configuration: Configuration) {
  6. sharedExamples("Value Object") {
  7. (sharedExampleContext: @escaping SharedExampleContext) in
  8. var isEqual: ((Any, Any) -> Bool)!
  9. var baseObject: Any!
  10. var sameObjects: [Any]!
  11. var differentObjects: [Any]!
  12. beforeEach {
  13. let _context = sharedExampleContext()
  14. isEqual = _context["isequal"] as! (Any, Any) -> Bool
  15. baseObject = _context["base"]
  16. sameObjects = _context["same"] as! [Any]!
  17. differentObjects = _context["different"] as! [Any]!
  18. }
  19. context("when two objects are same") {
  20. it("should be equal") {
  21. for (i, same) in sameObjects.enumerated() {
  22. expect(isEqual(baseObject, same)).to(beTrue(), description: "\n" +
  23. "same[\(i)] should equal to base.\n" +
  24. "base: \n" +
  25. "\(stringify(baseObject))\n" +
  26. "same[\(i)]: \n" +
  27. "\(stringify(same))\n"
  28. )
  29. }
  30. }
  31. }
  32. context("when two objects are different") {
  33. it("should not be equal") {
  34. for (i, different) in differentObjects.enumerated() {
  35. expect(isEqual(baseObject, different))
  36. .to(beFalse(), description: "\n" +
  37. "different[\(i)] should not equal to base.\n" +
  38. "base: \n" +
  39. "\(stringify(baseObject))\n" +
  40. "different[\(i)]: \n" +
  41. "\(stringify(different))\n"
  42. )
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. fileprivate func equalFunction<T: Equatable>(type: T.Type) -> (Any, Any) -> Bool {
  51. return {
  52. (lhs: Any, rhs: Any) -> Bool in
  53. guard let lhs = lhs as? T, let rhs = rhs as? T else { return false }
  54. return lhs == rhs
  55. }
  56. }
  57.  
  58. func itIsValueObject<T: Equatable>(context: @escaping () -> [String:[T]]) {
  59. itBehavesLike("Value Object") {
  60. var _context = context()
  61. return [
  62. "base": _context["base"]![0],
  63. "same": _context["same"] as Any,
  64. "different": _context["different"] as Any,
  65. "isequal": equalFunction(type: T.self)
  66. ]
  67. }
  68. }
  69.  
  70. // Usage:
  71. class MatrixSpec: QuickSpec {
  72. override func spec() {
  73. describe("Matrix") {
  74. var aMatrix: Matrix<Double>!
  75. var sameMatrix: Matrix<Double>!
  76. var matrixWithDifferentRowCount: Matrix<Double>!
  77. var matrixWithDifferentColumnCount: Matrix<Double>!
  78.  
  79. beforeEach {
  80. aMatrix = Matrix<Double>(rows: 3, columns: 2)
  81. sameMatrix = Matrix<Double>(rows: 3, columns: 2)
  82. matrixWithDifferentRowCount = Matrix<Double>(rows: 4, columns: 2)
  83. matrixWithDifferentColumnCount = Matrix<Double>(rows: 3, columns: 3)
  84. }
  85.  
  86. itIsValueObject{
  87. () -> [String:[Matrix<Double>]] in
  88. [ "base": [ aMatrix ],
  89. "same": [ sameMatrix ],
  90. "different": [ matrixWithDifferentRowCount, matrixWithDifferentColumnCount ]
  91. ]
  92. }
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement