import XCTest
import SwiftSyntax
import SwiftParser

public class ParserTests: XCTestCase {
  func testSelfParse() throws {
    let currentDir = URL(fileURLWithPath: #file)
      .deletingLastPathComponent()
      .deletingLastPathComponent()
      .deletingLastPathComponent()
      .appendingPathComponent("Sources")
    let fileURLs = FileManager.default
      .enumerator(at: currentDir, includingPropertiesForKeys: nil)!
      .compactMap({ $0 as? URL })
      .filter({$0.pathExtension == "swift"})
    for fileURL in fileURLs {
      XCTAssertNoThrow(try {
        let fileContents = try String(contentsOf: fileURL)
        let parsed = try Parser.parse(source: fileContents)
        AssertStringsEqualWithDiff("\(parsed)", fileContents)
        let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: parsed)
        if !diagnostics.isEmpty {
          var locationAndDiagnostics: [String] = []
          let locationConverter = SourceLocationConverter(file: fileURL.lastPathComponent, tree: parsed)
          for diag in diagnostics {
            let location = diag.location(converter: locationConverter)
            let message = diag.message
            locationAndDiagnostics.append("\(location): \(message)")
          }
          XCTFail("""
          Received the following diagnostics while parsing \(fileURL)
          \(locationAndDiagnostics.joined(separator: "\n"))
          """)
        }
      }())
    }
  }
}

import XCTest
import SwiftSyntax
import SwiftParser

public class ParserTests: XCTestCase {
  func testSelfParse() throws {
    let currentDir = URL(fileURLWithPath: #file)
      .deletingLastPathComponent()
      .deletingLastPathComponent()
      .deletingLastPathComponent()
      .appendingPathComponent("Sources")
    let fileURLs = FileManager.default
      .enumerator(at: currentDir, includingPropertiesForKeys: nil)!
      .compactMap({ $0 as? URL })
      .filter({$0.pathExtension == "swift"})
    for fileURL in fileURLs {
      XCTAssertNoThrow(try {
        let fileContents = try String(contentsOf: fileURL)
        let parsed = try Parser.parse(source: fileContents)
        AssertStringsEqualWithDiff("\(parsed)", fileContents)
      }())
    }
  }
}

--- 
+++ 
@@ -18,6 +18,20 @@
         let fileContents = try String(contentsOf: fileURL)
         let parsed = try Parser.parse(source: fileContents)
         AssertStringsEqualWithDiff("\(parsed)", fileContents)
+        let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: parsed)
+        if !diagnostics.isEmpty {
+          var locationAndDiagnostics: [String] = []
+          let locationConverter = SourceLocationConverter(file: fileURL.lastPathComponent, tree: parsed)
+          for diag in diagnostics {
+            let location = diag.location(converter: locationConverter)
+            let message = diag.message
+            locationAndDiagnostics.append("\(location): \(message)")
+          }
+          XCTFail("""
+          Received the following diagnostics while parsing \(fileURL)
+          \(locationAndDiagnostics.joined(separator: "\n"))
+          """)
+        }
       }())
     }
   }
Check that self-parsing does not produce any diagnostics
