Skip to content

Commit

Permalink
Merge pull request #344 from robertcoltheart/feature/add-behavior-spe…
Browse files Browse the repository at this point in the history
…c-source

[Feature] Track behavior field from context in behavior spec
  • Loading branch information
ivanz authored Oct 10, 2017
2 parents df5ccca + af05faa commit 5437790
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,23 @@ public class and_the_nested_context_is_generic
}
}
}

[Subject(typeof(ContextFactory))]
public class when_creating_behavior_specifications_and_tracking_original_behavior_field
{
static Context newContext;

Establish context = () =>
{
var factory = new ContextFactory();
newContext = factory.CreateContextFrom(new context_with_behaviors());
};

It should_create_behavior_specs_with_original_behavior_field =
() => newContext.Specifications
.OfType<BehaviorSpecification>()
.First()
.BehaviorFieldInfo.Name.Should().BeEquivalentTo("behavior");

}
}
4 changes: 3 additions & 1 deletion Source/Machine.Specifications/Factories/BehaviorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Behavior CreateBehaviorFrom(FieldInfo behaviorField, Context context)
var behavior = new Behavior(behaviorField.FieldType, behaviorInstance, context, isIgnored);

var itFieldInfos = behaviorType.GetInstanceFieldsOfUsage(new AssertDelegateAttributeFullName());
CreateBehaviorSpecifications(itFieldInfos, behavior);
CreateBehaviorSpecifications(itFieldInfos, behaviorField, behavior);

return behavior;
}
Expand Down Expand Up @@ -126,11 +126,13 @@ static void EnsureContextFieldIsCompatibleType(Context context, FieldInfo contex
}

void CreateBehaviorSpecifications(IEnumerable<FieldInfo> itFieldInfos,
FieldInfo behaviorField,
Behavior behavior)
{
foreach (var itFieldInfo in itFieldInfos)
{
var specification = _specificationFactory.CreateSpecificationFromBehavior(behavior,
behaviorField,
itFieldInfo);
behavior.AddSpecification(specification);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public Specification CreateSpecification(Context context, FieldInfo specificatio
return new Specification(name, specificationField.FieldType, it, isIgnored, specificationField);
}

public Specification CreateSpecificationFromBehavior(Behavior behavior, FieldInfo specificationField)
public Specification CreateSpecificationFromBehavior(Behavior behavior, FieldInfo behaviorField, FieldInfo specificationField)
{
bool isIgnored = behavior.IsIgnored || specificationField.HasAttribute(new IgnoreAttributeFullName());
var it = (Delegate)specificationField.GetValue(behavior.Instance);
string name = specificationField.Name.ToFormat();

return new BehaviorSpecification(name, specificationField.FieldType, it, isIgnored, specificationField, behavior.Context, behavior);
return new BehaviorSpecification(name, specificationField.FieldType, behaviorField, it, isIgnored, specificationField, behavior.Context, behavior);
}
}
}
8 changes: 8 additions & 0 deletions Source/Machine.Specifications/Model/BehaviorSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ namespace Machine.Specifications.Model
{
public class BehaviorSpecification : Specification
{
readonly FieldInfo _behaviorfield;
readonly object _behaviorInstance;
readonly object _contextInstance;
readonly ConventionMapper _mapper;

public BehaviorSpecification(string name,
Type fieldType,
FieldInfo behaviorfield,
Delegate it,
bool isIgnored,
FieldInfo fieldInfo,
Context context,
Behavior behavior)
: base(name, fieldType, it, isIgnored, fieldInfo)
{
_behaviorfield = behaviorfield;
_contextInstance = context.Instance;
_behaviorInstance = behavior.Instance;

_mapper = new ConventionMapper();
}

public FieldInfo BehaviorFieldInfo
{
get { return _behaviorfield; }
}

protected override void InvokeSpecificationField()
{
_mapper.MapPropertiesOf(_contextInstance).To(_behaviorInstance);
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
environment:
nuget_version: '0.11.1'
nuget_version: '0.12.0'
nuget_prerelease: false
assembly_version: '0.11.0.0'
assembly_version: '0.12.0.0'

image: Visual Studio 2017

deploy:
- provider: GitHub
description: |
* Fix ArgumentOutOfRangeException thrown when tests are concurrently writing diagnostic output. (#224 - thanks to dannyvincent)
* Track behavior field from context in behavior specification
on:
appveyor_repo_tag: true
Expand Down

0 comments on commit 5437790

Please sign in to comment.