Skip to content

Mock State not behaving correctly with non-user-defined variables #3681

@bclothier

Description

@bclothier

While building an unit test, I encountered the case where the mocked parser will wrongly label built-in functions as user-defined.

Given the parameters:

            var inputCode = 
@"Option Explicit

Public Sub Test()
  Dim SomeString As String
  Dim OtherString As String

  SomeString = ""Hello, world!""
  OtherString = ""Goodbye, world!""
  
  Msgbox SomeString
  Msgbox OtherString
End Sub";
            var selection = new Selection(11, 1, 11, 21);

            var expectedPreviewCode = 
@"Public Sub NewMethod(OtherString As String)
  Msgbox OtherString
End Sub";

The unit test will fail, because the preview code is built as thus:

Private Sub NewMethod(OtherString As String, Msgbox As Variant)
    Msgbox OtherString
End Sub

When we look at this state, it lists Msgbox as one of user-defined declaration:

State.DeclarationFinder.UserDeclarations(DeclarationType.Variable)
Count = 3
    [0]: (Variable) Implicit "SomeString" As "String" | {L4C7 - L4C17}
    [1]: (Variable) Implicit "Msgbox" As "Variant" | {L10C3 - L10C9}
    [2]: (Variable) Implicit "OtherString" As "String" | {L5C7 - L5C18}
State.DeclarationFinder.UserDeclarations(DeclarationType.Variable).ElementAt(1).IsUserDefined
true

The issue is NOT observed with the actual state when running RD so the issue is only with the mocked state, which I'm not too sure how to fix.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions