private static void AddDefaultCollectionProcessorFactories(this SpecFlowContext context)
        {
            const string defaultKey = "__defaultCollectionProcessorFactoriesAdded__";

            if (context.ContainsKey(defaultKey) && context.GetEx <bool>(defaultKey))
            {
                return;
            }

            context.Set(true, defaultKey);

            var lookup        = new Dictionary <string, CollectionProcessorFactory>(StringComparer.OrdinalIgnoreCase);
            var listProcessor = new ListProcessorFactory(context);

            AddToLookup(new ArrayProcessorFactory(context));
            AddToLookup(new EnumerableProcessorFactory(context));
            AddToLookup(new ICollectionProcessorFactory(context));
            AddToLookup(new IListProcessorFactory(context));
            AddToLookup(new IReadOnlyCollectionProcessorFactory(context));
            AddToLookup(new IReadOnlyListProcessorFactory(context));
            AddToLookup(listProcessor);

            SetCollectionProcessorFactoryLookup(context, lookup);
            SetDefaultCollectionProcessorFactory(context, listProcessor);

            void AddToLookup(CollectionProcessorFactory factory)
            => factory.CollectionKindNames.ForEach(n => lookup.Add(n, factory));
        }
        public static T Obtain <T>(this SpecFlowContext context, string key = null)
            where T : new()
        {
            T instance;

            return(context.TryGetValue(key, out instance) ? instance : new T());
        }
Example #3
0
 private static void DisposeServiceProvider(SpecFlowContext context)
 {
     if (context.Get <IServiceProvider>(ServiceProviderKey) is IDisposable spDisposable)
     {
         spDisposable.Dispose();
     }
 }
        public static T Obtain <T>(this SpecFlowContext context, Func <T> factory, Expression <Func <T> > property)
            where T : class
        {
            var member = ((MemberExpression)property.Body).Member;

            return(Obtain(context, factory, member.Name));
        }
Example #5
0
        private static void CreateServiceCollection(SpecFlowContext scenarioContext)
        {
            scenarioContext.Set(true, ContainerBindingPhaseKey);
            var serviceCollection = new ServiceCollection();

            scenarioContext.Set(serviceCollection, ServiceCollectionKey);
        }
 public static void SetDefaultCollectionProcessorFactory(
     this SpecFlowContext context,
     CollectionProcessorFactory factory)
 {
     AddDefaultCollectionProcessorFactories(context);
     context.Set(factory, "__defaultCollectionProcessorFactory__");
 }
        /// <summary>Updates the context value associated with a specified key.</summary>
        /// <typeparam name="T">The type of the value to update.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="updater">The action to use to update the value.</param>
        public static void Update <T>(this SpecFlowContext context, string key, Action <T> updater)
        {
            key = NormalizeKey <T>(context, key);
            T currentValue = GetEx <T>(context, key);

            updater(currentValue);
        }
Example #8
0
 public static void AssertNoLastException(SpecFlowContext context)
 {
     if (context.TryGetValue(LastExceptionContextKey, out Exception ex))
     {
         Assert.Fail($"Expected no last exception, but an exception of type '{ex.GetType().Name}' was thrown: '{ex.Message}'");
     }
 }
        public static T ParseSpecValue <T>(SpecFlowContext scenarioContext, string property)
        {
            property = PerformSubstitutions(property);

            if (property is null)
            {
                return(default);
        private static void AddCosmosDbContainerForCleanup(
            SpecFlowContext context,
            Container container,
            Database?database = null)
        {
            if (!context.ContainsKey(CosmosDbContainersToDelete))
            {
                context.Set(new List <Container>(), CosmosDbContainersToDelete);
            }

            if (database is not null)
            {
                if (!context.ContainsKey(CosmosDbDatabasesToDelete))
                {
                    context.Set(new List <Database>(), CosmosDbDatabasesToDelete);
                }

                List <Database> databasesToDelete = context.Get <List <Database> >(CosmosDbDatabasesToDelete);
                databasesToDelete.Add(database);
            }

            List <Container> containers = context.Get <List <Container> >(CosmosDbContainersToDelete);

            containers.Add(container);
        }
        private static void AddDefaultComparerMappings(this SpecFlowContext context)
        {
            const string defaultKey = "__defaultComparersAdded__";

            if (context.GetInheritedOrDefault(defaultKey, false))
            {
                return;
            }

            context.Set(true, defaultKey);

            var stringLookup = new Dictionary <string, ComparerMapping>(StringComparer.OrdinalIgnoreCase);
            var typeLookup   = new Dictionary <Type, ComparerMapping>();

            SetComparerStringMappingLookup(context, stringLookup);
            SetComparerTypeMappingLookup(context, typeLookup);

            Add(new DoubleComparer(context), typeof(double), "Double");
            Add(new FloatComparer(context), typeof(float), "Float", "Single");

            void Add(IComparer comparer, Type type, params string[] keys)
            {
                foreach (string key in keys)
                {
                    AddComparerMapping(context, new ComparerMapping(key, type, comparer));
                }
            }
        }
Example #12
0
 public IReadOnlyCollectionProcessorFactory(SpecFlowContext context)
     : base(
         typeof(IReadOnlyCollectionProcessor <>),
         typeof(IReadOnlyCollection <>),
         context,
         "IReadOnlyCollection")
 {
 }
        //public static IComparer GetComparer(this SpecFlowContext context, string typeName)
        //{
        //    Dictionary<string, ComparerMapping> mapping = GetComparerStringMappingLookup(context);
        //    ComparerMapping result;
        //    mapping.TryGetValue(typeName, out result);
        //    return result.Comparer;
        //}

        public static IComparer GetComparer(this SpecFlowContext context, Type type)
        {
            Dictionary <Type, ComparerMapping> mapping = GetComparerTypeMappingLookup(context);
            ComparerMapping result;

            mapping.TryGetValue(type, out result);
            return(result.Comparer);
        }
        public static IValueProcessor GetValueProcessor(this SpecFlowContext context, Type dataType)
        {
            AddDefaultValueProcessors(context);
            var lookup = context.GetInherited <Dictionary <Type, IValueProcessor> >("__processorTypeLookup__");

            lookup.TryGetValue(dataType, out IValueProcessor result);
            return(result);
        }
        /// <summary>Returns the given key or the name of the specified type if the key is <c>null</c>.</summary>
        /// <typeparam name="T">Type of the data associated with the key.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="key">The key to process.</param>
        /// <returns>The given key or the name of the specified type if the key is <c>null</c>.</returns>
        public static string NormalizeKey <T>(this SpecFlowContext context, string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                key = typeof(T).FullName;
            }

            return(key);
        }
        /// <summary>
        /// Copies all of the values from the supplied <see cref="IConfigurationRoot"/> to function configuration
        /// environment variables.
        /// </summary>
        /// <param name="testContext">The context to add the configuration to.</param>
        /// <param name="configuration">The configuration to copy.</param>
        public static void CopyToFunctionConfigurationEnvironmentVariables(this SpecFlowContext testContext, IConfigurationRoot configuration)
        {
            FunctionConfiguration config = testContext.GetFunctionConfiguration();

            foreach (KeyValuePair <string, string> item in configuration.AsEnumerable())
            {
                config.EnvironmentVariables.Add(item.Key, item.Value);
            }
        }
        /// <summary>Gets a value from the context using a specified key.  Asserts if the key is not found.</summary>
        /// <typeparam name="T">The type of the value to return.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="key">The key of the value to retrieve.</param>
        /// <returns>The value associated with the specified key.</returns>
        public static T GetEx <T>(this SpecFlowContext context, string key)
        {
            if (!context.TryGetValue(key, out T value))
            {
                throw new Exception($"'{key}' was not found.");
            }

            return(value);
        }
Example #18
0
        public static void AssertLastExceptionWasOfType(SpecFlowContext context, string expectedExceptionTypeName)
        {
            if (!context.TryGetValue(LastExceptionContextKey, out Exception exception))
            {
                Assert.Fail($"Expected an exception of type '{expectedExceptionTypeName}' to have been thrown, but none was.");
            }

            Assert.AreEqual(expectedExceptionTypeName, exception.GetType().Name);
        }
        ///// <summary>
        ///// Gets a value from the context using a specified key.  Adds and returns a specified default value if the
        ///// key was not found.
        ///// </summary>
        ///// <typeparam name="T">The type of the value to return.</typeparam>
        ///// <param name="context">The context.</param>
        ///// <param name="defaultValue">The value to return if the value was not found.</param>
        ///// <returns>The value associated with the default key or the default value if not found.</returns>
        //public static T GetOrDefault<T>(this SpecFlowContext context, T defaultValue)
        //    => GetOrDefault(context, typeof(T).FullName, defaultValue);

        /// <summary>Updates the context value associated with a specified key.</summary>
        /// <typeparam name="T">The type of the value to update.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="updater">The function to use to update the value.</param>
        /// <returns>The updated value.</returns>
        public static T Update <T>(this SpecFlowContext context, string key, Func <T, T> updater)
        {
            key = NormalizeKey <T>(context, key);
            T currentValue = GetEx <T>(context, key);
            T newValue     = updater(currentValue);

            context.Set(newValue, key);
            return(newValue);
        }
        private static void RethrowTeardownExceptions(SpecFlowContext context, string key)
        {
            context.Set(false, TeardownBindingPhaseKey);

            if (context.TryGetValue(key, out List <Exception> elist))
            {
                throw new AggregateException(elist);
            }
        }
        public static void AddCollectionProcessorFactory(
            this SpecFlowContext context,
            CollectionProcessorFactory factory)
        {
            AddDefaultCollectionProcessorFactories(context);
            Dictionary <string, CollectionProcessorFactory> lookup = GetCollectionProcessorFactoryLookup(context);

            factory.CollectionKindNames.ForEach(n => lookup[n] = factory);
        }
        public static int GetClipLength(this SpecFlowContext context)
        {
            if (!context.TryGetInherited("__clipLength__", out int result))
            {
                result = DefaultClipLength;
                SetClipLength(context, result);
            }

            return(result);
        }
Example #23
0
        private static void StoreExceptionInContext(SpecFlowContext context, Exception x, string key)
        {
            if (!context.TryGetValue(key, out List <Exception> elist))
            {
                elist = new List <Exception>();
                context.Add(key, elist);
            }

            elist.Add(x);
        }
        /// <summary>
        /// Retrieves the current functions controller from the supplied context.
        /// </summary>
        /// <param name="context">The SpecFlow context to retrieve from.</param>
        /// <returns>The FunctionsController.</returns>
        /// <remarks>
        /// If the controller hasn't already been added to the context, this method will create
        /// and add a new instance.
        /// </remarks>
        public static FunctionsController GetFunctionsController(SpecFlowContext context)
        {
            if (!context.TryGetValue(out FunctionsController controller))
            {
                controller = new FunctionsController();
                context.Set(controller);
            }

            return(controller);
        }
        /// <summary>
        /// Retrieves the <see cref="FunctionConfiguration"/> from the context.
        /// </summary>
        /// <param name="context">The context in which the configuration is stored.</param>
        /// <returns>The <see cref="FunctionConfiguration"/>.</returns>
        /// <remarks>
        /// If a <see cref="FunctionConfiguration"/> hasn't already been added to the context,
        /// this method will create and add a new instance.
        /// </remarks>
        public static FunctionConfiguration GetFunctionConfiguration(SpecFlowContext context)
        {
            if (!context.TryGetValue(out FunctionConfiguration value))
            {
                value = new FunctionConfiguration();
                context.Set(value);
            }

            return(value);
        }
Example #26
0
        private static bool VerifyBindingAvailable(SpecFlowContext featureContext)
        {
            if (!featureContext.TryGetValue(ContainerBindingPhaseKey, out bool serviceBuildInProgress))
            {
                throw new InvalidOperationException(
                          $"This method requires {typeof(ContainerBindings).FullName} to be registered with SpecFlow and either the @perScenarioContainer or the @perFeatureContainer tag to be specified");
            }

            return(serviceBuildInProgress);
        }
        public static SpecFlowContext Copy(this SpecFlowContext specflowContext, VariableController variableController)
        {
            var context = specflowContext;

            foreach (var(key, value) in variableController.Variables)
            {
                context.Add(key, value);
            }
            return(context);
        }
Example #28
0
        private static void CompleteContainerSetup(SpecFlowContext context)
        {
            context.Set(false, ContainerBindingPhaseKey);

            ServiceCollection serviceCollection = context.Get <ServiceCollection>(ServiceCollectionKey);

            IServiceProvider service = serviceCollection.BuildServiceProvider();

            context.Set(service, ServiceProviderKey);
        }
 private static void ApplyAll <T>(SpecFlowContext context, Action <T> act)
 {
     foreach (var x in context)
     {
         if (x.Value is T t)
         {
             act(t);
         }
     }
 }
 public SimpleCollectionProcessorFactory(
     Type processorType,
     Type collectionType,
     SpecFlowContext context,
     params string[] collectionKindNames)
     : base(context, collectionKindNames)
 {
     _processorType  = processorType;
     _collectionType = collectionType;
 }