• Stars
    star
    292
  • Rank 142,152 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A smart stubbing system.

StubKit

Run unit tests codecov

A smart stubbing system.

Installation

Using Swift Package Manager

.package(url: "https://github.com/kateinoigakukun/StubKit.git", from: "0.1.0"),

Using Carthage

github "kateinoigakukun/StubKit"

Using CocoaPods

pod "SwiftStubKit"

Usage

Getting started

You can instantiate any kind of Decodable with a single line.

import StubKit

// Codable struct
struct User: Codable {
  let id: Int
  let name: String
  let sex: Sex
}

let stubUser = try Stub.make(User.self)
// User(id: 1234, name: "This is Stub String", sex: .female)

Customize property

You can customize properties even if the property is defined as let.

let maleUser = try Stub.make(User.self) {
  $0.set(\.sex, value: .male)
}
// User(id: 1234, name: "This is Stub String", sex: .male)

Using Stubbable

If you want to customize the default stub value, please conform Stubbable.

extension String: Stubbable {
  static func stub() -> String {
    return "This is custommized Stub String"
  }
}

let stubUser = try Stub.make(User.self)
// User(id: 1234, name: "This is customized Stub String", sex: .female)

Advanced Usage

struct RandomIntStubProvider: StubProvider {
    func stub<T>(of type: T.Type) -> T? {
        if type is Int.Type {
            return Int.random(in: 0..<100) as? T
        }
        return nil
    }
}

let userStub = Stub(type: User.self, provider: [RandomIntStubProvider()])
try userStub.make() // User(id: 97)
try userStub.make() // User(id: 54)
try userStub.make() // User(id: 12)

Need to conform non-final class as Stubbable?

You can make it Stubbable by defining the UnsafeStubbable.

public protocol UnsafeStubbable: Stubbable {
    associatedtype Target = Self
    static func unsafeStub() -> Target
}

extension UnsafeStubbable {
    public static func stub() -> Self {
        return unsafeStub() as! Self
    }
}

extension UIImage: UnsafeStubbable {
    public static func unsafeStub() -> UIImage {
        return #imageLiteral(resourceName: "dummy")
    }
}

How does it work

StubKit mainly uses two techniques.

  • Traverse using Decoder protocol.
  • Inject value with non-mutable KeyPath.
  • Existential type using Self.

Traverse struct using Decoder protocol

Swift has Decodable protocol and if a type conforms to Decodable, Swift compiler generates some code to decode internally. So we can decode a JSON to Swift struct without any configuration. StubKit uses this system to construct instance through Decoder. Decoder is a protocol which provide a value by key or index like JSONDecoder. If we pass the Decoder which just provide a stub value recursively, we can instantiate any kind of Decodable instance.

Inject value with non-mutable KeyPath

I know it's only natural but, Swift can't mutate let defined property. But Swift has MemoryLayout<T>.offset which provide the offset to the property from its own address. So actually in memory we can mutate let property.

Existential type using Self

A protocol that has associatedtype or uses Self type can't be existential type. But using Self for return type of method is only available in Swift4.2. (I think using Self for type of getter should be also available.) This technique makes Stubbale type-safely.

More Repositories

1

wasi-vfs

A virtual filesystem layer for WASI.
Rust
148
star
2

TouchTypist

Reduce up your build time. A tool that rewrites your source code as type annotated.
Swift
115
star
3

wasminspect

An interactive debugger for WebAssembly
Rust
100
star
4

irb.wasm

IRB on browser powered by WebAssembly
TypeScript
77
star
5

swiftwasm-game-of-life

Demo project of Swift on Web App
Swift
72
star
6

wasmic-ios

Wasmic allows you to run WebAssembly in a safe way on iOS.
Swift
66
star
7

swift-indexstore

SwiftIndexStore is a IndexStore reader library for Swift.
Swift
49
star
8

mull-xctest

Experimental mutation testing tool for Swift and XCTest powered by mull
C++
44
star
9

swift-playdate

Swift
32
star
10

AnyEquatable

Swift
23
star
11

ruby-compute-runtime

C
18
star
12

swift-renamer

SwiftRenamer is a tool that renames Swift identifiers
Swift
17
star
13

MergeableLibraryInternals

Internals of Mach-O Mergeable Library
16
star
14

chibi-link

A toy linker for WebAssembly object files.
Swift
15
star
15

swift-fastly-edge-example

PoC of Swift for Compute@Edge
Swift
12
star
16

BlurEditor

This provides image editor for drawing blur effects.
Swift
11
star
17

swift-concurrency-playground

C++
9
star
18

DispatchBenchmark

Swift
9
star
19

AnnictAPI

[Deprecated] Use GraphQL API
Swift
8
star
20

wasi-preset-args

A tool to preset command-line arguments to a WASI module
Rust
8
star
21

llvm-next-function-merging

An experimental LLVM pass plugin that allows you to apply the State of the Art function merging techniques
LLVM
8
star
22

kcc-swift

kateinoigakukun's C compiler written in Swift
Swift
7
star
23

swift-lto-benchmark

CMake
7
star
24

MachOParser

Swift
7
star
25

swift-objdump

Swift
6
star
26

optcarrot.wasm

A NES emulator written in Ruby on browser powered by WebAssembly
TypeScript
6
star
27

jupyterlite-ruby-kernel

TypeScript
6
star
28

rbwasm

Rust
5
star
29

rb-wasm-support

C
5
star
30

wasminspect-agent.js

A debugger agent of wasminspect for JavaScript host environment.
TypeScript
4
star
31

IBCodeGen

A tool to transition from Interface Builder to Swift code
Swift
4
star
32

swift-demangle-vim

Translates cryptic Swift symbols into a human-readable form.
Vim Script
4
star
33

swift-stepic

Swift Image steganography
Swift
4
star
34

PoppyBot

A chat bot for Slack
Swift
3
star
35

swiftwasm-simd-samples

swiftwasm-simd-samples
Swift
3
star
36

swiftwasm-threading-example

JavaScript
3
star
37

wasm-shrink

Rust
3
star
38

swiftwasm-scripts

Shell
3
star
39

fastlane-plugin-compilation_time_profiler

Ruby
3
star
40

ruby-component-linking-demo

Demo of Ruby + Wasm + Component Model + Extension libraries
Makefile
2
star
41

KateiSandbox-Carthage-As-Static

Swift
2
star
42

SwiftAwake

Swift
2
star
43

XcodeGenWithPods

Swift
1
star
44

chibi-ray

Swift
1
star
45

CodableURL

Swift
1
star
46

chibi-sat

A toy SAT solver written in Swift
Swift
1
star
47

swift-autolink-issue-repro

Swift
1
star
48

swift-wasm-distributed-actor-samples

Swift
1
star
49

swiftc

Swift
1
star
50

kfortran

Swift
1
star
51

swift-malloctrace

MallocTrace
C
1
star
52

swiftwasm-issue-2851

Swift
1
star
53

sudoku-solver

A PoC project of chibi-sat, which is a toy SAT solver written in Swift.
Swift
1
star
54

swiftwasm-destination

swiftwasm-destination
Ruby
1
star
55

swift-stringify-macro.wasm

Swift
1
star