@@ -30,6 +30,7 @@ class TestNSDictionary : XCTestCase {
30
30
( " test_equality " , test_equality) ,
31
31
( " test_copying " , test_copying) ,
32
32
( " test_mutableCopying " , test_mutableCopying) ,
33
+ ( " test_writeToFile " , test_writeToFile) ,
33
34
]
34
35
}
35
36
@@ -162,4 +163,52 @@ class TestNSDictionary : XCTestCase {
162
163
XCTAssertTrue ( dictMutableCopy2 == dictMutableCopy1)
163
164
}
164
165
166
+ func test_writeToFile( ) {
167
+ let testFilePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) )
168
+ if let _ = testFilePath {
169
+ let d1 : NSDictionary = [ " foo " : " bar " , " baz " : " qux " ]
170
+ let isWritten = d1. write ( toFile: testFilePath!, atomically: true )
171
+ if ( isWritten) {
172
+ do {
173
+ let plistDoc = try XMLDocument ( contentsOf: URL ( fileURLWithPath: testFilePath!, isDirectory: false ) , options: [ ] )
174
+ try plistDoc. validate ( )
175
+ XCTAssert ( plistDoc. rootElement ( ) ? . name == " plist " )
176
+ let plist = try PropertyListSerialization . propertyList ( from: plistDoc. xmlData, options: [ ] , format: nil ) as! [ String : Any ]
177
+ XCTAssert ( ( plist [ " foo " ] as? String ) == d1 [ " foo " ] as? String )
178
+ XCTAssert ( ( plist [ " baz " ] as? String ) == d1 [ " baz " ] as? String )
179
+ } catch {
180
+ XCTFail ( " XMLDocument failes to read / validate contenets " )
181
+ }
182
+ } else {
183
+ XCTFail ( " Write to file failed " )
184
+ }
185
+ removeTestFile ( testFilePath!)
186
+ } else {
187
+ XCTFail ( " Temporary file creation failed " )
188
+ }
189
+ }
190
+
191
+ private func createTestFile( _ path: String , _contents: Data ) -> String ? {
192
+ let tempDir = " /tmp/TestFoundation_Playground_ " + NSUUID( ) . uuidString + " / "
193
+ do {
194
+ try FileManager . default. createDirectory ( atPath: tempDir, withIntermediateDirectories: false , attributes: nil )
195
+ if FileManager . default. createFile ( atPath: tempDir + " / " + path, contents: _contents,
196
+ attributes: nil ) {
197
+ return tempDir + path
198
+ } else {
199
+ return nil
200
+ }
201
+ } catch _ {
202
+ return nil
203
+ }
204
+ }
205
+
206
+ private func removeTestFile( _ location: String ) {
207
+ do {
208
+ try FileManager . default. removeItem ( atPath: location)
209
+ } catch _ {
210
+
211
+ }
212
+ }
213
+
165
214
}
0 commit comments