示例#1
0
        public ParsedSpec Parse(string specText)
        {
            var parsedSpec = new ParsedSpec();
            foreach (var line in specText.SplitIntoLines())
            {
                if (string.IsNullOrEmpty(line.Trim()))
                    continue;

                if (IsScenarioLine(line))
                {
                    if (_inScenario)
                        parsedSpec.AddScenario(_currentScenario);

                    _currentScenario = new Scenario();

                    var match = Regex.Match(line.Trim(), "^Scenario:(.*)$");
                    _currentScenario.Name = StringifyMethodName(match.Groups[1].Value.Trim());
                    _inScenario = true;
                }
                else
                {
                    var method = StringifyMethodName(line.Trim());
                    method = ReplaceWithPhraseStarter(method);
                    _currentScenario.CalledMethods.Add(method);
                    parsedSpec.AddHelperMethod(method);
                }
            }

            if (!string.IsNullOrEmpty(_currentScenario.Name))
                parsedSpec.AddScenario(_currentScenario);

            return parsedSpec;
        }
示例#2
0
        public string WriteCode(ParsedSpec parsedSpec)
        {
            var result = new StringBuilder();
                var firstTest = true;
            foreach (var scenario in parsedSpec.Scenarios)
            {
                if (!firstTest)
                    result.AppendLine();
                firstTest = false;

                result.AppendLine(string.Format("    [{0}]", _configuration.TestAttributeClass));
                result.AppendLine(string.Format("    public void {0}()", scenario.Name));
                result.AppendLine("    {");

                foreach (var method in scenario.CalledMethods)
                    result.AppendLine(string.Format("        {0}();", method));

                result.AppendLine("    }");
            }

            if (parsedSpec.HelperMethods.Any())
            {
                result.AppendLine();
                result.AppendLine("    #region Helper methods");
            }

            foreach (var method in parsedSpec.HelperMethods)
            {
                result.AppendLine();
                result.AppendLine(string.Format("    private void {0}()", method));
                result.AppendLine("    {");
                result.AppendLine("        throw new NotImplementedException();");
                result.AppendLine("    }");
            }

            result.AppendLine("");
            result.AppendLine("    #endregion");
            return result.ToString();
        }