Create a gist now

Instantly share code, notes, and snippets.

Test Structure
public class MeasuredInput
{
public string Identifier { get; set; }
public int MeasuredValue { get; set; }
public double Confidence { get; set; }
}
public class Measurement
{
public Measurement(string primaryId, string secondaryId, int value, double adjustedMeasurement)
{
PrimaryId = primaryId;
SecondaryId = secondaryId;
Value = value;
AdjustedValue = adjustedMeasurement;
}
public string PrimaryId { get; private set; }
public string SecondaryId { get; private set; }
public int Value { get; private set; }
public double AdjustedValue { get; private set; }
}
public class Mapper
{
public Measurement Map(MeasuredInput input)
{
var stringInParts = input.Identifier.Split('|');
return new Measurement(stringInParts[0], stringInParts[1], input.MeasuredValue, input.Confidence*input.MeasuredValue);
}
}
public class MapperTests
{
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenTheMeasurementIsCorrect()
{
var input = new MeasuredInput {MeasuredValue = 3, Confidence = 0.5, Identifier = "PrimaryId|SecondaryId"};
var output = new Mapper().Map(input);
Assert.That(output.PrimaryId, Is.EqualTo("PrimaryId"), "PrimaryId");
Assert.That(output.SecondaryId, Is.EqualTo("SecondaryId"), "SecondaryId");
Assert.That(output.Value, Is.EqualTo(3), "Value");
Assert.That(output.AdjustedValue, Is.EqualTo(1.5).Within(0.001), "AdjustedValue");
}
}
public class MapperTests
{
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenTheIdIsMappedCorrectly()
{
var input = new MeasuredInput { Identifier = "PrimaryId|SecondaryId" };
var output = new Mapper().Map(input);
Assert.That(output.PrimaryId, Is.EqualTo("PrimaryId"), "PrimaryId");
Assert.That(output.SecondaryId, Is.EqualTo("SecondaryId"), "SecondaryId");
}
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenTheValueIsMappedCorrectly()
{
var input = new MeasuredInput { MeasuredValue = 3, Confidence = 0.5 };
var output = new Mapper().Map(input);
Assert.That(output.Value, Is.EqualTo(3), "Value");
Assert.That(output.AdjustedValue, Is.EqualTo(1.5).Within(0.001), "AdjustedValue");
}
}
public class MapperTests
{
private const string PrimaryId = "PrimaryId";
private const string SecondaryId = "SecondaryId";
private const int Measurement = 3;
private const double Confidence = 0.5;
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenThePrimaryIdIsCorrect()
{
var input = ArrangeValidInput();
var output = Act(input);
Assert.That(output.PrimaryId, Is.EqualTo(PrimaryId));
}
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenTheSecondaryIdIsCorrect()
{
var input = ArrangeValidInput();
var output = Act(input);
Assert.That(output.SecondaryId, Is.EqualTo(SecondaryId));
}
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenTheValueIsCorrect()
{
var input = ArrangeValidInput();
var output = Act(input);
Assert.That(output.Value, Is.EqualTo(Measurement));
}
[Test]
public void GivenAValidMeasurementInput_WhenMappingToAMeasurement_ThenTheAdjustedValueIsCorrect()
{
var input = ArrangeValidInput();
var output = Act(input);
Assert.That(output.AdjustedValue, Is.EqualTo(Measurement * Confidence).Within(0.001));
}
private Measurement Act(MeasuredInput input)
{
return new Mapper().Map(input);
}
private MeasuredInput ArrangeValidInput()
{
return new MeasuredInput
{
MeasuredValue = Measurement,
Confidence = Confidence,
Identifier = string.Format("{0}|{1}", PrimaryId, SecondaryId)
};
}
}
class GivenAValidMeasurementInput_WhenMappingToAMeasurement
{
const string PrimaryId = "PrimaryId";
const string SecondaryId = "SecondaryId";
const int Measurement = 3;
const double Confidence = 0.5;
private readonly Measurement _output;
public GivenAValidMeasurementInput_WhenMappingToAMeasurement()
{
var measurement = new MeasuredInput
{
MeasuredValue = Measurement,
Confidence = Confidence,
Identifier = string.Format("{0}|{1}", PrimaryId, SecondaryId)
};
_output = new Mapper().Map(measurement);
}
[Test]
public void ThenThePrimaryIdIsCorrect()
{
Assert.That(_output.PrimaryId, Is.EqualTo(PrimaryId));
}
[Test]
public void ThenTheSecondaryIdIsCorrect()
{
Assert.That(_output.SecondaryId, Is.EqualTo(SecondaryId));
}
[Test]
public void ThenTheValueIsCorrect()
{
Assert.That(_output.Value, Is.EqualTo(Measurement));
}
[Test]
public void ThenTheAdjustedValueIsCorrect()
{
Assert.That(_output.AdjustedValue, Is.EqualTo(Measurement * Confidence).Within(0.001));
}
}
public abstract class ConcernFor<T>
{
[SetUp]
public virtual void Setup()
{
Subject = Given();
When();
}
protected abstract T Given();
protected virtual void When() {}
protected T Subject { get; private set; }
}
class GivenAValidMeasurementInput_WhenMappingToAMeasurement2 : ConcernFor<Mapper>
{
const string PrimaryId = "PrimaryId";
const string SecondaryId = "SecondaryId";
const int Measurement = 3;
const double Confidence = 0.5;
private Measurement _output;
private MeasuredInput _measurement;
protected override Mapper Given()
{
_measurement = new MeasuredInput
{
MeasuredValue = Measurement,
Confidence = Confidence,
Identifier = string.Format("{0}|{1}", PrimaryId, SecondaryId)
};
return new Mapper();
}
protected override void When()
{
_output = Subject.Map(_measurement);
}
[Test]
public void ThenThePrimaryIdIsCorrect()
{
Assert.That(_output.PrimaryId, Is.EqualTo(PrimaryId));
}
[Test]
public void ThenTheSecondaryIdIsCorrect()
{
Assert.That(_output.SecondaryId, Is.EqualTo(SecondaryId));
}
[Test]
public void ThenTheValueIsCorrect()
{
Assert.That(_output.Value, Is.EqualTo(Measurement));
}
[Test]
public void ThenTheAdjustedValueIsCorrect()
{
Assert.That(_output.AdjustedValue, Is.EqualTo(Measurement * Confidence).Within(0.001));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment