Skip to content

Commit

Permalink
Makes FileSystemSequence CustomStringConvertible to print all fil…
Browse files Browse the repository at this point in the history
…es and subfolders of a `Folder`
  • Loading branch information
gwikiera committed Jan 24, 2018
1 parent 9d25d1c commit 8d30d00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public protocol FileSystemIterable {
* You don't create instances of this class yourself. Instead, you can access various sequences on a `Folder`, for example
* containing its files and subfolders. The sequence is lazily evaluated when you perform operations on it.
*/
public class FileSystemSequence<T: FileSystem.Item>: Sequence where T: FileSystemIterable {
public class FileSystemSequence<T: FileSystem.Item>: Sequence, CustomStringConvertible where T: FileSystemIterable {
/// The number of items contained in this sequence. Accessing this causes the sequence to be evaluated.
public var count: Int {
var count = 0
Expand Down Expand Up @@ -896,6 +896,10 @@ public class FileSystemSequence<T: FileSystem.Item>: Sequence where T: FileSyste
public func move(to newParent: Folder) throws {
try forEach { try $0.move(to: newParent) }
}

public var description: String {
return map { $0.description }.joined(separator: "\n")
}
}

/// Iterator used to iterate over an instance of `FileSystemSequence`
Expand Down
18 changes: 18 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,22 @@ class FilesTests: XCTestCase {
}
}

func testFilesDescription() {
performTest {
let fileA = try folder.createFile(named: "fileA")
let fileB = try folder.createFile(named: "fileB")
XCTAssertEqual(folder.files.description, "\(fileA.description)\n\(fileB.description)")
}
}

func testSubfoldersDescription() {
performTest {
let folderA = try folder.createSubfolder(named: "folderA")
let folderB = try folder.createSubfolder(named: "folderB")
XCTAssertEqual(folder.subfolders.description, "\(folderA.description)\n\(folderB.description)")
}
}

func testMovingFolderContents() {
performTest {
let parentFolder = try folder.createSubfolder(named: "parentA")
Expand Down Expand Up @@ -719,6 +735,8 @@ class FilesTests: XCTestCase {
("testWritingStringToFile", testWritingStringToFile),
("testFileDescription", testFileDescription),
("testFolderDescription", testFolderDescription),
("testFilesDescription", testFilesDescription),
("testSubfoldersDescription", testSubfoldersDescription),
("testMovingFolderContents", testMovingFolderContents),
("testMovingFolderHiddenContents", testMovingFolderHiddenContents),
("testAccessingHomeFolder", testAccessingHomeFolder),
Expand Down

0 comments on commit 8d30d00

Please sign in to comment.