77
88namespace  RubberduckTests . Mocks 
99{ 
10+     /// <summary> 
11+     /// Builds a mock <see cref="VBProject"/>. 
12+     /// </summary> 
1013    public  class  MockProjectBuilder 
1114    { 
1215        private  readonly  Func < VBE >  _getVbe ; 
1316        private  readonly  Mock < VBProject >  _project ; 
1417        private  readonly  Mock < VBComponents >  _vbComponents ; 
1518        private  readonly  Mock < References >  _vbReferences ; 
1619
17-         private  readonly  List < Mock < VBComponent > >  _components  =  new  List < Mock < VBComponent > > ( ) ; 
18-         private  readonly  List < Mock < Reference > >  _references  =  new  List < Mock < Reference > > ( ) ;  
20+         private  readonly  ICollection < Mock < VBComponent > >  _components  =  new  List < Mock < VBComponent > > ( ) ; 
21+         private  readonly  ICollection < Mock < Reference > >  _references  =  new  List < Mock < Reference > > ( ) ;  
1922
2023        public  MockProjectBuilder ( string  name ,  vbext_ProjectProtection  protection ,  Func < VBE >  getVbe ) 
2124        { 
@@ -30,20 +33,58 @@ public MockProjectBuilder(string name, vbext_ProjectProtection protection, Func<
3033            _project . SetupGet ( m =>  m . References ) . Returns ( _vbReferences . Object ) ; 
3134        } 
3235
36+         /// <summary> 
37+         /// Adds a new component to the project. 
38+         /// </summary> 
39+         /// <param name="name">The name of the new component.</param> 
40+         /// <param name="type">The type of component to create.</param> 
41+         /// <param name="content">The VBA code associated to the component.</param> 
42+         /// <returns>Returns the <see cref="MockProjectBuilder"/> instance.</returns> 
3343        public  MockProjectBuilder  AddComponent ( string  name ,  vbext_ComponentType  type ,  string  content ) 
3444        { 
3545            var  component  =  CreateComponentMock ( name ,  type ,  content ) ; 
36-             _components . Add ( component ) ; 
46+             return  AddComponent ( component ) ; 
47+         } 
3748
38-             return  this ; 
49+         /// <summary> 
50+         /// Adds a new mock component to the project. 
51+         /// Use the <see cref="AddComponent(string,vbext_ComponentType,string)"/> overload to add module components. 
52+         /// Use this overload to add user forms created with a <see cref="MockUserFormBuilder"/> instance. 
53+         /// </summary> 
54+         /// <param name="component">The component to add.</param> 
55+         /// <returns>Returns the <see cref="MockProjectBuilder"/> instance.</returns> 
56+         public  MockProjectBuilder  AddComponent ( Mock < VBComponent >  component ) 
57+         { 
58+             _components . Add ( component ) ; 
59+             return  this ;             
3960        } 
4061
62+         /// <summary> 
63+         /// Adds a mock reference to the project. 
64+         /// </summary> 
65+         /// <param name="name">The name of the referenced library.</param> 
66+         /// <param name="filePath">The path to the referenced library.</param> 
67+         /// <returns>Returns the <see cref="MockProjectBuilder"/> instance.</returns> 
4168        public  MockProjectBuilder  AddReference ( string  name ,  string  filePath ) 
4269        { 
4370            _references . Add ( CreateReferenceMock ( name ,  filePath ) ) ; 
4471            return  this ; 
4572        } 
4673
74+         /// <summary> 
75+         /// Creates a <see cref="MockUserFormBuilder"/> to build a new form component. 
76+         /// </summary> 
77+         /// <param name="name">The name of the component.</param> 
78+         /// <param name="content">The VBA code associated to the component.</param> 
79+         public  MockUserFormBuilder  UserFormBuilder ( string  name ,  string  content ) 
80+         { 
81+             var  component  =  CreateComponentMock ( name ,  vbext_ComponentType . vbext_ct_MSForm ,  content ) ; 
82+             return  new  MockUserFormBuilder ( component ) ; 
83+         } 
84+ 
85+         /// <summary> 
86+         /// Gets the mock <see cref="VBProject"/> instance. 
87+         /// </summary> 
4788        public  Mock < VBProject >  Build ( ) 
4889        { 
4990            return  _project ; 
@@ -70,8 +111,9 @@ private Mock<VBComponents> CreateComponentsMock()
70111            result . Setup ( c =>  c . GetEnumerator ( ) ) . Returns ( ( )  =>  _components . GetEnumerator ( ) ) ; 
71112            result . As < IEnumerable > ( ) . Setup ( c =>  c . GetEnumerator ( ) ) . Returns ( ( )  =>  _components . GetEnumerator ( ) ) ; 
72113
73-             result . Setup ( m =>  m . Item ( It . IsAny < int > ( ) ) ) . Returns < int > ( index =>  _components [ index ] . Object ) ; 
114+             result . Setup ( m =>  m . Item ( It . IsAny < int > ( ) ) ) . Returns < int > ( index =>  _components . ElementAt ( index ) . Object ) ; 
74115            result . Setup ( m =>  m . Item ( It . IsAny < string > ( ) ) ) . Returns < string > ( name =>  _components . Single ( item =>  item . Object . Name  ==  name ) . Object ) ; 
116+             result . SetupGet ( m =>  m . Count ) . Returns ( _components . Count ) ; 
75117
76118            return  result ; 
77119        } 
@@ -86,7 +128,8 @@ private Mock<References> CreateReferencesMock()
86128            result . Setup ( m =>  m . GetEnumerator ( ) ) . Returns ( ( )  =>  _references . GetEnumerator ( ) ) ; 
87129            result . As < IEnumerable > ( ) . Setup ( m =>  m . GetEnumerator ( ) ) . Returns ( ( )  =>  _references . GetEnumerator ( ) ) ; 
88130
89-             result . Setup ( m =>  m . Item ( It . IsAny < int > ( ) ) ) . Returns < int > ( index =>  _references [ index ] . Object ) ; 
131+             result . Setup ( m =>  m . Item ( It . IsAny < int > ( ) ) ) . Returns < int > ( index =>  _references . ElementAt ( index ) . Object ) ; 
132+             result . SetupGet ( m =>  m . Count ) . Returns ( _references . Count ) ; 
90133
91134            return  result ; 
92135        } 
0 commit comments