Click here to Skip to main content
15,886,066 members
Articles / Programming Languages / C#

Static Code Analysis

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
15 Mar 2010CPOL16 min read 85.8K   1.1K   63  
A static code analyzer building method call networks + sample applications
This article describes the operation of a method-based static code analyzer for .NET that constructs in-memory method call networks of compiled assemblies. You will also see a concrete application of static code analysis to generate a website providing easy insights on a sample application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToDoSample.Service;
using ToDoSample.Testing;
using ToDoSample.Contract;

namespace ToDoSample.UnitTests
{
    /// <summary>
    /// UnitTests for ToDoCOmponentManager operations.
    /// </summary>
    [TestClass]
    public class ToDoComponentManagerTests
    {
        [TestMethod]
        public void ListAllUsersTest()
        {
            var result = ToDoComponentManager.ListAllUsers();
            
            result.AssertNotEmpty("Result should not be empty.");
        }

        [TestMethod]
        public void ListToDoItemsForUserTest()
        {
            var result = ToDoComponentManager.ListToDoItemsForUser(3);
            
            result.AssertNotEmpty("Result should not be empty.");
            result.AssertAllTrue(p => !p.IsDone, "ToDoItem should not be marked done.");
        }

        [TestMethod]
        public void ListDoneItemsForUserTest()
        {
            var result = ToDoComponentManager.ListDoneItemsForUser(1);
            
            result.AssertNotEmpty("Result should not be empty.");
            result.AssertAllTrue(p => p.IsDone, "ToDoItem should be marked done.");
        }

        [TestMethod]
        public void CreateToDoItemTest()
        {
            var nowIn8Days = DateTime.Today.AddDays(8);
            var result = ToDoComponentManager.CreateToDoItem(
                3,
                "New ToDoItem for Testing",
                nowIn8Days);

            Assert.AreEqual(3, result.Owner.Id);
            Assert.AreEqual("New ToDoItem for Testing", result.Text);
            Assert.IsTrue(result.DueDate.HasValue, "DueDate should be filled-in.");
            Assert.AreEqual(nowIn8Days, result.DueDate.Value);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidIdException))]
        public void CreateToDoItemInvalidUserIdTest()
        {
            var result = ToDoComponentManager.CreateToDoItem(
                -1,
                "New ToDoItem for Testing",
                null);
        }

        [TestMethod]
        public void MarkToDoItemDoneTest()
        {
            int todoItemId = 12;

            var before = ToDoComponentManager.GetToDoItem(todoItemId);
            Assert.IsFalse(before.IsDone, "Precondition of test failed: toDoItem should not yet be done.");

            ToDoComponentManager.MarkToDoItemDone(todoItemId);
            var after = ToDoComponentManager.GetToDoItem(todoItemId);

            Assert.AreEqual(before.Id, after.Id);
            Assert.AreEqual(before.DueDate, after.DueDate);
            Assert.IsTrue(after.IsDone, "ToDoItem should be marked done.");
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Architect AREBIS
Belgium Belgium
Senior Software Architect and independent consultant.

Comments and Discussions