CustomAssertion.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » CustomAssertion.cs
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using NUnit.Framework;

namespace ThoughtWorks.CruiseControl.UnitTests{
  public class CustomAssertion
  {
    public static void AssertContains(string search, string target)
    {
      string message = string.Format("Search substring: {0} is not contained in target: {1}", search, target);
      Assert.IsTrue(target.IndexOf(search) >= 0, message);
    }

    public static void AssertNotContains(string search, string target)
    {
      string message = string.Format("Search substring: {0} should not be contained in target: {1}", search, target);
      Assert.IsFalse(target.IndexOf(search) >= 0, message);
    }

    public static void AssertContainsInArray(object search, object[] target)
    {
      foreach (object a in target)
      {
        if (a.Equals(search)) return;
      }
      string message = string.Format("Did not find {0} in the array", search);
      Assert.Fail(message);
    }

    public static void AssertStartsWith(string expected, string actual)
    {
      string message = string.Format("<{0}> does not start wth \n<{1}>", actual, expected);
      Assert.IsTrue(actual != null && actual.StartsWith(expected), message);
    }

    public static void AssertMatches(string pattern, string actual)
    {
      string message = string.Format("Pattern string <{0}> does not match \nactual string <{1}>", pattern, actual);
      Assert.IsTrue(Regex.IsMatch(actual, pattern), message);
    }

    public static void AssertFalse(bool assert)
    {
      Assert.IsTrue(!assert);
    }

    public static void AssertFalse(string message, bool assert)
    {
      Assert.IsTrue(!assert, message);
    }

    /// <summary>
    /// Verifies that two objects are of the same Type. Objects are the same Type if the actual 
    /// object is the expected Type, or an instance of the expected Type.
    /// </summary>
    /// <param name="expectedType"></param>
    /// <param name="actual"></param>
    public static void AssertEquals(Type expectedType, object actual)
    {
      if (expectedType == null)
      {
        Assert.AreEqual(expectedType, actual);
        return;
      }
      Assert.IsNotNull(actual, string.Format("object of expected type {0} is null", expectedType.FullName));
      Type actualType = (actual is Type) ? (Type) actual : actual.GetType();
      Assert.AreEqual(expectedType, actualType, "object of the wrong type");
    }

    public static void AssertNotEquals(object expected, object actual)
    {
      string message = string.Format("Values ({0}) and ({1}) should not be equal", expected, actual);
      Assert.IsTrue(!expected.Equals(actual), message);
      Assert.IsTrue(!actual.Equals(expected), message);
    }

    public static void AssertApproximatelyEqual(double expected, double actual, double tolerance)
    {
      AssertApproximatelyEqual(string.Empty, expected, actual, tolerance);
    }

    public static void AssertApproximatelyEqual(string message, double expected, double actual, double tolerance)
    {
      string expectation = string.Format("Expected {0}, but was {1}", expected, actual);
      Assert.IsTrue(Math.Abs(expected - actual) < tolerance, message + expectation);
    }

    public static void AssertEqualArrays(Array expected, Array actual)
    {
      Assert.AreEqual(actual.Length, expected.Length, "Arrays should have same length");

      for (int i = 0; i < expected.Length; i++)
      {
        Assert.AreEqual(expected.GetValue(i), actual.GetValue(i), "Comparing array index " + i);
      }
    }

    public static void AssertXPathExists(string xml, string xpath)
    {
      Assert.IsTrue(SelectNodeIterator(xpath, xml).Count > 0,
                    string.Format("Unable to locate xpath expression <{0}>\n\t in xml <{1}>", xpath, xml));
    }

    public static void AssertXPathNodeValue(string expectedValue, string xml, string xpath)
    {
      string actual = SelectNodeIterator(xpath, xml).Current.Value;
      Assert.AreEqual(expectedValue, actual,
                      string.Format("Expected value <{0}> does not equal <{1}>\n\t in xml <{2}>", xpath, actual, xml));
    }

    private static XPathNodeIterator SelectNodeIterator(string xpath, string xml)
    {
      try
      {
        XPathDocument document = new XPathDocument(new XmlTextReader(new StringReader(xml)));
        return document.CreateNavigator().Select(xpath);
      }
      catch (Exception e)
      {
        throw new AssertionException(string.Format("Unable to parse xml <{0}> or xpath expression <{1}>\n\t{2}", xml, xpath, e), e);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.