From 60b80c54186ddf46b9b2a80ce2f671b0c634cc94 Mon Sep 17 00:00:00 2001 From: PanayotCankov Date: Wed, 14 May 2014 17:26:21 +0300 Subject: [PATCH] Added some tests for file system. Improved the structure of the FileSystem help. --- Tests/file_system_tests.ts | 266 +++++++++++++++++++++++++++++-------- 1 file changed, 208 insertions(+), 58 deletions(-) diff --git a/Tests/file_system_tests.ts b/Tests/file_system_tests.ts index 4646a7839..61bdccaf2 100644 --- a/Tests/file_system_tests.ts +++ b/Tests/file_system_tests.ts @@ -3,23 +3,192 @@ // # File System // Using the file system requires the FileSystem module. // ``` JavaScript -var fs = require("filesystem"); +import fs = require("filesystem/file_system"); // ``` // The pre-required `fs` module is used throughout the following code snippets. // -var TKUnit = require("Tests/TKUnit"); +import TKUnit = require("Tests/TKUnit"); + +// +// ## Path +// + +export var testPathNormalize = function () { + // + // ### Normalize a Path + // ``` JavaScript + var documents = fs.knownFolders.documents(); + var testPath = "///test.txt"; + //// Get a normalized path such as /test.txt from ///test.txt + var normalizedPath = fs.path.normalize(documents.path + testPath); + // + var expected = documents.path + "/test.txt"; + TKUnit.assert(normalizedPath === expected); + // + // ``` + // +}; + +export var testPathJoin = function () { + // + // ### Path Join + // Concatinate a path to a file by providing multiple path arguments. + // ``` JavaScript + var documents = fs.knownFolders.documents(); + //// Generate a path like /myFiles/test.txt + var path = fs.path.join(documents.path, "myFiles", "test.txt"); + // + var expected = documents.path + "/myFiles/test.txt"; + TKUnit.assert(path === expected); + // + // ``` + // +}; + +export var testPathSeparator = function () { + // + // ### Get the Path Separator + // ``` JavaScript + //// An OS dependant path separator, "\" or "/". + var separator = fs.path.separator; + // + var expected = "/"; + TKUnit.assert(separator === expected); + // + // ``` + // +}; + +export var testFileFromPath = function () { + // + // ### Get or Create a Folder With Path + // The following example writes some text to a file created for path. + // It will create a new file or overwrite an existing file. + // ``` JavaScript + var documents = fs.knownFolders.documents(); + var path = fs.path.join(documents.path, "FileFromPath.txt"); + var file = fs.File.fromPath(path); + + //// Writing text to the file. + file.writeText("Something") + .then(function () { + //// Succeeded writing to the file. + // + file.readText() + .then(function (content) { + TKUnit.assert(content === "Something", "File read/write not working."); + file.remove(); + }) + .fail(function (error) { + console.error("Failed to read/write text"); + console.dump(error); + }); + // + }) + .fail(function (error) { + //// Failed to write to the file. + // + console.error("Failed to read/write text"); + console.dump(error); + // + }); + // ``` + // +} + +export var testFolderFromPath = function () { + // + // ### Get or Create a File With Path + // ``` JavaScript + var path = fs.path.join(fs.knownFolders.documents().path, "music"); + var folder = fs.Folder.fromPath(path); + // + TKUnit.assert(folder, "Folder.getFolder API not working."); + TKUnit.assert(fs.Folder.exists(folder.path), "Folder.getFolder API not working."); + folder.remove(); + // + // ``` + // +} // // ## Create // -// TODO: Split the "Read and Write" in "Read" and "Write" -export var testFileReadWrite = function () { - // TODO: Add description if writeText replaces the file? Does it create the file? Does it append to existing file? - +export var testFileWrite = function () { // - // ### Writing and Reading a String to a File + // ### Writing a string to a File + // The following example writes some text to a file. + // It will create a new file or overwrite an existing file. + // ``` JavaScript + var documents = fs.knownFolders.documents(); + var file = documents.getFile("Test_Write.txt"); + + //// Writing text to the file. + file.writeText("Something") + .then(function () { + //// Succeeded writing to the file. + // + file.readText() + .then(function (content) { + TKUnit.assert(content === "Something", "File read/write not working."); + file.remove(); + }) + .fail(function (error) { + console.error("Failed to read/write text"); + console.dump(error); + }); + // + }) + .fail(function (error) { + //// Failed to write to the file. + // + console.error("Failed to read/write text"); + console.dump(error); + // + }); + // ``` + // +}; + +export var testGetFile = function () { + // + // ### Get or Create a File + // ``` JavaScript + var documents = fs.knownFolders.documents(); + var file = documents.getFile("NewFileToCreate.txt"); + // + TKUnit.assert(file, "File.getFile API not working."); + TKUnit.assert(fs.File.exists(file.path), "File.getFile API not working."); + file.remove(); + // + // ``` + // +} + +export var testGetFolder = function () { + // + // ### Get or Create a Folder + // ``` JavaScript + var documents = fs.knownFolders.documents(); + var folder = documents.getFolder("NewFolderToCreate"); + // + TKUnit.assert(folder, "Folder.getFolder API not working."); + TKUnit.assert(fs.Folder.exists(folder.path), "Folder.getFolder API not working."); + folder.remove(); + // + // ``` + // +}; + +// +// ## Read +// + +export var testFileRead = function () { + // + // ### Reading from a File // The following example writes some text to a file and then reads it back. // ``` JavaScript var documents = fs.knownFolders.documents(); @@ -60,10 +229,6 @@ export var testFileReadWrite = function () { // }; -// -// ## Read -// - export var testGetKnownFolders = function () { // // ### Getting the Known Folders @@ -72,13 +237,13 @@ export var testGetKnownFolders = function () { //// Getting the application's 'documents' folder. var documents = fs.knownFolders.documents(); // - TKUnit.assert(documents, "Could not retrieve the Documents known folder."); + TKUnit.assert(documents, "Could not retrieve the Documents known folder."); TKUnit.assert(documents.isKnown, "The Documents folder should have its isKnown property set to true."); // //// Getting the application's 'temp' folder. var temp = fs.knownFolders.temp(); // - TKUnit.assert(temp, "Could not retrieve the Temporary known folder."); + TKUnit.assert(temp, "Could not retrieve the Temporary known folder."); TKUnit.assert(temp.isKnown, "The Temporary folder should have its isKnown property set to true."); // // ``` @@ -158,6 +323,8 @@ export var testEnumEntities = function () { // documents.eachEntity(function (entity) { console.log(entity.name); + //// Return true to continue, or return false to stop the itteration. + return true; }); // TKUnit.assert(fileFound, "Failed to enumerate Test.txt"); @@ -170,23 +337,6 @@ export var testEnumEntities = function () { // }; -// TODO: testGetFile - -export var testGetFolder = function () { - // - // ### Getting Folder by Name - // ``` JavaScript - var documents = fs.knownFolders.documents(); - var myFolder = documents.getFolder("Tests__"); - // - TKUnit.assert(myFolder, "Folder.getFolder API not working."); - TKUnit.assert(fs.Folder.exists(myFolder.path), "Folder.getFolder API not working."); - myFolder.remove(); - // - // ``` - // -}; - export var testGetParent = function () { // // ### Getting Parent Folder @@ -194,7 +344,7 @@ export var testGetParent = function () { var documents = fs.knownFolders.documents(); var file = documents.getFile("Test.txt"); // - TKUnit.assert(file, "Failed to create file in the Documents folder."); + TKUnit.assert(file, "Failed to create file in the Documents folder."); // //// The parent folder of the file would be the documents folder. var parent = file.getParent(); @@ -217,8 +367,8 @@ export var testFileNameExtension = function () { //// Getting the file extension ".txt". var fileExtension = file.extension; // - TKUnit.assert(fileName, "Test.txt"); - TKUnit.assert(fileExtension, ".txt"); + TKUnit.assert(fileName === "Test.txt", "Wrong file name."); + TKUnit.assert(fileExtension === ".txt", "Wrong extension."); file.remove(); // // ``` @@ -335,7 +485,7 @@ export var testFileRemove = function () { // To 'delete', 'remove' or 'unlink' a file use the file's remove method: // ``` JavaScript var documents = fs.knownFolders.documents(); - var file = documents.getFile("Test.txt"); + var file = documents.getFile("AFileToRemove.txt"); file.remove() .then(function (result) { //// Success removing the file. @@ -353,6 +503,30 @@ export var testFileRemove = function () { // }; +export var testFolderRemove = function () { + // + // ### Removing a Folder + // ``` JavaScript + var documents = fs.knownFolders.documents(); + var file = documents.getFolder("AFolderToRemove"); + //// Remove a folder and recursively its content. + file.remove() + .then(function (result) { + //// Success removing the folder. + // + TKUnit.assert(!fs.File.exists(file.path)); + // + }) + .fail(function (error) { + //// Failed to remove the folder. + // + TKUnit.assert(false, "File.remove API not working."); + // + }); + // ``` + // +} + export var testFolderClear = function () { // // ### Clearing the Contents of a Folder @@ -389,8 +563,6 @@ export var testFolderClear = function () { // }; -// TODO: Removing a folder. - // misc export var testKnownFolderRename = function () { @@ -404,25 +576,3 @@ export var testKnownFolderRename = function () { TKUnit.assert(true); }); }; - -export var testPathNormalize = function () { - var folder = fs.knownFolders.documents(); - var testPath = "///test.txt"; - var normalizedPath = fs.path.normalize(folder.path + testPath); - var expected = folder.path + "/test.txt"; - TKUnit.assert(normalizedPath === expected); -}; - -export var testPathJoin = function () { - var folder = fs.knownFolders.documents(); - var path = fs.path.join(folder.path, "myFiles", "test.txt"); - var expected = folder.path + "/myFiles/test.txt"; - TKUnit.assert(path === expected); -}; - -export var testPathSeparator = function () { - var separator = fs.path.separator; - var expected = "/"; - TKUnit.assert(separator === expected); -}; - \ No newline at end of file