|
| 1 | +// |
| 2 | +// CodeFileDocumentTests.swift |
| 3 | +// CodeEditModules/CodeFileTests |
| 4 | +// |
| 5 | +// Created by Marco Carnevali on 18/03/22. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import SwiftUI |
| 10 | +import Testing |
| 11 | +@testable import CodeEdit |
| 12 | + |
| 13 | +@Suite(.serialized) |
| 14 | +struct CodeFileDocumentTests { |
| 15 | + let defaultString = "func test() { }" |
| 16 | + |
| 17 | + private func withFile(_ operation: (URL) throws -> Void) throws { |
| 18 | + try withTempDir { dir in |
| 19 | + let fileURL = dir.appending(path: "file.swift") |
| 20 | + try operation(fileURL) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + private func withCodeFile(_ operation: (CodeFileDocument) throws -> Void) throws { |
| 25 | + try withFile { fileURL in |
| 26 | + try defaultString.write(to: fileURL, atomically: true, encoding: .utf8) |
| 27 | + let codeFile = try CodeFileDocument(contentsOf: fileURL, ofType: "public.source-code") |
| 28 | + try operation(codeFile) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + func testLoadUTF8Encoding() throws { |
| 34 | + try withFile { fileURL in |
| 35 | + try defaultString.write(to: fileURL, atomically: true, encoding: .utf8) |
| 36 | + let codeFile = try CodeFileDocument( |
| 37 | + for: fileURL, |
| 38 | + withContentsOf: fileURL, |
| 39 | + ofType: "public.source-code" |
| 40 | + ) |
| 41 | + #expect(codeFile.content?.string == defaultString) |
| 42 | + #expect(codeFile.sourceEncoding == .utf8) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + func testWriteUTF8Encoding() throws { |
| 48 | + try withFile { fileURL in |
| 49 | + let codeFile = CodeFileDocument() |
| 50 | + codeFile.content = NSTextStorage(string: defaultString) |
| 51 | + codeFile.sourceEncoding = .utf8 |
| 52 | + try codeFile.write(to: fileURL, ofType: "public.source-code") |
| 53 | + |
| 54 | + let data = try Data(contentsOf: fileURL) |
| 55 | + var nsString: NSString? |
| 56 | + let fileEncoding = NSString.stringEncoding( |
| 57 | + for: data, |
| 58 | + encodingOptions: [ |
| 59 | + .suggestedEncodingsKey: FileEncoding.allCases.map { $0.nsValue }, |
| 60 | + .useOnlySuggestedEncodingsKey: true |
| 61 | + ], |
| 62 | + convertedString: &nsString, |
| 63 | + usedLossyConversion: nil |
| 64 | + ) |
| 65 | + |
| 66 | + #expect(codeFile.content?.string as NSString? == nsString) |
| 67 | + #expect(fileEncoding == NSUTF8StringEncoding) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + func ignoresExternalUpdatesWithOutstandingChanges() throws { |
| 73 | + try withCodeFile { codeFile in |
| 74 | + // Mark the file dirty |
| 75 | + codeFile.updateChangeCount(.changeDone) |
| 76 | + |
| 77 | + // Update the modification date |
| 78 | + try "different contents".write(to: codeFile.fileURL!, atomically: true, encoding: .utf8) |
| 79 | + |
| 80 | + // Tell the file the disk representation changed |
| 81 | + codeFile.presentedItemDidChange() |
| 82 | + |
| 83 | + // The file should not have reloaded |
| 84 | + #expect(codeFile.content?.string == defaultString) |
| 85 | + #expect(codeFile.isDocumentEdited == true) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + func loadsExternalUpdatesWithNoOutstandingChanges() throws { |
| 91 | + try withCodeFile { codeFile in |
| 92 | + // Update the modification date |
| 93 | + try "different contents".write(to: codeFile.fileURL!, atomically: true, encoding: .utf8) |
| 94 | + |
| 95 | + // Tell the file the disk representation changed |
| 96 | + codeFile.presentedItemDidChange() |
| 97 | + |
| 98 | + // The file should have reloaded (it was clean) |
| 99 | + #expect(codeFile.content?.string == "different contents") |
| 100 | + #expect(codeFile.isDocumentEdited == false) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments