public void Test_Container_AddTwoEqualNames_Exception() { IElement dir = ElementFactory.CreateDirectory("dir"); dir.AddChild(ElementFactory.CreateFile("file")); dir.AddChild(ElementFactory.CreateFile("file")); }
public void Test_Container_AddNestedElement() { IElement root = ElementFactory.CreateRoot(); IElement directory = root.AddChild(ElementFactory.CreateDirectory("Dir")); directory.AddChild(ElementFactory.CreateFile("file1")); directory.AddChild(ElementFactory.CreateFile("file2")); IElement nestedDirectory = directory.AddChild(ElementFactory.CreateDirectory("nested")); nestedDirectory.AddChild(ElementFactory.CreateFile("file11")); nestedDirectory.AddChild(ElementFactory.CreateFile("file12")); nestedDirectory.AddChild(ElementFactory.CreateFile("file13")); nestedDirectory.AddChild(ElementFactory.CreateFile("file14")); Assert.AreEqual(root.ChildrenCount, 1); Assert.AreEqual(root["dir"].ChildrenCount, 3); Assert.AreEqual(root["dir"][0].Name, "file1"); Assert.AreEqual(root["dir"][1].Name, "file2"); Assert.AreEqual(root["dir"][2].Name, "nested"); Assert.AreEqual(root["dir"][2].ChildrenCount, 4); Assert.AreEqual(root["dir"]["nested"][0].Name, "file11"); Assert.AreEqual(root["dir"]["nested"][1].Name, "file12"); Assert.AreEqual(root["dir"]["nested"][2].Name, "file13"); Assert.AreEqual(root["dir"]["nested"][3].Name, "file14"); }
public void Test_Container_AutoSorting() { IElement root = ElementFactory.CreateRoot(); root.AddChild(ElementFactory.CreateFile("bbb")); root.AddChild(ElementFactory.CreateFile("ddd")); root.AddChild(ElementFactory.CreateFile("aaa")); root.AddChild(ElementFactory.CreateFile("ccc")); Assert.AreEqual(root[0].Name, "aaa"); Assert.AreEqual(root[1].Name, "bbb"); Assert.AreEqual(root[2].Name, "ccc"); Assert.AreEqual(root[3].Name, "ddd"); }
public void Test_Container_AddElement_ChildrenCount_Indexers() { IElement root = ElementFactory.CreateRoot(); root.AddChild(ElementFactory.CreateFile("file1")); Assert.AreEqual(root.ChildrenCount, 1); root.AddChild(ElementFactory.CreateFile("file2")); root.AddChild(ElementFactory.CreateFile("file3")); Assert.AreEqual(root.ChildrenCount, 3); Assert.AreEqual(root["file2"].Name, "file2"); Assert.AreEqual(root[0].Name, "file1"); }
public void Test_Container_CaseInsensitive() { IElement root = ElementFactory.CreateRoot(); root.AddChild(ElementFactory.CreateFile("aAa")); root.AddChild(ElementFactory.CreateDirectory("BbB")); root.AddChild(ElementFactory.CreateFile("cCc")); Assert.AreSame(root["aaa"], root["AaA"]); Assert.AreEqual(root["aaa"].Name, "aAa"); Assert.AreSame(root["bbb"], root["bBb"]); Assert.AreEqual(root["BBB"].Name, "BbB"); }
protected IElement Clone(IElement destinationElement) { for (int childIndex = 0; childIndex < ChildrenCount; childIndex++) destinationElement.AddChild(ChildrenList[childIndex].Clone()); return destinationElement; }
protected IElement Clone(IElement destinationElement) { for (int childIndex = 0; childIndex < ChildrenCount; childIndex++) { destinationElement.AddChild(ChildrenList[childIndex].Clone()); } return(destinationElement); }
public void Test_Container_RemoveChild() { IElement dir = ElementFactory.CreateDirectory("dir"); IElement file = dir.AddChild(ElementFactory.CreateFile("file")); Assert.AreEqual(dir.ChildrenCount, 1); dir.RemoveChild(file); Assert.AreEqual(dir.ChildrenCount, 0); }
/// <summary> /// 解析表 /// 先计算所有嵌套的字表,再计算正常值 /// </summary> /// <returns><c>true</c>, if table was parsed, <c>false</c> otherwise.</returns> /// <param name="data">Data.</param> /// <param name="startIndex">Start index.</param> /// <param name="endIndex">End index.</param> private bool ParseTable(string data, int startIndex, IElement node, out int endIndex) { endIndex = 0; int endIdx = data.IndexOf('}', startIndex); if (endIdx < 0) { return(false); } int pos = startIndex + 1; StringBuilder sb = new StringBuilder(); while (pos < data.Length) { if (data [pos] == '{') { string value = sb.ToString(); int idx = value.LastIndexOf(' ', value.LastIndexOf('=') - 2); idx = idx < 0 ? 0 : idx; sb = new StringBuilder(value.Substring(0, idx)); IElement child = JSONFilter.Instance.Match(data, pos - value.Length + idx, out endIdx); if (child != null) { node.AddChild(child); pos = endIdx; } else { return(false); } } else if (data [pos] == '}') { break; } else { sb.Append(data [pos]); } pos++; } if (sb.Length != 0) { ParseChildren(node, sb.ToString()); } endIndex = pos; return(true); }
public virtual void MoveTo(IElement parent) { if (IsLocked) { throw new ElementIsLockedException(Name); } IElement oldParent = Parent; // если parent не контейнер, будет exception, но элемент останется со старым parent'ом parent.AddChild(this); oldParent.RemoveChild(this); }
/// <summary> /// 解析子节点 /// </summary> /// <returns><c>true</c>, if children was parsed, <c>false</c> otherwise.</returns> /// <param name="parent">Parent.</param> /// <param name="value">Value.</param> private bool ParseChildren(IElement parent, string value) { int p1 = 0; int endIdx = 0; while (p1 < value.Length) { IElement e = Parse(value, p1, out endIdx); if (e != null) { parent.AddChild(e); p1 = endIdx; } else { return(false); } p1++; } return(true); }
public virtual void CopyTo(IElement parent) { parent.AddChild(Clone()); }