4
4
using Microsoft . VisualStudio . TestTools . UnitTesting ;
5
5
using Rubberduck . Parsing . Symbols ;
6
6
using Rubberduck . Parsing . VBA ;
7
- using RubberduckTests . Inspections ;
8
7
using RubberduckTests . Mocks ;
9
8
10
9
namespace RubberduckTests . Grammar
@@ -31,7 +30,7 @@ private RubberduckParserState Resolve(string code, vbext_ComponentType moduleTyp
31
30
private RubberduckParserState Resolve ( params string [ ] classes )
32
31
{
33
32
var builder = new MockVbeBuilder ( ) ;
34
- var projectBuilder = builder . ProjectBuilder ( "TestProject " , vbext_ProjectProtection . vbext_pp_none ) ;
33
+ var projectBuilder = builder . ProjectBuilder ( "TestProject1 " , vbext_ProjectProtection . vbext_pp_none ) ;
35
34
for ( var i = 0 ; i < classes . Length ; i ++ )
36
35
{
37
36
projectBuilder . AddComponent ( "Class" + ( i + 1 ) , vbext_ComponentType . vbext_ct_ClassModule , classes [ i ] ) ;
@@ -971,6 +970,217 @@ End Sub
971
970
Assert . AreEqual ( string . Empty , usage . Annotations ) ;
972
971
}
973
972
973
+ [ TestMethod ]
974
+ public void GivenUDT_NamedAfterProject_LocalResolvesToUDT ( )
975
+ {
976
+ var code = @"
977
+ Private Type TestProject1
978
+ Foo As Integer
979
+ Bar As String
980
+ End Type
981
+
982
+ Public Sub DoSomething()
983
+ Dim Foo As TestProject1
984
+ Foo.Bar = ""DoSomething""
985
+ Foo.Foo = 42
986
+ End Sub
987
+ " ;
988
+ var state = Resolve ( code ) ;
989
+
990
+ var declaration = state . AllUserDeclarations . Single ( item =>
991
+ item . DeclarationType == DeclarationType . UserDefinedType ) ;
992
+
993
+ if ( declaration . ProjectName != declaration . IdentifierName )
994
+ {
995
+ Assert . Inconclusive ( "UDT should be named after project." ) ;
996
+ }
997
+
998
+ var usage = declaration . References . SingleOrDefault ( ) ;
999
+
1000
+ Assert . IsNotNull ( usage ) ;
1001
+ }
1002
+
1003
+ [ TestMethod ]
1004
+ public void GivenUDT_NamedAfterProject_FieldResolvesToUDT_EvenIfHiddenByLocal ( )
1005
+ {
1006
+ var code = @"
1007
+ Private Type TestProject1
1008
+ Foo As Integer
1009
+ Bar As String
1010
+ End Type
1011
+
1012
+ Private Foo As TestProject1
1013
+
1014
+ Public Sub DoSomething()
1015
+ Dim Foo As TestProject1
1016
+ Foo.Bar = ""DoSomething""
1017
+ Foo.Foo = 42
1018
+ End Sub
1019
+ " ;
1020
+ var state = Resolve ( code ) ;
1021
+
1022
+ var declaration = state . AllUserDeclarations . Single ( item =>
1023
+ item . DeclarationType == DeclarationType . UserDefinedType ) ;
974
1024
1025
+ if ( declaration . ProjectName != declaration . IdentifierName )
1026
+ {
1027
+ Assert . Inconclusive ( "UDT should be named after project." ) ;
1028
+ }
1029
+
1030
+ var usages = declaration . References ;
1031
+
1032
+ Assert . AreEqual ( 2 , usages . Count ( ) ) ;
1033
+ }
1034
+
1035
+ [ TestMethod ]
1036
+ public void GivenLocalVariable_NamedAfterUDTMember_ResolvesToLocalVariable ( )
1037
+ {
1038
+ var code = @"
1039
+ Private Type TestProject1
1040
+ Foo As Integer
1041
+ Bar As String
1042
+ End Type
1043
+
1044
+ Public Sub DoSomething()
1045
+ Dim Foo As TestProject1
1046
+ Foo.Bar = ""DoSomething""
1047
+ Foo.Foo = 42
1048
+ End Sub
1049
+ " ;
1050
+ var state = Resolve ( code ) ;
1051
+
1052
+ var declaration = state . AllUserDeclarations . Single ( item =>
1053
+ item . DeclarationType == DeclarationType . Variable ) ;
1054
+
1055
+ if ( declaration . ProjectName != declaration . AsTypeName )
1056
+ {
1057
+ Assert . Inconclusive ( "variable should be named after project." ) ;
1058
+ }
1059
+ var usages = declaration . References ;
1060
+
1061
+ Assert . AreEqual ( 2 , usages . Count ( ) ) ;
1062
+ }
1063
+
1064
+ [ TestMethod ]
1065
+ public void GivenLocalVariable_NamedAfterUDTMember_MemberCallResolvesToUDTMember ( )
1066
+ {
1067
+ var code = @"
1068
+ Private Type TestProject1
1069
+ Foo As Integer
1070
+ Bar As String
1071
+ End Type
1072
+
1073
+ Public Sub DoSomething()
1074
+ Dim Foo As TestProject1
1075
+ Foo.Bar = ""DoSomething""
1076
+ Foo.Foo = 42
1077
+ End Sub
1078
+ " ;
1079
+ var state = Resolve ( code ) ;
1080
+
1081
+ var declaration = state . AllUserDeclarations . Single ( item =>
1082
+ item . DeclarationType == DeclarationType . UserDefinedTypeMember
1083
+ && item . IdentifierName == "Foo" ) ;
1084
+
1085
+ var usages = declaration . References . Where ( item =>
1086
+ item . ParentScoping . IdentifierName == "DoSomething" ) ;
1087
+
1088
+ Assert . AreEqual ( 1 , usages . Count ( ) ) ;
1089
+ }
1090
+
1091
+ [ TestMethod ]
1092
+ public void GivenUDTMember_OfUDTType_ResolvesToDeclaredUDT ( )
1093
+ {
1094
+ var code = @"
1095
+ Private Type TestProject1
1096
+ Foo As Integer
1097
+ Bar As String
1098
+ End Type
1099
+
1100
+ Private Type Foo
1101
+ Foo As TestProject1
1102
+ End Type
1103
+
1104
+ Public Sub DoSomething()
1105
+ Dim Foo As Foo
1106
+ Foo.Foo.Bar = ""DoSomething""
1107
+ Foo.Foo.Foo = 42
1108
+ End Sub
1109
+ " ;
1110
+ var state = Resolve ( code ) ;
1111
+
1112
+ var declaration = state . AllUserDeclarations . Single ( item =>
1113
+ item . DeclarationType == DeclarationType . UserDefinedTypeMember
1114
+ && item . IdentifierName == "Foo"
1115
+ && item . AsTypeName == item . ProjectName
1116
+ && item . IdentifierName == item . ParentDeclaration . IdentifierName ) ;
1117
+
1118
+ var usages = declaration . References . Where ( item =>
1119
+ item . ParentScoping . IdentifierName == "DoSomething" ) ;
1120
+
1121
+ Assert . AreEqual ( 2 , usages . Count ( ) ) ;
1122
+ }
1123
+
1124
+ [ TestMethod ]
1125
+ public void GivenUDT_NamedAfterModule_LocalAsTypeResolvesToUDT ( )
1126
+ {
1127
+ var code = @"
1128
+ Private Type TestProject1
1129
+ Foo As Integer
1130
+ Bar As String
1131
+ End Type
1132
+
1133
+ Private Type TestModule1
1134
+ Foo As TestProject1
1135
+ End Type
1136
+
1137
+ Public Sub DoSomething()
1138
+ Dim Foo As TestModule1
1139
+ Foo.Foo.Bar = ""DoSomething""
1140
+ Foo.Foo.Foo = 42
1141
+ End Sub
1142
+ " ;
1143
+ var state = Resolve ( code ) ;
1144
+
1145
+ var declaration = state . AllUserDeclarations . Single ( item =>
1146
+ item . DeclarationType == DeclarationType . UserDefinedType
1147
+ && item . IdentifierName == item . ComponentName ) ;
1148
+
1149
+ var usages = declaration . References . Where ( item =>
1150
+ item . ParentScoping . IdentifierName == "DoSomething" ) ;
1151
+
1152
+ Assert . AreEqual ( 1 , usages . Count ( ) ) ;
1153
+ }
1154
+
1155
+ [ TestMethod ]
1156
+ public void GivenUDTMember_NamedAfterUDTType_NamedAfterModule_LocalAsTypeResolvesToUDT ( )
1157
+ {
1158
+ var code = @"
1159
+ Private Type TestProject1
1160
+ Foo As Integer
1161
+ Bar As String
1162
+ End Type
1163
+
1164
+ Private Type TestModule1
1165
+ TestModule1 As TestProject1
1166
+ End Type
1167
+
1168
+ Public Sub DoSomething()
1169
+ Dim TestModule1 As TestModule1
1170
+ TestModule1.TestModule1.Bar = ""DoSomething""
1171
+ TestModule1.TestModule1.Foo = 42
1172
+ End Sub
1173
+ " ;
1174
+ var state = Resolve ( code ) ;
1175
+
1176
+ var declaration = state . AllUserDeclarations . Single ( item =>
1177
+ item . DeclarationType == DeclarationType . UserDefinedType
1178
+ && item . IdentifierName == item . ComponentName ) ;
1179
+
1180
+ var usages = declaration . References . Where ( item =>
1181
+ item . ParentScoping . IdentifierName == "DoSomething" ) ;
1182
+
1183
+ Assert . AreEqual ( 1 , usages . Count ( ) ) ;
1184
+ }
975
1185
}
976
1186
}
0 commit comments