1414*/
1515
1616using System ;
17- using System . Collections ;
1817using System . Collections . Generic ;
19- using System . IO ;
2018using System . Linq ;
21- using System . Reflection ;
2219using System . Threading ;
2320using FluentAssertions ;
2421using MongoDB . Bson ;
2522using MongoDB . Bson . TestHelpers ;
23+ using MongoDB . Bson . TestHelpers . JsonDrivenTests ;
2624using MongoDB . Driver . Core . Clusters ;
2725using MongoDB . Driver . Core . Configuration ;
2826using MongoDB . Driver . Core . Connections ;
@@ -43,9 +41,11 @@ public class TestRunner
4341
4442 [ Theory ]
4543 [ ClassData ( typeof ( TestCaseFactory ) ) ]
46- public void RunTestDefinition ( BsonDocument definition )
44+ public void RunTestDefinition ( JsonDrivenTestCase testCase )
4745 {
48- VerifyFields ( definition , "description" , "path" , "phases" , "uri" ) ;
46+ var definition = testCase . Test ;
47+
48+ JsonDrivenHelper . EnsureAllFieldsAreValid ( definition , "description" , "_path" , "phases" , "uri" ) ;
4949
5050 _cluster = BuildCluster ( definition ) ;
5151 _cluster . Initialize ( ) ;
@@ -59,7 +59,7 @@ public void RunTestDefinition(BsonDocument definition)
5959
6060 private void ApplyPhase ( BsonDocument phase )
6161 {
62- VerifyFields ( phase , "outcome" , "responses" ) ;
62+ JsonDrivenHelper . EnsureAllFieldsAreValid ( phase , "outcome" , "responses" ) ;
6363
6464 var responses = phase [ "responses" ] . AsBsonArray ;
6565 foreach ( BsonArray response in responses )
@@ -80,7 +80,7 @@ private void ApplyResponse(BsonArray response)
8080
8181 var address = response [ 0 ] . AsString ;
8282 var isMasterDocument = response [ 1 ] . AsBsonDocument ;
83- VerifyFields ( isMasterDocument , "arbiterOnly" , "arbiters" , "electionId" , "hidden" , "hosts" , "ismaster" , "isreplicaset" , "logicalSessionTimeoutMinutes" , "maxWireVersion" , "me" , "minWireVersion" , "msg" , "ok" , "passive" , "passives" , "primary" , "secondary" , "setName" , "setVersion" ) ;
83+ JsonDrivenHelper . EnsureAllFieldsAreValid ( isMasterDocument , "arbiterOnly" , "arbiters" , "electionId" , "hidden" , "hosts" , "ismaster" , "isreplicaset" , "logicalSessionTimeoutMinutes" , "maxWireVersion" , "me" , "minWireVersion" , "msg" , "ok" , "passive" , "passives" , "primary" , "secondary" , "setName" , "setVersion" ) ;
8484
8585 var endPoint = EndPointHelper . Parse ( address ) ;
8686 var isMasterResult = new IsMasterResult ( isMasterDocument ) ;
@@ -99,17 +99,6 @@ private void ApplyResponse(BsonArray response)
9999 SpinWait . SpinUntil ( ( ) => ! object . ReferenceEquals ( _cluster . Description , currentClusterDescription ) , 100 ) ; // sometimes returns false and that's OK
100100 }
101101
102- private void VerifyFields ( BsonDocument document , params string [ ] expectedNames )
103- {
104- foreach ( var name in document . Names )
105- {
106- if ( ! expectedNames . Contains ( name ) )
107- {
108- throw new FormatException ( $ "Invalid field: \" { name } \" .") ;
109- }
110- }
111- }
112-
113102 private void VerifyTopology ( ICluster cluster , string expectedType )
114103 {
115104 switch ( expectedType )
@@ -141,7 +130,7 @@ private void VerifyTopology(ICluster cluster, string expectedType)
141130
142131 private void VerifyOutcome ( BsonDocument outcome )
143132 {
144- VerifyFields ( outcome , "compatible" , "logicalSessionTimeoutMinutes" , "servers" , "setName" , "topologyType" ) ;
133+ JsonDrivenHelper . EnsureAllFieldsAreValid ( outcome , "compatible" , "logicalSessionTimeoutMinutes" , "servers" , "setName" , "topologyType" ) ;
145134
146135 var expectedTopologyType = ( string ) outcome [ "topologyType" ] ;
147136 VerifyTopology ( _cluster , expectedTopologyType ) ;
@@ -194,7 +183,7 @@ private void VerifyOutcome(BsonDocument outcome)
194183
195184 private void VerifyServerDescription ( ServerDescription actualDescription , BsonDocument expectedDescription )
196185 {
197- VerifyFields ( expectedDescription , "electionId" , "setName" , "setVersion" , "type" ) ;
186+ JsonDrivenHelper . EnsureAllFieldsAreValid ( expectedDescription , "electionId" , "setName" , "setVersion" , "type" ) ;
198187
199188 var expectedType = ( string ) expectedDescription [ "type" ] ;
200189 switch ( expectedType )
@@ -282,37 +271,19 @@ private ICluster BuildCluster(BsonDocument definition)
282271 . CreateCluster ( ) ;
283272 }
284273
285- private class TestCaseFactory : IEnumerable < object [ ] >
274+ private class TestCaseFactory : JsonDrivenTestCaseFactory
286275 {
287- public IEnumerator < object [ ] > GetEnumerator ( )
288- {
289- const string prefix = "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests." ;
290- const string monitoringPrefix = "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests.monitoring." ;
291- var executingAssembly = typeof ( TestCaseFactory ) . GetTypeInfo ( ) . Assembly ;
292- var enumerable = executingAssembly
293- . GetManifestResourceNames ( )
294- . Where ( path => path . StartsWith ( prefix ) && path . EndsWith ( ".json" ) )
295- . Where ( path => ! path . StartsWith ( monitoringPrefix ) )
296- . Select ( path => ReadDefinition ( path ) )
297- . Select ( definition => new object [ ] { definition } ) ;
298- return enumerable . GetEnumerator ( ) ;
299- }
276+ private readonly string __ignoredTestFolder = "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests.monitoring." ;
300277
301- IEnumerator IEnumerable . GetEnumerator ( )
302- {
303- return GetEnumerator ( ) ;
304- }
278+ protected override string PathPrefix => "MongoDB.Driver.Core.Tests.Specifications.server_discovery_and_monitoring.tests." ;
305279
306- private static BsonDocument ReadDefinition ( string path )
280+ protected override IEnumerable < JsonDrivenTestCase > CreateTestCases ( BsonDocument document )
307281 {
308- var executingAssembly = typeof ( TestCaseFactory ) . GetTypeInfo ( ) . Assembly ;
309- using ( var definitionStream = executingAssembly . GetManifestResourceStream ( path ) )
310- using ( var definitionStringReader = new StreamReader ( definitionStream ) )
282+ var path = document [ "_path" ] . ToString ( ) ;
283+ if ( ! path . StartsWith ( __ignoredTestFolder ) )
311284 {
312- var definitionString = definitionStringReader . ReadToEnd ( ) ;
313- var definition = BsonDocument . Parse ( definitionString ) ;
314- definition . InsertAt ( 0 , new BsonElement ( "path" , path ) ) ;
315- return definition ;
285+ var name = GetTestCaseName ( document , document , 0 ) ;
286+ yield return new JsonDrivenTestCase ( name , document , document ) ;
316287 }
317288 }
318289 }
0 commit comments