private static StringList MakeAbsolute(AbsoluteFolderPath fromHere, RelativePath adjustment)
        {
            var result     = fromHere.Parts.CloneSublist(fromHere.Parts.Count);
            var enumerator = adjustment.Parts.GetEnumerator();

            // skip the root
            enumerator.MoveNext();
            while (enumerator.MoveNext())
            {
                string part = enumerator.Current;
                switch (part)
                {
                case ".":
                    break;

                case "..":
                    // bug? can we go above the root here?
                    result.RemoveLast();
                    break;

                default:
                    result.Add(part);
                    break;
                }
            }
            return(result);
        }
 public void AbsoluteFolder_Theory(FolderArgs args)
 {
     if (args.Expected == null)
     {
         Trap.Exception(() => new AbsoluteFolderPath(args.Source))
         .ShouldNotBeNull();
     }
     else
     {
         var folder = new AbsoluteFolderPath(args.Source);
         Debug.Print(folder.ToString());
         folder.ToString()
         .ShouldEqual(args.Expected);
     }
 }
Exemple #3
0
            public void SubtractAbsoluteFile_Theory(TheoryArgs args)
            {
                AbsoluteFolderPath finish = args.LeftPath;
                AbsoluteFilePath   start  = args.RightPath;

                if (args.ExpectedPath == null)
                {
                    Trap.Exception(() => { var _ = finish - start; })
                    .ShouldNotBeNull();
                }
                else
                {
                    RelativeFolderPath relative = finish - start;
                    relative.ToString()
                    .ShouldEqual(args.ExpectedPath);
                }
            }
Exemple #4
0
            public void AddRelativeFolderPath_Theory(TheoryArgs args)
            {
                AbsoluteFolderPath absoulte = args.LeftPath;
                RelativeFolderPath relative = args.RightPath;

                if (args.ExpectedPath == null)
                {
                    Trap.Exception(() => { var _ = absoulte + relative; })
                    .ShouldNotBeNull();
                }
                else
                {
                    var combined = absoulte + relative;
                    combined.ToString()
                    .ShouldEqual(args.ExpectedPath);
                }
            }
        private static StringList MakeRelative(AbsoluteFolderPath fromHere, AbsoluteFolderPath toHere)
        {
            if (fromHere.RootValue != toHere.RootValue)
            {
                throw new Exception("No shared root between: " + fromHere + " -> " + toHere);
            }

            bool hasFrom;
            var  @from = fromHere.Parts.GetEnumerator();
            bool hasTo;
            var  @to = toHere.Parts.GetEnumerator();

            while (true)
            {
                hasFrom = from.MoveNext();
                hasTo   = to.MoveNext();
                if (!hasFrom || !hasTo || from.Current != to.Current)
                {
                    break;
                }
                // eat all matches
            }

            //NB: dont forget the null absolute root
            var relative = new StringList(new string[] {
                null
            });

            while (hasFrom)
            {
                relative.Add("..");
                hasFrom = from.MoveNext();
            }
            //NB: ensure we are relativly rooted
            if (relative.Count == 1)
            {
                relative.Add(".");
            }
            while (hasTo)
            {
                relative.Add(to.Current);
                hasTo = to.MoveNext();
            }
            return(relative);
        }