Click here to Skip to main content
15,892,575 members
Articles / Programming Languages / XML

Yet Another XML Serialization Library for the .NET Framework

Rate me:
Please Sign up or sign in to vote.
4.92/5 (91 votes)
2 Oct 2012MIT24 min read 516.2K   207  
A flexible XML serialization library that lets developers design the XML file structure, and select the exception handling policy. YAXLib supports polymorphic serialization and serializing generic and non-generic collection classes and arrays.
// Copyright 2009 - 2010 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
//-----------------------------------------------------------------------

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using YAXLib;

namespace YAXLibTests
{
    /// <summary>
    /// Summary description for StringUtilsTest
    /// </summary>
    [TestClass]
    public class StringUtilsTest
    {
        [TestMethod]
        public void RefineElementNameTest()
        {
            Assert.AreEqual(StringUtils.RefineLocationString(".."), "..");
            Assert.AreEqual(StringUtils.RefineLocationString("."), ".");
            Assert.AreEqual(StringUtils.RefineLocationString("      "), ".");
            Assert.AreEqual(StringUtils.RefineLocationString(" /      \\ "), ".");
            Assert.AreEqual(StringUtils.RefineLocationString("ans"), "ans");
            Assert.AreEqual(StringUtils.RefineLocationString("/ans"), "ans");
            Assert.AreEqual(StringUtils.RefineLocationString("/ans/"), "ans");
            Assert.AreEqual(StringUtils.RefineLocationString("ans/"), "ans");
            Assert.AreEqual(StringUtils.RefineLocationString("ans/////"), "ans");
            Assert.AreEqual(StringUtils.RefineLocationString("ans\\\\\\"), "ans");
            Assert.AreEqual(StringUtils.RefineLocationString("..."), "___");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two / three / four "), "one/two/three/four");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two \\ three / four "), "one/two/three/four");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two / three and else / four "), "one/two/three_and_else/four");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two / .. / four "), "one/two/../four");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two / .. / four / "), "one/two/../four");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two / . . / four / "), "one/two/___/four");
            Assert.AreEqual(StringUtils.RefineLocationString("one / two / two:words.are / four "), "one/two/two_words_are/four");
        }

        [TestMethod]
        public void IsLocationAllGenericTest()
        {
            Assert.IsTrue(StringUtils.IsLocationAllGeneric(".."));
            Assert.IsTrue(StringUtils.IsLocationAllGeneric("."));
            Assert.IsTrue(StringUtils.IsLocationAllGeneric("./.."));
            Assert.IsTrue(StringUtils.IsLocationAllGeneric("../.."));

            Assert.IsFalse(StringUtils.IsLocationAllGeneric("../one/.."));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("../one"));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("one/.."));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("one"));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("one/../two"));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("../one/../two"));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("../one/../two/.."));
            Assert.IsFalse(StringUtils.IsLocationAllGeneric("one/../two/.."));
        }

        [TestMethod]
        public void DivideLocationOneStepTest()
        {
            bool returnValue = false;
            string location = "..";
            string newLocation = "";
            string newElement = "";

            location = "..";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, "..");
            Assert.AreEqual(newElement, null);
            Assert.AreEqual(returnValue, false);

            location = ".";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, ".");
            Assert.AreEqual(newElement, null);
            Assert.AreEqual(returnValue, false);

            location = "../..";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, "../..");
            Assert.AreEqual(newElement, null);
            Assert.AreEqual(returnValue, false);

            location = "../../folder";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, "../..");
            Assert.AreEqual(newElement, "folder");
            Assert.AreEqual(returnValue, true);

            location = "../../folder/..";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, "../../folder/..");
            Assert.AreEqual(newElement, null);
            Assert.AreEqual(returnValue, false);

            location = "one/two/three/four";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, "one/two/three");
            Assert.AreEqual(newElement, "four");
            Assert.AreEqual(returnValue, true);

            location = "one";
            returnValue = StringUtils.DivideLocationOneStep(location, out newLocation, out newElement);
            Assert.AreEqual(newLocation, ".");
            Assert.AreEqual(newElement, "one");
            Assert.AreEqual(returnValue, true);

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Australia Australia
A software designer and developer

Comments and Discussions