Skip to content

Commit

Permalink
Finally have it so we can get values back from the struct matching sy…
Browse files Browse the repository at this point in the history
…stem
  • Loading branch information
Aaronontheweb committed Nov 22, 2012
1 parent 660d048 commit 691c17b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
15 changes: 13 additions & 2 deletions Faker.Tests/MatcherTests/StructMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ public class StructMatcherTests
{
private Matcher _matcher;

#region Custom structs

public struct TestStruct
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime When { get; set; }
}

#endregion

#region Setup / Teardown

[SetUp]
Expand All @@ -26,9 +37,9 @@ public void SetUp()
[Test(Description = "Matcher should be able to match simple DateTime structs if needed")]
public void Should_Bind_DateTime()
{
var dateTimeTest = new DateTime();
object dateTimeTest = new DateTime();

_matcher.Match(dateTimeTest);
_matcher.MatchStruct<DateTime>(ref dateTimeTest);

/* Assert that we populated all of the fields of the DateTime object */
Assert.AreNotEqual(DateTime.MinValue, dateTimeTest);
Expand Down
13 changes: 11 additions & 2 deletions Faker/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ public Matcher(TypeTable table)
}


public virtual S MatchStruct<S>(S targetStruct) where S : struct
public virtual object MatchStruct<S>(ref object targetStruct) where S : struct
{

//Evaluate all of the possible selectors and find the first available match
var selector = EvaluateSelectors(typeof(S), TypeMap.GetSelectors(typeof(S)));

//We found a matching selector
if (!(selector is MissingSelector))
{
selector.Generate(ref targetStruct); //Bind the object's value directly
}

return targetStruct;
}

/// <summary>
Expand Down

0 comments on commit 691c17b

Please sign in to comment.