Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created September 26, 2013 12:08
Show Gist options
  • Save ToJans/6713257 to your computer and use it in GitHub Desktop.
Save ToJans/6713257 to your computer and use it in GitHub Desktop.
New spike for property based testing
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Propern.Specs
{
[TestClass]
public class Math_operations
{
static Func<int> an_integer = Spec.Given(R => R.Next());
static Func<int> a_small_integer = Spec.Given(R => R.Next(-10, 10));
static Func<int> a_small_integer_not_0 = Spec.Given(_ => a_small_integer(), x => x != 0);
[TestMethod]
public void Subtract_2_numbers()
{
Spec.Given(_ => new { number_to_subtract_from = an_integer(), number_to_subtract_by = a_small_integer() })
.Times(100)
.When(givens => givens.number_to_subtract_from - givens.number_to_subtract_by)
.Assert((givens, result) => Assert.AreEqual(givens.number_to_subtract_by + result, givens.number_to_subtract_from))
.Check();
}
[TestMethod]
public void Divide_2_numbers()
{
Spec.Given(_ => new { number_to_divide = an_integer(), number_to_divide_by = a_small_integer_not_0() })
.Times(100)
.When(givens => givens.number_to_divide / givens.number_to_divide_by)
.Assert((i, o) => Assert.IsTrue(Math.Abs(o * i.number_to_divide_by) <= Math.Abs(i.number_to_divide)))
.Check();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment