Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rider does not recognize custom StepDefinitionBaseAttribute #213

Open
stevozilik opened this issue Feb 9, 2024 · 0 comments
Open

Rider does not recognize custom StepDefinitionBaseAttribute #213

stevozilik opened this issue Feb 9, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@stevozilik
Copy link

stevozilik commented Feb 9, 2024

Hi,

we are using a bespoke Step attribute to introduce consistency when naming steps . This works in Visual Studio, but the Rider (plugin) does not recognize any steps annotated with our custom attribute so the navigation/step highlighting/suggestions in SpecFlow feature files do not work

public partial class AutoStepAttribute : StepDefinitionBaseAttribute
{
    // https://stackoverflow.com/questions/3216085/split-a-pascalcase-string-into-separate-words
    [GeneratedRegex(@"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])")]
    private static partial Regex CreateStepNameRegex();
    private static readonly Regex _stepNameRegex = CreateStepNameRegex();

    [Obsolete("This constructor should not be used, instead the step name should be auto generated. It is only defined so SpecFlow step scanning works without the explicitely defined regex. Seems like SpecFlow is using StepDefinition constructors definitions to influence their behaviour")]
    public AutoStepAttribute(string regex, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "")
       : this(memberName, sourceFilePath)
    { }

    public AutoStepAttribute([CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "")
        : base(null, new[] { StepDefinitionType.Given, StepDefinitionType.When, StepDefinitionType.Then })
    {
        Regex = GetStepName(memberName, sourceFilePath);
    }

    private static string GetStepName(string memberName, string sourceFilePath)
    {
        var fileName = Path.GetFileNameWithoutExtension(sourceFilePath);
        var stepsName = fileName.Replace("Steps", string.Empty);
        var stepName = ParseStepName(memberName);
        var result = $"{stepsName} {stepName}";
        return result;
    }

    private static string ParseStepName(string defintion)
    {
        defintion = defintion.Replace("_", string.Empty);
        var words = _stepNameRegex.Split(defintion);
        StringBuilder sb = new();

        foreach (var word in words)
        {
            if (sb.Length > 0)
            {
                sb.Append(' ');
            }

            if (word.Length > 1 && char.IsUpper(word[1])) // if it's a variable
            {
                sb.Append("(.*)");
            }
            else
            {
                sb.Append(word.ToLower(CultureInfo.InvariantCulture));
            }
        }

        return sb.ToString();
    }
}

Usage inside Steps file

[AutoStep]
public async Task IsSelected()
 {
        ....
 }

Is implementing StepDefinitionBaseAttribute not supported by the Rider plugin?

Kind regards,
Stefan

@Socolin Socolin added the bug Something isn't working label Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants