Ejemplo n.º 1
0
        public static FeatureClass FromFeatureInstance(Feature featureInstance)
        {
            if (featureInstance == null)
            {
                throw new ArgumentNullException(nameof(featureInstance));
            }

            Type featureType = featureInstance.GetType();

            var featureFileAttribute = featureType
                                       .GetTypeInfo()
                                       .GetCustomAttribute <FeatureFileAttribute>();
            var featureFilePath = featureFileAttribute?.Path ?? $"{featureType.Name}.feature";

            var stepMethods = featureType.GetTypeInfo().GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)
                              .Where(m => m.IsDefined(typeof(BaseStepDefinitionAttribute)))
                              .Select(m => StepMethodInfo.FromMethodInfo(m, featureInstance))
                              .ToList();

            var sharedSteps =
                featureType.GetTypeInfo()
                .GetCustomAttributes <IncludeStepsAttribute>()
                .SelectMany(attr => GetSharedSteps(attr.FromType));

            stepMethods.AddRange(sharedSteps);

            return(new FeatureClass(featureFilePath, stepMethods));
        }
        public bool IsSameAs(StepMethodInfo other)
        {
            if (other == this)
                return true;

            return other != null
                && other._methodInfoWrapper.IsSameAs(_methodInfoWrapper);
        }
Ejemplo n.º 3
0
        private static IEnumerable <StepMethodInfo> GetSharedSteps(Type fromType)
        {
            var instance = Activator.CreateInstance(fromType);

            return
                (fromType.GetTypeInfo().GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)
                 .Where(m => m.IsDefined(typeof(BaseStepDefinitionAttribute)))
                 .Select(m => StepMethodInfo.FromMethodInfo(m, instance)));
        }
Ejemplo n.º 4
0
        private StepMethod(StepMethodInfo stepMethodInfo, PatternKind kind, string pattern, string stepText)
        {
            _stepMethodInfo = stepMethodInfo ?? throw new ArgumentNullException(nameof(stepMethodInfo));
            Kind            = kind;
            Pattern         = !string.IsNullOrWhiteSpace(pattern) ? pattern : throw new ArgumentNullException(nameof(pattern));

            StepText = !string.IsNullOrWhiteSpace(stepText)
                ? stepText
                : throw new ArgumentNullException(nameof(stepText));
        }
Ejemplo n.º 5
0
        public static StepMethod FromStepMethodInfo(StepMethodInfo stepMethodInfo, Step gherkinScenarioStep)
        {
            var matchingPattern = GetMatchingPattern(stepMethodInfo, gherkinScenarioStep);

            if (matchingPattern == null)
            {
                throw new InvalidOperationException($"This step method info (`{stepMethodInfo.GetMethodName()}`) cannot handle given scenario step: `{gherkinScenarioStep.Keyword.Trim()} {gherkinScenarioStep.Text.Trim()}`.");
            }

            var stepMethodInfoClone = stepMethodInfo.Clone();

            stepMethodInfoClone.DigestScenarioStepValues(gherkinScenarioStep);
            return(new StepMethod(stepMethodInfoClone, matchingPattern.Kind, matchingPattern.Pattern, gherkinScenarioStep.Text));
        }
Ejemplo n.º 6
0
        private static ScenarioStepPattern GetMatchingPattern(StepMethodInfo stepMethod, Step gherkinScenarioStep)
        {
            var gherkinStepText = gherkinScenarioStep.Text.Trim();

            foreach (var pattern in stepMethod.ScenarioStepPatterns)
            {
                if (!pattern.Kind.ToString().Equals(gherkinScenarioStep.Keyword.Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var match = Regex.Match(gherkinStepText, pattern.Pattern);
                if (!match.Success || !match.Value.Equals(gherkinStepText))
                {
                    continue;
                }

                return(pattern);
            }

            return(null);
        }