예제 #1
0
 public void Size_IncreseElementSize_ParentSizeAlsoIncreased()
 {
   var folderElement = new FolderElement("root");
   var fileElement1 = new FileElement("file1.txt", 10);
   var fileElement2 = new FileElement("file2.txt", 50);
   folderElement.Add(fileElement1);
   folderElement.Add(fileElement2);
   // change the size from 10 to 100
   fileElement1.Size = 100;
   Assert.AreEqual(150, folderElement.Size);
 }
    private static FileElement AddNewFile(FolderElement folderElement, string fileName, int fileSize)
    {
      var fileElement = new FileElement(fileName, fileSize);
      folderElement.Add(fileElement);

      return fileElement;
    }
    // Initialize the directories as below
    // - root
    //    - root-file1.text
    //    - root-file2.text
    //    - sub
    //        - sub file1.txt
    //        - sub file2.txt
    private void InitTheElementsList()
    {
      var rootFolder = new FolderElement("root");
      var rootFile1 = new FileElement("root-file1.text", 100);
      var rootFile2 = new FileElement("root-file1.text", 100);
      var subFolder = new FolderElement("sub");
      var subFile1 = new FileElement("sub file1.txt", 300);
      var subFile2 = new FileElement("sub file2.txt", 400);
      
      rootFolder.Add(rootFile1);
      rootFolder.Add(rootFile2);
      rootFolder.Add(subFolder);

      subFolder.Add(subFile1);
      subFolder.Add(subFile2);

      _SizeCounterViewModel.ElementList.Add(rootFolder);
    }
    public void Children_AddFileTo2LevelFoldersAndThenRemoveTheSubLevelFolder_TheChildrenAreUpdated()
    {
      var rootFolder = new FolderElement("root");
      var rootFile = AddNewFile(rootFolder, "rootFile", 100);

      var leafLevel1Folder = new FolderElement("Leaf_Level_1");
      var leafLevel2Folder = new FolderElement("Leaf_Level_2");
      rootFolder.Add(leafLevel1Folder);
      leafLevel1Folder.Add(leafLevel2Folder);

      AddNewFile(leafLevel2Folder, "leaf2", 1000);

      // act
      rootFolder.Remove(leafLevel1Folder);

      Assert.AreEqual(1, rootFolder.Children.Count);
      Assert.AreSame(rootFile, rootFolder.Children[0]);
    }
    public void Children_AddFileTo2LevelFolders_TheChildrenAreAllRight()
    {
      var rootFolder = new FolderElement("root");
      var rootFile = AddNewFile(rootFolder, "rootFile", 100);

      var leafLevel1Folder = new FolderElement("Leaf_Level_1");
      var leafLevel2Folder = new FolderElement("Leaf_Level_2");
      rootFolder.Add(leafLevel1Folder);
      leafLevel1Folder.Add(leafLevel2Folder);

      // act
      var actualChildrenCount = rootFolder.Children.Count;

      Assert.AreEqual(2, actualChildrenCount);
      Assert.AreSame(rootFile, rootFolder.Children[0]);
      Assert.AreSame(leafLevel1Folder, rootFolder.Children[1]);

      Assert.AreEqual(1, leafLevel1Folder.Children.Count);
      Assert.AreSame(leafLevel2Folder, leafLevel1Folder.Children[0]);
    }
    public void Parent_AddFolderBToFolderA_AShouldBeParentOfB()
    {
      // arrange
      var rootFolder = new FolderElement("root");
      rootFolder.Add(_FolderElementTested);

      Assert.AreSame(rootFolder, _FolderElementTested.Parent);
    }
    public void Remove_AddFileTo2LevelFoldersAndThenRemoveTheSubLevelFolder_TheRootFolderSizeIsCorrect()
    {
      var rootFolder = new FolderElement("root");
      AddNewFile(rootFolder, "rootFile", 100);

      var leafLevel1Folder = new FolderElement("Leaf_Level_1");
      var leafLevel2Folder = new FolderElement("Leaf_Level_2");
      rootFolder.Add(leafLevel1Folder);
      leafLevel1Folder.Add(leafLevel2Folder);

      AddNewFile(leafLevel2Folder, "leaf2", 1000);

      // act
      rootFolder.Remove(leafLevel1Folder);

      Assert.AreEqual(100, rootFolder.Size);
    }
    public void Add_AddFileTo2LevelFolders_TheRootFolderSizeIsCorrect()
    {
      var rootFolder = new FolderElement("root");
      AddNewFile(rootFolder, 100);

      var leafLevel1Folder = new FolderElement("Leaf_Level_1");
      var leafLevel2Folder = new FolderElement("Leaf_Level_2");
      rootFolder.Add(leafLevel1Folder);
      leafLevel1Folder.Add(leafLevel2Folder);

      AddNewFile(leafLevel2Folder, 1000);

      Assert.AreEqual(1100, rootFolder.Size);
    }
    public void Add_AddFileToFolder_FolderParentSizeShouldRecalculate()
    {
      // arrange
      var rootFolder = new FolderElement("root");
      rootFolder.Add(_FolderElementTested);

      // assign
      const int fileSize = 100;
      AddNewFile(_FolderElementTested, fileSize);

      // assert
      var actual = _FolderElementTested.Parent.Size;
      var expected = fileSize;
      Assert.AreEqual(expected, actual);
    }
    private void ParseDirectory(string directory, FolderElement currentFolderElement)
    {
      //TODO: change the recursive to do-while
      var fileEntries = Directory.GetFiles(directory);
      foreach (var fileName in fileEntries)
      {
        var fileInfo = new FileInfo(fileName);
        var fileElement = new FileElement(fileName, fileInfo.Length);
        currentFolderElement.Add(fileElement);
      }

      var subDirectoryEntries = Directory.GetDirectories(directory);
      foreach (var subDirectory in subDirectoryEntries)
      {
        var folderElement = new FolderElement(subDirectory);
        currentFolderElement.Add(folderElement);
        ParseDirectory(subDirectory, folderElement);
      }
    }