Skip to content

Instantly share code, notes, and snippets.

View asharov's full-sized avatar

Jaakko Kangasharju asharov

View GitHub Profile
@asharov
asharov / JsonRoundTripMatchers.swift
Last active May 23, 2020 10:57
Two helper matchers for Nimble to test that a Swift Codable implementation round-trips through its decode-encode and encode-decode cycles
import Foundation
import Nimble
public func roundTripFromJson<T: Codable>(throughType type: T.Type) -> Predicate<Data> {
return Predicate { (actualExpression: Expression<Data>) throws -> PredicateResult in
guard let initialData = try actualExpression.evaluate() else {
return PredicateResult(status: .fail, message: .fail("expected a non-<nil> Data"))
}
let object = try JSONDecoder().decode(type, from: initialData)
let encodedData = try JSONEncoder().encode(object)

Keybase proof

I hereby claim:

  • I am asharov on github.
  • I am ashar (https://keybase.io/ashar) on keybase.
  • I have a public key whose fingerprint is 96CC 11C7 B3EA D9B9 30BD 1B25 F57B 3466 9FFF 57FF

To claim this, I am signing this object:

@asharov
asharov / gist:5511991
Created May 3, 2013 17:55
Prepending stuff to an Rx stream
function prependNilObject(stream) {
var nilValue = {};
for (var i = 1; i < arguments.length; ++i) {
nilValue[arguments[i]] = null;
}
var nilStream = Rx.Observable.returnValue(nilValue);
return nilStream.concat(stream);
}