Skip to content

Instantly share code, notes, and snippets.

@starteleport
Created January 21, 2013 10:49
Show Gist options
  • Save starteleport/4585220 to your computer and use it in GitHub Desktop.
Save starteleport/4585220 to your computer and use it in GitHub Desktop.
#region Types
public class TestStep
{
public string Step { get; set; }
}
internal class Test_Int
{
private BindingList<TestStep> _steps;
public Test_Int()
{
InitializeStepsList();
}
public bool NotNullSetterCalled = false;
private void InitializeStepsList(IList<TestStep> init = null)
{
_steps = new BindingList<TestStep>(init ?? new List<TestStep>());
if (init != null) NotNullSetterCalled = true;
}
[MapField(Storage = "Steps")]
public IList<TestStep> Steps
{
get { return _steps; }
private set { InitializeStepsList(value); }
}
}
public class Test_Pub
{
private BindingList<TestStep> _steps;
public Test_Pub()
{
InitializeStepsList();
}
public bool NotNullSetterCalled = false;
private void InitializeStepsList(IList<TestStep> init = null)
{
_steps = new BindingList<TestStep>(init ?? new List<TestStep>());
if (init != null) NotNullSetterCalled = true;
}
[MapField(Storage = "Steps")]
public IList<TestStep> Steps
{
get { return _steps; }
private set { InitializeStepsList(value); }
}
}
public class TestStepRecord
{
public string Step { get; set; }
}
public class TestRecord
{
public List<TestStepRecord> Steps;
}
#endregion
[TestMethod]
public void TestMapFieldSetter()
{
//Arrange
var tta = TypeAccessor.GetAccessor<Test_Int>();
var ma = ExprMemberAccessor.GetMemberAccessor(tta, "Steps");
var test = (Test_Int)tta.CreateInstance();
var list = new List<TestStep>() { new TestStep() };
//Act
ma.SetValue(test, list);
//Assert
Assert.IsTrue(test.Steps.Count == 1);
}
[TestMethod]
public void TestExpressionMapper_Int()
{
//Arrange
var em = new ExpressionMapper<TestRecord, Test_Int>();
var tr = new TestRecord {Steps = new List<TestStepRecord> {new TestStepRecord {Step = "Test"}}};
//Act
var t = em.GetMapper()(tr);
//Assert
Assert.IsTrue(t.Steps.Count == 1);
Assert.IsTrue(t.NotNullSetterCalled);
}
[TestMethod]
public void TestExpressionMapper_Pub()
{
//Arrange
var em = new ExpressionMapper<TestRecord, Test_Pub>();
var tr = new TestRecord { Steps = new List<TestStepRecord> { new TestStepRecord { Step = "Test" } } };
//Act
var t = em.GetMapper()(tr);
//Assert
Assert.IsTrue(t.Steps.Count == 1);
Assert.IsTrue(t.NotNullSetterCalled);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment