@@ -282,11 +282,6 @@ public void shouldNotSetFlag()
282
282
public class ExtractMethodModelTests
283
283
{
284
284
285
- List < IExtractMethodRule > emRules = new List < IExtractMethodRule > ( ) {
286
- new ExtractMethodRuleInSelection ( ) ,
287
- new ExtractMethodRuleIsAssignedInSelection ( ) ,
288
- new ExtractMethodRuleUsedBefore ( ) ,
289
- new ExtractMethodRuleUsedAfter ( ) } ;
290
285
[ TestClass ]
291
286
public class WhenExtractingFromASelection : ExtractedMethodTests
292
287
{
@@ -327,6 +322,11 @@ DebugPrint y
327
322
End Sub" ;
328
323
#endregion
329
324
325
+ List < IExtractMethodRule > emRules = new List < IExtractMethodRule > ( ) {
326
+ new ExtractMethodRuleInSelection ( ) ,
327
+ new ExtractMethodRuleIsAssignedInSelection ( ) ,
328
+ new ExtractMethodRuleUsedBefore ( ) ,
329
+ new ExtractMethodRuleUsedAfter ( ) } ;
330
330
331
331
[ TestMethod ]
332
332
[ TestCategory ( "ExtractMethodModelTests" ) ]
@@ -702,6 +702,10 @@ Debug.Print a
702
702
[ TestClass ]
703
703
public class WhenVariableIsDefinedInTheSelection : ExtractMethodModelTests
704
704
{
705
+ [ TestClass ]
706
+ public class AndIsUsedAfterSelection : WhenVariableIsDefinedInTheSelection
707
+ {
708
+
705
709
#region variableInternalAndOnlyUsedInternally
706
710
string internalVariable = @"
707
711
Option explicit
@@ -716,8 +720,9 @@ Dim y as long
716
720
x = 2
717
721
DebugPrint y '12:
718
722
719
- z = x
720
- DebugPrint z
723
+ y = x
724
+ DebugPrint y
725
+ z = 1
721
726
722
727
End Sub
723
728
Public Sub DebugPrint(byval g as long)
@@ -731,28 +736,95 @@ End Sub
731
736
Debug.Print y" ;
732
737
733
738
string outputCode = @"
734
- Public Sub NewVal( byval x as long)
735
- Dim y as long
739
+ Public Sub NewVal( byval x as long, byval y as long)
736
740
DebugPrint ""something""
737
741
y = x + 1
738
742
x = 2
739
743
DebugPrint y
740
744
End Sub" ;
741
745
#endregion
742
746
747
+ List < IExtractMethodRule > emRules = new List < IExtractMethodRule > ( ) {
748
+ new ExtractMethodRuleInSelection ( ) ,
749
+ new ExtractMethodRuleIsAssignedInSelection ( ) ,
750
+ new ExtractMethodRuleUsedBefore ( ) ,
751
+ new ExtractMethodRuleUsedAfter ( ) ,
752
+ new ExtractMethodRuleExternalReference ( ) } ;
753
+
754
+ [ TestMethod ]
755
+ [ TestCategory ( "ExtractMethodModelTests" ) ]
756
+ public void shouldMarkDeclarationForExternalUse ( )
757
+ {
758
+ QualifiedModuleName qualifiedModuleName ;
759
+ RubberduckParserState state ;
760
+ MockParser . ParseString ( internalVariable , out qualifiedModuleName , out state ) ;
761
+ var declarations = state . AllDeclarations ;
762
+
763
+ var selection = new Selection ( 8 , 1 , 12 , 24 ) ;
764
+ QualifiedSelection ? qSelection = new QualifiedSelection ( qualifiedModuleName , selection ) ;
765
+
766
+ var emr = new Mock < IExtractMethodRule > ( ) ;
767
+ var extractedMethod = new Mock < IExtractedMethod > ( ) ;
768
+ var extractedMethodProc = new Mock < IExtractMethodProc > ( ) ;
769
+ var SUT = new ExtractMethodModel ( emRules , extractedMethod . Object ) ;
770
+ SUT . extract ( declarations , qSelection . Value , selectedCode ) ;
771
+
772
+ var actual = SUT . ExtractDeclarations . Count ( x => x . Item2 ) ;
773
+ Assert . AreEqual ( 1 , actual , "y should have been marked as external" ) ;
774
+ }
775
+ }
743
776
[ TestClass ]
744
777
public class AndIsNotUsedOutsideOfSelection : WhenVariableIsDefinedInTheSelection
745
778
{
746
779
780
+ #region variableInternalAndOnlyUsedInternally
781
+ string internalVariable = @"
782
+ Option explicit
783
+ Public Sub CodeWithDeclaration()
784
+ Dim x as long
785
+ Dim z as long
786
+
787
+ x = 1 + 2
788
+ DebugPrint ""something"" '8:
789
+ Dim y as long
790
+ y = x + 1
791
+ x = 2
792
+ DebugPrint y '12:
793
+
794
+ z = x
795
+ DebugPrint z
796
+
797
+ End Sub
798
+ Public Sub DebugPrint(byval g as long)
799
+ End Sub
800
+
801
+
802
+ " ;
803
+ string selectedCode = @"
804
+ y = x + 1
805
+ x = 2
806
+ Debug.Print y" ;
807
+
808
+ string outputCode = @"
809
+ Public Sub NewVal( byval x as long)
810
+ Dim y as long
811
+ DebugPrint ""something""
812
+ y = x + 1
813
+ x = 2
814
+ DebugPrint y
815
+ End Sub" ;
816
+ #endregion
817
+
747
818
List < IExtractMethodRule > emRules = new List < IExtractMethodRule > ( ) {
748
819
new ExtractMethodRuleInSelection ( ) ,
749
820
new ExtractMethodRuleIsAssignedInSelection ( ) ,
750
821
new ExtractMethodRuleUsedBefore ( ) ,
751
- new ExtractMethodRuleUsedAfter ( ) } ;
822
+ new ExtractMethodRuleUsedAfter ( ) ,
823
+ new ExtractMethodRuleExternalReference ( ) } ;
752
824
753
825
[ TestMethod ]
754
826
[ TestCategory ( "ExtractMethodModelTests" ) ]
755
- public void shouldOnlyHaveInternallyMarkedDefinitions ( )
827
+ public void shouldNotHaveAnyExternallyMarkedDeclarations ( )
756
828
{
757
829
758
830
@@ -770,8 +842,8 @@ public void shouldOnlyHaveInternallyMarkedDefinitions()
770
842
var SUT = new ExtractMethodModel ( emRules , extractedMethod . Object ) ;
771
843
SUT . extract ( declarations , qSelection . Value , selectedCode ) ;
772
844
773
- var actual = SUT . ExtractDeclarations . Count ( x => ! x . Item2 ) ;
774
- Assert . AreEqual ( 0 , SUT . ExtractDeclarations . Count ( ) , "There should be no declarations in the collection" ) ;
845
+ var actual = SUT . ExtractDeclarations . Count ( x => x . Item2 ) ;
846
+ Assert . AreEqual ( 0 , actual , "There should be no declarations in the collection" ) ;
775
847
776
848
}
777
849
}
0 commit comments