Skip to content

Commit cddc136

Browse files
kevbitevincentkam
authored andcommitted
CSHARP-3037: Correct types in ArgumentException message.
1 parent 995a53f commit cddc136

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

src/MongoDB.Driver/PipelineDefinition.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,34 +332,29 @@ public override RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInpu
332332

333333
private static List<IPipelineStageDefinition> VerifyStages(List<IPipelineStageDefinition> stages)
334334
{
335-
var nextInputType = typeof(TInput);
335+
var expectedInputType = typeof(TInput);
336336
for (int i = 0; i < stages.Count; i++)
337337
{
338-
if (stages[i].InputType != nextInputType)
338+
if (stages[i].InputType != expectedInputType)
339339
{
340-
var message = string.Format(
341-
"The input type to stage[{0}] was expected to be {1}, but was {2}.",
342-
i,
343-
nextInputType,
344-
stages[i].InputType);
345-
throw new ArgumentException(message, "stages");
340+
var message =
341+
$"The input type to stage[{i}] was expected to be {expectedInputType}, but was {stages[i].InputType}.";
342+
throw new ArgumentException(message, nameof(stages));
346343
}
347344

348-
nextInputType = stages[i].OutputType;
345+
expectedInputType = stages[i].OutputType;
349346
}
347+
var lastStageOutputType = expectedInputType;
350348

351-
if (nextInputType != typeof(TOutput))
349+
if (lastStageOutputType != typeof(TOutput))
352350
{
353-
var message = string.Format(
354-
"The output type to the last stage was expected to be {0}, but was {1}.",
355-
nextInputType,
356-
stages.Last().OutputType);
357-
throw new ArgumentException(message, "stages");
351+
var message =
352+
$"The output type to the last stage was expected to be {typeof(TOutput)}, but was {lastStageOutputType}.";
353+
throw new ArgumentException(message, nameof(stages));
358354
}
359355

360356
return stages;
361357
}
362-
}
363358

364359
internal class OptimizingPipelineDefinition<TInput, TOutput> : PipelineDefinition<TInput, TOutput>
365360
{
@@ -380,7 +375,7 @@ public override RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInpu
380375
{
381376
var rendered = _wrapped.Render(inputSerializer, serializerRegistry);
382377

383-
// do some combining of $match documents if possible. This is optimized for the
378+
// do some combining of $match documents if possible. This is optimized for the
384379
// OfType case where we've added a discriminator as a match at the beginning of the pipeline.
385380
if (rendered.Documents.Count > 1)
386381
{
@@ -401,4 +396,4 @@ public override RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInpu
401396
return rendered;
402397
}
403398
}
404-
}
399+
}

tests/MongoDB.Driver.Tests/PipelineDefinitionTests.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,43 @@ namespace MongoDB.Driver.Tests
2525
public class PipelineStagePipelineDefinitionTests
2626
{
2727
[Fact]
28-
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_invalid()
28+
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_intermediate_stage_is_invalid()
2929
{
30-
var stages = new IPipelineStageDefinition[]
30+
var stages = new IPipelineStageDefinition[]
3131
{
3232
new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
3333
new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
3434
new BsonDocumentPipelineStageDefinition<BsonDocument, Person>(new BsonDocument())
3535
};
3636

37-
Action act = () => new PipelineStagePipelineDefinition<Person, Person>(stages);
37+
var exception = Record.Exception(() => new PipelineStagePipelineDefinition<Person, Person>(stages));
3838

39-
act.ShouldThrow<ArgumentException>();
39+
var e = exception.Should().BeOfType<ArgumentException>().Subject;
40+
e.ParamName.Should().Be("stages");
41+
e.Message.Should().Contain($"The input type to stage[2] was expected to be {typeof(Pet)}, but was {typeof(BsonDocument)}.");
4042
}
4143

44+
[Fact]
45+
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_final_stage_is_invalid()
46+
{
47+
var stages = new IPipelineStageDefinition[]
48+
{
49+
new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
50+
new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
51+
new BsonDocumentPipelineStageDefinition<Pet, BsonDocument>(new BsonDocument())
52+
};
53+
54+
var exception = Record.Exception(() => new PipelineStagePipelineDefinition<Person, Person>(stages));
55+
56+
var e = exception.Should().BeOfType<ArgumentException>().Subject;
57+
e.ParamName.Should().Be("stages");
58+
e.Message.Should().Contain($"The output type to the last stage was expected to be {typeof(Person)}, but was {typeof(BsonDocument)}.");
59+
}
60+
4261
[Fact]
4362
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages()
4463
{
45-
var stages = new IPipelineStageDefinition[]
64+
var stages = new IPipelineStageDefinition[]
4665
{
4766
new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
4867
new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
@@ -71,7 +90,7 @@ private class Person
7190
{
7291
[BsonElement("fn")]
7392
public string FirstName { get; set; }
74-
93+
7594
[BsonElement("pets")]
7695
public Pet[] Pets { get; set; }
7796
}
@@ -80,6 +99,6 @@ private class Pet
8099
{
81100
[BsonElement("name")]
82101
public string Name { get; set; }
83-
}
102+
}
84103
}
85104
}

0 commit comments

Comments
 (0)