How to use appendDetails method of FailureMessage class

Best Nimble code snippet using FailureMessage.appendDetails

Run Nimble automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ExpectationMessage.swift

Source: ExpectationMessage.swift Github

copy
1import Foundation
2
3public indirect enum ExpectationMessage {
4    // --- Primary Expectations ---
5    /// includes actual value in output ("expected to <message>, got <actual>")
6    case expectedActualValueTo(/* message: */ String)
7    /// uses a custom actual value string in output ("expected to <message>, got <actual>")
8    case expectedCustomValueTo(/* message: */ String, /* actual: */ String)
9    /// excludes actual value in output ("expected to <message>")
10    case expectedTo(/* message: */ String)
11    /// allows any free-form message ("<message>")
12    case fail(/* message: */ String)
13
14    // --- Composite Expectations ---
15    // Generally, you'll want the methods, appended(message:) and appended(details:) instead.
16
17    /// Not Fully Implemented Yet.
18    case prepends(/* Prepended Message */ String, ExpectationMessage)
19
20    /// appends after an existing message ("<expectation> (use beNil() to match nils)")
21    case appends(ExpectationMessage, /* Appended Message */ String)
22
23    /// provides long-form multi-line explainations ("<expectation>\n\n<string>")
24    case details(ExpectationMessage, String)
25
26    internal var sampleMessage: String {
27        let asStr = toString(actual: "<ACTUAL>", expected: "expected", to: "to")
28        let asFailureMessage = FailureMessage()
29        update(failureMessage: asFailureMessage)
30        // swiftlint:disable:next line_length
31        return "(toString(actual:expected:to:) -> \(asStr) || update(failureMessage:) -> \(asFailureMessage.stringValue))"
32    }
33
34    /// Returns the smallest message after the "expected to" string that summarizes the error.
35    ///
36    /// Returns the message part from ExpectationMessage, ignoring all .appends and .details.
37    public var expectedMessage: String {
38        switch self {
39        case let .fail(msg):
40            return msg
41        case let .expectedTo(msg):
42            return msg
43        case let .expectedActualValueTo(msg):
44            return msg
45        case let .expectedCustomValueTo(msg, _):
46            return msg
47        case let .prepends(_, expectation):
48            return expectation.expectedMessage
49        case let .appends(expectation, msg):
50            return "\(expectation.expectedMessage)\(msg)"
51        case let .details(expectation, _):
52            return expectation.expectedMessage
53        }
54    }
55
56    /// Appends a message after the primary expectation message
57    public func appended(message: String) -> ExpectationMessage {
58        switch self {
59        case .fail, .expectedTo, .expectedActualValueTo, .expectedCustomValueTo, .appends, .prepends:
60            return .appends(self, message)
61        case let .details(expectation, msg):
62            return .details(expectation.appended(message: message), msg)
63        }
64    }
65
66    /// Appends a message hinting to use beNil() for when the actual value given was nil.
67    public func appendedBeNilHint() -> ExpectationMessage {
68        return appended(message: " (use beNil() to match nils)")
69    }
70
71    /// Appends a detailed (aka - multiline) message after the primary expectation message
72    /// Detailed messages will be placed after .appended(message:) calls.
73    public func appended(details: String) -> ExpectationMessage {
74        return .details(self, details)
75    }
76
77    internal func visitLeafs(_ f: (ExpectationMessage) -> ExpectationMessage) -> ExpectationMessage {
78        // swiftlint:disable:previous identifier_name
79        switch self {
80        case .fail, .expectedTo, .expectedActualValueTo, .expectedCustomValueTo:
81            return f(self)
82        case let .prepends(msg, expectation):
83            return .prepends(msg, expectation.visitLeafs(f))
84        case let .appends(expectation, msg):
85            return .appends(expectation.visitLeafs(f), msg)
86        case let .details(expectation, msg):
87            return .details(expectation.visitLeafs(f), msg)
88        }
89    }
90
91    /// Replaces a primary expectation with one returned by f. Preserves all composite expectations
92    /// that were built upon it (aka - all appended(message:) and appended(details:).
93    public func replacedExpectation(_ f: @escaping (ExpectationMessage) -> ExpectationMessage) -> ExpectationMessage {
94        // swiftlint:disable:previous identifier_name
95        func walk(_ msg: ExpectationMessage) -> ExpectationMessage {
96            switch msg {
97            case .fail, .expectedTo, .expectedActualValueTo, .expectedCustomValueTo:
98                return f(msg)
99            default:
100                return msg
101            }
102        }
103        return visitLeafs(walk)
104    }
105
106    /// Wraps a primary expectation with text before and after it.
107    /// Alias to prepended(message: before).appended(message: after)
108    public func wrappedExpectation(before: String, after: String) -> ExpectationMessage {
109        return prepended(expectation: before).appended(message: after)
110    }
111
112    /// Prepends a message by modifying the primary expectation
113    public func prepended(expectation message: String) -> ExpectationMessage {
114        func walk(_ msg: ExpectationMessage) -> ExpectationMessage {
115            switch msg {
116            case let .expectedTo(msg):
117                return .expectedTo(message + msg)
118            case let .expectedActualValueTo(msg):
119                return .expectedActualValueTo(message + msg)
120            case let .expectedCustomValueTo(msg, actual):
121                return .expectedCustomValueTo(message + msg, actual)
122            default:
123                return msg.visitLeafs(walk)
124            }
125        }
126        return visitLeafs(walk)
127    }
128
129    // swiftlint:disable:next todo
130    // TODO: test & verify correct behavior
131    internal func prepended(message: String) -> ExpectationMessage {
132        return .prepends(message, self)
133    }
134
135    /// Converts the tree of ExpectationMessages into a final built string.
136    public func toString(actual: String, expected: String = "expected", to: String = "to") -> String {
137        switch self {
138        case let .fail(msg):
139            return msg
140        case let .expectedTo(msg):
141            return "\(expected) \(to) \(msg)"
142        case let .expectedActualValueTo(msg):
143            return "\(expected) \(to) \(msg), got \(actual)"
144        case let .expectedCustomValueTo(msg, actual):
145            return "\(expected) \(to) \(msg), got \(actual)"
146        case let .prepends(msg, expectation):
147            return "\(msg)\(expectation.toString(actual: actual, expected: expected, to: to))"
148        case let .appends(expectation, msg):
149            return "\(expectation.toString(actual: actual, expected: expected, to: to))\(msg)"
150        case let .details(expectation, msg):
151            return "\(expectation.toString(actual: actual, expected: expected, to: to))\n\n\(msg)"
152        }
153    }
154
155    // Backwards compatibility: converts ExpectationMessage tree to FailureMessage
156    internal func update(failureMessage: FailureMessage) {
157        switch self {
158        case let .fail(msg) where !msg.isEmpty:
159            failureMessage.stringValue = msg
160        case .fail:
161            break
162        case let .expectedTo(msg):
163            failureMessage.actualValue = nil
164            failureMessage.postfixMessage = msg
165        case let .expectedActualValueTo(msg):
166            failureMessage.postfixMessage = msg
167        case let .expectedCustomValueTo(msg, actual):
168            failureMessage.postfixMessage = msg
169            failureMessage.actualValue = actual
170        case let .prepends(msg, expectation):
171            expectation.update(failureMessage: failureMessage)
172            if let desc = failureMessage.userDescription {
173                failureMessage.userDescription = "\(msg)\(desc)"
174            } else {
175                failureMessage.userDescription = msg
176            }
177        case let .appends(expectation, msg):
178            expectation.update(failureMessage: failureMessage)
179            failureMessage.appendMessage(msg)
180        case let .details(expectation, msg):
181            expectation.update(failureMessage: failureMessage)
182            failureMessage.appendDetails(msg)
183        }
184    }
185}
186
187extension FailureMessage {
188    internal func toExpectationMessage() -> ExpectationMessage {
189        let defaultMessage = FailureMessage()
190        if expected != defaultMessage.expected || _stringValueOverride != nil {
191            return .fail(stringValue)
192        }
193
194        var message: ExpectationMessage = .fail(userDescription ?? "")
195        if actualValue != "" && actualValue != nil {
196            message = .expectedCustomValueTo(postfixMessage, actualValue ?? "")
197        } else if postfixMessage != defaultMessage.postfixMessage {
198            if actualValue == nil {
199                message = .expectedTo(postfixMessage)
200            } else {
201                message = .expectedActualValueTo(postfixMessage)
202            }
203        }
204        if postfixActual != defaultMessage.postfixActual {
205            message = .appends(message, postfixActual)
206        }
207        if let extended = extendedMessage {
208            message = .details(message, extended)
209        }
210        return message
211    }
212}
213
214#if canImport(Darwin)
215
216public class NMBExpectationMessage: NSObject {
217    private let msg: ExpectationMessage
218
219    internal init(swift msg: ExpectationMessage) {
220        self.msg = msg
221    }
222
223    public init(expectedTo message: String) {
224        self.msg = .expectedTo(message)
225    }
226    public init(expectedActualValueTo message: String) {
227        self.msg = .expectedActualValueTo(message)
228    }
229
230    public init(expectedActualValueTo message: String, customActualValue actual: String) {
231        self.msg = .expectedCustomValueTo(message, actual)
232    }
233
234    public init(fail message: String) {
235        self.msg = .fail(message)
236    }
237
238    public init(prepend message: String, child: NMBExpectationMessage) {
239        self.msg = .prepends(message, child.msg)
240    }
241
242    public init(appendedMessage message: String, child: NMBExpectationMessage) {
243        self.msg = .appends(child.msg, message)
244    }
245
246    public init(prependedMessage message: String, child: NMBExpectationMessage) {
247        self.msg = .prepends(message, child.msg)
248    }
249
250    public init(details message: String, child: NMBExpectationMessage) {
251        self.msg = .details(child.msg, message)
252    }
253
254    public func appendedBeNilHint() -> NMBExpectationMessage {
255        return NMBExpectationMessage(swift: msg.appendedBeNilHint())
256    }
257
258    public func toSwift() -> ExpectationMessage { return self.msg }
259}
260
261extension ExpectationMessage {
262    func toObjectiveC() -> NMBExpectationMessage {
263        return NMBExpectationMessage(swift: self)
264    }
265}
266
267#endif
268
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in FailureMessage

Trigger appendDetails code on LambdaTest Cloud Grid

Execute automation tests with appendDetails on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)