Skip to content

Commit

Permalink
Merge pull request petabridge#8 from AOA/master
Browse files Browse the repository at this point in the history
Quick fix for petabridge#7 Fake<string> generates only string.empty values
  • Loading branch information
Aaronontheweb committed Oct 23, 2013
2 parents d2f3b75 + f46c253 commit acd4a40
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
10 changes: 10 additions & 0 deletions Faker.Tests/FakeTests/FakeTests.cs
Expand Up @@ -125,6 +125,16 @@ public void Should_Fake_Single_Instance_of_ClassThatContainsStruct()
Assert.IsNotNullOrEmpty(classWithStructInstance.Name);
}

[Test(Description = "Should be able to fake a single instance of string which has matching selectors.")]
public void Should_Fake_Single_Instance_of_String()
{
var fake = new Fake<string>();

var stringInstance = fake.Generate();

Assert.IsNotNullOrEmpty(stringInstance);
}

#endregion
}
}
2 changes: 1 addition & 1 deletion Faker/Fake.cs
Expand Up @@ -41,7 +41,7 @@ public T Generate()
else
{
//Match all of the properties of the object and come up with the most reasonable guess we can as to the type of data needed
_matcher.Match(instance);
instance = _matcher.Match(instance);
}

//Return the instance once matching is complete
Expand Down
20 changes: 14 additions & 6 deletions Faker/Matcher.cs
Expand Up @@ -36,19 +36,24 @@ public Matcher(TypeTable table)
/// </summary>
/// <typeparam name="T">a class with a parameterless constructor (POCO class)</typeparam>
/// <param name="targetObject">an instance of the class</param>
public virtual void Match<T>(T targetObject)
/// <returns>The instance whose value has replaced.</returns>
public virtual T Match<T>(T targetObject)
{
//Check to see if we have a TypeSelector that matches the entire object wholesale first

//If we don't have a mapper for the wholesale class, map the properties and bind them individually
if (!MapFromSelector(targetObject, typeof (T)))
bool isMatched = false;
T generatedObject = (T)MapFromSelector(targetObject, typeof(T), out isMatched);
if (!isMatched)
{
//Get all of the properties of the class
var properties = typeof(T).GetProperties();

ProcessProperties(properties, targetObject);

}

return generatedObject;
}

/// <summary>
Expand Down Expand Up @@ -227,8 +232,9 @@ protected virtual bool MapFromSelector(PropertyInfo property, object targetObjec
/// </summary>
/// <param name="targetObject">The target object who's value will be replaced</param>
/// <param name="propertyType">The type of the object</param>
/// <returns>true if a match was made and bound successfully; false otherwise</returns>
protected virtual bool MapFromSelector(object targetObject, Type propertyType)
/// <param name="isMached">true if a match was made and bound successfully; false otherwise</param>
/// <returns>The instance whose value has replaced.</returns>
protected virtual object MapFromSelector(object targetObject, Type propertyType, out bool isMached)
{
//Determine if we have a selector-on-hand for this data type
var selectorCount = TypeMap.CountSelectors(propertyType);
Expand All @@ -243,10 +249,12 @@ protected virtual bool MapFromSelector(object targetObject, Type propertyType)
if (!(selector is MissingSelector))
{
selector.Generate(ref targetObject); //Bind the object's value directly
return true;
isMached = true;
return targetObject;
}
}
return false;
isMached = false;
return targetObject;
}

/// <summary>
Expand Down

0 comments on commit acd4a40

Please sign in to comment.