Skip to content

Malformed mock generated from [<T>: Observable<K>] using RxSwift #319

@jwsinner

Description

@jwsinner

Basic reproducer:

import RxSwift

/// @mockable
protocol Testing {
    var stream: [String: Observable<Int>] { get }
}

Produces:

import RxSwift


class TestingMock: Testing {
    init() { }
    init(stream: [String: Observable<Int>]) {
        self._stream = stream
    }

    private var streamSubjectKind = 0
    private(set) var streamSubjectSetCallCount = 0
    var streamSubject = PublishSubject<Int>() { didSet { streamSubjectSetCallCount += 1 } }
    var streamReplaySubject = ReplaySubject<Int>.create(bufferSize: 1) { didSet { streamSubjectSetCallCount += 1 } }
    var streamBehaviorSubject: BehaviorSubject<Int>! { didSet { streamSubjectSetCallCount += 1 } }
    var _stream: [String: Observable<Int>! { didSet { streamSubjectSetCallCount += 1 } } // <- Malformed dictionary
    var stream: [String: Observable<Int>] {
        get {
            if streamSubjectKind == 0 {
                return streamSubject // <- Incorrect return type (same for others below aside from _stream)
            } else if streamSubjectKind == 1 {
                return streamBehaviorSubject
            } else if streamSubjectKind == 2 {
                return streamReplaySubject
            } else {
                return _stream
            }
        }
        set {
            if let val = newValue as? PublishSubject<Int> { // <- Incorrect cast
                streamSubject = val
                streamSubjectKind = 0
            } else if let val = newValue as? BehaviorSubject<Int> {
                streamBehaviorSubject = val
                streamSubjectKind = 1
            } else if let val = newValue as? ReplaySubject<Int> {
                streamReplaySubject = val
                streamSubjectKind = 2
            } else {
                _stream = newValue
                streamSubjectKind = 3
            }
        }
    }
}

I'm assuming this is related to #307. I should note that until very recently the mock being generated for something like this was

private var _streams: [String: Observable<Int>]!
    var streams: [String: Observable<Int>] {
        get { _streams }
        set { _streams = newValue }
    }

which has been working nicely. A flag to disable decomposition in the rx arguments would definitely be a nice to have.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions