Skip to content

Instantly share code, notes, and snippets.

View frootloops's full-sized avatar

Arsen Gasparyan frootloops

  • Revolut
  • Russia, Moscow
View GitHub Profile
@frootloops
frootloops / risk-warning.html
Created October 30, 2023 11:38
risk-warning.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./Risk warning_files/css" rel="stylesheet" type="text/css">
<title>Risk warning</title>
<style>
body {
let repository = builder.makeRepository(
with: [.sample(id: "1"), .sample(id: "2")]
)
// preparation
let builder = Builder()
let repository = builder.makeRepository()
// actual test
builder.priceAlertStorageService.cachedPriceAlertsMockFunc
.returns([.sample(id: "1"), .sample(id: "2")])
let result = repository.cachedPriceAlerts(symbol: "AAPL")
expect(result.map { $0.id }) == ["1", "2"]
private class Builder {
var priceAlertApiService = PriceAlertApiServiceMock()
var priceAlertStorageService = PriceAlertStorageServiceMock()
func makeRepository() -> PriceAlertRepository {
let repository = PriceAlertRepository(
network: priceAlertApiService,
storage: priceAlertStorageService
)
return repository
// preparation
var priceAlertApiService = PriceAlertApiServiceMock()
var priceAlertStorageService = PriceAlertStorageServiceMock()
let repository = PriceAlertRepository(
network: priceAlertApiService,
storage: priceAlertStorageService
)
// actual test
someMockFunc.succeeds(.sample()) // if ouput is Result<T>
someMockFunc.fails(error) // if ouput is Result<T>
someMockFunc.returnsNil() // if ouput is T?
someMockFunc.returns() // if ouput is Void
expect(someMockFunc).to(beCalled())
var priceAlertRepository = PriceAlertRepositoryMock()
priceAlertRepository.cachedPriceAlertsMockFunc.returns([.sample(id: "53")])
final class PriceAlertRepositoryMock: PriceAlertRepositoryProtocol {
var cachedPriceAlertsMockFunc = MockFunc<String?, [PriceAlert]>()
func cachedPriceAlerts(symbol: String?) -> [PriceAlert] {
return cachedPriceAlertsMockFunc.callAndReturn(symbol)
}
}
public struct MockFunc<Input, Output> {
public var parameters: [Input] = []
public var result: (Input) -> Output = { _ in fatalError() }
public mutating func callAndReturn(_ input: Input) -> Output {
parameters.append(input)
return result(input)
}
public mutating func returns(_ value: Output) {
class AnimatorSpy: Animator {
var invokedAnimate = false
var invokedAnimateCount = 0
var invokedAnimateParameters: (duration: TimeInterval, Void)?
var invokedAnimateParametersList = [(duration: TimeInterval, Void)]()
var shouldInvokeAnimateAnimations = false
var stubbedAnimateCompletionResult: (Bool, Void)?
var stubbedAnimateResult: Bool! = false