コード例 #1
0
ファイル: Behavior.cs プロジェクト: simonlaroche/machine
 public Behavior(object instance, Context context, bool isIgnored)
 {
     _instance = instance;
       _context = context;
       IsIgnored = isIgnored;
       _specifications = new List<Specification>();
 }
コード例 #2
0
    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;
    }
コード例 #3
0
    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;
    }
コード例 #4
0
    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;
    }
コード例 #5
0
 public Behavior(Type fieldType, object instance, Context context, bool isIgnored)
 {
     _leader = fieldType.ToFormat();
     _instance = instance;
     _context = context;
     IsIgnored = isIgnored;
     _specifications = new List<Specification>();
 }
コード例 #6
0
    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);
    }
コード例 #7
0
    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);
    }
コード例 #8
0
    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);
    }
コード例 #9
0
 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);
   }
 }
コード例 #10
0
 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);
   }
 }
コード例 #11
0
 public static IContextRunner GetContextRunnerFor(Context context)
 {
   if (context.IsSetupForEachSpec)
   {
     return new SetupForEachContextRunner();
   }
   else
   {
     return new SetupOnceContextRunner();
   }
 }
コード例 #12
0
    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;
    }
コード例 #13
0
    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);
    }
コード例 #14
0
    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;
    }
コード例 #15
0
    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();
    }
コード例 #16
0
    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);
      }
    }
コード例 #17
0
    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;
    }
コード例 #18
0
    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;
 }
コード例 #20
0
    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;
    }
コード例 #21
0
        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;
            }
        }
コード例 #22
0
    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;
    }
コード例 #23
0
 public MachineContextTest(Context context)
   : base(context.Name, Reflector.Wrap(context.Type))
 {
   this.Kind = TestKinds.Fixture;
   this._context = context;
 }   
コード例 #24
0
 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));
 }
コード例 #27
0
 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());
 }