Code Monkey home page Code Monkey logo

Comments (3)

amichnia avatar amichnia commented on May 10, 2024

Postponing - will not be part of 2.0.0

from swiftymocky.

amichnia avatar amichnia commented on May 10, 2024

@Przemyslaw-Wosko I'm finishing PR for sequencing. It got a bit bigger than I thought, as I also added policies for better mock control.

let mock = SimpleProtocolWithMethodsMock()

Given(mock, .simpleMehtodThatReturns(willReturn: 1,2,3))
while true {
  mock.simpleMehtodThatReturns()
}
// 1, 2, 3, 1, 2, 3, 1, 2, 3, ...

Similar for errors:

let mock = ProtocolWithThrowingMethodsMock()
Given(mock, .methodThatThrows(willThrow: TestError.first, TestError.second, TestError.third))

while true {
  try? mock.methodThatThrows()
}
// throwing TestError.first, TestError.second, ...

Also, as we might want to mix the stuff, new Given option is created:

let mock = ProtocolWithThrowingMethodsMock()

Given(mock, .methodThatReturnsAndThrows(param: .any, willProduce: { make in
    make.throw(TestError.first)
    make.throw(TestError.second)
    make.throw(TestError.third)
    make.return(true)
    make.return(true)
    make.return(true)
    make.return(true)
}))

while true {
  try? mock.methodThatReturnsAndThrows(param: 0)
}
// TestError.first, TestError.second, TestError.third, true, true, true , true, ... 
// (go back to the beginning of sequence)

Need some feedback about that part.

As for policies, I'm proposing two:

  1. StubbingPolicy defined for mock in general, and per method stub in particular (if not defined, using mock default value)
    • default - it's default :) use mock default policy
    • wrap - default one, iterate over sequence of return values indefinetely
    • drop - every returned/throw value is being consumed, so it will not be reused again.
  2. SequencingPolicy - in what order should we process Given's.
    • lastWrittenResolvedFirst - default, same behaviour as SwiftyMocky have now. We process givens from most specific ones to most general, and if they are same, last written hides previous one.
    • inWritingOrder - We process givens from most specific ones to most general, and if they are same, we process them in writing order

Simple examples:

// Default policy is drop, so if not specified differently, every given return/throw value
// will be consumed once
let mock = ProtocolWithThrowingMethodsMock(sequencing: .inWritingOrder, stubbing: .drop)

Given(mock, .methodThatReturnsAndThrows(param: .any, willThrow: TestError.first))
Given(mock, .methodThatReturnsAndThrows(param: .any, willThrow: TestError.second))
Given(mock, .methodThatReturnsAndThrows(param: .any, willThrow: TestError.third))
Given(mock, .methodThatReturnsAndThrows(param: .any, willReturn: true), .wrap)

// TestError.first, .second, .third, true, true, true .... (forever, as the last given has policy wrap)
let mock = SimpleProtocolWithMethodsMock()
mock.stubbingPolicy = .drop

Given(mock, .simpleMehtodThatReturns(willReturn: 5))
Given(mock, .simpleMehtodThatReturns(willReturn: 1,2,3))

while true {
  mock.simpleMehtodThatReturns()
}
// 1, 2, 3, 5, [fatal error] - no stubs defined for method ...

For these I'll also need some feedback.

from swiftymocky.

amichnia avatar amichnia commented on May 10, 2024

v3.0.0

from swiftymocky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.