Skip to content

Commit bc9d74b

Browse files
committed
Add Tests!
1 parent a229e28 commit bc9d74b

File tree

4 files changed

+151
-63
lines changed

4 files changed

+151
-63
lines changed

CodeEditTests/Features/Documents/CodeFileDocument+UTTypeTests.swift renamed to CodeEditTests/Features/CodeFile/CodeFileDocument+UTTypeTests.swift

File renamed without changes.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

CodeEditTests/Features/CodeFile/CodeFileTests.swift

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// withTempDir.swift
3+
// CodeEditTests
4+
//
5+
// Created by Khan Winter on 7/3/25.
6+
//
7+
8+
import Foundation
9+
10+
func withTempDir(_ test: (URL) async throws -> Void) async throws {
11+
let tempDirURL = try createAndClearDir()
12+
do {
13+
try await test(tempDirURL)
14+
} catch {
15+
try clearDir(tempDirURL)
16+
throw error
17+
}
18+
try clearDir(tempDirURL)
19+
}
20+
21+
func withTempDir(_ test: (URL) throws -> Void) throws {
22+
let tempDirURL = try createAndClearDir()
23+
do {
24+
try test(tempDirURL)
25+
} catch {
26+
try clearDir(tempDirURL)
27+
throw error
28+
}
29+
try clearDir(tempDirURL)
30+
}
31+
32+
private func createAndClearDir() throws -> URL {
33+
let tempDirURL = FileManager.default.temporaryDirectory
34+
.appending(path: "CodeEditTestDirectory", directoryHint: .isDirectory)
35+
36+
// If it exists, delete it before the test
37+
try clearDir(tempDirURL)
38+
39+
try FileManager.default.createDirectory(at: tempDirURL, withIntermediateDirectories: true)
40+
41+
return tempDirURL
42+
}
43+
44+
private func clearDir(_ url: URL) throws {
45+
if FileManager.default.fileExists(atPath: url.absoluteURL.path(percentEncoded: false)) {
46+
try FileManager.default.removeItem(at: url)
47+
}
48+
}

0 commit comments

Comments
 (0)