How to use testCreateSnapshotWithExtendedDelimiterSingleLine2 method of InlineSnapshotTests class

Best Swift-snapshot-testing code snippet using InlineSnapshotTests.testCreateSnapshotWithExtendedDelimiterSingleLine2

Run Swift-snapshot-testing automation tests on LambdaTest cloud grid

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

InlineSnapshotTests.swift

Source: InlineSnapshotTests.swift Github

copy
1import XCTest
2@testable import SnapshotTesting
3
4class InlineSnapshotTests: XCTestCase {
5
6  func testCreateSnapshotSingleLine() {
7    let diffable = "NEW_SNAPSHOT"
8    let source = """
9    _assertInlineSnapshot(matching: diffable, as: .lines, with: "")
10    """
11
12    var recordings: Recordings = [:]
13    let newSource = writeInlineSnapshot(
14      &recordings,
15      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
16    ).sourceCode
17
18    assertSnapshot(source: newSource, diffable: diffable)
19  }
20
21  func testCreateSnapshotMultiLine() {
22    let diffable = "NEW_SNAPSHOT"
23    let source = #"""
24    _assertInlineSnapshot(matching: diffable, as: .lines, with: """
25    """)
26    """#
27
28    var recordings: Recordings = [:]
29    let newSource = writeInlineSnapshot(
30      &recordings,
31      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
32    ).sourceCode
33
34    assertSnapshot(source: newSource, diffable: diffable)
35  }
36
37  func testUpdateSnapshot() {
38    let diffable = "NEW_SNAPSHOT"
39    let source = #"""
40    _assertInlineSnapshot(matching: diffable, as: .lines, with: """
41    OLD_SNAPSHOT
42    """)
43    """#
44
45    var recordings: Recordings = [:]
46    let newSource = writeInlineSnapshot(
47      &recordings,
48      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
49    ).sourceCode
50
51    assertSnapshot(source: newSource, diffable: diffable)
52  }
53
54  func testUpdateSnapshotWithMoreLines() {
55    let diffable = "NEW_SNAPSHOT\nNEW_SNAPSHOT"
56    let source = #"""
57    _assertInlineSnapshot(matching: diffable, as: .lines, with: """
58    OLD_SNAPSHOT
59    """)
60    """#
61
62    var recordings: Recordings = [:]
63    let newSource = writeInlineSnapshot(
64      &recordings,
65      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
66      ).sourceCode
67
68    assertSnapshot(source: newSource, diffable: diffable)
69  }
70
71  func testUpdateSnapshotWithLessLines() {
72    let diffable = "NEW_SNAPSHOT"
73    let source = #"""
74    _assertInlineSnapshot(matching: diffable, as: .lines, with: """
75    OLD_SNAPSHOT
76    OLD_SNAPSHOT
77    """)
78    """#
79
80    var recordings: Recordings = [:]
81    let newSource = writeInlineSnapshot(
82      &recordings,
83      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
84      ).sourceCode
85
86    assertSnapshot(source: newSource, diffable: diffable)
87  }
88
89  func testCreateSnapshotWithExtendedDelimiterSingleLine1() {
90    let diffable = #"\""#
91    let source = """
92    _assertInlineSnapshot(matching: diffable, as: .lines, with: "")
93    """
94
95    var recordings: Recordings = [:]
96    let newSource = writeInlineSnapshot(
97      &recordings,
98      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
99      ).sourceCode
100
101    assertSnapshot(source: newSource, diffable: diffable)
102  }
103
104  func testCreateSnapshotEscapedNewlineLastLine() {
105    let diffable = #"""
106    abc \
107    cde \
108    """#
109    let source = """
110    _assertInlineSnapshot(matching: diffable, as: .lines, with: "")
111    """
112
113    var recordings: Recordings = [:]
114    let newSource = writeInlineSnapshot(
115      &recordings,
116      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
117      ).sourceCode
118
119    assertSnapshot(source: newSource, diffable: diffable)
120  }
121
122  func testCreateSnapshotWithExtendedDelimiterSingleLine2() {
123    let diffable = ##"\"""#"##
124    let source = ##"""
125    _assertInlineSnapshot(matching: diffable, as: .lines, with: "")
126    """##
127
128    var recordings: Recordings = [:]
129    let newSource = writeInlineSnapshot(
130      &recordings,
131      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
132      ).sourceCode
133
134    assertSnapshot(source: newSource, diffable: diffable)
135  }
136
137  func testCreateSnapshotWithExtendedDelimiter1() {
138    let diffable = #"\""#
139    let source = ##"""
140    _assertInlineSnapshot(matching: diffable, as: .lines, with: #"""
141    """#)
142    """##
143
144    var recordings: Recordings = [:]
145    let newSource = writeInlineSnapshot(
146      &recordings,
147      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
148      ).sourceCode
149
150    assertSnapshot(source: newSource, diffable: diffable)
151  }
152
153  func testCreateSnapshotWithExtendedDelimiter2() {
154    let diffable = ##"\"""#"##
155    let source = ###"""
156    _assertInlineSnapshot(matching: diffable, as: .lines, with: ##"""
157    """##)
158    """###
159
160    var recordings: Recordings = [:]
161    let newSource = writeInlineSnapshot(
162      &recordings,
163      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
164      ).sourceCode
165
166    assertSnapshot(source: newSource, diffable: diffable)
167  }
168
169  func testCreateSnapshotWithLongerExtendedDelimiter1() {
170    let diffable =  #"\""#
171    let source = ###"""
172    _assertInlineSnapshot(matching: diffable, as: .lines, with: ##"""
173    """##)
174    """###
175
176    var recordings: Recordings = [:]
177    let newSource = writeInlineSnapshot(
178      &recordings,
179      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
180      ).sourceCode
181
182    assertSnapshot(source: newSource, diffable: diffable)
183  }
184
185  func testCreateSnapshotWithLongerExtendedDelimiter2() {
186    let diffable = ##"\"""#"##
187    let source = ####"""
188    _assertInlineSnapshot(matching: diffable, as: .lines, with: ###"""
189    """###)
190    """####
191
192    var recordings: Recordings = [:]
193    let newSource = writeInlineSnapshot(
194      &recordings,
195      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
196      ).sourceCode
197
198    assertSnapshot(source: newSource, diffable: diffable)
199  }
200
201  func testCreateSnapshotWithShorterExtendedDelimiter1() {
202    let diffable = #"\""#
203    let source = #"""
204    _assertInlineSnapshot(matching: diffable, as: .lines, with: """
205    """)
206    """#
207
208    var recordings: Recordings = [:]
209    let newSource = writeInlineSnapshot(
210      &recordings,
211      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
212      ).sourceCode
213
214    assertSnapshot(source: newSource, diffable: diffable)
215  }
216
217  func testCreateSnapshotWithShorterExtendedDelimiter2() {
218    let diffable = ##"\"""#"##
219    let source = ##"""
220    _assertInlineSnapshot(matching: diffable, as: .lines, with: #"""
221    """#)
222    """##
223
224    var recordings: Recordings = [:]
225    let newSource = writeInlineSnapshot(
226      &recordings,
227      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
228      ).sourceCode
229
230    assertSnapshot(source: newSource, diffable: diffable)
231  }
232
233  func testUpdateSnapshotWithExtendedDelimiter1() {
234    let diffable = #"\""#
235    let source = ##"""
236    _assertInlineSnapshot(matching: diffable, as: .lines, with: #"""
237    \"
238    """#)
239    """##
240
241    var recordings: Recordings = [:]
242    let newSource = writeInlineSnapshot(
243      &recordings,
244      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
245      ).sourceCode
246
247    assertSnapshot(source: newSource, diffable: diffable)
248  }
249
250  func testUpdateSnapshotWithExtendedDelimiter2() {
251    let diffable = ##"\"""#"##
252    let source = ###"""
253    _assertInlineSnapshot(matching: diffable, as: .lines, with: ##"""
254    "#
255    """##)
256    """###
257
258    var recordings: Recordings = [:]
259    let newSource = writeInlineSnapshot(
260      &recordings,
261      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
262      ).sourceCode
263
264    assertSnapshot(source: newSource, diffable: diffable)
265  }
266
267  func testUpdateSnapshotWithLongerExtendedDelimiter1() {
268    let diffable = #"\""#
269    let source = #"""
270    _assertInlineSnapshot(matching: diffable, as: .lines, with: """
271    \"
272    """)
273    """#
274
275    var recordings: Recordings = [:]
276    let newSource = writeInlineSnapshot(
277      &recordings,
278      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
279      ).sourceCode
280
281    assertSnapshot(source: newSource, diffable: diffable)
282  }
283
284  func testUpdateSnapshotWithLongerExtendedDelimiter2() {
285    let diffable = ##"\"""#"##
286    let source = ##"""
287    _assertInlineSnapshot(matching: diffable, as: .lines, with: #"""
288    "#
289    """#)
290    """##
291
292    var recordings: Recordings = [:]
293    let newSource = writeInlineSnapshot(
294      &recordings,
295      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
296      ).sourceCode
297
298    assertSnapshot(source: newSource, diffable: diffable)
299  }
300
301  func testUpdateSnapshotWithShorterExtendedDelimiter1() {
302    let diffable = #"\""#
303    let source = ###"""
304    _assertInlineSnapshot(matching: diffable, as: .lines, with: ##"""
305    \"
306    """##)
307    """###
308
309    var recordings: Recordings = [:]
310    let newSource = writeInlineSnapshot(
311      &recordings,
312      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
313      ).sourceCode
314
315    assertSnapshot(source: newSource, diffable: diffable)
316  }
317
318  func testUpdateSnapshotWithShorterExtendedDelimiter2() {
319    let diffable = ##"\"""#"##
320    let source = ####"""
321    _assertInlineSnapshot(matching: diffable, as: .lines, with: ###"""
322    "#
323    """###)
324    """####
325
326    var recordings: Recordings = [:]
327    let newSource = writeInlineSnapshot(
328      &recordings,
329      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
330      ).sourceCode
331
332    assertSnapshot(source: newSource, diffable: diffable)
333  }
334
335  func testUpdateSeveralSnapshotsWithMoreLines() {
336    let diffable1 = """
337    NEW_SNAPSHOT
338    with two lines
339    """
340
341    let diffable2 = "NEW_SNAPSHOT"
342
343    let source = """
344    _assertInlineSnapshot(matching: diffable, as: .lines, with: \"""
345    OLD_SNAPSHOT
346    \""")
347
348    _assertInlineSnapshot(matching: diffable2, as: .lines, with: \"""
349    OLD_SNAPSHOT
350    \""")
351    """
352
353    var recordings: Recordings = [:]
354    let sourceAfterFirstSnapshot = writeInlineSnapshot(
355      &recordings,
356      Context(sourceCode: source, diffable: diffable1, fileName: "filename", lineIndex: 1)
357    ).sourceCode
358
359    let newSource = writeInlineSnapshot(
360      &recordings,
361      Context(sourceCode: sourceAfterFirstSnapshot, diffable: diffable2, fileName: "filename", lineIndex: 5)
362    ).sourceCode
363
364    assertSnapshot(source: newSource, diffable: diffable1, diffable2: diffable2)
365  }
366
367  func testUpdateSeveralSnapshotsWithLessLines() {
368    let diffable1 = """
369    NEW_SNAPSHOT
370    """
371
372    let diffable2 = "NEW_SNAPSHOT"
373
374    let source = """
375    _assertInlineSnapshot(matching: diffable, as: .lines, with: \"""
376    OLD_SNAPSHOT
377    with two lines
378    \""")
379
380    _assertInlineSnapshot(matching: diffable2, as: .lines, with: \"""
381    OLD_SNAPSHOT
382    \""")
383    """
384
385    var recordings: Recordings = [:]
386    let sourceAfterFirstSnapshot = writeInlineSnapshot(
387      &recordings,
388      Context(sourceCode: source, diffable: diffable1, fileName: "filename", lineIndex: 1)
389      ).sourceCode
390
391    let newSource = writeInlineSnapshot(
392      &recordings,
393      Context(sourceCode: sourceAfterFirstSnapshot, diffable: diffable2, fileName: "filename", lineIndex: 6)
394      ).sourceCode
395
396    assertSnapshot(source: newSource, diffable: diffable1, diffable2: diffable2)
397  }
398
399  func testUpdateSeveralSnapshotsSwapingLines1() {
400    let diffable1 = """
401    NEW_SNAPSHOT
402    with two lines
403    """
404
405    let diffable2 = """
406    NEW_SNAPSHOT
407    """
408
409    let source = """
410    _assertInlineSnapshot(matching: diffable, as: .lines, with: \"""
411    OLD_SNAPSHOT
412    \""")
413
414    _assertInlineSnapshot(matching: diffable2, as: .lines, with: \"""
415    OLD_SNAPSHOT
416    with two lines
417    \""")
418    """
419
420    var recordings: Recordings = [:]
421    let sourceAfterFirstSnapshot = writeInlineSnapshot(
422      &recordings,
423      Context(sourceCode: source, diffable: diffable1, fileName: "filename", lineIndex: 1)
424      ).sourceCode
425
426    let newSource = writeInlineSnapshot(
427      &recordings,
428      Context(sourceCode: sourceAfterFirstSnapshot, diffable: diffable2, fileName: "filename", lineIndex: 5)
429      ).sourceCode
430
431    assertSnapshot(source: newSource, diffable: diffable1, diffable2: diffable2)
432  }
433
434  func testUpdateSeveralSnapshotsSwapingLines2() {
435    let diffable1 = """
436    NEW_SNAPSHOT
437    """
438
439    let diffable2 = """
440    NEW_SNAPSHOT
441    with two lines
442    """
443
444    let source = """
445    _assertInlineSnapshot(matching: diffable, as: .lines, with: \"""
446    OLD_SNAPSHOT
447    with two lines
448    \""")
449
450    _assertInlineSnapshot(matching: diffable2, as: .lines, with: \"""
451    OLD_SNAPSHOT
452    \""")
453    """
454
455    var recordings: Recordings = [:]
456    let sourceAfterFirstSnapshot = writeInlineSnapshot(
457      &recordings,
458      Context(sourceCode: source, diffable: diffable1, fileName: "filename", lineIndex: 1)
459      ).sourceCode
460
461    let newSource = writeInlineSnapshot(
462      &recordings,
463      Context(sourceCode: sourceAfterFirstSnapshot, diffable: diffable2, fileName: "filename", lineIndex: 6)
464      ).sourceCode
465
466    assertSnapshot(source: newSource, diffable: diffable1, diffable2: diffable2)
467  }
468
469  func testUpdateSnapshotCombined1() {
470    let diffable = ##"""
471    ▿ User
472      - bio: "Blobbed around the world."
473      - id: 1
474      - name: "Bl#\"\"#obby"
475    """##
476
477    let source = ######"""
478    _assertInlineSnapshot(matching: diffable, as: .lines, with: #####"""
479    """#####)
480    """######
481
482    var recordings: Recordings = [:]
483    let newSource = writeInlineSnapshot(
484      &recordings,
485      Context(sourceCode: source, diffable: diffable, fileName: "filename", lineIndex: 1)
486      ).sourceCode
487
488    assertSnapshot(source: newSource, diffable: diffable)
489  }
490}
491
492func assertSnapshot(source: String, diffable: String, record: Bool = false, file: StaticString = #file, testName: String = #function, line: UInt = #line) {
493  let indentedDiffable = diffable.split(separator: "\n").map { "    " + $0 }.joined(separator: "\n")
494  let indentedSource = source.split(separator: "\n").map { "    " + $0 }.joined(separator: "\n")
495  let decoratedCode = ########"""
496  import XCTest
497  @testable import SnapshotTesting
498  extension InlineSnapshotsValidityTests {
499    func \########(testName) {
500      let diffable = #######"""
501  \########(indentedDiffable)
502      """#######
503
504  \########(indentedSource)
505    }
506  }
507  """########
508  assertSnapshot(matching: decoratedCode, as: .swift, record: record, file: file, testName: testName, line: line)
509}
510
511func assertSnapshot(source: String, diffable: String, diffable2: String, record: Bool = false, file: StaticString = #file, testName: String = #function, line: UInt = #line) {
512  let indentedDiffable = diffable.split(separator: "\n").map { "    " + $0 }.joined(separator: "\n")
513  let indentedDiffable2 = diffable2.split(separator: "\n").map { "    " + $0 }.joined(separator: "\n")
514  let indentedSource = source.split(separator: "\n").map { "    " + $0 }.joined(separator: "\n")
515  let decoratedCode = ########"""
516  import XCTest
517  @testable import SnapshotTesting
518  extension InlineSnapshotsValidityTests {
519    func \########(testName) {
520      let diffable = #######"""
521  \########(indentedDiffable)
522      """#######
523
524      let diffable2 = #######"""
525  \########(indentedDiffable2)
526      """#######
527
528  \########(indentedSource)
529     }
530  }
531  """########
532  assertSnapshot(matching: decoratedCode, as: .swift, record: record, file: file, testName: testName, line: line)
533}
534
535extension Snapshotting where Value == String, Format == String {
536  public static var swift: Snapshotting {
537    var snapshotting = Snapshotting(pathExtension: "txt", diffing: .lines)
538    snapshotting.pathExtension = "swift"
539    return snapshotting
540  }
541}
542
543// Class that is extended with the generated code to check that it builds.
544// Besides that, the generated code is a test itself, which tests that the
545// snapshotted value is equal to the original value.
546// With this test we check that we escaped correctly
547// e.g. if we enclose \" in """ """ instead of #""" """#,
548// the character sequence will be interpreted as " instead of \"
549// The generated tests check this issues.
550class InlineSnapshotsValidityTests: XCTestCase {}
551
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
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)