Skip to content
This repository was archived by the owner on Apr 20, 2019. It is now read-only.

Commit bb12bc7

Browse files
committed
CSHARP-2066 Unit tests for array index formatting
Also rewrote the production code in C# 6 compliant syntax
1 parent 79f90c6 commit bb12bc7

File tree

4 files changed

+515
-463
lines changed

4 files changed

+515
-463
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace MongoDB.Driver.Linq
2+
{
3+
/// <summary>
4+
/// In order to support the positional operator, the number -1 should be treated specially when used as an array index.
5+
/// </summary>
6+
public class ArrayIndexFormatter
7+
{
8+
/// <summary>
9+
/// Formats any object to its string representation, unless <paramref name="index"/> is of type <c>int</c>, in which case a dollar sign ($) is returned.
10+
/// </summary>
11+
/// <param name="index"></param>
12+
/// <returns></returns>
13+
public static string FormatArrayIndex(object index)
14+
{
15+
// We've treated -1 as meaning $ operator. We can't break this now,
16+
// so, specifically when we are flattening fields names, this is
17+
// how we'll continue to treat -1.
18+
19+
return index is int && (int)index == -1
20+
? "$"
21+
: index.ToString();
22+
}
23+
}
24+
}

src/MongoDB.Driver/Linq/FieldExpressionFlattener.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@
2020

2121
namespace MongoDB.Driver.Linq
2222
{
23-
internal class FieldExpressionFlattener : ExtensionExpressionVisitor
23+
internal class FieldExpressionFlattener : ExtensionExpressionVisitor
2424
{
2525
public static Expression FlattenFields(Expression node)
2626
{
2727
var visitor = new FieldExpressionFlattener();
2828
return visitor.Visit(node);
2929
}
3030

31-
public FieldExpressionFlattener()
32-
{
33-
}
34-
3531
protected internal override Expression VisitArrayIndex(ArrayIndexExpression node)
3632
{
3733
var field = Visit(node.Array) as IFieldExpression;
@@ -43,16 +39,10 @@ protected internal override Expression VisitArrayIndex(ArrayIndexExpression node
4339
throw new NotSupportedException($"Only a constant index is supported in the expression {node}.");
4440
}
4541

46-
// We've treated -1 as meaning $ operator. We can't break this now,
47-
// so, specifically when we are flattening fields names, this is
48-
// how we'll continue to treat -1.
49-
50-
var index = constantIndex.Value is int intIndex && intIndex == -1
51-
? "$"
52-
: constantIndex.Value.ToString();
53-
42+
var formattedIndex = ArrayIndexFormatter.FormatArrayIndex(constantIndex.Value);
43+
5444
return new FieldExpression(
55-
field.AppendFieldName(index),
45+
field.AppendFieldName(formattedIndex),
5646
node.Serializer);
5747
}
5848

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Threading;
4+
using FluentAssertions;
5+
using MongoDB.Driver.Linq;
6+
using Xunit;
7+
8+
namespace MongoDB.Driver.Tests.ExpressionFlattening
9+
{
10+
public class ArrayIndexFormatterTests
11+
{
12+
[Fact]
13+
public void MinusOneShouldResultInDollarSign()
14+
{
15+
ArrayIndexFormatter.FormatArrayIndex(-1).Should().Be("$");
16+
}
17+
18+
[Fact]
19+
public void ZeroOrGreaterShouldBeFormattedToStringRepresentationOfNumber()
20+
{
21+
ArrayIndexFormatter.FormatArrayIndex(0).Should().Be("0");
22+
ArrayIndexFormatter.FormatArrayIndex(1).Should().Be("1");
23+
ArrayIndexFormatter.FormatArrayIndex(99).Should().Be("99");
24+
}
25+
26+
[Fact]
27+
public void ShouldBeCultureInsensitive()
28+
{
29+
var swedishCulture = CultureInfo.GetCultureInfo("sv-SE");
30+
31+
Thread.CurrentThread.CurrentCulture = swedishCulture;
32+
Thread.CurrentThread.CurrentUICulture = swedishCulture;
33+
34+
ArrayIndexFormatter.FormatArrayIndex(-1).Should().Be("$");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)