@@ -16,6 +16,102 @@ import JExtractSwift
1616import Testing
1717import struct Foundation. CharacterSet
1818
19+ func assertOutput(
20+ dump: Bool = false ,
21+ _ translator: Swift2JavaTranslator ,
22+ input: String ,
23+ detectChunkByInitialLines: Int = 4 ,
24+ expectedChunks: [ String ] ,
25+ fileID: String = #fileID,
26+ filePath: String = #filePath,
27+ line: Int = #line,
28+ column: Int = #column
29+ ) {
30+ try ! translator. analyze ( swiftInterfacePath: " /fake/Fake.swiftinterface " , text: input)
31+
32+ let output = CodePrinter . toString { printer in
33+ for ty in translator. importedTypes. values {
34+ translator. printImportedNominalType ( & printer, ty)
35+ printer. print ( " \n " )
36+ printer. print ( " // ====================================================================== " )
37+ printer. print ( " // ====================================================================== " )
38+ printer. print ( " \n " )
39+ }
40+ translator. printModule ( & printer)
41+ }
42+
43+ let gotLines = output. split ( separator: " \n " )
44+ for expected in expectedChunks {
45+ let expectedLines = expected. split ( separator: " \n " )
46+
47+ var matchingOutputOffset = 0
48+ let expectedInitialMatchingLines = expectedLines [ 0 ..< detectChunkByInitialLines]
49+ . map ( { $0. trimmingCharacters ( in: . whitespacesAndNewlines) } )
50+ . joined ( separator: " \n " )
51+ for offset in 0 ..< gotLines. count where gotLines. count > ( offset+ detectChunkByInitialLines) {
52+ let textLinesAtOffset = gotLines [ offset..< offset+ detectChunkByInitialLines]
53+ . map ( { $0. trimmingCharacters ( in: . whitespacesAndNewlines) } )
54+ . joined ( separator: " \n " )
55+
56+ // print("CHECKING GOT@\(offset)->\(offset + detectChunkByInitialLines) ==========")
57+ // print(textLinesAtOffset)
58+ // print("AGAINST EXPECTED@\(0)->\(expectedInitialMatchingLines) ==========")
59+ // print(expectedInitialMatchingLines)
60+ // print("")
61+ // print("")
62+
63+ if textLinesAtOffset == expectedInitialMatchingLines {
64+ matchingOutputOffset = offset
65+ // print("MATCHED \(matchingOutputOffset)")
66+ break
67+ }
68+ }
69+
70+ var diffLineNumbers : [ Int ] = [ ]
71+
72+ for (no, ( g, e) ) in zip ( gotLines. dropFirst ( matchingOutputOffset) , expectedLines) . enumerated ( ) {
73+ if g. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines) . count == 0
74+ || e. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines) . count == 0 {
75+ continue
76+ }
77+
78+ let ge = g. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
79+ let ee = e. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
80+ if ge. commonPrefix ( with: ee) != ee {
81+ diffLineNumbers. append ( no + matchingOutputOffset)
82+
83+ let sourceLocation = SourceLocation (
84+ fileID: fileID, filePath: filePath, line: line, column: column)
85+ #expect( ge == ee, sourceLocation: sourceLocation)
86+ }
87+ }
88+
89+ let hasDiff = diffLineNumbers. count > 0
90+ if hasDiff || dump {
91+ print ( " " )
92+ if hasDiff {
93+ print ( " error: Number of not matching lines: \( diffLineNumbers. count) ! " . red)
94+
95+ print ( " ==== --------------------------------------------------------------- " )
96+ print ( " Expected output: " )
97+ for (n, e) in expectedLines. enumerated ( ) {
98+ print ( " \( n) : \( e) " . yellow ( if: diffLineNumbers. map ( { $0 - matchingOutputOffset} ) . contains ( n) ) )
99+ }
100+ }
101+
102+ print ( " ==== --------------------------------------------------------------- " )
103+ print ( " Got output: " )
104+ let surroundingContext = 5
105+ let printFromLineNo = matchingOutputOffset
106+ let printToLineNo = matchingOutputOffset + expectedLines. count
107+ for (n, g) in gotLines. enumerated ( ) where n >= printFromLineNo && n <= printToLineNo {
108+ print ( " \( n) : \( g) " . red ( if: diffLineNumbers. contains ( n) ) )
109+ }
110+ print ( " ==== --------------------------------------------------------------- \n " )
111+ }
112+ }
113+ }
114+
19115func assertOutput(
20116 dump: Bool = false ,
21117 _ got: String ,
@@ -32,21 +128,13 @@ func assertOutput(
32128
33129 for (no, ( g, e) ) in zip ( gotLines, expectedLines) . enumerated ( ) {
34130 if g. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines) . count == 0
35- || e. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines) . count == 0
36- {
131+ || e. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines) . count == 0 {
37132 continue
38133 }
39134
40135 let ge = g. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
41136 let ee = e. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
42137 if ge. commonPrefix ( with: ee) != ee {
43- // print("")
44- // print("[\(file):\(line)] " + "Difference found on line: \(no + 1)!".red)
45- // print("Expected @ \(file):\(Int(line) + no + 3 /*formatting*/ + 1):")
46- // print(e.yellow)
47- // print("Got instead:")
48- // print(g.red)
49-
50138 diffLineNumbers. append ( no)
51139
52140 let sourceLocation = SourceLocation (
@@ -57,7 +145,7 @@ func assertOutput(
57145 }
58146
59147 let hasDiff = diffLineNumbers. count > 0
60- if hasDiff || dump{
148+ if hasDiff || dump {
61149 print ( " " )
62150 if hasDiff {
63151 print ( " error: Number of not matching lines: \( diffLineNumbers. count) ! " . red)
0 commit comments