Browse Source

Fixed unit tests.

pull/23/head
Tomasz Tretkowski 14 years ago
parent
commit
9764e948ec
  1. 42
      src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs
  2. 18
      src/AddIns/Analysis/MachineSpecifications/MachineSpecifications/src/MSpecTestFramework.cs

42
src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs

@ -60,6 +60,15 @@ namespace ICSharpCode.MachineSpecifications.Tests @@ -60,6 +60,15 @@ namespace ICSharpCode.MachineSpecifications.Tests
public abstract class MSpecTestFrameworkFieldsConcern : Observes<MSpecTestFramework>
{
protected static ICompilationUnit CompilationUnit;
Establish ctx = () =>
{
var ProjectContent = fake.an<IProjectContent>();
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 @@ -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<IMember> 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<IReturnType> { SetupReturnType(BehaviorClassName)});
behaviorField = SetupField(MSpecBehavesTypeName);
behaviorClass = new DefaultClass(CompilationUnit, "BehaviorClass");
testSpecificationInBehavior = new DefaultField(itReturnType, "testSpecificationInBehavior", ModifierEnum.None, DomRegion.Empty, behaviorClass);
behaviorClass.Fields.Add(testSpecificationInBehavior);
testSpecificationInBehavior = SetupField(MSpecItTypeName);
testClass = new DefaultClass(CompilationUnit, "TestClass");
testSpecification = new DefaultField(itReturnType, "testSpecification", ModifierEnum.None, DomRegion.Empty, testClass);
testClass.Fields.Add(testSpecification);
otherField = new DefaultField(fake.an<IReturnType>(), "OtherField", ModifierEnum.None, DomRegion.Empty, testClass);
testClass.Fields.Add(otherField);
testClass = fake.an<IClass>();
testClass.setup(x => x.Fields).Return(new List<IField> { testSpecification, otherField, behaviorField });
var behavesLikeReturnType = new ConstructedReturnType(SetupReturnType(MSpecBehavesTypeName), new List<IReturnType>{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 @@ -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");
}
}

18
src/AddIns/Analysis/MachineSpecifications/MachineSpecifications/src/MSpecTestFramework.cs

@ -23,11 +23,11 @@ namespace ICSharpCode.MachineSpecifications @@ -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<IMember> GetTestMembersFor(IClass @class) {
@ -36,9 +36,9 @@ namespace ICSharpCode.MachineSpecifications @@ -36,9 +36,9 @@ namespace ICSharpCode.MachineSpecifications
private IEnumerable<IMember> GetTestMembers(IClass testClass, IList<IField> fields)
{
var result = fields.Where(IsSpecificationMember).Cast<IMember>().ToList();
var result = fields.Where(HasItReturnType).Cast<IMember>().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 @@ -74,7 +74,7 @@ namespace ICSharpCode.MachineSpecifications
{
var fieldReturnType = field.ReturnType.CastToConstructedReturnType();
if (fieldReturnType == null) return new List<IField>();
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 @@ -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));
}

Loading…
Cancel
Save