Tools for better thinking.. Found this site https://untools.co/ and this it looks promising.
Collection of thinking tools and frameworks to help you solve problems, make decisions and understand systems.---
Tools for better thinking.. Found this site https://untools.co/ and this it looks promising.
Collection of thinking tools and frameworks to help you solve problems, make decisions and understand systems.---
Some things are just funny and some things just hit to close to home.
-In computing, the mean time to failure keeps getting shorter.
“There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.” – Jiddu Krishnamurti
Use NCover or dotCover you might say. And you would be right.
But for the current project I was looking for a free solution. I started to google the web for a free alternative and found PartCover, OpenCover and this article.
PartCover and OpenCover does noe have any GUI or plugin in Visual Studio, so when I saw that it was possible to use the same window in VS as MSTest I was sold.
Run the following command in the visual studio prompt. (Add the correct paths and dlls for your system)
vsinstr -coverage MyLibrary.dll start vsperfmon -coverage -output:mytestrun.coverage nunit-console.exe /noshadow UnitTests.dll vsperfcmd -shutdownWhats happening here is that we are first adding instrumentation to the dll. Then we are starting the vsperfmon service to listen to whats beeing executed. Execute the test runner and shutdown the vsperfmon service.
The Test coverage is displayed with the information.
I guess the only catch is that you need Team System Tools for this to work.
Off course you need to manually set this up. Hmm maybe I should write a Visual Studio plugin..
public void SomeMethod(int arg1, int arg2)
{
// This line will throw an exception when the arg1 is less or equal to arg2
Guard.That(() => arg1).IsGreaterThan(arg2);
// This will check that arg1 is not null and that is in some range 1..100
Guard.That(arg2).IsNotNull().IsInRange(1,100);
// Several checks can be added.
Guard.That(arg1)
.IsInRange(100,1000)
.IsEven()
.IsTrue(x => x > 50, "Must be over 500");
// Do stuff
}
public void OtherMethod(int arg1)
{
// Get a list of errors
List<string> errors = Validate.That(() => arg1).IsNotNull().GetResult();
}
---var c = new Container(); c.For<IFoo>().Use<Bar>();
c.For<IFoo>().Use(() => new Bar1("someArgument"));
c.For<IFoo>().Use(ctx => new Bar2(ctx.Get<ISomeinterface>()));
var foo = c.Get<IFoo>();
dsmgmt "Configurable Settings" Connections "connect to server localhost:389" q "Set ADAMDisablePasswordPolicies to 1" "Commit changes" q q
public void SomeMethod(int arg1, int arg2)
{
// This line will throw an exception when the arg1 is less or equal to arg2
Guard.Check(() => arg1).IsGreaterThan(arg2);
// This will check that arg1 is not null and that is in some range 1..100
Guard.Check(arg2).IsNotNull().IsInRange(1,100);
// Do stuff
}
Source code can be found on GitHub
using System;
using System.Xml;
using System.Xml.XPath;
namespace XmlTidy
{
public class Program
{
private enum ExitCodes
{
Success = 0,
Failure = 1
}
private static int Main(string[] args)
{
if (args.Length == 0)
{
return ShowUsage();
}
var inputFile = args[0];
var outputFile = args.Length == 1 ? args[0] : args[1];
return Tidy(inputFile, outputFile);
}
private static int ShowUsage()
{
Console.WriteLine("Usage: XmlTidy <inputfile> <outputfile>");
Console.WriteLine("if <outputfile> is not specified, the <inputfile> is overwritten.");
return (int)ExitCodes.Success;
}
private static int Tidy(string inputFile, string outputFile)
{
var result = (int)ExitCodes.Success;
try
{
var document = new XPathDocument(inputFile);
var settings = new XmlWriterSettings { IndentChars = "\t", Indent = true };
var writer = XmlWriter.Create(outputFile, settings);
document.CreateNavigator().WriteSubtree(writer);
writer.Close();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
result = (int)ExitCodes.Failure;
}
return result;
}
}
}
makecert -r -pe -n "CN=Test" -b 01/01/2010 -e 01/01/2020 -sky exchange Test.cer -sv Test.pvk pvk2pfx.exe -pvk Test.pvk -spc Test.cer -pfx Test.pfxWhen executing the commands above you will be asked for some information like password etc. After you are finished the file Test.pfx includes a self-signed certificate with the a private key.
public class ClassUnderTest
{
private int DoSomePrivateStuff()
{
// Something is happening here
}
}
Since the method is private you can not access the it from the outside of the object.public class TestableClassUnderTest : ClassUnderTest
{
public int DoSomePrivateStuff()
{
base.DoSomePrivateStuff();
}
}
[TestClass]
public class ClassUnderTestTests
{
[TestMethod]
public void DoSomePrivateStuff_WhenCalled_ReturnsZero()
{
//Arrange
var testClass = new TestableClassUnderTest();
//Act
var actual = testClass.DoSomePrivateStuff();
//Assert
Assert.AreEqual(0, actual);
}
}
[TestClass]
public class ClassUnderTestTests
{
[TestMethod]
public void DoSomePrivateStuff_WhenCalled_ReturnsZero()
{
//Arrange
var testClass = new ClassUnderTest_accessor();
//Act
var actual = testClass.DoSomePrivateStuff();
//Assert
Assert.AreEqual(0, actual);
}
}
What's nice about this is that you don't need to create a bunch of testable classes. They are automagically created with reflection for you. Now you got more time to do fun stuff.... :-) You can read more about this here: http://msdn.microsoft.com/en-us/library/bb385974.aspx
[TestMethod]
public void WithTryCatch()
{
// Arrange
ApplicationException actualException = null;
// Act
try
{
ThrowSomeException();
}
catch (ApplicationExceptionex)
{
actualException = ex;
}
// Assert
Assert.IsNotNull(actualException);
Assert.AreEqual("Message", actualException.Message);
}
[TestMethod]
[ExpectedException(typeof(ApplicationException), "ExceptionMessage", true)]
public SomeTest()
{
DoSomething();
}
[TestMethod]
public void WithHelperClass()
{
// Arrange
// Act
// Assert
ExceptionAssert.Throws<ApplicationException>(
() => ThrowSomeException(),
ex => Assert.AreEqual("Message", ex.Message));
}
The Throws method catches the exception specified and will run the asserts on it. If no (or wrong) exception is thrown the test will fail.
[DebuggerStepThrough]
public static class ExceptionAssert
{
///
/// Asserts that an exception of type T is not thrown
///
/// >Typeparam name="T">Exception to look for
/// Action to execute
public static void DoesNotThrow<T>(Action action) where T : Exception
{
if (action == null)
{
throw new ArgumentNullException("action");
}
Exception actualException = null;
try
{
action();
}
catch (T ex)
{
actualException = ex;
}
if (actualException != null)
{
throw new AssertFailedException(String.Format(
"ExceptionAssert.DoesNotThrow failed. Exception <{0}> thrown with message <{1}>",
actualException.GetType().FullName,
actualException.Message));
}
}
///
/// Asserts that an exception is not thrown
///
/// Action to execute
public static void DoesNotThrow(Action action)
{
DoesNotThrow(action);
}
///
/// Asserts that an exception of type T is thrown
///
/// Exception to look for
/// Action to execute
public static void Throws<T>(Action action) where T : Exception
{
if (action == null)
{
throw new ArgumentNullException("action");
}
Exception actualException = null;
try
{
action();
}
catch (Exception ex)
{
actualException = ex;
}
ValidateThrownException<T>(actualException, null);
}
///
/// Asserts that an exception of type T is thrown
///
/// Exception to look for
/// Action to execute
/// Additional assert to be made on the exception
public static void Throws<T>(Action action, Action<T> asserts) where T : Exception
{
if (action == null)
{
throw new ArgumentNullException("action");
}
Exception actualException = null;
try
{
action();
}
catch (Exception ex)
{
actualException = ex;
}
ValidateThrownException(actualException, asserts);
}
///
/// Asserts that an exception of type T is thrown
///
/// Exception to look for
/// Action to execute
/// Additional assert to be made on the exception
/// Cleanup action to be executed.
public static void Throws<T>(Action action, Action<T> asserts, Action finalAction) where T : Exception
{
if (action == null)
{
throw new ArgumentNullException("action");
}
Exception actualException = null;
try
{
action();
}
catch (Exception ex)
{
actualException = ex;
}
finally
{
if (finalAction != null)
{
finalAction();
}
}
ValidateThrownException(actualException, asserts);
}
///
/// Valdidates the exception
///
/// Exception type to look for
/// Exception to validate
/// Additional asserts to be made on the exception
private static void ValidateThrownException<T>(Exception actualException, Action<T> asserts) where T : Exception
{
if (actualException is T)
{
if (asserts != null)
{
asserts(actualException as T);
}
}
else if (actualException == null)
{
throw new AssertFailedException(String.Format(
"ExceptionAssert.Throws failed. No exception was thrown. Expected <{0}>.",
typeof(T).FullName));
}
else
{
throw new AssertFailedException(String.Format(
"ExceptionAssert.Throws failed. Expected <{0}>. Actual <{1}>",
typeof(T).FullName,
actualException.GetType().FullName));
}
}
}
// This method can take a variable number of ints
public int Sum(params int[] values)
{
int sum = 0;
foreach(int value in values)
{
sum += value;
}
return sum;
}
// Method calls
int sum1 = Sum(1, 2, 3, 4);
int sum2 = Sum(1, 2);
// Override with three params
public int Sum(int val1, int val2, int val3) { /* ... */ }
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace TestArgumentParser
{
public class ArgumentParser
{
public string QuoteChars { get; set; }
public string ValueSeparatorChars { get; set; }
public string PrefixChars { get; set; }
public Dictionary Params = new Dictionary();
public string this[string key] { get{ return Params[key]; } }
public ArgumentParser()
{
SetDefaultValues();
string argString = GetComandLineArguments();
Parse(argString);
}
public ArgumentParser(string args)
{
SetDefaultValues();
Parse(args);
}
private static string GetComandLineArguments()
{
string argString = Environment.CommandLine;
argString = argString.Replace("\"" + Process.GetCurrentProcess().MainModule.FileName + "\"", "");
return argString;
}
public void Parse(string arguments)
{
string currentParam = string.Empty;
string currentValue = string.Empty;
bool readingParam = false;
bool readingValue = false;
bool startQuotes = false;
foreach (char c in arguments)
{
if (IsPrefix(c))
{
HandlePrefix(Params, ref currentParam, ref currentValue, ref readingParam);
continue;
}
if (readingParam)
{
HandleParam(ref currentParam, ref readingParam, ref readingValue, c);
continue;
}
if (readingValue)
{
HandleValue(ref currentValue, ref startQuotes, c);
continue;
}
}
if (!string.IsNullOrEmpty(currentParam))
{
Params.Add(currentParam, currentValue);
}
}
private void SetDefaultValues()
{
QuoteChars = "\"\'";
ValueSeparatorChars = ":= ";
PrefixChars = "-/";
}
private void HandlePrefix(Dictionary list, ref string currentParam, ref string currentValue, ref bool readingParam)
{
if (!string.IsNullOrEmpty(currentParam))
{
list.Add(currentParam, currentValue);
}
currentParam = string.Empty;
currentValue = string.Empty;
readingParam = true;
}
private void HandleValue(ref string currentValue, ref bool startQuotes, char c)
{
if (IsQuote(c))
{
startQuotes = !startQuotes;
return;
}
if (!startQuotes && char.IsWhiteSpace(c))
{
return;
}
currentValue += c;
}
private void HandleParam(ref string currentParam, ref bool readingParam, ref bool readingValue, char c)
{
bool isValueSeparator = IsValueSeparator(c);
if (!isValueSeparator)
{
currentParam += c;
}
else
{
readingValue = true;
readingParam = false;
}
}
private bool IsQuote(char c)
{
return QuoteChars.IndexOf(c) > -1;
}
private bool IsValueSeparator(char c)
{
return ValueSeparatorChars.IndexOf(c) > -1;
}
private bool IsPrefix(char c)
{
return PrefixChars.IndexOf(c) > -1;
}
}
}