Skip to content

Instantly share code, notes, and snippets.

View ncreated's full-sized avatar
💭
⚡️

Maciek Grzybowski ncreated

💭
⚡️
View GitHub Profile
@ncreated
ncreated / FlakyTests.swift
Created September 14, 2023 12:54
Basic setup for repeating tests in random order
class FlakyTests: XCTestCase {
struct FlakySuite {
let testCase: XCTestCase
let selectors: [Selector]
}
let suites: [FlakySuite] = [
FlakySuite(
testCase: CrashContextProviderTests(),
selectors: [
@ncreated
ncreated / NSObject+allMethods+allProperties.swift
Created March 2, 2023 10:19
Inspect NSObject private properties and methods
extension NSObject {
var __methods: [Selector] {
var methodCount: UInt32 = 0
guard
let methodList = class_copyMethodList(type(of: self), &methodCount),
methodCount != 0
else { return [] }
return (0 ..< Int(methodCount))
.compactMap({ method_getName(methodList[$0]) })
}
@ncreated
ncreated / GenerateColors.swift
Last active September 19, 2024 05:45
iOS 16.2 colors
import Framer
func testGenerateColors() {
struct CC {
let name: String
let color: UIColor
}
let colors: [CC] = [
CC(name: "systemRed", color: UIColor.systemRed),
@ncreated
ncreated / XCTestCase+Benchmark.swift
Last active November 4, 2022 21:29
Basic XCTestCase Benchmark
extension XCTestCase {
func benchmark(_ label: String, iterations: Int = 100, block: () throws -> Void) rethrows {
var measures: [TimeInterval] = []
let warmUp = iterations / 10
for i in (0..<iterations) {
let start = Date()
try block()
if i >= warmUp { // only measure after it is warmed-up
@ncreated
ncreated / UserInfo.json
Last active July 8, 2020 12:28
UserInfo - SK docs
{
"\/Users\/maciek.grzybowski\/Products\/dd-sdk-ios\/Sources\/Datadog\/Core\/Attributes\/UserInfo.swift" : {
"key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
"key.length" : 999,
"key.offset" : 0,
"key.substructure" : [
{
"key.accessibility" : "source.lang.swift.accessibility.internal",
"key.annotated_decl" : "<Declaration>internal class UserInfoProvider<\/Declaration>",
"key.attributes" : [
@ncreated
ncreated / URLRequestForURL.swift
Last active June 10, 2020 11:16
Replacing URL with URLRequest
import PlaygroundSupport
let url = URL(string: "https://picsum.photos/200/300")!
private extension URLSession {
/// Method under test
func urlRequest(with url: URL) -> URLRequest {
return URLRequest(
url: url,
cachePolicy: configuration.requestCachePolicy,
@ncreated
ncreated / ObjectBasedSwizzling.swift
Last active June 8, 2020 09:00
Comparison of Object-Oriented vs Singleton-Based swizzlers.
// MARK: - UIViewController.viewDidLoad() swizzling
class ViewDidLoadSwizzler: MethodSwizzler<
@convention(c) (UIViewController, Selector) -> Void, // <- Swizzle from
@convention(block) (UIViewController) -> Void // <- Swizzle to
> {
init() throws {
try super.init(selector: #selector(UIViewController.viewDidLoad), inClass: UIViewController.self)
}
}
@ncreated
ncreated / Method+Locker.swift
Last active June 5, 2020 15:32
Object-oriented method swizzling
private class Locker {}
private let locker = Locker()
internal class Method {
static func `for`(selector: Selector, inClass `class`: AnyClass) -> Method? {
objc_sync_enter(locker)
if let existingSwizzling = existingSwizzlings[hash(class, selector]) {
return existingSwizzlings
}
@ncreated
ncreated / MulticastDelegate.swift
Created June 14, 2018 16:37
Container for storing many delegates and calling an operation on all at once. Uses weak references to store delegates.
import Foundation
/**
Container for storing many delegates and calling an operation on all at once.
Uses weak references to store delegates.
*/
public final class MulticastDelegate<T> {
private var delegates: [WeakDelegate] = []
@ncreated
ncreated / TestTask.swift
Created September 5, 2017 21:10
Medium blogpost snippet
func test_givenTask_whenExecuted_itUpdatesStatusInSpecificOrder() {
let task = Task()
let mockDelegate = MockTaskStatusDelegate()
task.statusDelegate = mockDelegate
let downloadingExpectation = self.expectation(description: "status changes to .downloading")
let processingExpectation = self.expectation(description: "status changes to .processing")
let finishedExpectation = self.expectation(description: "status changes to .finished")
mockDelegate.didCall_taskDidChangeStatus = { (_, status) in