1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using NUnit . Framework ;
4+ using Rubberduck . Parsing . Annotations ;
5+ using Rubberduck . Parsing . Symbols ;
6+ using Rubberduck . VBEditor . SafeComWrappers ;
7+ using RubberduckTests . Mocks ;
8+
9+ namespace RubberduckTests . Annotations
10+ {
11+ [ TestFixture ]
12+ public class AttributeAnnotationTests
13+ {
14+ [ TestCase ( "VB_Description" , "\" SomeDescription\" " , "ModuleDescription" , "\" SomeDescription\" " ) ]
15+ [ TestCase ( "VB_Exposed" , "True" , "Exposed" ) ]
16+ [ TestCase ( "VB_PredeclaredId" , "True" , "PredeclaredId" ) ]
17+ public void ModuleAttributeAnnotationReturnsReturnsCorrectAttribute ( string expectedAttribute , string expectedAttributeValues , string annotationName , string annotationValue = null )
18+ {
19+ var code = $@ "
20+ '@{ annotationName } { annotationValue } " ;
21+
22+ var vbe = MockVbeBuilder . BuildFromSingleModule ( code , "Class1" , ComponentType . ClassModule , out _ ) . Object ;
23+ using ( var state = MockParser . CreateAndParse ( vbe ) )
24+ {
25+ var moduleDeclaration = state . DeclarationFinder . UserDeclarations ( DeclarationType . ClassModule ) . Single ( ) ;
26+ var moduleAnnotations = moduleDeclaration . Annotations
27+ . Select ( pta => ( pta . Annotation , pta . AnnotationArguments ) ) ;
28+ var ( annotation , annotationArguments ) = moduleAnnotations . Single ( tpl => tpl . Annotation . Name . Equals ( annotationName ) ) ;
29+
30+ var actualAttribute = ( ( IAttributeAnnotation ) annotation ) . Attribute ( annotationArguments ) ;
31+ var actualAttributeValues = ( ( IAttributeAnnotation ) annotation ) . AnnotationToAttributeValues ( annotationArguments ) ;
32+ var actualAttributesValuesText = string . Join ( ", " , actualAttributeValues ) ;
33+
34+ Assert . AreEqual ( expectedAttribute , actualAttribute ) ;
35+ Assert . AreEqual ( actualAttributesValuesText , expectedAttributeValues ) ;
36+ }
37+ }
38+
39+ [ TestCase ( "VB_ProcData.VB_Invoke_Func" , @"""A\n14""" , "ExcelHotkey" , "A" ) ] //See issue #5268 at https://github.com/rubberduck-vba/Rubberduck/issues/5268
40+ [ TestCase ( "VB_Description" , "\" SomeDescription\" " , "Description" , "\" SomeDescription\" " ) ]
41+ [ TestCase ( "VB_UserMemId" , "0" , "DefaultMember" ) ]
42+ [ TestCase ( "VB_UserMemId" , "-4" , "Enumerator" ) ]
43+ public void MemberAttributeAnnotationReturnsReturnsCorrectAttribute ( string expectedAttribute , string expectedAttributeValues , string annotationName , string annotationValue = null )
44+ {
45+ var code = $@ "
46+ '@{ annotationName } { annotationValue }
47+ Public Function Foo()
48+ End Function" ;
49+
50+ var vbe = MockVbeBuilder . BuildFromSingleModule ( code , "Class1" , ComponentType . ClassModule , out _ ) . Object ;
51+ using ( var state = MockParser . CreateAndParse ( vbe ) )
52+ {
53+ var memberDeclaration = state . DeclarationFinder . UserDeclarations ( DeclarationType . Function ) . Single ( ) ;
54+ var memberAnnotations = memberDeclaration . Annotations
55+ . Select ( pta => ( pta . Annotation , pta . AnnotationArguments ) ) ;
56+ var ( annotation , annotationArguments ) = memberAnnotations . Single ( tpl => tpl . Annotation . Name . Equals ( annotationName ) ) ;
57+
58+ var actualAttribute = ( ( IAttributeAnnotation ) annotation ) . Attribute ( annotationArguments ) ;
59+ var actualAttributeValues = ( ( IAttributeAnnotation ) annotation ) . AnnotationToAttributeValues ( annotationArguments ) ;
60+ var actualAttributesValuesText = string . Join ( ", " , actualAttributeValues ) ;
61+
62+ Assert . AreEqual ( expectedAttribute , actualAttribute ) ;
63+ Assert . AreEqual ( expectedAttributeValues , actualAttributesValuesText ) ;
64+ }
65+ }
66+
67+ [ TestCase ( "VB_VarDescription" , "\" SomeDescription\" " , "VariableDescription" , "\" SomeDescription\" " ) ]
68+ public void VariableAttributeAnnotationReturnsReturnsCorrectAttribute ( string expectedAttribute , string expectedAttributeValues , string annotationName , string annotationValue = null )
69+ {
70+ var code = $@ "
71+ '@{ annotationName } { annotationValue }
72+ Public MyVariable" ;
73+
74+ var vbe = MockVbeBuilder . BuildFromSingleModule ( code , "Class1" , ComponentType . ClassModule , out _ ) . Object ;
75+ using ( var state = MockParser . CreateAndParse ( vbe ) )
76+ {
77+ var memberDeclaration = state . DeclarationFinder . UserDeclarations ( DeclarationType . Variable ) . Single ( ) ;
78+ var memberAnnotations = memberDeclaration . Annotations
79+ . Select ( pta => ( pta . Annotation , pta . AnnotationArguments ) ) ;
80+ var ( annotation , annotationArguments ) = memberAnnotations . Single ( tpl => tpl . Annotation . Name . Equals ( annotationName ) ) ;
81+
82+ var actualAttribute = ( ( IAttributeAnnotation ) annotation ) . Attribute ( annotationArguments ) ;
83+ var actualAttributeValues = ( ( IAttributeAnnotation ) annotation ) . AnnotationToAttributeValues ( annotationArguments ) ;
84+ var actualAttributesValuesText = string . Join ( ", " , actualAttributeValues ) ;
85+
86+ Assert . AreEqual ( expectedAttribute , actualAttribute ) ;
87+ Assert . AreEqual ( actualAttributesValuesText , expectedAttributeValues ) ;
88+ }
89+ }
90+ }
91+ }
0 commit comments