public void ReferenceCollectionChangedTest() { NotifyCollectionChangedEventArgs args = null; // Initialize Reference root = new Reference(String.Empty, "D:\\Data\\Project"); Reference folder1 = new Reference("folder1", ReferenceTargetKind.Directory); Reference file1 = new Reference("file1.txt", ReferenceTargetKind.File); Reference file2 = new Reference("file2.txt", ReferenceTargetKind.File); Reference file3 = new Reference("file3.txt", ReferenceTargetKind.File); root.CollectionChanged += new NotifyCollectionChangedEventHandler((sender, e) => args = e); // Add item args = null; root.Add(folder1); Assert.IsNotNull(args); Assert.AreEqual(NotifyCollectionChangedAction.Add, args.Action); Assert.AreEqual(1, args.NewItems.Count); Assert.AreEqual(folder1, args.NewItems[0]); Assert.IsNull(args.OldItems); args = null; folder1.Add(file1); Assert.IsNull(args); args = null; root.Add(file2); root.Add(file3); Assert.IsNotNull(args); // Remove args = null; root.Remove(file3); Assert.IsNotNull(root); Assert.AreEqual(NotifyCollectionChangedAction.Remove, args.Action); Assert.IsNull(args.NewItems); Assert.AreEqual(1, args.OldItems.Count); Assert.AreEqual(file3, args.OldItems[0]); // Unparent args = null; file2.Unparent(); Assert.IsNotNull(args); Assert.AreEqual(NotifyCollectionChangedAction.Remove, args.Action); Assert.IsNull(args.NewItems); Assert.AreEqual(1, args.OldItems.Count); Assert.AreEqual(file2, args.OldItems[0]); // Clear args = null; root.Clear(); Assert.IsNotNull(args); Assert.AreEqual(NotifyCollectionChangedAction.Reset, args.Action); Assert.IsNull(args.NewItems); Assert.IsNull(args.OldItems); }
public void ReferenceGetReferenceTest() { Reference root = new Reference(String.Empty, "D:\\Data\\Project"); Reference folder1 = new Reference("folder1", ReferenceTargetKind.Directory); Reference folder2 = new Reference("folder 2", ReferenceTargetKind.Directory); Reference file1 = new Reference("file1", ReferenceTargetKind.File); Reference file2 = new Reference("file2.txt", ReferenceTargetKind.File); Reference file3 = new Reference("file 3.png", ReferenceTargetKind.File); Reference notInTree = new Reference("file4.txt", ReferenceTargetKind.File); root.Add(folder1); root.Add(file3); folder1.Add(file1); folder1.Add(folder2); folder2.Add(file2); // Test 'get reference' method Assert.AreEqual(root, root.GetReference("")); Assert.AreEqual(folder2, root.GetReference("/folder1/folder 2")); Assert.AreEqual(file3, file2.GetReference("/file 3.png")); Assert.AreEqual(file2, file2.GetReference("/folder1/folder 2/file2.txt")); Assert.AreEqual(file2, file3.GetReference("/folder1/folder 2/file2.txt")); try { file3.GetReference("/file 3.png/some nonexistant file"); Assert.Fail(); } catch (ArgumentException) { } // Test 'try get reference' method Reference res; Assert.IsTrue(root.TryGetReference("", out res)); Assert.AreEqual(root, res); Assert.IsTrue(root.TryGetReference("/folder1/folder 2", out res)); Assert.AreEqual(folder2, res); Assert.IsTrue(file2.TryGetReference("/file 3.png", out res)); Assert.AreEqual(file3, res); Assert.IsTrue(file2.TryGetReference("/folder1/folder 2/file2.txt", out res)); Assert.AreEqual(file2, res); Assert.IsTrue(file3.TryGetReference("/folder1/folder 2/file2.txt", out res)); Assert.AreEqual(file2, res); Assert.IsFalse(root.TryGetReference("/file 3.png/some nonexistant file", out res)); // Test 'tree contains' method Assert.IsTrue(file2.TreeContains(root)); Assert.IsTrue(root.TreeContains(file3)); Assert.IsFalse(root.TreeContains(notInTree)); Assert.IsFalse(notInTree.TreeContains(root)); }
/// <summary> /// Creates a copy of a project item to another folder /// </summary> /// <param name="ref">Project item to copy</param> /// <param name="dest">Destination folder</param> /// <returns>Reference to the copy</returns> public Reference ProjectItemCopy(Reference @ref, Reference dest) { // Create a clone reference var copyRef = (Reference)@ref.Clone(); // Copy storage file string refPath = Path.GetFileName(@ref.StoragePath.TrimEnd('\\')); string destinationPath = (dest.TargetKind == ReferenceTargetKind.Directory) ? dest.StoragePath : Path.GetDirectoryName(dest.StoragePath); string newPath = Path.Combine(destinationPath, refPath); if (@ref.TargetKind == ReferenceTargetKind.Directory) { DirectoryHelper.CopyDirectory(@ref.StoragePath, newPath); // Update children UpdateRenameChildren(copyRef, copyRef.StoragePath, newPath); } else { // Find a nonconflicting file name newPath = GetNonConflictingPath(refPath, destinationPath); // Copy File.Copy(@ref.StoragePath, newPath); } // Parent reference copyRef.Name = Path.GetFileName(newPath); copyRef.StoragePath = newPath; dest.Add(copyRef); return(copyRef); }
private Project CreateProject() { // Create some file references Reference folder1 = new Reference("folder1", "folder1"); Reference folder2 = new Reference("folder2", "folder2"); Reference file1 = new Reference("file1.txt", "folder1/file1.txt"); Reference file2 = new Reference("file2.ini", "folder2/file2.ini"); Reference file3 = new Reference("file3.bmp", "file3.bmp"); // Create a project Project project = new Project(); project.Author = "Tiberiu Chibici"; project.MinimumRainmeter = new Version("3.1"); project.MinimumWindows = new Version("5.1"); project.Name = "My project"; project.Version = new Version("1.0.1"); project.AutoLoadFile = file2; project.VariableFiles.Add(file1); // Set project references project.Root.Add(folder1); project.Root.Add(folder2); folder1.Add(file1); folder2.Add(file2); project.Root.Add(file3); return(project); }
/// <summary> /// Creates a new folder with given name /// </summary> /// <param name="name">Name of folder</param> /// <param name="parent">Parent folder</param> public void CreateFolder(string name, Reference parent) { string dir = (parent.TargetKind == ReferenceTargetKind.Directory) ? parent.StoragePath : Path.GetDirectoryName(parent.StoragePath); string newDirPath = Path.Combine(dir, name); Directory.CreateDirectory(newDirPath); parent.Add(new Reference(name, newDirPath, ReferenceTargetKind.Directory)); }
public void ReferenceQualifiedNameTest() { Reference root = new Reference(String.Empty, "D:\\Data\\Project"); Reference folder1 = new Reference("folder1", ReferenceTargetKind.Directory); Reference folder2 = new Reference("folder 2", ReferenceTargetKind.Directory); Reference file1 = new Reference("file1", ReferenceTargetKind.File); Reference file2 = new Reference("file2.txt", ReferenceTargetKind.File); Reference file3 = new Reference("file 3.png", ReferenceTargetKind.File); root.Add(folder1); root.Add(file3); folder1.Add(file1); folder1.Add(folder2); folder2.Add(file2); // Test qualified names Assert.AreEqual(String.Empty, root.QualifiedName); Assert.AreEqual("/folder1", folder1.QualifiedName); Assert.AreEqual("/folder1/folder 2", folder2.QualifiedName); Assert.AreEqual("/folder1/file1", file1.QualifiedName); Assert.AreEqual("/folder1/folder 2/file2.txt", file2.QualifiedName); Assert.AreEqual("/file 3.png", file3.QualifiedName); // Test qualified parts Assert.IsTrue(new[] { String.Empty } .SequenceEqual(root.QualifiedParts)); Assert.IsTrue(new[] { String.Empty, "folder1" } .SequenceEqual(folder1.QualifiedParts)); Assert.IsTrue(new[] { String.Empty, "folder1", "folder 2" } .SequenceEqual(folder2.QualifiedParts)); Assert.IsTrue(new[] { String.Empty, "folder1", "file1" } .SequenceEqual(file1.QualifiedParts)); Assert.IsTrue(new[] { String.Empty, "folder1", "folder 2", "file2.txt" } .SequenceEqual(file2.QualifiedParts)); Assert.IsTrue(new[] { String.Empty, "file 3.png" } .SequenceEqual(file3.QualifiedParts)); }
private void FindReferencesRecurcive(IEvent current, Reference <uint, IEvent> reference, byte eventType, uint unitId) { if (current.EventType == eventType) { if (current.Unit == unitId) { reference.Add(current); } } foreach (IEvent @event in current.Children) { FindReferencesRecurcive(@event, reference, eventType, unitId); } }
public void ReferenceParentingTest() { Reference root = new Reference(String.Empty, "D:\\Data\\Project"); Reference folder1 = new Reference("folder1", ReferenceTargetKind.Directory); Reference folder2 = new Reference("folder 2", ReferenceTargetKind.Directory); Reference file1 = new Reference("file1", ReferenceTargetKind.File); Reference file2 = new Reference("file2.txt", ReferenceTargetKind.File); Reference file3 = new Reference("file 3.png", ReferenceTargetKind.File); root.Add(folder1); root.Add(file3); folder1.Add(file1); folder1.Add(folder2); folder2.Add(file2); Assert.IsNull(root.Parent); Assert.AreEqual(2, root.ChildrenDictionary.Count); Assert.AreEqual(root, folder1.Parent); Assert.AreEqual(root, file3.Parent); Assert.AreEqual(2, folder1.ChildrenDictionary.Count); Assert.AreEqual(folder1, folder2.Parent); Assert.AreEqual(folder1, file1.Parent); Assert.IsNotNull(folder2.Children.FirstOrDefault(x => x == file2)); // Unparent something file3.Unparent(); Assert.IsNull(file3.Parent); Assert.IsNull(root.Children.FirstOrDefault(x => x == file3)); Assert.AreEqual(1, root.ChildrenDictionary.Count); // Remove something root.Remove(folder1); Assert.IsNull(folder1.Parent); Assert.AreEqual(0, root.ChildrenDictionary.Count); }
public void ReferencePropertyChangedTest() { List <string> propNames = new List <string>(); // Initialize Reference root = new Reference("Project", "D:\\Data\\Project", ReferenceTargetKind.Project); Reference folder1 = new Reference("folder1", ReferenceTargetKind.Directory); Reference file1 = new Reference("file1.txt", ReferenceTargetKind.File); file1.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler((sender, e) => propNames.Add(e.PropertyName)); // Parent propNames.Clear(); folder1.Add(file1); Assert.AreEqual(2, propNames.Count); Assert.IsTrue(propNames.Contains("Parent")); Assert.IsTrue(propNames.Contains("QualifiedName")); // Storage path propNames.Clear(); file1.StoragePath = "D:\\Data\\Project\\folder1\\file1.txt"; Assert.AreEqual(1, propNames.Count); Assert.AreEqual("StoragePath", propNames[0]); // Target kind propNames.Clear(); file1.TargetKind = ReferenceTargetKind.None; Assert.AreEqual(1, propNames.Count); Assert.AreEqual("TargetKind", propNames[0]); // Name propNames.Clear(); file1.Name = "file10.txt"; Assert.AreEqual(2, propNames.Count); Assert.IsTrue(propNames.Contains("Name")); Assert.IsTrue(propNames.Contains("QualifiedName")); // Qualified name propagation propNames.Clear(); root.Add(folder1); Assert.AreEqual(1, propNames.Count); Assert.IsTrue(propNames.Contains("QualifiedName")); }
/// <summary> /// Gets a tree of the folder structure /// </summary> /// <param name="folder">Folder</param> /// <returns>A tree</returns> public static Reference GetFolderTree(string folder) { // Build tree object Reference refTree = new Reference(Path.GetFileName(folder), folder, ReferenceTargetKind.File); // Navigate folder structure if (Directory.Exists(folder)) { refTree.TargetKind = ReferenceTargetKind.Directory; foreach (var item in Directory.EnumerateDirectories(folder) .Concat(Directory.EnumerateFiles(folder))) { refTree.Add(GetFolderTree(item)); } } // Return tree return(refTree); }
/// <summary> /// Moves a project item to another folder /// </summary> /// <param name="ref">Project item to move</param> /// <param name="dest">Destination folder</param> public void ProjectItemMove(Reference @ref, Reference dest) { // Move storage file string refPath = Path.GetFileName(@ref.StoragePath.TrimEnd('\\')); string destinationPath = (dest.TargetKind == ReferenceTargetKind.Directory) ? dest.StoragePath : Path.GetDirectoryName(dest.StoragePath); string newPath = Path.Combine(destinationPath, refPath); if (@ref.TargetKind == ReferenceTargetKind.Directory) { Directory.Move(@ref.StoragePath, newPath); // Update children UpdateRenameChildren(@ref, @ref.StoragePath, newPath); } else { File.Move(@ref.StoragePath, newPath); } // Set up reference object @ref.Unparent(); @ref.StoragePath = newPath; dest.Add(@ref); }
public void AddReferencia(string referencia) { Reference.Add(referencia); }