/// <summary> /// Creates a new RegionArranger. /// </summary> /// <param name="regionConfiguration">Region configuration.</param> /// <param name="parentConfiguration">Parent configuration.</param> public RegionArranger(RegionConfiguration regionConfiguration, ConfigurationElement parentConfiguration) { if (regionConfiguration == null) { throw new ArgumentNullException("regionConfiguration"); } if (parentConfiguration == null) { throw new ArgumentNullException("parentConfiguration"); } _regionConfiguration = regionConfiguration; _parentConfiguration = parentConfiguration; List<string> levelRegions = new List<string>(); foreach (ConfigurationElement siblingConfiguration in _parentConfiguration.Elements) { RegionConfiguration siblingRegionConfiguration = siblingConfiguration as RegionConfiguration; if (siblingRegionConfiguration != null) { levelRegions.Add(siblingRegionConfiguration.Name); } } _levelRegions = levelRegions.AsReadOnly(); }
public void ArrangeNestedRegionTest() { List<ICodeElement> elements = new List<ICodeElement>(); TypeElement type = new TypeElement(); type.Type = TypeElementType.Class; type.Name = "TestClass"; FieldElement field = new FieldElement(); field.Name = "val"; field.Type = "int"; type.AddChild(field); elements.Add(type); // Create a configuration with a nested region CodeConfiguration codeConfiguration = new CodeConfiguration(); ElementConfiguration typeConfiguration = new ElementConfiguration(); typeConfiguration.ElementType = ElementType.Type; RegionConfiguration regionConfiguration1 = new RegionConfiguration(); regionConfiguration1.Name = "Region1"; RegionConfiguration regionConfiguration2 = new RegionConfiguration(); regionConfiguration2.Name = "Region2"; ElementConfiguration fieldConfiguration = new ElementConfiguration(); fieldConfiguration.ElementType = ElementType.Field; regionConfiguration2.Elements.Add(fieldConfiguration); regionConfiguration1.Elements.Add(regionConfiguration2); typeConfiguration.Elements.Add(regionConfiguration1); codeConfiguration.Elements.Add(typeConfiguration); CodeArranger arranger = new CodeArranger(codeConfiguration); ReadOnlyCollection<ICodeElement> arrangedElements = arranger.Arrange(elements.AsReadOnly()); Assert.AreEqual(1, arrangedElements.Count, "Unexpected number of arranged elements."); TypeElement arrangedType = arrangedElements[0] as TypeElement; Assert.IsNotNull(arrangedType, "Expected a type element after arranging."); Assert.AreEqual(1, arrangedType.Children.Count, "Unexpected number of arranged child elements."); RegionElement arrangedRegion1 = arrangedType.Children[0] as RegionElement; Assert.IsNotNull(arrangedRegion1, "Expected a region element after arranging."); Assert.AreEqual(regionConfiguration1.Name, arrangedRegion1.Name); Assert.AreEqual(1, arrangedRegion1.Children.Count, "Unexpected number of arranged child elements."); RegionElement arrangedRegion2 = arrangedRegion1.Children[0] as RegionElement; Assert.IsNotNull(arrangedRegion2, "Expected a region element after arranging."); Assert.AreEqual(regionConfiguration2.Name, arrangedRegion2.Name); Assert.AreEqual(1, arrangedRegion2.Children.Count, "Unexpected number of arranged child elements."); FieldElement arrangedFieldElement = arrangedRegion2.Children[0] as FieldElement; Assert.IsNotNull(arrangedFieldElement, "Expected a field element after arranging."); }
public void ToStringTest() { RegionConfiguration regionConfiguration = new RegionConfiguration(); regionConfiguration.Name = "Test Region"; string str = regionConfiguration.ToString(); Assert.AreEqual("Region: Test Region", str, "Unexpected string representation."); }
public void CreateTest() { RegionConfiguration regionConfiguration = new RegionConfiguration(); // // Verify default state // Assert.IsNull(regionConfiguration.Name, "Unexpected default value for Name."); Assert.IsNotNull(regionConfiguration.Elements, "Elements collection should not be null."); Assert.AreEqual(0, regionConfiguration.Elements.Count, "Elements collection should be empty."); Assert.IsTrue(regionConfiguration.DirectivesEnabled, "Directives should be enabled."); }
public void CloneTest() { RegionConfiguration regionConfiguration = new RegionConfiguration(); regionConfiguration.Name = "Some Region"; regionConfiguration.DirectivesEnabled = false; RegionConfiguration clone = regionConfiguration.Clone() as RegionConfiguration; Assert.IsNotNull(clone, "Clone did not return a valid instance."); Assert.AreEqual(regionConfiguration.Name, clone.Name); Assert.AreEqual(regionConfiguration.DirectivesEnabled, clone.DirectivesEnabled); }
public void CanArrangeTest() { RegionConfiguration methodRegionConfiguration = new RegionConfiguration(); methodRegionConfiguration.Name = "Methods"; ElementConfiguration methodConfiguration = new ElementConfiguration(); methodConfiguration.ElementType = ElementType.Method; methodRegionConfiguration.Elements.Add(methodConfiguration); RegionConfiguration propertyRegionConfiguration = new RegionConfiguration(); propertyRegionConfiguration.Name = "Properties"; ElementConfiguration propertyConfiguration = new ElementConfiguration(); propertyConfiguration.ElementType = ElementType.Property; propertyRegionConfiguration.Elements.Add(propertyConfiguration); ElementConfiguration parentConfiguration = new ElementConfiguration(); parentConfiguration.ElementType = ElementType.Type; RegionArranger methodRegionArranger = new RegionArranger( methodRegionConfiguration, parentConfiguration); RegionArranger propertyRegionArranger = new RegionArranger( propertyRegionConfiguration, parentConfiguration); MethodElement method = new MethodElement(); method.Name = "DoSomething"; Assert.IsTrue( methodRegionArranger.CanArrange(method), "Expected region arranger to be able to arrange the element."); Assert.IsFalse( propertyRegionArranger.CanArrange(method), "Expected region arranger to not be able to arrange the element."); Assert.IsFalse( methodRegionArranger.CanArrange(null), "Expected region arranger to not be able to arrange a null element."); }
/// <summary> /// Creates a clone of this instance. /// </summary> /// <returns>Clone of this instance.</returns> protected override ConfigurationElement DoClone() { RegionConfiguration clone = new RegionConfiguration(); clone._name = _name; clone._directivesEnabled = _directivesEnabled; return clone; }
public void SerializeAndDeserializeTest() { CodeConfiguration origConfig = new CodeConfiguration(); ElementConfiguration elementConfiguration1 = new ElementConfiguration(); elementConfiguration1.ElementType = ElementType.Using; elementConfiguration1.Id = "TestId"; origConfig.Elements.Add(elementConfiguration1); ElementConfiguration elementConfiguration2 = new ElementConfiguration(); elementConfiguration2.ElementType = ElementType.Namespace; origConfig.Elements.Add(elementConfiguration2); ElementReferenceConfiguration elementReferenceConfiguration = new ElementReferenceConfiguration(); elementReferenceConfiguration.Id = "TestId"; origConfig.Elements.Add(elementReferenceConfiguration); RegionConfiguration regionConfiguration = new RegionConfiguration(); regionConfiguration.Name = "Test Region"; origConfig.Elements.Add(regionConfiguration); origConfig.ResolveReferences(); Assert.AreEqual( elementConfiguration1.Elements.Count, elementReferenceConfiguration.ReferencedElement.Elements.Count, "Element reference was not resolved."); string tempFile = Path.GetTempFileName(); try { // // Save the configuration to an XML file // origConfig.Save(tempFile); // // Load the configuration from the XML file // CodeConfiguration loadedConfig = CodeConfiguration.Load(tempFile); Assert.IsNotNull(loadedConfig, "Loaded configuration should not be null."); Assert.AreEqual(origConfig.Elements.Count, loadedConfig.Elements.Count, "An unexpected number of config elements were deserialized."); for (int index = 0; index < origConfig.Elements.Count; index++) { if (origConfig.Elements[index] is ElementConfiguration) { ElementConfiguration origElement = origConfig.Elements[index] as ElementConfiguration; ElementConfiguration loadedElement = loadedConfig.Elements[index] as ElementConfiguration; Assert.AreEqual(origElement.ElementType, loadedElement.ElementType, "Unexpected element type."); } else if (origConfig.Elements[index] is ElementReferenceConfiguration) { ElementReferenceConfiguration origElement = origConfig.Elements[index] as ElementReferenceConfiguration; ElementReferenceConfiguration loadedElement = loadedConfig.Elements[index] as ElementReferenceConfiguration; Assert.AreEqual(origElement.Id, loadedElement.Id, "Unexpected element type."); Assert.AreEqual( origElement.ReferencedElement.Id, loadedElement.ReferencedElement.Id, "Unexpected referenced element."); } else if (origConfig.Elements[index] is RegionConfiguration) { RegionConfiguration origRegion = origConfig.Elements[index] as RegionConfiguration; RegionConfiguration loadedRegion = loadedConfig.Elements[index] as RegionConfiguration; Assert.AreEqual(origRegion.Name, loadedRegion.Name, "Unexpected region name."); } } } finally { File.Delete(tempFile); } }