Esempio n. 1
0
 public Behavior(object instance, Context context, bool isIgnored)
 {
     _instance = instance;
       _context = context;
       IsIgnored = isIgnored;
       _specifications = new List<Specification>();
 }
    public Behavior CreateBehaviorFrom(FieldInfo behaviorField, Context context)
    {
      Type behaviorType = behaviorField.FieldType.GetGenericArguments().First();

      if(!behaviorType.HasAttribute<BehaviorsAttribute>())
      {
        throw new SpecificationUsageException("Behaviors require the BehaviorsAttribute on the type containing the Specifications. Attribute is missing from " + behaviorType.FullName);
      }

      object behaviorInstance = Activator.CreateInstance(behaviorType);

      if (behaviorType.GetPrivateFieldsOfType<Establish>().Any())
      {
        throw new SpecificationUsageException("You cannot have Establishs on Behaviors. Establish found in " + behaviorType.FullName);
      }
      
      if (behaviorType.GetPrivateFieldsOfType<Because>().Any())
      {
        throw new SpecificationUsageException("You cannot have Becauses on Behaviors. Because found in " + behaviorType.FullName);
      }

      if (behaviorType.GetPrivateFieldsWith(typeof(Behaves_like<>)).Any())
      {
        throw new SpecificationUsageException("You cannot nest Behaviors. Nested Behaviors found in " + behaviorType.FullName);
      }

      var isIgnored = behaviorField.HasAttribute<IgnoreAttribute>() ||
                      behaviorInstance.GetType().HasAttribute<IgnoreAttribute>();
      var behavior = new Behavior(behaviorInstance, context, isIgnored);

      IEnumerable<FieldInfo> itFieldInfos = behaviorType.GetPrivateFieldsOfType<It>();
      CreateBehaviorSpecifications(itFieldInfos, behavior);

      return behavior;
    }
    public IEnumerable<Result> Run(Context context, ISpecificationRunListener listener, RunOptions options, IEnumerable<ICleanupAfterEveryContextInAssembly> globalCleanups, IEnumerable<ISupplementSpecificationResults> resultSupplementers)
    {
      IEnumerable<Result> results;
      listener.OnContextStart(context.GetInfo());
      Result result = Result.Pass();

      if (context.HasExecutableSpecifications)
      {
        result = context.EstablishContext();
      }

      if (result.Passed)
      {
        results = RunSpecifications(context, listener, options, resultSupplementers);
      }
      else
      {
        results = FailSpecifications(context, listener, result, resultSupplementers);
      }

      if (context.HasExecutableSpecifications)
      {
        result = context.Cleanup();
        foreach (var cleanup in globalCleanups)
        {
          cleanup.AfterContextCleanup();
        }
      }

      listener.OnContextEnd(context.GetInfo());

      return results;
    }
    public IEnumerable<Result> Run(Context context, ISpecificationRunListener listener, RunOptions options)
    {
      IEnumerable<Result> results;
      listener.OnContextStart(context.GetInfo());
      Result result = Result.Pass();

      if (context.HasExecutableSpecifications)
      {
        result = context.EstablishContext();
      }

      if (result.Passed)
      {
        results = RunSpecifications(context, listener, options);
      }
      else
      {
        results = FailSpecifications(context, listener, options, result);
      }

      if (context.HasExecutableSpecifications)
      {
        result = context.Cleanup();
      }

      listener.OnContextEnd(context.GetInfo());

      return results;
    }
 public Behavior(Type fieldType, object instance, Context context, bool isIgnored)
 {
     _leader = fieldType.ToFormat();
     _instance = instance;
     _context = context;
     IsIgnored = isIgnored;
     _specifications = new List<Specification>();
 }
    public Specification CreateSpecification(Context context, FieldInfo specificationField)
    {
      bool isIgnored = context.IsIgnored || specificationField.HasAttribute<IgnoreAttribute>();
      It it = (It) specificationField.GetValue(context.Instance);
      string name = specificationField.Name.ReplaceUnderscores();

      return new Specification(name, it, isIgnored, specificationField);
    }
    public Specification CreateSpecification(Context context, FieldInfo specificationField)
    {
      bool isIgnored = context.IsIgnored || specificationField.HasAttribute<IgnoreAttribute>();
      Then then = (Then) specificationField.GetValue(context.Instance);
      string name = specificationField.Name.ToFormat();

      return new Specification(name, then, isIgnored, specificationField);
    }
    public Specification CreateSpecification(Context context, FieldInfo specificationField)
    {
      bool isIgnored = context.IsIgnored || specificationField.HasAttribute(new IgnoreAttributeFullName());
      var it = (Delegate) specificationField.GetValue(context.Instance);
      string name = specificationField.Name.ToFormat();

      return new Specification(name, specificationField.FieldType, it, isIgnored, specificationField);
    }
 void EnsureContextFieldIsCompatibleType(Context context, FieldInfo contextField, FieldInfo requiredField, Type behaviorType)
 {
   if (!requiredField.FieldType.IsAssignableFrom(contextField.FieldType))
   {
     string format = "The context {0} behaves like {1} but defines the behavior field {2} {3} using an incompatible type.";
     string message = String.Format(format, context.Instance.GetType().FullName, behaviorType.FullName,
                                             requiredField.FieldType.FullName, requiredField.Name);
     throw new SpecificationUsageException(message);
   }
 }
 void EnsureContextFieldExists(Context context, FieldInfo contextField, FieldInfo requiredField, Type behaviorType)
 {
   if (contextField == null)
   {
     string format = "The context {0} behaves like {1} but does not define the behavior required field {2} {3}";
     string message = String.Format(format, context.Instance.GetType().FullName, behaviorType.FullName,
                                             requiredField.FieldType.FullName, requiredField.Name);
     throw new SpecificationUsageException(message);
   }
 }
 public static IContextRunner GetContextRunnerFor(Context context)
 {
   if (context.IsSetupForEachSpec)
   {
     return new SetupForEachContextRunner();
   }
   else
   {
     return new SetupOnceContextRunner();
   }
 }
    private IEnumerable<Result> FailSpecifications(Context context, ISpecificationRunListener listener, RunOptions options, Result result)
    {
      var results = new List<Result>();
      foreach (var specification in context.Specifications)
      {
        listener.OnSpecificationStart(specification.GetInfo());
        listener.OnSpecificationEnd(specification.GetInfo(), result);
        results.Add(result);
      }

      return results;
    }
    public Specification CreateSpecification(Context context, FieldInfo specificationField)
    {
      bool isIgnored = context.IsIgnored || specificationField.HasAttribute<IgnoreAttribute>();
      It it;
      if(typeof(IProvideFieldValues).IsAssignableFrom(context.Type))
        it = (It) context.Type.GetMethod("GetValue").Invoke(context.Instance, new[]{specificationField});
      else
        it = (It) specificationField.GetValue(context.Instance);
      string name = specificationField.Name.ToFormat();

      return new Specification(name, it, isIgnored, specificationField);
    }
    private static IEnumerable<Result> RunSpecifications(Context context, ISpecificationRunListener listener, RunOptions options, IEnumerable<ISupplementSpecificationResults> resultSupplementers)
    {
      var results = new List<Result>();
      foreach (var specification in context.Specifications)
      {
        var runner = new SpecificationRunner(listener, options, resultSupplementers);
        var result = runner.Run(specification);

        results.Add(result);
      }

      return results;
    }
    public BehaviorSpecification(string name,
                                 It it,
                                 bool isIgnored,
                                 FieldInfo fieldInfo,
                                 Context context,
                                 Behavior behavior)
      : base(name, it, isIgnored, fieldInfo)
    {
      _contextInstance = context.Instance;
      _behaviorInstance = behavior.Instance;

      _mapper = new ConventionMapper();
    }
    void EnsureAllBehaviorFieldsAreInContext(Type behaviorType, Context context)
    {
      var behaviorFieldsRequiredInContext = behaviorType.GetStaticProtectedOrInheritedFields().Where(x => !x.IsLiteral);
      var contextFields = context.Instance.GetType().GetStaticProtectedOrInheritedFields();

      foreach (var requiredField in behaviorFieldsRequiredInContext)
      {
        var requiredFieldName = requiredField.Name;
        var contextField = contextFields.SingleOrDefault(x => x.Name == requiredFieldName);
        
        EnsureContextFieldExists(context, contextField, requiredField, behaviorType);
        EnsureContextFieldIsCompatibleType(context, contextField, requiredField, behaviorType);
      }
    }
    private static IEnumerable<Result> FailSpecifications(Context context, ISpecificationRunListener listener, Result result, IEnumerable<ISupplementSpecificationResults> resultSupplementers)
    {
      result = resultSupplementers.Aggregate(result, (r, supplement) => supplement.SupplementResult(r));

      var results = new List<Result>();
      foreach (var specification in context.Specifications)
      {
        listener.OnSpecificationStart(specification.GetInfo());
        listener.OnSpecificationEnd(specification.GetInfo(), result);

        results.Add(result);
      }

      return results;
    }
    public IEnumerable<Result> Run(Context context, ISpecificationRunListener listener, RunOptions options, IEnumerable<ICleanupAfterEveryContextInAssembly> globalCleanups, IEnumerable<ISupplementSpecificationResults> resultSupplementers)
    {
      var results = new List<Result>();
      listener.OnContextStart(context.GetInfo());

      foreach (var specification in context.Specifications)
      {
        Result result = Result.Pass();

        if (specification.IsExecutable)
        {
          result = context.EstablishContext();
        }

        if (result.Passed)
        {
          var runner = new SpecificationRunner(listener, options, resultSupplementers);
          result = runner.Run(specification);
        }
        else
        {
            results = FailSpecification(listener, specification, result);
        }

        if (specification.IsExecutable)
        {
          var cleanupResult = context.Cleanup();

          if (result.Passed && !cleanupResult.Passed)
          {
            result = cleanupResult;
          }

          foreach (var cleanup in globalCleanups)
          {
            cleanup.AfterContextCleanup();
          }
        }

        results.Add(result);
      }

      listener.OnContextEnd(context.GetInfo());

      return results;
    }
 private Test ConvertToVisualStudioTest(Context context, Specification specification)
 {
     var test = converter.GetVisualStudioTest(context, specification);
     if (context.Instance == null)
     {
         return test;
     }
     var it = (Delegate) specification.FieldInfo.GetValue(context.Instance);
     if (it == null)
     {
         return test;
     }
     var methodInfo = it.GetMethodInfo();
     var sourceInfo = provider.GetSourceInformation(methodInfo);
     test.CodeFilePath = sourceInfo.Filename;
     test.LineNumber = sourceInfo.LineNumber;
     return test;
 }
    public Behavior CreateBehaviorFrom(FieldInfo behaviorField, Context context)
    {
      var behaviorType = behaviorField.FieldType.GetGenericArguments().First();

      EnsureBehaviorHasBehaviorsAttribute(behaviorType);
      EnsureBehaviorDoesNotHaveFrameworkFieldsExceptIt(behaviorType);

      var behaviorInstance = Activator.CreateInstance(behaviorType);

      EnsureAllBehaviorFieldsAreInContext(behaviorType, context);

      var isIgnored = behaviorField.HasAttribute<IgnoreAttribute>() ||
                      behaviorInstance.GetType().HasAttribute<IgnoreAttribute>();
      var behavior = new Behavior(behaviorInstance, context, isIgnored);

      var itFieldInfos = behaviorType.GetInstanceFieldsOfType<Then>();
      CreateBehaviorSpecifications(itFieldInfos, behavior);

      return behavior;
    }
        private IEnumerable<MSpecTestCase> CreateTestCase(Context context, string assemblyPath)
        {

            SourceCodeLocationFinder locationFinder = new SourceCodeLocationFinder(assemblyPath);


            foreach (Specification spec in context.Specifications.ToList())
            {
                MSpecTestCase testCase = new MSpecTestCase();

                testCase.ClassName = context.Type.Name;
                testCase.ContextFullType = context.Type.FullName;
                testCase.ContextDisplayName = GetContextDisplayName(context.Type);

                testCase.SpecificationName = spec.FieldInfo.Name;
                testCase.SpecificationDisplayName = spec.Name;

                string fieldDeclaringType;
                if (spec.FieldInfo.DeclaringType.IsGenericType && !spec.FieldInfo.DeclaringType.IsGenericTypeDefinition)
                    fieldDeclaringType = spec.FieldInfo.DeclaringType.GetGenericTypeDefinition().FullName;
                else
                    fieldDeclaringType = spec.FieldInfo.DeclaringType.FullName;
                    
                SourceCodeLocationInfo locationInfo = locationFinder.GetFieldLocation(fieldDeclaringType, spec.FieldInfo.Name);
                if (locationInfo != null)
                {
                    testCase.CodeFilePath = locationInfo.CodeFilePath;
                    testCase.LineNumber = locationInfo.LineNumber;
                }


                if (context.Tags != null)
                    testCase.Tags = context.Tags.Select(tag => tag.Name).ToArray();

                if (context.Subject != null)
                    testCase.Subject = context.Subject.FullConcern;

                yield return testCase;
            }
        }
    public IEnumerable<Result> Run(Context context, ISpecificationRunListener listener, RunOptions options)
    {
      var results = new List<Result>();
      listener.OnContextStart(context.GetInfo());

      foreach (var specification in context.Specifications)
      {
        Result result = Result.Pass();

        if (specification.IsExecutable)
        {
          result = context.EstablishContext();
        }

        if (result.Passed)
        {
          var runner = new SpecificationRunner(listener, options);
          result = runner.Run(specification);
        }

        if (specification.IsExecutable)
        {
          var cleanupResult = context.Cleanup();

          if (result.Passed && !cleanupResult.Passed)
          {
            result = cleanupResult;
          }
        }

        results.Add(result);
      }

      listener.OnContextEnd(context.GetInfo());

      return results;
    }
 public MachineContextTest(Context context)
   : base(context.Name, Reflector.Wrap(context.Type))
 {
   this.Kind = TestKinds.Fixture;
   this._context = context;
 }   
 void RunContext(Context context,
                 IEnumerable<ICleanupAfterEveryContextInAssembly> globalCleanups,
                 IEnumerable<ISupplementSpecificationResults> supplements)
 {
     var runner = ContextRunnerFactory.GetContextRunnerFor(context);
     runner.Run(context, _listener, _options, globalCleanups, supplements);
 }
 private IEnumerable<Test> ConvertToVisualStudioTests(Context context)
 {
     return context.Specifications.Select(specification => ConvertToVisualStudioTest(context, specification));
 }
 public static VisualStudioTestIdentifier ToVisualStudioTestIdentifier(this Specification specification, Context context)
 {
     return new VisualStudioTestIdentifier(String.Format(CultureInfo.InvariantCulture, "{0}::{1}", context.Type.FullName, specification.FieldInfo.Name));
 }
 void RunContext(Context context)
 {
   IContextRunner runner = ContextRunnerFactory.GetContextRunnerFor(context);
   runner.Run(context, _listener, _options);
 }
 public Test GetVisualStudioTest(Context context, Specification specification)
 {
     return GetVisualStudioTest(context.GetInfo(), specification.GetInfo());
 }