// Copyright Keysight Technologies 2012-2019 // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, you can obtain one at http://mozilla.org/MPL/2.0/. using NUnit.Framework; using System.IO; using System.Linq; using System.Xml.Linq; namespace OpenTap.Package.UnitTests { [TestFixture] class PackageDefLoadSaveCompare { /// /// Load the input as a PackageDef, saves it again and compares the saved output to the original input /// /// private static void LoadSaveCompare(string input, string expectedOutput) { PackageDef pkg; byte[] inputArray = System.Text.Encoding.ASCII.GetBytes(input); using (MemoryStream inputStream = new MemoryStream(inputArray)) { pkg = PackageDef.FromXml(inputStream); } string output = ""; using (Stream str = new MemoryStream()) { pkg.SaveTo(str); using (StreamReader reader = new StreamReader(str)) { reader.BaseStream.Seek(0, 0); output = reader.ReadToEnd(); } } XDocument inputDoc = XDocument.Parse(expectedOutput); XDocument outputDoc = XDocument.Parse(output); AssertElementEquals(inputDoc.Root, outputDoc.Root); AssertElementEquals(outputDoc.Root, inputDoc.Root); } private static void AssertElementEquals(XElement elm1, XElement elm2) { Assert.AreEqual(elm1.Attributes().Count(), elm2.Attributes().Count(), $"Number of attributes does not match on element {elm1.Name.LocalName}"); foreach (XAttribute a1 in elm1.Attributes()) { var a2 = elm2.Attribute(a1.Name); if (a2 == null) Assert.Fail($"Attribute {a1.Name.LocalName} on element {elm1.Name.LocalName} does not exist in output."); if (a1.Value != a2.Value) Assert.Fail($"Value of attribute {a1.Name.LocalName} on element {elm1.Name.LocalName} does not match. ('{a1.Value}' vs. '{a2.Value}')"); } Assert.AreEqual(elm1.Elements().Count(), elm2.Elements().Count(), $"Number of child nodes does not match on element {elm1.Name.LocalName}"); foreach (var n1 in elm1.Nodes()) { if (n1 is XElement e1) { XElement e2 = elm2.Element(e1.Name); if (e2 == null) Assert.Fail($"Output does not contain element {e1.Name.LocalName}."); AssertElementEquals(e1, e2); } if (n1 is XText t1) { Assert.IsTrue(elm2.Nodes().OfType().Any(t => t.Value == t1.Value), $"Text value '{t1.Value}' does not exist in output."); } } } /// /// Tests that OS attribute is kept even when empty (as it's default value is non empty ('Windows')) /// [Test] public void EmptyOS() { string input = @" "; LoadSaveCompare(input,input); } /// /// duplicate of the old PackageDefTests.SaveTo_Simple test /// [Test] public void Simple() { int retryCount = 10; while(retryCount-- > 0) { string input = @" Everything goes here tags. System.Reflection.Metadata "; LoadSaveCompare(input, input); } } /// /// Tests that the FileType attribute is removed if it has its default value 'tappackage' /// [Test] public void DefaultFileType() { string input = @" "; string output = @" "; LoadSaveCompare(input, output); } /// /// Tests that the FileType attribute is removed if it has its default value 'tappackage' /// [Test] public void NoNamespace() { string input = @" "; string output = @" "; LoadSaveCompare(input, output); } /// /// Tests that the FileType attribute is removed if it has its default value 'tappackage' /// [Test] public void WrongNamespace() { string input = @" "; string output = @" "; LoadSaveCompare(input, output); } } }