Skip to content

Commit fa32d7e

Browse files
committed
🔨 Added some additional tests, including write tests
1 parent e6d3b50 commit fa32d7e

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

LSFileWrapperTests/LSFileWrapperTests.swift

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,51 @@ class LSFileWrapperTests: XCTestCase {
2828
XCTAssertEqual(fileWrapper.string(), "Hello World!")
2929
}
3030

31+
func testUpdateDataContent() {
32+
// This is a test case for testing update(newContent:) method
33+
let fileWrapper = LSFileWrapper(file: ())
34+
fileWrapper.update(newContent: Data("Hello World!".utf8) as NSData)
35+
36+
XCTAssertNotNil(fileWrapper.data())
37+
XCTAssertEqual(fileWrapper.data(), Data("Hello World!".utf8))
38+
XCTAssertEqual(fileWrapper.string(), "Hello World!") // This sets the type
39+
XCTAssertNil(fileWrapper.data()) // So here data() should be nil
40+
}
41+
42+
func testUpdateDictionaryContent() {
43+
// This is a test case for testing update(newContent:) method
44+
let testDict = ["Name": "LSFileWrapper", "Platforms": "macOS, iOS", "Version": "2"]
45+
let fileWrapper = LSFileWrapper(file: ())
46+
fileWrapper.update(newContent: testDict as NSDictionary)
47+
48+
XCTAssertNotNil(fileWrapper.dictionary())
49+
XCTAssertEqual(fileWrapper.dictionary() as? [String: String], testDict)
50+
}
51+
52+
func testUpdateAnyDictionaryContent() {
53+
// This is a test case for testing update(newContent:) method
54+
let testDict = ["Name": "LSFileWrapper", "Platforms": ["macOS", "iOS"], "Version": 2] as [String : Any]
55+
let fileWrapper = LSFileWrapper(file: ())
56+
fileWrapper.update(newContent: testDict as NSDictionary)
57+
58+
XCTAssertNotNil(fileWrapper.dictionary())
59+
let dict = fileWrapper.dictionary() as? [String: Any]
60+
XCTAssertNotNil(dict)
61+
XCTAssertEqual(dict?["Name"] as? String, (testDict["Name"] as! String))
62+
XCTAssertEqual(dict?["Version"] as? Int, (testDict["Version"] as! Int))
63+
XCTAssertEqual(dict?["Platforms"] as? [String], (testDict["Platforms"] as! [String]))
64+
}
65+
66+
func testUpdateImageContent() {
67+
// This is a test case for testing update(newContent:) method
68+
let testImg = NSImage(named: "NSApplicationIcon") ?? NSImage()
69+
let fileWrapper = LSFileWrapper(file: ())
70+
fileWrapper.update(newContent: testImg)
71+
72+
XCTAssertNotNil(fileWrapper.image())
73+
XCTAssertEqual(fileWrapper.image()?.size, testImg.size) // More or less enough
74+
}
75+
3176
func testAddContent() {
3277
// This is a test case for testing add(content: withFilename:) method
3378
let wrapper = LSFileWrapper(directory: ())
@@ -94,4 +139,116 @@ class LSFileWrapperTests: XCTestCase {
94139

95140
XCTAssertEqual(wrapperNames.sorted(by: { $1 ?? "" < $0 ?? "" }), ["hello.txt", "directory"])
96141
}
142+
143+
func testWriteToURL() throws {
144+
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("testWrite.package")
145+
let testDict = ["Name": "LSFileWrapper", "Platforms": ["macOS", "iOS"], "Version": 2] as [String : Any]
146+
let testImg = NSImage(named: "NSApplicationIcon") ?? NSImage()
147+
let wrapper = LSFileWrapper(directory: ())
148+
wrapper.add(content: "Hello World!" as NSString, withFilename: "hello.txt")
149+
wrapper.add(content: testDict as NSDictionary, withFilename: "hello.plist")
150+
wrapper.add(content: testImg, withFilename: "hello.jpeg")
151+
wrapper.add(wrapper: LSFileWrapper(directory: ()), withFilename: "directory")
152+
153+
try wrapper.write(to: url)
154+
155+
let writtenWrapper = LSFileWrapper(url: url, isDirectory: true)
156+
157+
XCTAssertNotNil(writtenWrapper)
158+
XCTAssert(writtenWrapper?.isDirectory == true)
159+
160+
XCTAssertNotNil(writtenWrapper?.wrapper(with: "hello.txt"))
161+
XCTAssert(writtenWrapper?.wrapper(with: "hello.txt")?.isDirectory == false)
162+
XCTAssertNotNil(writtenWrapper?.wrapper(with: "hello.plist"))
163+
XCTAssert(writtenWrapper?.wrapper(with: "hello.plist")?.isDirectory == false)
164+
XCTAssertNotNil(writtenWrapper?.wrapper(with: "hello.jpeg"))
165+
XCTAssert(writtenWrapper?.wrapper(with: "hello.jpeg")?.isDirectory == false)
166+
XCTAssertNotNil(writtenWrapper?.wrapper(with: "directory"))
167+
XCTAssert(writtenWrapper?.wrapper(with: "directory")?.isDirectory == true)
168+
169+
XCTAssertEqual(writtenWrapper?.wrapper(with: "hello.txt")?.string(), "Hello World!")
170+
XCTAssertEqual(writtenWrapper?.wrapper(with: "hello.jpeg")?.image()?.size, testImg.size) // More or less enough
171+
172+
let dict = writtenWrapper?.wrapper(with: "hello.plist")?.dictionary() as? [String: Any]
173+
XCTAssertNotNil(dict)
174+
XCTAssertEqual(dict?["Name"] as? String, (testDict["Name"] as! String))
175+
XCTAssertEqual(dict?["Version"] as? Int, (testDict["Version"] as! Int))
176+
XCTAssertEqual(dict?["Platforms"] as? [String], (testDict["Platforms"] as! [String]))
177+
178+
try FileManager.default.removeItem(at: url)
179+
}
180+
181+
func testWriteFileToURL() throws {
182+
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("hello.txt")
183+
let fileWrapper = LSFileWrapper(file: ())
184+
fileWrapper.update(newContent: "Hello World!" as NSString)
185+
186+
try fileWrapper.write(to: url)
187+
188+
let writtenFileWrapper = LSFileWrapper(url: url, isDirectory: false)
189+
190+
XCTAssertNotNil(writtenFileWrapper)
191+
192+
XCTAssert(writtenFileWrapper?.isDirectory == false)
193+
194+
XCTAssertEqual(writtenFileWrapper?.data(), Data("Hello World!".utf8))
195+
XCTAssertEqual(writtenFileWrapper?.string(), "Hello World!") // This sets the type
196+
XCTAssertNil(writtenFileWrapper?.data()) // So here data() should be nil
197+
198+
try FileManager.default.removeItem(at: url)
199+
}
200+
201+
func testInitWithContentsAutomaticDirDiscovery() throws {
202+
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("testAutoDir.package")
203+
let wrapper = LSFileWrapper(directory: ())
204+
wrapper.add(content: "Hello World!" as NSString, withFilename: "hello.txt")
205+
206+
try wrapper.write(to: url)
207+
208+
let writtenWrapper = LSFileWrapper(url: url, isDirectory: false)
209+
210+
XCTAssertNotNil(writtenWrapper)
211+
212+
XCTAssert(writtenWrapper?.isDirectory == true)
213+
214+
XCTAssertNotNil(writtenWrapper?.wrapper(with: "hello.txt"))
215+
XCTAssert(writtenWrapper?.wrapper(with: "hello.txt")?.isDirectory == false)
216+
217+
try FileManager.default.removeItem(at: url)
218+
}
219+
220+
func testWriteUpdatesToURL() throws {
221+
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("testWriteUpdates.package")
222+
let wrapper = LSFileWrapper(directory: ())
223+
wrapper.add(content: "Hello World!" as NSString, withFilename: "hello.txt")
224+
225+
try wrapper.write(to: url)
226+
227+
let writtenWrapper = LSFileWrapper(url: url, isDirectory: true)
228+
229+
XCTAssertNotNil(writtenWrapper)
230+
XCTAssert(writtenWrapper?.isDirectory == true)
231+
232+
XCTAssertNotNil(writtenWrapper?.wrapper(with: "hello.txt"))
233+
XCTAssert(writtenWrapper?.wrapper(with: "hello.txt")?.isDirectory == false)
234+
235+
XCTAssertEqual(writtenWrapper?.wrapper(with: "hello.txt")?.string(), "Hello World!")
236+
237+
wrapper.wrapper(with: "hello.txt")?.update(newContent: "Hello Updated World!" as NSString)
238+
XCTAssertEqual(wrapper.wrapper(with: "hello.txt")?.string(), "Hello Updated World!")
239+
240+
try wrapper.writeUpdates(to: url)
241+
242+
let updatedWrapper = LSFileWrapper(url: url, isDirectory: true)
243+
244+
XCTAssertNotNil(updatedWrapper)
245+
XCTAssert(updatedWrapper?.isDirectory == true)
246+
247+
XCTAssertNotNil(updatedWrapper?.wrapper(with: "hello.txt"))
248+
XCTAssert(updatedWrapper?.wrapper(with: "hello.txt")?.isDirectory == false)
249+
250+
XCTAssertEqual(updatedWrapper?.wrapper(with: "hello.txt")?.string(), "Hello Updated World!")
251+
252+
try FileManager.default.removeItem(at: url)
253+
}
97254
}

0 commit comments

Comments
 (0)