diff --git a/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs b/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs index a676936450..1383f63697 100644 --- a/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs +++ b/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs @@ -60,6 +60,15 @@ namespace ICSharpCode.MachineSpecifications.Tests public abstract class MSpecTestFrameworkFieldsConcern : Observes { + protected static ICompilationUnit CompilationUnit; + + Establish ctx = () => + { + var ProjectContent = fake.an(); + ProjectContent.setup(x => x.SystemTypes).Return(new SystemTypes(ProjectContent)); + CompilationUnit = new DefaultCompilationUnit(ProjectContent); + }; + protected const string MSpecItTypeName = "Machine.Specifications.It"; protected const string MSpecBehavesTypeName = "Machine.Specifications.Behaves_like"; protected const string MSpecBehaviorTypeName = "Machine.Specifications.BehaviorsAttribute"; @@ -139,30 +148,35 @@ namespace ICSharpCode.MachineSpecifications.Tests public class When_enumerating_test_members : MSpecTestFrameworkFieldsConcern { + static IClass behaviorClass; + static IField testSpecificationInBehavior; + static IClass testClass; static IField testSpecification; static IField otherField; - static IField behaviorField; - static IField testSpecificationInBehavior; + static IField behavesLikeField; + static IEnumerable result; - static IClass behaviorClass; const string BehaviorClassName = "Test.Behavior"; Establish ctx = () => { var itReturnType = SetupReturnType(MSpecItTypeName); - - testSpecification = SetupField(MSpecItTypeName); - otherField = SetupField("TestType"); - - var behaviorFieldReturnType = new ConstructedReturnType(SetupReturnType(MSpecBehavesTypeName), new List { SetupReturnType(BehaviorClassName)}); - behaviorField = SetupField(MSpecBehavesTypeName); - - testSpecificationInBehavior = SetupField(MSpecItTypeName); - - testClass = fake.an(); - testClass.setup(x => x.Fields).Return(new List { testSpecification, otherField, behaviorField }); + + behaviorClass = new DefaultClass(CompilationUnit, "BehaviorClass"); + testSpecificationInBehavior = new DefaultField(itReturnType, "testSpecificationInBehavior", ModifierEnum.None, DomRegion.Empty, behaviorClass); + behaviorClass.Fields.Add(testSpecificationInBehavior); + + testClass = new DefaultClass(CompilationUnit, "TestClass"); + testSpecification = new DefaultField(itReturnType, "testSpecification", ModifierEnum.None, DomRegion.Empty, testClass); + testClass.Fields.Add(testSpecification); + otherField = new DefaultField(fake.an(), "OtherField", ModifierEnum.None, DomRegion.Empty, testClass); + testClass.Fields.Add(otherField); + + var behavesLikeReturnType = new ConstructedReturnType(SetupReturnType(MSpecBehavesTypeName), new List{new DefaultReturnType(behaviorClass)}); + behavesLikeField = new DefaultField(behavesLikeReturnType, "behavesLikeField", ModifierEnum.None, new DomRegion(), testClass); + testClass.Fields.Add(behavesLikeField); }; Because of = () => result = sut.GetTestMembersFor(testClass); @@ -170,10 +184,10 @@ namespace ICSharpCode.MachineSpecifications.Tests It should_contain_field_with_it_return_type = () => result.ShouldContain(testSpecification); - It should_not_containt_field_with_arbitrary_return_type = () => + It should_not_contain_field_with_arbitrary_return_type = () => result.ShouldNotContain(otherField); - It should_containt_it_field_from_behavior = () => - result.ShouldContain(testSpecificationInBehavior); + It should_contain_imported_field_from_behavior = () => + result.ShouldContain(member => member.FullyQualifiedName == "TestClass.testSpecificationInBehavior"); } } \ No newline at end of file diff --git a/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications/src/MSpecTestFramework.cs b/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications/src/MSpecTestFramework.cs index 34421e935d..b7d0220fd6 100644 --- a/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications/src/MSpecTestFramework.cs +++ b/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications/src/MSpecTestFramework.cs @@ -23,11 +23,11 @@ namespace ICSharpCode.MachineSpecifications { public bool IsTestMember(IMember member) { return member is IField - && IsSpecificationMember(member as IField); + && HasItReturnType(member as IField); } public bool IsTestClass(IClass c) { - return HasSpecificationMembers(c) && !IsBehavior(c); + return HasSpecificationMembers(c) && !HasBehaviorAttribute(c); } public IEnumerable GetTestMembersFor(IClass @class) { @@ -36,9 +36,9 @@ namespace ICSharpCode.MachineSpecifications private IEnumerable GetTestMembers(IClass testClass, IList fields) { - var result = fields.Where(IsSpecificationMember).Cast().ToList(); + var result = fields.Where(HasItReturnType).Cast().ToList(); foreach (var field in fields) - if (IsBehaviorReference(field)) + if (HasBehavesLikeReturnType(field)) { var behaviorFields = ResolveBehaviorFieldsOf(field); var behaviorTestMembers = GetTestMembers(testClass, behaviorFields); @@ -74,7 +74,7 @@ namespace ICSharpCode.MachineSpecifications { var fieldReturnType = field.ReturnType.CastToConstructedReturnType(); if (fieldReturnType == null) return new List(); - if (fieldReturnType.TypeArgumentCount != 1) + if (fieldReturnType.TypeArguments.Count != 1) LoggingService.Error(string.Format("Expected behavior specification {0} to have one type argument but {1} found.", field.FullyQualifiedName, fieldReturnType.TypeArgumentCount)); var behaviorClassType = fieldReturnType.TypeArguments.FirstOrDefault(); @@ -83,18 +83,18 @@ namespace ICSharpCode.MachineSpecifications private bool HasSpecificationMembers(IClass c) { return !c.IsAbstract - && c.Fields.Any(IsSpecificationMember); + && c.Fields.Any(f => HasItReturnType(f) || HasBehavesLikeReturnType(f)); } - private bool IsBehaviorReference(IField field) { + private bool HasBehavesLikeReturnType(IField field) { return MSpecBehavesLikeFQName.Equals(field.ReturnType.FullyQualifiedName); } - private bool IsSpecificationMember(IField field) { + private bool HasItReturnType(IField field) { return MSpecItFQName.Equals(field.ReturnType.FullyQualifiedName); } - private bool IsBehavior(IClass c) { + private bool HasBehaviorAttribute(IClass c) { return c.Attributes.Any( attribute => MSpecBehaviorsAttributeFQName.Equals(attribute.AttributeType.FullyQualifiedName)); }