Source

NContracts / Test.Contracts / UnitTest1.cs

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

namespace Test.Contracts
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

       

        [TestMethod]
        public void TestPrerequisites()
        {
            int[] myParameter = {0,1,2,3};
            try
            {
                Contract.Expect(() => myParameter)
                    .Meet(k => k.First() != 0 && k.Length>1);
                Assert.Fail();
            }
            catch (ArgumentException exc)
            {
                Assert.IsTrue(exc.Message.Contains("myParameter"));
            }
            Contract.Expect(() => myParameter)
                    .Meet(k => k.First() == 0 && k.Length > 1);

        }



        [TestMethod]
        public void TestArgumentOfType()
        {
            int z = 0;
            try
            {
                Contract.Expect(() => z).Is<string>();
                Assert.Fail();
            }
            catch (ArgumentException)
            {
 
            }
            try
            {
                Contract.Expect(() => z).IsNot<int>();
                Assert.Fail();
            }
            catch (ArgumentException)
            {

            }
            Contract.Expect(() => z).Is<int>();

        }
        
        [TestMethod]
        public void TestArgumentNull()
        {
            CallMethodNull(null);
           

        }
        [TestMethod]
        public void TestGtOrEq()
        {
            int z = 123;
            try
            {
                Contract.Expect(() => z)
                      .IsGreatherThan(200)
                      ;
                Assert.Fail();
               
            }
            catch (ArgumentOutOfRangeException)
            { 
            }
            Contract.Expect(() => z)
                     .IsGreatherThanOrEqual(123)
                     ;
            Contract.Expect(() => z)
                .IsGreatherThan(100);
        }

        [TestMethod]
        public void TestLtOrEq()
        {
            int z = 123;
            try
            {
                Contract.Expect(() => z)
                      .IsLessThan(100)
                      ;
                Assert.Fail();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            Contract.Expect(() => z)
                     .IsLessThanOrEq(123)
                     ;
            Contract.Expect(() => z)
                .IsLessThan(124);
        }

        [TestMethod]
        public void TestEq()
        {
            int z = 123;
            try
            {
                Contract.Expect(() => z)
                      .IsNotEqual(123)
                      ;
                Assert.Fail();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            Contract.Expect(() => z)
                     .IsEqual(123)
                     ;
            Contract.Expect(() => z)
                .IsNotEqual(124);
        }

        [TestMethod]
        public void SingleLineMultipleExpect()
        {
            int z = 123;
            int y = 44;

            Contract.Expect(() => z)
                    .IsEqual(123)
                    .Expect(() => y)
                    .IsEqual(44);
                    ;
             
        }



        [TestMethod]
        public void TestOnField()
        {
            int z = 123;
            Contract.Expect(() => z)
                  
                  ;
        }

        [TestMethod]
        public void TestArgumentNotNulll()
        {
            CallMethodNotNull("sss");
        }

        private void CallMethodNotNull(string arg)
        {
            Contract.Expect(() => arg)
                    .IsNotNull()
                    ;
        }
        void CallMethodNull(string arg)
        {
            try
            {
                Contract.Expect(() => arg)
                    .IsNotNull()
                    ;
                Assert.Fail();
            }
            catch (ArgumentNullException )
            {
                
            }
        }
    }
}