Test GetAssemblyTest(IAssemblyInfo assembly, Test parentTest, Version frameworkVersion, bool populateRecursively)
    {
      MachineAssemblyTest assemblyTest;
      if (!assemblyTests.TryGetValue(assembly, out assemblyTest))
      {
        assemblyTest = new MachineAssemblyTest(assembly.Name, assembly, frameworkVersion);
        assemblyTest.Kind = TestKinds.Assembly;

        ModelUtils.PopulateMetadataFromAssembly(assembly, assemblyTest.Metadata);

        string frameworkName = String.Format("Machine Specifications v{0}", frameworkVersion);
        assemblyTest.Metadata.SetValue(MetadataKeys.Framework, frameworkName);
        assemblyTest.Metadata.SetValue(MetadataKeys.File, assembly.Path);
        assemblyTest.Kind = TestKinds.Assembly;

        parentTest.AddChild(assemblyTest);
        assemblyTests.Add(assembly, assemblyTest);
      }

      if (populateRecursively)
      {
        AssemblyExplorer explorer = new AssemblyExplorer();
        Assembly resolvedAssembly = assembly.Resolve(false);

        assemblyTest.AssemblyContexts = explorer.FindAssemblyContextsIn( resolvedAssembly).ToList();
        assemblyTest.GlobalCleanup = explorer.FindAssemblyWideContextCleanupsIn(resolvedAssembly).ToList();
        assemblyTest.SpecificationSupplements = explorer.FindSpecificationSupplementsIn(resolvedAssembly).ToList();
        
        explorer.FindContextsIn(resolvedAssembly)
          .Select( context => GetContextTest( context))
          .Each( test => assemblyTest.AddChild( test));
      }

      return assemblyTest;
    }
    public void Run(Assembly assembly, IEnumerable<Context> contexts)
    {
      var hasExecutableSpecifications = contexts.Any(x => x.HasExecutableSpecifications);

      var explorer = new AssemblyExplorer();
      var globalCleanups = explorer.FindAssemblyWideContextCleanupsIn(assembly).ToList();
      var specificationSupplements = explorer.FindSpecificationSupplementsIn(assembly).ToList();

      try
      {
        if (hasExecutableSpecifications)
        {
          _assemblyStart(assembly);
        }

        foreach (var context in contexts)
        {
          RunContext(context, globalCleanups, specificationSupplements);
        }
      }
      catch (Exception err)
      {
        _listener.OnFatalError(new ExceptionResult(err));
      }
      finally
      {
        if (hasExecutableSpecifications)
        {
          _assemblyEnd(assembly);
        }
      }
    }
        public IEnumerable<MSpecTestCase> DiscoverTests(string assemblyPath)
        {
            AssemblyExplorer assemblyExplorer = new AssemblyExplorer();

            Assembly assembly = Assembly.LoadFile(assemblyPath);
            IEnumerable<Context> contexts = assemblyExplorer.FindContextsIn(assembly);

            return contexts.SelectMany(context => CreateTestCase(context, assemblyPath)).ToList();
        }
    public DefaultRunner(ISpecificationRunListener listener, RunOptions options)
    {
      _listener = listener;
      _options = options;
      _assemblyRunner = new AssemblyRunner(_listener, _options);

      _explorer = new AssemblyExplorer();

      _runStart = () => _listener.OnRunStart();
      _runEnd = () => _listener.OnRunEnd();
    }
        public DefaultRunner(ISpecificationRunListener listener, RunOptions options, bool signalRunStartAndEnd)
        {
            _listener = listener;
            _options = options;
            _assemblyRunner = new AssemblyRunner(_listener, _options);

            _explorer = new AssemblyExplorer();

            if (signalRunStartAndEnd)
            {
                _runStart = new InvokeOnce(() => _listener.OnRunStart());
                _runEnd = new InvokeOnce(() => _listener.OnRunEnd());
            }
        }
    public void Run(Assembly assembly, IEnumerable<Context> contexts)
    {
      bool hasExecutableSpecifications = contexts.Where(x => x.HasExecutableSpecifications).Any();

      var explorer = new AssemblyExplorer();
      var assemblyContexts = new List<IAssemblyContext>(explorer.FindAssemblyContextsIn(assembly));

      _listener.OnAssemblyStart(assembly.GetInfo());

      var executedAssemblyContexts = new List<IAssemblyContext>();

      try
      {
        if (hasExecutableSpecifications)
        {
          assemblyContexts.ForEach(assemblyContext =>
          {
            assemblyContext.OnAssemblyStart();
            executedAssemblyContexts.Add(assemblyContext);
          });
        }

        foreach (var context in contexts)
        {
          RunContext(context);
        }
      }
      catch (Exception err)
      {
        _listener.OnFatalError(new ExceptionResult(err));
      }
      finally
      {
        if (hasExecutableSpecifications)
        {
          try
          {
            executedAssemblyContexts.Reverse();
            executedAssemblyContexts.ForEach(assemblyContext => assemblyContext.OnAssemblyComplete());
          }
          catch (Exception err)
          {
            _listener.OnFatalError(new ExceptionResult(err));
            throw;
          }
        }
      }

      _listener.OnAssemblyEnd(assembly.GetInfo());
    }
 public void SendToVisualStudio(Assembly assembly)
 {
     if (sink == null)
     {
         return;
     }
     var explorer = new AssemblyExplorer();
     var contexts = explorer.FindContextsIn(assembly);
     foreach (var context in contexts)
     {
         foreach (var test in ConvertToVisualStudioTests(context))
         {
             sink?.SendTest(test);
         }
     }
 }
        public AssemblyRunner(ISpecificationRunListener listener, RunOptions options)
        {
            RedirectOutputState state = new RedirectOutputState();
            _listener = new AggregateRunListener(new[]
                                           {
                                             new AssemblyLocationAwareListener(),
                                             new SetUpRedirectOutputRunListener(state),
                                             listener,
                                             new TearDownRedirectOutputRunListener(state),
                                           });
            _options = options;
            _explorer = new AssemblyExplorer();
            _executedAssemblyContexts = new List<IAssemblyContext>();

            _assemblyStart = OnAssemblyStart;
            _assemblyEnd = OnAssemblyEnd;
        }
        public void RunTestsInAssembly(string pathToAssembly, IEnumerable<VisualStudioTestIdentifier> specsToRun, ISpecificationRunListener specificationRunListener)
        {
            DefaultRunner mspecRunner = null;
            Assembly assemblyToRun = null;
       
            try
            {
                assemblyToRun = Assembly.LoadFrom(pathToAssembly);
                mspecRunner = new DefaultRunner(specificationRunListener, RunOptions.Default);

                IEnumerable<Context> specificationContexts = new AssemblyExplorer().FindContextsIn(assemblyToRun) ?? Enumerable.Empty<Context>();
                Dictionary<string, Context> contextMap = specificationContexts.ToDictionary(c => c.Type.FullName, StringComparer.Ordinal);

                // We use explicit assembly start and end to wrap the RunMember loop
                mspecRunner.StartRun(assemblyToRun);

                foreach (VisualStudioTestIdentifier test in specsToRun)
                {
                    Context context = contextMap[test.ContainerTypeFullName];
                    if (context == null)
                        continue;

                    Specification specification = context.Specifications.SingleOrDefault(spec => spec.FieldInfo.Name.Equals(test.FieldName, StringComparison.Ordinal));
                    
                    if (specification is BehaviorSpecification)
                    {
                        // MSpec doesn't expose any way to run an an "It" coming from a "[Behavior]", so we have to do some trickery
                        VisualStudioTestIdentifier listenFor = specification.ToVisualStudioTestIdentifier(context);
                        DefaultRunner behaviorRunner = new DefaultRunner(new SingleBehaviorTestRunListenerWrapper(specificationRunListener, listenFor), RunOptions.Default);
                        behaviorRunner.RunMember(assemblyToRun, context.Type);
                    } 
                    else 
                    {
                        mspecRunner.RunMember(assemblyToRun, specification.FieldInfo);
                    }

                }
            } catch (Exception e) {
                specificationRunListener.OnFatalError(new ExceptionResult(e));
            }
            finally
            {
                if (mspecRunner != null && assemblyToRun != null)
                    mspecRunner.EndRun(assemblyToRun);
            }
        }
 private Controller(Action<string> listenCallback, RunOptions runOptions)
 {
     _listener = new ControllerRunListener(listenCallback);
     _explorer = new AssemblyExplorer();
     _runner = new DefaultRunner(_listener, runOptions, signalRunStartAndEnd: false);
 }
 public IEnumerable<Context> GetContexts(Assembly assembly)
 {
     var explorer = new AssemblyExplorer();
     var contexts = explorer.FindContextsIn(assembly);
     return contexts;
 }
 public DefaultRunner(ISpecificationRunListener listener, RunOptions options)
 {
   _listener = listener;
   _options = options;
   _explorer = new AssemblyExplorer();
 }