1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
- using System . Linq ;
5
- using System . Text ;
6
- using System . Threading . Tasks ;
7
4
using Microsoft . Vbe . Interop ;
8
5
using Moq ;
9
6
10
7
namespace RubberduckTests . Mocks
11
8
{
12
9
static class MockFactory
13
10
{
11
+ /// <summary>
12
+ /// Creates a mock <see cref="Window"/> that is particularly useful for passing into <see cref="MockWindowsCollection"/>'s ctor.
13
+ /// </summary>
14
+ /// <returns>
15
+ /// A <see cref="Mock{Window}"/>that has all the properties needed for <see cref="Rubberduck.UI.DockablePresenterBase"/> pre-setup.
16
+ /// </returns>
14
17
internal static Mock < Window > CreateWindowMock ( )
15
18
{
16
19
var window = new Mock < Window > ( ) ;
@@ -22,6 +25,14 @@ internal static Mock<Window> CreateWindowMock()
22
25
return window ;
23
26
}
24
27
28
+ /// <summary>
29
+ /// Creates a new <see cref="Mock{VBE}"/> that returns the <see cref="Windows"/> collection argument out of the Windows property.
30
+ /// </summary>
31
+ /// <param name="windows">
32
+ /// A <see cref="MockWindowsCollection"/> is expected.
33
+ /// Other objects implementing the<see cref="Windows"/> interface could cause issues.
34
+ /// </param>
35
+ /// <returns></returns>
25
36
internal static Mock < VBE > CreateVbeMock ( Windows windows )
26
37
{
27
38
var vbe = new Mock < VBE > ( ) ;
@@ -30,6 +41,15 @@ internal static Mock<VBE> CreateVbeMock(Windows windows)
30
41
return vbe ;
31
42
}
32
43
44
+ /// <summary>
45
+ /// Creates a new <see cref="Mock{VBE}"/> with the <see cref="VBE.Windows"/> and <see cref="VBE.VBProjects"/> properties setup.
46
+ /// </summary>
47
+ /// <param name="windows">
48
+ /// A <see cref="MockWindowsCollection"/> is expected.
49
+ /// Other objects implementing the<see cref="Windows"/> interface could cause issues.
50
+ /// </param>
51
+ /// <param name="projects"><see cref="VBProjects"/> collecction.</param>
52
+ /// <returns></returns>
33
53
internal static Mock < VBE > CreateVbeMock ( Windows windows , VBProjects projects )
34
54
{
35
55
var vbe = CreateVbeMock ( windows ) ;
@@ -38,16 +58,37 @@ internal static Mock<VBE> CreateVbeMock(Windows windows, VBProjects projects)
38
58
return vbe ;
39
59
}
40
60
61
+ /// <summary>
62
+ /// Creates a new <see cref="Mock{CodeModule}"/> with <see cref="CodeModule.get_Lines"/> and <see cref="CodeModule.CountOfLines"/>
63
+ /// setup to appropriately mimic getting code out of the <see cref="CodeModule"/>.
64
+ /// </summary>
65
+ /// <param name="code">A block of VBA code.</param>
66
+ /// <returns></returns>
41
67
internal static Mock < CodeModule > CreateCodeModuleMock ( string code )
42
68
{
43
- var lineCount = code . Split ( new string [ ] { Environment . NewLine } , StringSplitOptions . None ) . Length ;
69
+ var lineCount = code . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) . Length ;
44
70
45
71
var codeModule = new Mock < CodeModule > ( ) ;
46
72
codeModule . SetupGet ( c => c . CountOfLines ) . Returns ( lineCount ) ;
73
+
74
+ // ReSharper disable once UseIndexedProperty
75
+ // No R#, the indexed property breaks the expression. I tried that first.
47
76
codeModule . SetupGet ( c => c . get_Lines ( 1 , lineCount ) ) . Returns ( code ) ;
48
77
return codeModule ;
49
78
}
50
79
80
+ /// <summary>
81
+ /// Creates a new <see cref="Mock{VBComponent}"/>.
82
+ /// </summary>
83
+ /// <param name="name">The name to return from the <see cref="VBComponent.Name"/> property.</param>
84
+ /// <param name="codeModule">The <see cref="CodeModule"/> to return from the CodeModule property.</param>
85
+ /// <param name="componentType">
86
+ /// The type of component to be simulated.
87
+ /// Use vbext_ct_StdModule for standard modules.
88
+ /// Use vbext_ct_ClassModule for classes.
89
+ /// vbext_ct_ActiveXDesigner is invalid for the VBE.
90
+ /// </param>
91
+ /// <returns></returns>
51
92
internal static Mock < VBComponent > CreateComponentMock ( string name , CodeModule codeModule , vbext_ComponentType componentType )
52
93
{
53
94
var component = new Mock < VBComponent > ( ) ;
@@ -57,16 +98,44 @@ internal static Mock<VBComponent> CreateComponentMock(string name, CodeModule co
57
98
return component ;
58
99
}
59
100
60
- internal static Mock < VBComponents > CreateComponentsMock ( List < VBComponent > componentList , VBProject project )
101
+ /// <summary>
102
+ /// Creates a new <see cref="Mock{VBComponets}"/> that can be iterated over as an <see cref="IEnumerable"/>.
103
+ /// </summary>
104
+ /// <param name="componentList">The collection to be iterated over.</param>
105
+ /// <returns></returns>
106
+ internal static Mock < VBComponents > CreateComponentsMock ( List < VBComponent > componentList )
61
107
{
62
108
var components = new Mock < VBComponents > ( ) ;
63
109
components . Setup ( c => c . GetEnumerator ( ) ) . Returns ( componentList . GetEnumerator ( ) ) ;
64
110
components . As < IEnumerable > ( ) . Setup ( c => c . GetEnumerator ( ) ) . Returns ( componentList . GetEnumerator ( ) ) ;
111
+
112
+ return components ;
113
+ }
114
+
115
+ /// <summary>
116
+ /// Creates a new <see cref="Mock{VBComponets}"/> that can be iterated over as an <see cref="IEnumerable"/>.
117
+ /// </summary>
118
+ /// <param name="componentList">The collection to be iterated over.</param>
119
+ /// <param name="project">The <see cref="VBComponents.Parent"/> property.</param>
120
+ /// <returns></returns>
121
+ internal static Mock < VBComponents > CreateComponentsMock ( List < VBComponent > componentList , VBProject project )
122
+ {
123
+ var components = CreateComponentsMock ( componentList ) ;
65
124
components . SetupGet ( c => c . Parent ) . Returns ( project ) ;
66
125
67
126
return components ;
68
127
}
69
128
129
+ /// <summary>
130
+ /// Creates a new <see cref="Mock{VBProject}"/>.
131
+ /// </summary>
132
+ /// <param name="name">The <see cref="VBProject.Name"/> property.</param>
133
+ /// <param name="protectionLevel">
134
+ /// The <see cref="VBProject.Protection"/> property.
135
+ /// Use vbext_pp_none to simulate a normal project.
136
+ /// Use vbext_pp_locked to simulate a password protected, or otherwise unavailable, project.
137
+ /// </param>
138
+ /// <returns></returns>
70
139
internal static Mock < VBProject > CreateProjectMock ( string name , vbext_ProjectProtection protectionLevel )
71
140
{
72
141
var project = new Mock < VBProject > ( ) ;
@@ -75,20 +144,18 @@ internal static Mock<VBProject> CreateProjectMock(string name, vbext_ProjectProt
75
144
return project ;
76
145
}
77
146
78
- internal static Mock < VBProjects > CreateProjectsMock ( List < VBProject > projectList , VBProject project )
147
+ /// <summary>
148
+ /// Creates a new <see cref="Mock{VBProjects}"/> that can be iterated over as an <see cref="IEnumerable"/>.
149
+ /// </summary>
150
+ /// <param name="projectList">The collection to be iterated over.</param>
151
+ /// <returns></returns>
152
+ internal static Mock < VBProjects > CreateProjectsMock ( List < VBProject > projectList )
79
153
{
80
154
var projects = new Mock < VBProjects > ( ) ;
81
155
projects . Setup ( p => p . GetEnumerator ( ) ) . Returns ( projectList . GetEnumerator ( ) ) ;
82
156
projects . As < IEnumerable > ( ) . Setup ( p => p . GetEnumerator ( ) ) . Returns ( projectList . GetEnumerator ( ) ) ;
83
157
84
158
return projects ;
85
159
}
86
-
87
- //internal static Mock<VBProjects> CreateProjectsMock(List<VBProject> projectList, VBProject project, VBComponents components)
88
- //{
89
- // CreateProjectsMock(projectList, project);
90
- // project.SetupGet(p => p.VBComponents).Returns(components.Object);
91
- // return projects;
92
- //}
93
160
}
94
161
}
0 commit comments