How to use appendMessage method of FailureMessage class

Best Nimble code snippet using FailureMessage.appendMessage

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        switch self {
79        case .fail, .expectedTo, .expectedActualValueTo, .expectedCustomValueTo:
80            return f(self)
81        case let .prepends(msg, expectation):
82            return .prepends(msg, expectation.visitLeafs(f))
83        case let .appends(expectation, msg):
84            return .appends(expectation.visitLeafs(f), msg)
85        case let .details(expectation, msg):
86            return .details(expectation.visitLeafs(f), msg)
87        }
88    }
89
90    /// Replaces a primary expectation with one returned by f. Preserves all composite expectations
91    /// that were built upon it (aka - all appended(message:) and appended(details:).
92    public func replacedExpectation(_ f: @escaping (ExpectationMessage) -> ExpectationMessage) -> ExpectationMessage {
93        func walk(_ msg: ExpectationMessage) -> ExpectationMessage {
94            switch msg {
95            case .fail, .expectedTo, .expectedActualValueTo, .expectedCustomValueTo:
96                return f(msg)
97            default:
98                return msg
99            }
100        }
101        return visitLeafs(walk)
102    }
103
104    /// Wraps a primary expectation with text before and after it.
105    /// Alias to prepended(message: before).appended(message: after)
106    public func wrappedExpectation(before: String, after: String) -> ExpectationMessage {
107        return prepended(expectation: before).appended(message: after)
108    }
109
110    /// Prepends a message by modifying the primary expectation
111    public func prepended(expectation message: String) -> ExpectationMessage {
112        func walk(_ msg: ExpectationMessage) -> ExpectationMessage {
113            switch msg {
114            case let .expectedTo(msg):
115                return .expectedTo(message + msg)
116            case let .expectedActualValueTo(msg):
117                return .expectedActualValueTo(message + msg)
118            case let .expectedCustomValueTo(msg, actual):
119                return .expectedCustomValueTo(message + msg, actual)
120            default:
121                return msg.visitLeafs(walk)
122            }
123        }
124        return visitLeafs(walk)
125    }
126
127    // TODO: test & verify correct behavior
128    internal func prepended(message: String) -> ExpectationMessage {
129        return .prepends(message, self)
130    }
131
132    /// Converts the tree of ExpectationMessages into a final built string.
133    public func toString(actual: String, expected: String = "expected", to: String = "to") -> String {
134        switch self {
135        case let .fail(msg):
136            return msg
137        case let .expectedTo(msg):
138            return "\(expected) \(to) \(msg)"
139        case let .expectedActualValueTo(msg):
140            return "\(expected) \(to) \(msg), got \(actual)"
141        case let .expectedCustomValueTo(msg, actual):
142            return "\(expected) \(to) \(msg), got \(actual)"
143        case let .prepends(msg, expectation):
144            return "\(msg)\(expectation.toString(actual: actual, expected: expected, to: to))"
145        case let .appends(expectation, msg):
146            return "\(expectation.toString(actual: actual, expected: expected, to: to))\(msg)"
147        case let .details(expectation, msg):
148            return "\(expectation.toString(actual: actual, expected: expected, to: to))\n\n\(msg)"
149        }
150    }
151
152    // Backwards compatibility: converts ExpectationMessage tree to FailureMessage
153    internal func update(failureMessage: FailureMessage) {
154        switch self {
155        case let .fail(msg):
156            failureMessage.stringValue = msg
157        case let .expectedTo(msg):
158            failureMessage.actualValue = nil
159            failureMessage.postfixMessage = msg
160        case let .expectedActualValueTo(msg):
161            failureMessage.postfixMessage = msg
162        case let .expectedCustomValueTo(msg, actual):
163            failureMessage.postfixMessage = msg
164            failureMessage.actualValue = actual
165        case let .prepends(msg, expectation):
166            expectation.update(failureMessage: failureMessage)
167            if let desc = failureMessage.userDescription {
168                failureMessage.userDescription = "\(msg)\(desc)"
169            } else {
170                failureMessage.userDescription = msg
171            }
172        case let .appends(expectation, msg):
173            expectation.update(failureMessage: failureMessage)
174            failureMessage.appendMessage(msg)
175        case let .details(expectation, msg):
176            expectation.update(failureMessage: failureMessage)
177            failureMessage.appendDetails(msg)
178        }
179    }
180}
181
182extension FailureMessage {
183    internal func toExpectationMessage() -> ExpectationMessage {
184        let defaultMsg = FailureMessage()
185        if expected != defaultMsg.expected || _stringValueOverride != nil {
186            return .fail(stringValue)
187        }
188
189        var msg: ExpectationMessage = .fail(userDescription ?? "")
190        if actualValue != "" && actualValue != nil {
191            msg = .expectedCustomValueTo(postfixMessage, actualValue ?? "")
192        } else if postfixMessage != defaultMsg.postfixMessage {
193            if actualValue == nil {
194                msg = .expectedTo(postfixMessage)
195            } else {
196                msg = .expectedActualValueTo(postfixMessage)
197            }
198        }
199        if postfixActual != defaultMsg.postfixActual {
200            msg = .appends(msg, postfixActual)
201        }
202        if let m = extendedMessage {
203            msg = .details(msg, m)
204        }
205        return msg
206    }
207}
208
209#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
210
211public class NMBExpectationMessage: NSObject {
212    private let msg: ExpectationMessage
213
214    internal init(swift msg: ExpectationMessage) {
215        self.msg = msg
216    }
217
218    public init(expectedTo message: String) {
219        self.msg = .expectedTo(message)
220    }
221    public init(expectedActualValueTo message: String) {
222        self.msg = .expectedActualValueTo(message)
223    }
224
225    public init(expectedActualValueTo message: String, customActualValue actual: String) {
226        self.msg = .expectedCustomValueTo(message, actual)
227    }
228
229    public init(fail message: String) {
230        self.msg = .fail(message)
231    }
232
233    public init(prepend message: String, child: NMBExpectationMessage) {
234        self.msg = .prepends(message, child.msg)
235    }
236
237    public init(appendedMessage message: String, child: NMBExpectationMessage) {
238        self.msg = .appends(child.msg, message)
239    }
240
241    public init(prependedMessage message: String, child: NMBExpectationMessage) {
242        self.msg = .prepends(message, child.msg)
243    }
244
245    public init(details message: String, child: NMBExpectationMessage) {
246        self.msg = .details(child.msg, message)
247    }
248
249    public func appendedBeNilHint() -> NMBExpectationMessage {
250        return NMBExpectationMessage(swift: msg.appendedBeNilHint())
251    }
252
253    public func toSwift() -> ExpectationMessage { return self.msg }
254}
255
256extension ExpectationMessage {
257    func toObjectiveC() -> NMBExpectationMessage {
258        return NMBExpectationMessage(swift: self)
259    }
260}
261
262#endif
263
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 appendMessage code on LambdaTest Cloud Grid

Execute automation tests with appendMessage 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)