// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; using System.Linq; using developwithpassion.specifications.extensions; using developwithpassion.specifications.rhinomocks; using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.UnitTesting; using Machine.Specifications; using Rhino.Mocks; namespace ICSharpCode.MachineSpecifications.Tests { public abstract class MSpecTestProjectFieldsConcern : Observes { Establish ctx = () => { }; protected const string MSpecItTypeName = "Machine.Specifications.It"; protected const string MSpecBehavesTypeName = "Machine.Specifications.Behaves_like"; protected const string MSpecBehaviorTypeName = "Machine.Specifications.BehaviorsAttribute"; protected static ITypeDefinition SetupClass(bool isAbstract, IList fields, IList attributes) { var c = fake.an(); c.setup(x => x.IsAbstract).Return(isAbstract); c.setup(x => x.Fields).Return(fields); c.setup(x => x.Attributes).Return(attributes); return c; } protected static IField SetupField(string returnTypeName) { var field = fake.an(); field.setup(f => f.ReturnType).Return(SetupReturnType(returnTypeName)); return field; } protected static IAttribute SetupBehaviorAttribute() { var attribute = fake.an(); attribute.setup(x => x.AttributeType).Return(SetupReturnType(MSpecBehaviorTypeName)); return attribute; } protected static IType SetupReturnType(string typeName) { var returnType = fake.an(); returnType.Stub(x => x.FullName).Return(typeName); return returnType; } } [Subject(typeof(MSpecTestProject))] public class When_checking_if_is_a_test_class : MSpecTestProjectFieldsConcern { static ITypeDefinition classWithoutSpecificationMembers; static ITypeDefinition classWithSpecificationMembers; static ITypeDefinition classWithBehavior; static ITypeDefinition classWithSpecificationMembersAndBehaviorAttribute; static bool resultForClassWithBehaviorAttribute; static bool resultForClassWithSpecifications; static bool resultForClassWithBehavior; static bool resultForClassWithoutSpecifications; Establish ctx = () => { classWithoutSpecificationMembers = SetupClass(false, new IField[0], new IAttribute[0]); classWithSpecificationMembers = SetupClass(false, new IField[] { SetupField(MSpecItTypeName) }, new IAttribute[0]); classWithBehavior = SetupClass(false, new IField[] { SetupField(MSpecBehavesTypeName) }, new IAttribute[0]); classWithSpecificationMembersAndBehaviorAttribute = SetupClass(false, new IField[] { SetupField(MSpecItTypeName) }, new IAttribute[] { SetupBehaviorAttribute() }); }; Because of = () => { resultForClassWithoutSpecifications = sut.CallIsTestClass(classWithoutSpecificationMembers); resultForClassWithSpecifications = sut.CallIsTestClass(classWithSpecificationMembers); resultForClassWithBehavior = sut.CallIsTestClass(classWithBehavior); resultForClassWithBehaviorAttribute = sut.CallIsTestClass(classWithSpecificationMembersAndBehaviorAttribute); }; It should_return_false_for_class_without_specification_members = () => resultForClassWithoutSpecifications.ShouldBeFalse(); It should_return_true_for_class_with_specification_members = () => resultForClassWithSpecifications.ShouldBeTrue(); It should_return_true_for_class_with_behavior = () => resultForClassWithBehavior.ShouldBeTrue(); It should_return_false_for_class_with_behavior_attribute = () => resultForClassWithBehaviorAttribute.ShouldBeFalse(); } public class When_enumerating_test_members : MSpecTestProjectFieldsConcern { static ITypeDefinition behaviorClass; static IField testSpecificationInBehavior; static ITypeDefinition testClass; static IField testSpecification; static IField otherField; static IField behavesLikeField; static IEnumerable result; const string BehaviorClassName = "Test.Behavior"; Establish ctx = () => { var itReturnType = SetupReturnType(MSpecItTypeName); testSpecificationInBehavior = fake.an(); testSpecificationInBehavior.setup(x => x.ReturnType).Return(itReturnType); var behaviorClassFields = new IField[] { testSpecificationInBehavior }; behaviorClass = SetupClass(false, behaviorClassFields, new IAttribute[0]); behaviorClass.Stub(b => b.GetFields()).Return(behaviorClassFields); testSpecification = fake.an(); testSpecification.setup(f => f.FullName).Return("TestClass.testSpecificationInBehavior"); testSpecification.setup(f => f.ReturnType).Return(itReturnType); otherField = fake.an(); otherField.setup(f => f.ReturnType).Return(fake.an()); var behavesLikeReturnType = SetupReturnType(MSpecBehavesTypeName); behavesLikeReturnType.setup(t => t.TypeArguments).Return(new List { behaviorClass }); behavesLikeField = fake.an(); behavesLikeField.setup(f => f.ReturnType).Return(behavesLikeReturnType); testClass = SetupClass(false, new IField[] { testSpecification, otherField, behavesLikeField }, new IAttribute[0]); }; Because of = () => result = sut.GetTestMembersFor(testClass); It should_contain_field_with_it_return_type = () => result.Select(m => m.Member).ShouldContain(testSpecification); It should_not_contain_field_with_arbitrary_return_type = () => result.Select(m => m.Member).ShouldNotContain(otherField); It should_contain_imported_field_from_behavior = () => result.Select(m => m.Member).ShouldContain(member => member.FullName == "TestClass.testSpecificationInBehavior"); } }