using System; using System.Collections.Generic; using System.Xml.Linq; using Mono.Documentation; using Mono.Documentation.Framework; using Mono.Documentation.Util; using NUnit.Framework; namespace mdoc.Test { [TestFixture] public class MDocFileSourceTests : BasicTests { #region DropExcludedFrameworks [Test] public void DropExcludedFrameworks_NoNodesWithFrameworkAlternate_NoChangesInXml() { XDocument document = ReadXDocument(XmlConsts.NormalSingleXml2); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); mdocFileSource.DropExcludedFrameworks(document); Assert.AreEqual(NormalizeXml(XmlConsts.NormalSingleXml2), NormalizeXml(document.ToString())); } [Test] public void DropExcludedFrameworks_FrameworkAlternateWithManyFrameworks_FrameworkOneLeft() { XDocument document = ReadXDocument(XmlConsts.MultiFrameworkXml2); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); mdocFileSource.DropExcludedFrameworks(document); Assert.AreEqual(NormalizeXml(MultiFrameworkXml2FrameworkOne), NormalizeXml(document.ToString())); } [Test] public void DropExcludedFrameworks_FrameworkAlternateWithOneFramework_FrameworkOneLeft() { XDocument document = ReadXDocument(XmlConsts.MultiFrameworkXml2Second); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); mdocFileSource.DropExcludedFrameworks(document); Assert.AreEqual(NormalizeXml(MultiFrameworkXml2SecondFrameworkTwo), NormalizeXml(document.ToString())); } [Test] public void DropExcludedFrameworks_NoFrameworkModeFrameworkAlternateWithManyFrameworks_NoChangesInXml() { XDocument document = ReadXDocument(XmlConsts.MultiFrameworkXml2); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, null); mdocFileSource.DropExcludedFrameworks(document); Assert.AreEqual(NormalizeXml(XmlConsts.MultiFrameworkXml2), NormalizeXml(document.ToString())); } [Test] public void DropExcludedFrameworks_NoFrameworkModeFrameworkAlternateWithOneFramework_NoChangesInXml() { XDocument document = ReadXDocument(XmlConsts.MultiFrameworkXml2Second); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, null); mdocFileSource.DropExcludedFrameworks(document); Assert.AreEqual(NormalizeXml(XmlConsts.MultiFrameworkXml2Second), NormalizeXml(document.ToString())); } #endregion #region DropExcludedFrameworksFromIndex [Test] public void DropExcludedFrameworksFromIndex_Type1InFrameworkType2IsNot_Type2IsExcluded() { XDocument document = ReadXDocument(IndexFileXml); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); Dictionary frameworkIndex = GetFrameworkIndex(XmlConsts.FrameworkIndexXml); mdocFileSource.DropExcludedFrameworksFromIndex(document, frameworkIndex, (ns, type) => { switch (type) { case "Type1": return ReadXDocument(Type1Xml); case "Type2": return ReadXDocument(Type2Xml); } throw new Exception("Error in test data."); }); Assert.AreEqual(NormalizeXml(IndexFileFilteredByFrameworkXml), NormalizeXml(document.ToString())); } [Test] public void DropExcludedFrameworksFromIndex_Type1AndType2InFramework_NoExcludedTypes() { XDocument document = ReadXDocument(IndexFileXml); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); Dictionary frameworkIndex = GetFrameworkIndex(XmlConsts.FrameworkIndexXml2); mdocFileSource.DropExcludedFrameworksFromIndex(document, frameworkIndex, (ns, type) => { switch (type) { case "Type1": return ReadXDocument(Type1Xml); case "Type2": return ReadXDocument(Type2Xml); } throw new Exception("Error in test data."); }); Assert.AreEqual(NormalizeXml(IndexFileXml), NormalizeXml(document.ToString())); } [Test] public void DropExcludedFrameworksFromIndex_NoFrameworkModeType1InFrameworkType2IsNot_NoChangesInXml() { XDocument document = ReadXDocument(IndexFileXml); var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, null); Dictionary frameworkIndex = GetFrameworkIndex(XmlConsts.FrameworkIndexXml); mdocFileSource.DropExcludedFrameworksFromIndex(document, frameworkIndex, (ns, type) => { switch (type) { case "Type1": return ReadXDocument(Type1Xml); case "Type2": return ReadXDocument(Type2Xml); } throw new Exception("Error in test data."); }); Assert.AreEqual(NormalizeXml(IndexFileXml), NormalizeXml(document.ToString())); } #endregion #region IsTypeInFramework [Test] public void IsTypeInFramework_TypeIsInFramework_ReturnsTrue() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); Dictionary frameworkIndex = GetFrameworkIndex(XmlConsts.FrameworkIndexXml); XDocument typeDoc = ReadXDocument(Type1Xml); string nsName = "Namespace1"; bool isTypeInFramework = mdocFileSource.IsTypeInFramework(typeDoc, nsName, frameworkIndex); Assert.IsTrue(isTypeInFramework); } [Test] public void IsTypeInFramework_NoNamespaceInFramework_ReturnsFalse() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); Dictionary frameworkIndex = GetFrameworkIndex(XmlConsts.FrameworkIndexXml); XDocument typeDoc = ReadXDocument(Type1Xml); string nsName = "Namespace3"; bool isTypeInFramework = mdocFileSource.IsTypeInFramework(typeDoc, nsName, frameworkIndex); Assert.IsFalse(isTypeInFramework); } [Test] public void IsTypeInFramework_TypeIsNotInFramework_ReturnsFalse() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); Dictionary frameworkIndex = GetFrameworkIndex(XmlConsts.FrameworkIndexXml); XDocument typeDoc = ReadXDocument(Type3Xml); string nsName = "Namespace1"; bool isTypeInFramework = mdocFileSource.IsTypeInFramework(typeDoc, nsName, frameworkIndex); Assert.IsFalse(isTypeInFramework); } #endregion #region IsExcludedFramework [Test] public void IsExcludedFramework_NoFrameworkAlternate_ReturnsFalse() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); var member = XElement.Parse(@""); var isExcludedFramework = mdocFileSource.IsExcludedFramework(member); Assert.IsFalse(isExcludedFramework); } [Test] public void IsExcludedFramework_FrameworkAlternateIncludesFrameworkName_ReturnsFalse() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); var member = XElement.Parse(@""); var isExcludedFramework = mdocFileSource.IsExcludedFramework(member); Assert.IsFalse(isExcludedFramework); } [Test] public void IsExcludedFramework_FrameworkAlternateIncludesFrameworkNameInManyValues_ReturnsFalse() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); var member = XElement.Parse(@""); var isExcludedFramework = mdocFileSource.IsExcludedFramework(member); Assert.IsFalse(isExcludedFramework); } [Test] public void IsExcludedFramework_FrameworkAlternateDoesntIncludeFrameworkName_ReturnsTrue() { var mdocFileSource = new MDocFileSource(null, ApiStyle.Classic, "One"); var member = XElement.Parse(@""); var isExcludedFramework = mdocFileSource.IsExcludedFramework(member); Assert.IsTrue(isExcludedFramework); } #endregion #region Helper private Dictionary GetFrameworkIndex(string frameworkIndexXml) { return FrameworkIndexHelper.ReadFrameworkIndex(ReadXDocument(frameworkIndexXml).CreateReader()); } #endregion #region Consts private const string MultiFrameworkXml2FrameworkOne = @" Method System.Void To be added. To be added. "; private const string MultiFrameworkXml2SecondFrameworkTwo = @" Method System.Void To be added. To be added. "; private const string IndexFileXml = @" To be added. To be added. Untitled "; private const string IndexFileFilteredByFrameworkXml = @" To be added. To be added. Untitled "; private const string Type1Xml = @" "; private const string Type2Xml = @" "; private const string Type3Xml = @" "; #endregion } }