summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLSDKTestHelpers.swift
blob: 727d8bf0c698219a84ce36fcdaa654455637f9a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import XCTest
import Foundation

class MGLSDKTestHelpers {

    class func checkTestsContainAllMethods(testClass: Swift.AnyClass, in p: Protocol) {
        let testMethods = self.classMethodDescriptions(testClass)
        let subjectMethods = self.protocolMethodDescriptions(p)

        for method in subjectMethods {
            if !testMethods.contains(method) {
                XCTFail("\(String(describing: testClass)) does not contain \(method) from \(String(describing: p))")
            }
        }

        XCTAssert(true)
    }

}

extension MGLSDKTestHelpers {

    class func protocolMethodDescriptions(_ p: Protocol) -> Set<String> {
        var methods = Set<String>()
        var methodCount = UInt32()
        let methodDescriptionList = protocol_copyMethodDescriptionList(p, false, true, &methodCount)
        for i in 0..<Int(methodCount) {
            let description = methodDescriptionList![i]
            XCTAssertNotNil(description.name?.description)
            methods.insert(description.name!.description)
        }
        free(methodDescriptionList)
        return methods
    }

    class func classMethodDescriptions(_ cls: Swift.AnyClass) -> Set<String> {
        var methods = Set<String>()
        var methodCount = UInt32()
        let methodList = class_copyMethodList(cls, &methodCount)
        for i in 0..<Int(methodCount) {
            let method = methodList![i]
            let selector = method_getName(method)
            #if os(macOS)
                methods.insert(selector.description)
            #else
                XCTAssertNotNil(selector)
                methods.insert(selector!.description)
            #endif
        }
        free(methodList)
        return methods
    }

}