Skip to content

Commit 189cc84

Browse files
committed
#844 part way through setup of moving external reference
1 parent 0fb69b3 commit 189cc84

File tree

3 files changed

+117
-21
lines changed

3 files changed

+117
-21
lines changed

RetailCoder.VBE/Refactorings/ExtractMethod/ExtractMethodModel.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public void extract(IEnumerable<Declaration> declarations, QualifiedSelection se
4040
_byref = new List<Declaration>();
4141
_byval = new List<Declaration>();
4242
_declarationsToMove = new List<Declaration>();
43-
_extractDeclarations = new List<Tuple<Declaration,bool>>();
43+
_extractDeclarations = new List<Tuple<Declaration, bool>>();
4444
_extractedMethod = new ExtractedMethod();
45-
45+
4646

4747
var selectionToRemove = new List<Selection>();
4848
var selectionStartLine = selection.Selection.StartLine;
@@ -73,8 +73,14 @@ public void extract(IEnumerable<Declaration> declarations, QualifiedSelection se
7373
else if (flags > 12)
7474
_byval.Add(item);
7575

76+
if (flags >= 18)
77+
{
78+
_extractDeclarations.Add(Tuple.Create(item, true));
79+
}
80+
7681
}
7782

83+
7884
_declarationsToMove.ForEach(d => selectionToRemove.Add(d.Selection));
7985
selectionToRemove.Add(selection.Selection);
8086

@@ -146,14 +152,14 @@ public void extract(IEnumerable<Declaration> declarations, QualifiedSelection se
146152
public string NewMethodCall { get { return _extractedMethod.NewMethodCall(); } }
147153

148154
private Selection _positionForNewMethod;
149-
public Selection PositionForNewMethod { get { return _positionForNewMethod; } }
155+
public Selection PositionForNewMethod { get { return _positionForNewMethod; } }
150156
IEnumerable<Selection> _selectionToRemove;
151157
private List<IExtractMethodRule> emRules;
152158

153159
public IEnumerable<Selection> SelectionToRemove { get { return _selectionToRemove; } }
154160

155-
private IEnumerable<Tuple<Declaration,bool>> _extractDeclarations;
156-
public IEnumerable<Tuple<Declaration,bool>> ExtractDeclarations { get { return _extractDeclarations; } }
161+
private List<Tuple<Declaration, bool>> _extractDeclarations;
162+
public IEnumerable<Tuple<Declaration, bool>> ExtractDeclarations { get { return _extractDeclarations; } }
157163

158164
}
159165
}

RetailCoder.VBE/Refactorings/ExtractMethod/IExtractMethodRule.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public enum ExtractMethodRuleFlags
1818
UsedBefore = 1,
1919
UsedAfter = 2,
2020
IsAssigned = 4,
21-
InSelection = 8
21+
InSelection = 8,
22+
IsExternallyReferenced = 16
2223

2324
}
2425

@@ -30,6 +31,21 @@ public void setValidFlag(ref byte flags, IdentifierReference reference, Selectio
3031
flags = (byte)(flags | (byte)ExtractMethodRuleFlags.UsedBefore);
3132
}
3233
}
34+
35+
public class ExtractMethodRuleExternalReference : IExtractMethodRule
36+
{
37+
public void setValidFlag(ref byte flags, IdentifierReference reference, Selection selection)
38+
{
39+
if (reference.Selection.StartLine > selection.EndLine &&
40+
selection.StartLine <= reference.Declaration.Selection.StartLine &&
41+
reference.Declaration.Selection.StartLine <= selection.EndLine)
42+
{
43+
44+
flags = (byte)(flags | (byte)ExtractMethodRuleFlags.IsExternallyReferenced);
45+
}
46+
}
47+
}
48+
3349
public class ExtractMethodRuleUsedAfter : IExtractMethodRule
3450
{
3551
public void setValidFlag(ref byte flags, IdentifierReference reference, Selection selection)
@@ -38,6 +54,7 @@ public void setValidFlag(ref byte flags, IdentifierReference reference, Selectio
3854
flags = (byte)(flags | (byte)ExtractMethodRuleFlags.UsedAfter);
3955
}
4056
}
57+
4158
public class ExtractMethodRuleIsAssignedInSelection : IExtractMethodRule
4259
{
4360
public void setValidFlag(ref byte flags, IdentifierReference reference, Selection selection)
@@ -49,12 +66,13 @@ public void setValidFlag(ref byte flags, IdentifierReference reference, Selectio
4966
}
5067
}
5168
}
69+
5270
public class ExtractMethodRuleInSelection : IExtractMethodRule
5371
{
5472
public void setValidFlag(ref byte flags, IdentifierReference reference, Selection selection)
5573
{
56-
if (selection.StartLine <= reference.Selection.StartLine &&
57-
reference.Selection.StartLine <= selection.EndLine &&
74+
if (selection.StartLine <= reference.Selection.StartLine &&
75+
reference.Selection.StartLine <= selection.EndLine &&
5876
((reference.Declaration == null) ? false : reference.Declaration.Selection.StartLine != reference.Selection.StartLine))
5977
{
6078
flags = (byte)(flags | ((byte)ExtractMethodRuleFlags.InSelection));

RubberduckTests/Refactoring/ExtractMethod/ExtractMethodModelTests.cs

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,6 @@ public void shouldNotSetFlag()
282282
public class ExtractMethodModelTests
283283
{
284284

285-
List<IExtractMethodRule> emRules = new List<IExtractMethodRule>(){
286-
new ExtractMethodRuleInSelection(),
287-
new ExtractMethodRuleIsAssignedInSelection(),
288-
new ExtractMethodRuleUsedBefore(),
289-
new ExtractMethodRuleUsedAfter()};
290285
[TestClass]
291286
public class WhenExtractingFromASelection : ExtractedMethodTests
292287
{
@@ -327,6 +322,11 @@ DebugPrint y
327322
End Sub";
328323
#endregion
329324

325+
List<IExtractMethodRule> emRules = new List<IExtractMethodRule>(){
326+
new ExtractMethodRuleInSelection(),
327+
new ExtractMethodRuleIsAssignedInSelection(),
328+
new ExtractMethodRuleUsedBefore(),
329+
new ExtractMethodRuleUsedAfter()};
330330

331331
[TestMethod]
332332
[TestCategory("ExtractMethodModelTests")]
@@ -702,6 +702,10 @@ Debug.Print a
702702
[TestClass]
703703
public class WhenVariableIsDefinedInTheSelection : ExtractMethodModelTests
704704
{
705+
[TestClass]
706+
public class AndIsUsedAfterSelection : WhenVariableIsDefinedInTheSelection
707+
{
708+
705709
#region variableInternalAndOnlyUsedInternally
706710
string internalVariable = @"
707711
Option explicit
@@ -716,8 +720,9 @@ Dim y as long
716720
x = 2
717721
DebugPrint y '12:
718722
719-
z = x
720-
DebugPrint z
723+
y = x
724+
DebugPrint y
725+
z = 1
721726
722727
End Sub
723728
Public Sub DebugPrint(byval g as long)
@@ -731,28 +736,95 @@ End Sub
731736
Debug.Print y";
732737

733738
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)
736740
DebugPrint ""something""
737741
y = x + 1
738742
x = 2
739743
DebugPrint y
740744
End Sub";
741745
#endregion
742746

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+
}
743776
[TestClass]
744777
public class AndIsNotUsedOutsideOfSelection : WhenVariableIsDefinedInTheSelection
745778
{
746779

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+
747818
List<IExtractMethodRule> emRules = new List<IExtractMethodRule>(){
748819
new ExtractMethodRuleInSelection(),
749820
new ExtractMethodRuleIsAssignedInSelection(),
750821
new ExtractMethodRuleUsedBefore(),
751-
new ExtractMethodRuleUsedAfter()};
822+
new ExtractMethodRuleUsedAfter(),
823+
new ExtractMethodRuleExternalReference()};
752824

753825
[TestMethod]
754826
[TestCategory("ExtractMethodModelTests")]
755-
public void shouldOnlyHaveInternallyMarkedDefinitions()
827+
public void shouldNotHaveAnyExternallyMarkedDeclarations()
756828
{
757829

758830

@@ -770,8 +842,8 @@ public void shouldOnlyHaveInternallyMarkedDefinitions()
770842
var SUT = new ExtractMethodModel(emRules, extractedMethod.Object);
771843
SUT.extract(declarations, qSelection.Value, selectedCode);
772844

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");
775847

776848
}
777849
}

0 commit comments

Comments
 (0)