@@ -849,6 +849,252 @@ End Sub
849
849
Assert . AreEqual ( expectedCode , actualCode ) ;
850
850
}
851
851
852
+ [ Test ]
853
+ [ Category ( "AttributesUpdater" ) ]
854
+ public void AddOrUpdateAttribute_UsualAttribute_NotThere_AddsAttribute ( )
855
+ {
856
+ const string inputCode =
857
+ @"VERSION 1.0 CLASS
858
+ BEGIN
859
+ MultiUse = -1 'True
860
+ END
861
+ Attribute VB_Name = ""ClassKeys""
862
+ Attribute VB_GlobalNameSpace = False
863
+ Public Sub Foo(bar As String)
864
+ bar = vbNullString
865
+ End Sub
866
+ " ;
867
+
868
+ const string expectedCode =
869
+ @"VERSION 1.0 CLASS
870
+ BEGIN
871
+ MultiUse = -1 'True
872
+ END
873
+ Attribute VB_Name = ""ClassKeys""
874
+ Attribute VB_GlobalNameSpace = False
875
+ Attribute VB_Exposed = True
876
+ Public Sub Foo(bar As String)
877
+ bar = vbNullString
878
+ End Sub
879
+ " ;
880
+ var attribute = "VB_Exposed" ;
881
+ var newValues = new List < string > { "True" } ;
882
+
883
+ string actualCode ;
884
+ var ( component , rewriteSession , state ) = TestSetup ( inputCode ) ;
885
+ using ( state )
886
+ {
887
+ var moduleDeclaration = state . DeclarationFinder
888
+ . UserDeclarations ( DeclarationType . Module )
889
+ . Single ( ) ;
890
+ var attributesUpdater = new AttributesUpdater ( state ) ;
891
+
892
+ attributesUpdater . AddOrUpdateAttribute ( rewriteSession , moduleDeclaration , attribute , newValues ) ;
893
+ rewriteSession . TryRewrite ( ) ;
894
+
895
+ actualCode = component . CodeModule . Content ( ) ;
896
+ }
897
+ Assert . AreEqual ( expectedCode , actualCode ) ;
898
+ }
899
+
900
+ [ Test ]
901
+ [ Category ( "AttributesUpdater" ) ]
902
+ public void AddOrUpdateAttribute_ExtKey_NotThere_AddsAttribute ( )
903
+ {
904
+ const string inputCode =
905
+ @"VERSION 1.0 CLASS
906
+ BEGIN
907
+ MultiUse = -1 'True
908
+ END
909
+ Attribute VB_Name = ""ClassKeys""
910
+ Attribute VB_GlobalNameSpace = False
911
+ Public Sub Foo(bar As String)
912
+ bar = vbNullString
913
+ End Sub
914
+ " ;
915
+
916
+ const string expectedCode =
917
+ @"VERSION 1.0 CLASS
918
+ BEGIN
919
+ MultiUse = -1 'True
920
+ END
921
+ Attribute VB_Name = ""ClassKeys""
922
+ Attribute VB_GlobalNameSpace = False
923
+ Attribute VB_Ext_Key = ""MyKey"", ""MyValue""
924
+ Public Sub Foo(bar As String)
925
+ bar = vbNullString
926
+ End Sub
927
+ " ;
928
+ var attribute = "VB_Ext_Key" ;
929
+ var newValues = new List < string > { "\" MyKey\" " , "\" MyValue\" " } ;
930
+
931
+ string actualCode ;
932
+ var ( component , rewriteSession , state ) = TestSetup ( inputCode ) ;
933
+ using ( state )
934
+ {
935
+ var moduleDeclaration = state . DeclarationFinder
936
+ . UserDeclarations ( DeclarationType . Module )
937
+ . Single ( ) ;
938
+ var attributesUpdater = new AttributesUpdater ( state ) ;
939
+
940
+ attributesUpdater . AddOrUpdateAttribute ( rewriteSession , moduleDeclaration , attribute , newValues ) ;
941
+ rewriteSession . TryRewrite ( ) ;
942
+
943
+ actualCode = component . CodeModule . Content ( ) ;
944
+ }
945
+ Assert . AreEqual ( expectedCode , actualCode ) ;
946
+ }
947
+
948
+ [ Test ]
949
+ [ Category ( "AttributesUpdater" ) ]
950
+ public void AddOrUpdateAttribute_UsualAttribute_AlreadyThere_UpdatesAttribute ( )
951
+ {
952
+ const string inputCode =
953
+ @"VERSION 1.0 CLASS
954
+ BEGIN
955
+ MultiUse = -1 'True
956
+ END
957
+ Attribute VB_Name = ""ClassKeys""
958
+ Attribute VB_GlobalNameSpace = False
959
+ Attribute VB_Exposed = False
960
+ Public Sub Foo(bar As String)
961
+ bar = vbNullString
962
+ End Sub
963
+ " ;
964
+
965
+ const string expectedCode =
966
+ @"VERSION 1.0 CLASS
967
+ BEGIN
968
+ MultiUse = -1 'True
969
+ END
970
+ Attribute VB_Name = ""ClassKeys""
971
+ Attribute VB_GlobalNameSpace = False
972
+ Attribute VB_Exposed = True
973
+ Public Sub Foo(bar As String)
974
+ bar = vbNullString
975
+ End Sub
976
+ " ;
977
+ var attribute = "VB_Exposed" ;
978
+ var newValues = new List < string > { "True" } ;
979
+
980
+ string actualCode ;
981
+ var ( component , rewriteSession , state ) = TestSetup ( inputCode ) ;
982
+ using ( state )
983
+ {
984
+ var moduleDeclaration = state . DeclarationFinder
985
+ . UserDeclarations ( DeclarationType . Module )
986
+ . Single ( ) ;
987
+ var attributesUpdater = new AttributesUpdater ( state ) ;
988
+
989
+ attributesUpdater . AddOrUpdateAttribute ( rewriteSession , moduleDeclaration , attribute , newValues ) ;
990
+ rewriteSession . TryRewrite ( ) ;
991
+
992
+ actualCode = component . CodeModule . Content ( ) ;
993
+ }
994
+ Assert . AreEqual ( expectedCode , actualCode ) ;
995
+ }
996
+
997
+ [ Test ]
998
+ [ Category ( "AttributesUpdater" ) ]
999
+ public void AddOrUpdateAttribute_ExtKey_KeyNotThere_AddsAttribute ( )
1000
+ {
1001
+ const string inputCode =
1002
+ @"VERSION 1.0 CLASS
1003
+ BEGIN
1004
+ MultiUse = -1 'True
1005
+ END
1006
+ Attribute VB_Name = ""ClassKeys""
1007
+ Attribute VB_GlobalNameSpace = False
1008
+ Attribute VB_Ext_Key = ""AnotherKey"", ""MyValuse""
1009
+ Public Sub Foo(bar As String)
1010
+ bar = vbNullString
1011
+ End Sub
1012
+ " ;
1013
+
1014
+ const string expectedCode =
1015
+ @"VERSION 1.0 CLASS
1016
+ BEGIN
1017
+ MultiUse = -1 'True
1018
+ END
1019
+ Attribute VB_Name = ""ClassKeys""
1020
+ Attribute VB_GlobalNameSpace = False
1021
+ Attribute VB_Ext_Key = ""AnotherKey"", ""MyValuse""
1022
+ Attribute VB_Ext_Key = ""MyKey"", ""MyValue""
1023
+ Public Sub Foo(bar As String)
1024
+ bar = vbNullString
1025
+ End Sub
1026
+ " ;
1027
+ var attribute = "VB_Ext_Key" ;
1028
+ var newValues = new List < string > { "\" MyKey\" " , "\" MyValue\" " } ;
1029
+
1030
+ string actualCode ;
1031
+ var ( component , rewriteSession , state ) = TestSetup ( inputCode ) ;
1032
+ using ( state )
1033
+ {
1034
+ var moduleDeclaration = state . DeclarationFinder
1035
+ . UserDeclarations ( DeclarationType . Module )
1036
+ . Single ( ) ;
1037
+ var attributesUpdater = new AttributesUpdater ( state ) ;
1038
+
1039
+ attributesUpdater . AddOrUpdateAttribute ( rewriteSession , moduleDeclaration , attribute , newValues ) ;
1040
+ rewriteSession . TryRewrite ( ) ;
1041
+
1042
+ actualCode = component . CodeModule . Content ( ) ;
1043
+ }
1044
+ Assert . AreEqual ( expectedCode , actualCode ) ;
1045
+ }
1046
+
1047
+ [ Test ]
1048
+ [ Category ( "AttributesUpdater" ) ]
1049
+ public void AddOrUpdateAttribute_ExtKey_KeyAlreadyThere_UpdatesAttribute ( )
1050
+ {
1051
+ const string inputCode =
1052
+ @"VERSION 1.0 CLASS
1053
+ BEGIN
1054
+ MultiUse = -1 'True
1055
+ END
1056
+ Attribute VB_Name = ""ClassKeys""
1057
+ Attribute VB_GlobalNameSpace = False
1058
+ Attribute VB_Ext_Key = ""AnotherKey"", ""MyValue""
1059
+ Attribute VB_Ext_Key = ""MyKey"", ""AnotherValue""
1060
+ Public Sub Foo(bar As String)
1061
+ bar = vbNullString
1062
+ End Sub
1063
+ " ;
1064
+
1065
+ const string expectedCode =
1066
+ @"VERSION 1.0 CLASS
1067
+ BEGIN
1068
+ MultiUse = -1 'True
1069
+ END
1070
+ Attribute VB_Name = ""ClassKeys""
1071
+ Attribute VB_GlobalNameSpace = False
1072
+ Attribute VB_Ext_Key = ""AnotherKey"", ""MyValue""
1073
+ Attribute VB_Ext_Key = ""MyKey"", ""MyValue""
1074
+ Public Sub Foo(bar As String)
1075
+ bar = vbNullString
1076
+ End Sub
1077
+ " ;
1078
+ var attribute = "VB_Ext_Key" ;
1079
+ var newValues = new List < string > { "\" MyKey\" " , "\" MyValue\" " } ;
1080
+
1081
+ string actualCode ;
1082
+ var ( component , rewriteSession , state ) = TestSetup ( inputCode ) ;
1083
+ using ( state )
1084
+ {
1085
+ var moduleDeclaration = state . DeclarationFinder
1086
+ . UserDeclarations ( DeclarationType . Module )
1087
+ . Single ( ) ;
1088
+ var attributesUpdater = new AttributesUpdater ( state ) ;
1089
+
1090
+ attributesUpdater . AddOrUpdateAttribute ( rewriteSession , moduleDeclaration , attribute , newValues ) ;
1091
+ rewriteSession . TryRewrite ( ) ;
1092
+
1093
+ actualCode = component . CodeModule . Content ( ) ;
1094
+ }
1095
+ Assert . AreEqual ( expectedCode , actualCode ) ;
1096
+ }
1097
+
852
1098
private ( IVBComponent component , IExecutableRewriteSession rewriteSession , RubberduckParserState state ) TestSetup ( string inputCode )
853
1099
{
854
1100
var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out var component ) . Object ;
0 commit comments