From a4b254d034b0725c78dafbd8d5d8313e03a08161 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Mon, 1 Dec 2025 12:19:10 -0800 Subject: [PATCH 1/4] CSHARP-5797: Add C# 14 testing --- CSharpDriver.sln | 33 +++++++++++ evergreen/compile-sources.sh | 4 +- evergreen/evergreen.yml | 15 +++++ evergreen/run-unit-tests.sh | 3 +- src/Directory.Build.props | 5 ++ tests/BuildProps/Tests.Build.props | 29 +++++++++- .../IO/BsonChunkPoolTests.cs | 8 +-- .../IO/BsonStreamAdapterTests.cs | 16 +++--- .../IO/BsonStreamExtensionsTests.cs | 4 +- .../IO/ByteArrayBufferTests.cs | 8 +-- .../IO/ByteArrayChunkTests.cs | 4 +- .../IO/ByteBufferSliceTests.cs | 12 ++-- .../IO/ByteBufferStreamTests.cs | 8 +-- .../IO/InputBufferChunkSourceTests.cs | 4 +- .../IO/MultiChunkBufferTests.cs | 12 ++-- .../IO/OutputBufferChunkSourceTests.cs | 16 +++--- .../IO/SingleChunkBufferTests.cs | 8 +-- .../Serialization/ExtensionMembersTests.cs | 57 +++++++++++++++++++ .../BasicTests.cs | 8 +-- .../Jira/CSharp5071Tests.cs | 40 ++++++------- .../Jira/CSharp5749Tests.cs | 12 ++-- ...dToAggregationExpressionTranslatorTests.cs | 4 +- .../AggregateProjectTranslatorTests.cs | 2 +- 23 files changed, 226 insertions(+), 86 deletions(-) create mode 100644 tests/MongoDB.Bson.Tests/Serialization/ExtensionMembersTests.cs diff --git a/CSharpDriver.sln b/CSharpDriver.sln index 3b6e1439ca5..bdf38cc2f62 100644 --- a/CSharpDriver.sln +++ b/CSharpDriver.sln @@ -64,72 +64,105 @@ Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU + Release_CSharp_14|Any CPU = Release_CSharp_14|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Debug|Any CPU.Build.0 = Debug|Any CPU {9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release|Any CPU.Build.0 = Release|Any CPU + {9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {9FCB42A5-3BC6-492B-8EA0-53EF32E9F8CD}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Debug|Any CPU.Build.0 = Debug|Any CPU {3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release|Any CPU.ActiveCfg = Release|Any CPU {3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release|Any CPU.Build.0 = Release|Any CPU + {3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {3CE61F2A-B852-45F9-B607-E0FFFEF9DBC6}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release|Any CPU.Build.0 = Release|Any CPU + {4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {4D6F0AD1-91CE-4736-AD4E-D9B11E49F206}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Debug|Any CPU.Build.0 = Debug|Any CPU {756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release|Any CPU.ActiveCfg = Release|Any CPU {756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release|Any CPU.Build.0 = Release|Any CPU + {756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {756980B3-0DCF-42F0-A324-C386B1A3E9F4}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Debug|Any CPU.Build.0 = Debug|Any CPU {253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release|Any CPU.ActiveCfg = Release|Any CPU {253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release|Any CPU.Build.0 = Release|Any CPU + {253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {253DBC37-A955-4CCE-8EAF-9CF4A91895DB}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release|Any CPU.Build.0 = Release|Any CPU + {FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {FD003B6D-C0C3-4D1E-832D-1AA96AF0244C}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Debug|Any CPU.Build.0 = Debug|Any CPU {F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release|Any CPU.ActiveCfg = Release|Any CPU {F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release|Any CPU.Build.0 = Release|Any CPU + {F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {F2E68945-76FC-49D5-8CB2-B23C76FF09C7}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Debug|Any CPU.Build.0 = Debug|Any CPU {C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release|Any CPU.ActiveCfg = Release|Any CPU {C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release|Any CPU.Build.0 = Release|Any CPU + {C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {C50D554C-2771-4CC1-9B2C-BB17FB27F935}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {B90F025F-89D3-436A-AD78-6AA304A6E240}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B90F025F-89D3-436A-AD78-6AA304A6E240}.Debug|Any CPU.Build.0 = Debug|Any CPU {B90F025F-89D3-436A-AD78-6AA304A6E240}.Release|Any CPU.ActiveCfg = Release|Any CPU {B90F025F-89D3-436A-AD78-6AA304A6E240}.Release|Any CPU.Build.0 = Release|Any CPU + {B90F025F-89D3-436A-AD78-6AA304A6E240}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {B90F025F-89D3-436A-AD78-6AA304A6E240}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {B711A69F-A337-452C-95E1-A6B15C727CBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B711A69F-A337-452C-95E1-A6B15C727CBA}.Debug|Any CPU.Build.0 = Debug|Any CPU {B711A69F-A337-452C-95E1-A6B15C727CBA}.Release|Any CPU.ActiveCfg = Release|Any CPU {B711A69F-A337-452C-95E1-A6B15C727CBA}.Release|Any CPU.Build.0 = Release|Any CPU + {B711A69F-A337-452C-95E1-A6B15C727CBA}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {B711A69F-A337-452C-95E1-A6B15C727CBA}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {DF888021-744F-4A8B-9324-831DEFC48AB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {DF888021-744F-4A8B-9324-831DEFC48AB8}.Debug|Any CPU.Build.0 = Debug|Any CPU {DF888021-744F-4A8B-9324-831DEFC48AB8}.Release|Any CPU.ActiveCfg = Release|Any CPU {DF888021-744F-4A8B-9324-831DEFC48AB8}.Release|Any CPU.Build.0 = Release|Any CPU + {DF888021-744F-4A8B-9324-831DEFC48AB8}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {DF888021-744F-4A8B-9324-831DEFC48AB8}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Debug|Any CPU.Build.0 = Debug|Any CPU {CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release|Any CPU.ActiveCfg = Release|Any CPU {CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release|Any CPU.Build.0 = Release|Any CPU + {CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {CF670F4A-49DD-4030-A4A0-1F4D600EB70A}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Debug|Any CPU.Build.0 = Debug|Any CPU {33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release|Any CPU.ActiveCfg = Release|Any CPU {33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release|Any CPU.Build.0 = Release|Any CPU + {33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {33B11279-DA4A-46EA-99BF-9DEDCAC50D95}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Debug|Any CPU.Build.0 = Debug|Any CPU {642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release|Any CPU.ActiveCfg = Release|Any CPU {642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release|Any CPU.Build.0 = Release|Any CPU + {642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {642518BC-D28A-4ABC-851C-BC18CC34EEDA}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {AB4AD0CB-756C-444F-9F37-26DA35148934}.Release|Any CPU.ActiveCfg = Release|Any CPU {AB4AD0CB-756C-444F-9F37-26DA35148934}.Release|Any CPU.Build.0 = Release|Any CPU {AB4AD0CB-756C-444F-9F37-26DA35148934}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AB4AD0CB-756C-444F-9F37-26DA35148934}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB4AD0CB-756C-444F-9F37-26DA35148934}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {AB4AD0CB-756C-444F-9F37-26DA35148934}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU {A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Debug|Any CPU.Build.0 = Debug|Any CPU {A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release|Any CPU.ActiveCfg = Release|Any CPU {A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release|Any CPU.Build.0 = Release|Any CPU + {A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release_CSharp_14|Any CPU.ActiveCfg = Release_CSharp_14|Any CPU + {A0CAC199-457E-4862-AF9E-971C7A77CBF9}.Release_CSharp_14|Any CPU.Build.0 = Release_CSharp_14|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/evergreen/compile-sources.sh b/evergreen/compile-sources.sh index 7b9fe71f717..345222e1daa 100644 --- a/evergreen/compile-sources.sh +++ b/evergreen/compile-sources.sh @@ -2,6 +2,8 @@ set -o errexit # Exit the script with error if any of the commands fail SOURCE_PROJECT=${1:-CSharpDriver.sln} +CONFIGURATION=${CONFIGURATION:-'Release'} + if [ -z "$PACKAGE_VERSION" ]; then PACKAGE_VERSION=$(bash ./evergreen/get-version.sh) echo Calculated PACKAGE_VERSION value: "$PACKAGE_VERSION" @@ -30,4 +32,4 @@ do sleep $DELAY done -dotnet build "${SOURCE_PROJECT}" -c Release --no-restore -p:Version="$PACKAGE_VERSION" +dotnet build "${SOURCE_PROJECT}" -c "${CONFIGURATION}" --no-restore -p:Version="$PACKAGE_VERSION" diff --git a/evergreen/evergreen.yml b/evergreen/evergreen.yml index 98f832891cf..2d9d2b18552 100644 --- a/evergreen/evergreen.yml +++ b/evergreen/evergreen.yml @@ -332,6 +332,7 @@ functions: shell: "bash" include_expansions_in_env: - "FRAMEWORK" + - "CONFIGURATION" script: | ${PREPARE_SHELL} bash ./evergreen/run-unit-tests.sh @@ -1123,6 +1124,16 @@ tasks: FRAMEWORK: net10.0 - func: run-unit-tests + - name: unit-tests-net100-v14 + commands: + - func: configure-framework + vars: + FRAMEWORK: net10.0 + DOTNET_SDK_VERSION: 10.0 + - func: run-unit-tests + vars: + CONFIGURATION: "Release_CSharp_14" + - name: test-net472 commands: - func: setup-csfle-secrets @@ -2390,6 +2401,7 @@ buildvariants: - name: unit-tests-netstandard21 - name: unit-tests-net60 - name: unit-tests-net100 + - name: unit-tests-net100-v14 - name: unit-tests-ubuntu display_name: Unit Tests on Ubuntu @@ -2400,6 +2412,7 @@ buildvariants: - name: unit-tests-netstandard21 - name: unit-tests-net60 - name: unit-tests-net100 + - name: unit-tests-net100-v14 - name: unit-tests-macos display_name: Unit Tests on MacOs @@ -2410,6 +2423,7 @@ buildvariants: - name: unit-tests-netstandard21 - name: unit-tests-net60 - name: unit-tests-net100 + - name: unit-tests-net100-v14 - name: unit-tests-macos-arm display_name: Unit Tests on MacOs Arm @@ -2419,6 +2433,7 @@ buildvariants: tasks: - name: unit-tests-net60 - name: unit-tests-net100 + - name: unit-tests-net100-v14 - matrix_name: stable-api-tests matrix_spec: { version: ["5.0", "6.0", "7.0", "8.0", "rapid", "latest"], topology: ["standalone", "sharded-cluster"], auth: "auth", ssl: "nossl", os: "windows-64" } diff --git a/evergreen/run-unit-tests.sh b/evergreen/run-unit-tests.sh index ba8c25d232f..39738acf6f2 100644 --- a/evergreen/run-unit-tests.sh +++ b/evergreen/run-unit-tests.sh @@ -1,7 +1,8 @@ -#!/usr/bin/env bash +#!/usr/bin/env bash set -o errexit # Exit the script with error if any of the commands fail FRAMEWORK=${FRAMEWORK:-net6.0} +CONFIGURATION=${CONFIGURATION:-'Release'} ./evergreen/compile-sources.sh TEST_CATEGORY="!Integration" ./evergreen/execute-tests.sh diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8f6aedf3237..35d61220ea6 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -10,6 +10,11 @@ ..\..\MongoDB.ruleset + + + true + + MongoDB Inc. Copyright © 2010-present MongoDB Inc. diff --git a/tests/BuildProps/Tests.Build.props b/tests/BuildProps/Tests.Build.props index 899132f35aa..4a3a40684ef 100644 --- a/tests/BuildProps/Tests.Build.props +++ b/tests/BuildProps/Tests.Build.props @@ -6,7 +6,22 @@ - netcoreapp3.1;net6.0;net10.0 + Debug;Release;Release_CSharp_14 + AnyCPU + + + + true + $(DefineConstants);CSHARP_14 + 14 + + + + 12 + + + + netcoreapp3.1;net6.0 $(TargetFrameworks);net472 false true @@ -14,10 +29,18 @@ - 12 true + + + + + + + + + 0.0.0-local @@ -28,7 +51,7 @@ - TRACE + $(DefineConstants);TRACE $(DefineConstants);WINDOWS diff --git a/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs index bfe39a7d557..4fb4dc66897 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs @@ -190,8 +190,8 @@ public bool _disposed { get { - var field = typeof(BsonChunkPool).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(BsonChunkPool).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } @@ -331,8 +331,8 @@ public bool _disposed { get { - var field = _instance.GetType().GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = _instance.GetType().GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs index 23fb1a74b5c..69611d93fb9 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs @@ -1701,8 +1701,8 @@ public bool _disposed { get { - var field = typeof(BsonStreamAdapter).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(BsonStreamAdapter).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } @@ -1710,8 +1710,8 @@ public bool _ownsStream { get { - var field = typeof(BsonStreamAdapter).GetField("_ownsStream", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(BsonStreamAdapter).GetField("_ownsStream", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } @@ -1719,8 +1719,8 @@ public Stream _stream { get { - var field = typeof(BsonStreamAdapter).GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance); - return (Stream)field.GetValue(_instance); + var @field = typeof(BsonStreamAdapter).GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance); + return (Stream)@field.GetValue(_instance); } } @@ -1728,8 +1728,8 @@ public byte[] _temp { get { - var field = typeof(BsonStreamAdapter).GetField("_temp", BindingFlags.NonPublic | BindingFlags.Instance); - return (byte[])field.GetValue(_instance); + var @field = typeof(BsonStreamAdapter).GetField("_temp", BindingFlags.NonPublic | BindingFlags.Instance); + return (byte[])@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs index 1a96006ad07..a6b3cf4e83b 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs @@ -774,8 +774,8 @@ public static bool[] __validBsonTypes { get { - var field = typeof(BsonStreamExtensions).GetField("__validBsonTypes", BindingFlags.Static | BindingFlags.NonPublic); - return (bool[])field.GetValue(null); + var @field = typeof(BsonStreamExtensions).GetField("__validBsonTypes", BindingFlags.Static | BindingFlags.NonPublic); + return (bool[])@field.GetValue(null); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs index af5208910e4..29346799bdb 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs @@ -882,8 +882,8 @@ public byte[] _bytes { get { - var field = typeof(ByteArrayBuffer).GetField("_bytes", BindingFlags.NonPublic | BindingFlags.Instance); - return (byte[])field.GetValue(_instance); + var @field = typeof(ByteArrayBuffer).GetField("_bytes", BindingFlags.NonPublic | BindingFlags.Instance); + return (byte[])@field.GetValue(_instance); } } @@ -891,8 +891,8 @@ public bool _disposed { get { - var field = typeof(ByteArrayBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(ByteArrayBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs index 4e45b31c18e..9316e33c701 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs @@ -179,8 +179,8 @@ public bool _disposed { get { - var field = typeof(ByteArrayChunk).GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic); - return (bool)field.GetValue(_instance); + var @field = typeof(ByteArrayChunk).GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs index d0d42b6c608..bd91eca4234 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs @@ -502,8 +502,8 @@ public bool _disposed { get { - var field = typeof(ByteBufferSlice).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(ByteBufferSlice).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } @@ -511,8 +511,8 @@ public int _length { get { - var field = typeof(ByteBufferSlice).GetField("_length", BindingFlags.NonPublic | BindingFlags.Instance); - return (int)field.GetValue(_instance); + var @field = typeof(ByteBufferSlice).GetField("_length", BindingFlags.NonPublic | BindingFlags.Instance); + return (int)@field.GetValue(_instance); } } @@ -520,8 +520,8 @@ public int _offset { get { - var field = typeof(ByteBufferSlice).GetField("_offset", BindingFlags.NonPublic | BindingFlags.Instance); - return (int)field.GetValue(_instance); + var @field = typeof(ByteBufferSlice).GetField("_offset", BindingFlags.NonPublic | BindingFlags.Instance); + return (int)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs index 5da0781ef36..7275d88cc4f 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs @@ -1668,8 +1668,8 @@ public bool _disposed { get { - var field = typeof(ByteBufferStream).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(ByteBufferStream).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } @@ -1677,8 +1677,8 @@ public bool _ownsBuffer { get { - var field = typeof(ByteBufferStream).GetField("_ownsBuffer", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(ByteBufferStream).GetField("_ownsBuffer", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs b/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs index 1d9db95f058..0acd9ea78cd 100644 --- a/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs @@ -300,8 +300,8 @@ public bool _disposed { get { - var field = typeof(InputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(InputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs b/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs index e28e95acf32..7f9d98dc999 100644 --- a/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs @@ -1062,8 +1062,8 @@ public List _chunks { get { - var field = typeof(MultiChunkBuffer).GetField("_chunks", BindingFlags.NonPublic | BindingFlags.Instance); - return (List)field.GetValue(_instance); + var @field = typeof(MultiChunkBuffer).GetField("_chunks", BindingFlags.NonPublic | BindingFlags.Instance); + return (List)@field.GetValue(_instance); } } @@ -1071,8 +1071,8 @@ public bool _disposed { get { - var field = typeof(MultiChunkBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(MultiChunkBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } @@ -1080,8 +1080,8 @@ public List _positions { get { - var field = typeof(MultiChunkBuffer).GetField("_positions", BindingFlags.NonPublic | BindingFlags.Instance); - return (List)field.GetValue(_instance); + var @field = typeof(MultiChunkBuffer).GetField("_positions", BindingFlags.NonPublic | BindingFlags.Instance); + return (List)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs b/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs index 843f7ec6201..9d638fe8f79 100644 --- a/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs @@ -302,8 +302,8 @@ public static int DefaultInitialUnpooledChunkSize { get { - var field = typeof(OutputBufferChunkSource).GetField("DefaultInitialUnpooledChunkSize", BindingFlags.NonPublic | BindingFlags.Static); - return (int)field.GetValue(null); + var @field = typeof(OutputBufferChunkSource).GetField("DefaultInitialUnpooledChunkSize", BindingFlags.NonPublic | BindingFlags.Static); + return (int)@field.GetValue(null); } } @@ -311,8 +311,8 @@ public static int DefaultMaxChunkSize { get { - var field = typeof(OutputBufferChunkSource).GetField("DefaultMaxChunkSize", BindingFlags.NonPublic | BindingFlags.Static); - return (int)field.GetValue(null); + var @field = typeof(OutputBufferChunkSource).GetField("DefaultMaxChunkSize", BindingFlags.NonPublic | BindingFlags.Static); + return (int)@field.GetValue(null); } } @@ -320,8 +320,8 @@ public static int DefaultMinChunkSize { get { - var field = typeof(OutputBufferChunkSource).GetField("DefaultMinChunkSize", BindingFlags.NonPublic | BindingFlags.Static); - return (int)field.GetValue(null); + var @field = typeof(OutputBufferChunkSource).GetField("DefaultMinChunkSize", BindingFlags.NonPublic | BindingFlags.Static); + return (int)@field.GetValue(null); } } #endregion @@ -337,8 +337,8 @@ public bool _disposed { get { - var field = typeof(OutputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(OutputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs b/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs index 5c9ef93fd0a..c2da92daca9 100644 --- a/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs @@ -872,8 +872,8 @@ public IBsonChunk _chunk { get { - var field = typeof(SingleChunkBuffer).GetField("_chunk", BindingFlags.NonPublic | BindingFlags.Instance); - return (IBsonChunk)field.GetValue(_instance); + var @field = typeof(SingleChunkBuffer).GetField("_chunk", BindingFlags.NonPublic | BindingFlags.Instance); + return (IBsonChunk)@field.GetValue(_instance); } } @@ -881,8 +881,8 @@ public bool _disposed { get { - var field = typeof(SingleChunkBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)field.GetValue(_instance); + var @field = typeof(SingleChunkBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)@field.GetValue(_instance); } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/ExtensionMembersTests.cs b/tests/MongoDB.Bson.Tests/Serialization/ExtensionMembersTests.cs new file mode 100644 index 00000000000..34aa13f436e --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/ExtensionMembersTests.cs @@ -0,0 +1,57 @@ +/* Copyright 2010-present MongoDB Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if CSHARP_14 + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization; + +public class ExtensionMembersTests +{ + [Fact] + public void Poco_with_extension_property_should_serialize_without_error() + { + var myPoco = new MyPoco { ExtensionProperty = "foo" }; + + var json = myPoco.ToJson(); + + json.Should().Be("{ \"SimpleProperty\" : \"foo\" }"); + } +} + +public class MyPoco +{ + public string SimpleProperty { get; set; } +} + +public static class MyPocoExtensions +{ +// Until resolution of https://github.com/dotnet/sdk/issues/51681 +#pragma warning disable CA1034 + extension(MyPoco myPoco) +#pragma warning restore CA1034 + { + // Extension property: + public string ExtensionProperty + { + get => myPoco.SimpleProperty + "_extended"; + set => myPoco.SimpleProperty = value; + } + } +} + +#endif diff --git a/tests/MongoDB.Driver.Encryption.Tests/BasicTests.cs b/tests/MongoDB.Driver.Encryption.Tests/BasicTests.cs index 8dc2663aed8..2880c3431a7 100644 --- a/tests/MongoDB.Driver.Encryption.Tests/BasicTests.cs +++ b/tests/MongoDB.Driver.Encryption.Tests/BasicTests.cs @@ -296,7 +296,7 @@ public void TestAwsKeyCreationWithkeyAltNames() var (_, dataKeyDocument) = ProcessContextToCompletion(context, isKmsDecrypt: false); dataKeyDocument.Should().NotBeNull(); var actualKeyAltNames = dataKeyDocument["keyAltNames"].AsBsonArray.Select(x => x.AsString); - var expectedKeyAltNames = keyAltNames.Reverse(); // https://jira.mongodb.org/browse/CDRIVER-3277? + var expectedKeyAltNames = Enumerable.Reverse(keyAltNames); // https://jira.mongodb.org/browse/CDRIVER-3277? actualKeyAltNames.Should().BeEquivalentTo(expectedKeyAltNames); } } @@ -322,7 +322,7 @@ public void TestAwsKeyCreationWithkeyAltNamesStepwise() state.Should().Be(CryptContext.StateCode.MONGOCRYPT_CTX_READY); dataKeyDocument.Should().NotBeNull(); var actualKeyAltNames = dataKeyDocument["keyAltNames"].AsBsonArray.Select(x => x.AsString); - var expectedKeyAltNames = keyAltNames.Reverse(); // https://jira.mongodb.org/browse/CDRIVER-3277? + var expectedKeyAltNames = Enumerable.Reverse(keyAltNames); // https://jira.mongodb.org/browse/CDRIVER-3277? actualKeyAltNames.Should().BeEquivalentTo(expectedKeyAltNames); (state, _, _) = ProcessState(context, isKmsDecrypt: false); @@ -347,7 +347,7 @@ public void TestLocalKeyCreationWithkeyAltNames() var (_, dataKeyDocument) = ProcessContextToCompletion(context); dataKeyDocument.Should().NotBeNull(); var actualKeyAltNames = dataKeyDocument["keyAltNames"].AsBsonArray.Select(x => x.AsString); - var expectedKeyAltNames = keyAltNames.Reverse(); // https://jira.mongodb.org/browse/CDRIVER-3277? + var expectedKeyAltNames = Enumerable.Reverse(keyAltNames); // https://jira.mongodb.org/browse/CDRIVER-3277? actualKeyAltNames.Should().BeEquivalentTo(expectedKeyAltNames); } } @@ -370,7 +370,7 @@ public void TestLocalKeyCreationWithkeyAltNamesStepwise() state.Should().Be(CryptContext.StateCode.MONGOCRYPT_CTX_READY); dataKeyDocument.Should().NotBeNull(); var actualKeyAltNames = dataKeyDocument["keyAltNames"].AsBsonArray.Select(x => x.AsString); - var expectedKeyAltNames = keyAltNames.Reverse(); // https://jira.mongodb.org/browse/CDRIVER-3277? + var expectedKeyAltNames = Enumerable.Reverse(keyAltNames); // https://jira.mongodb.org/browse/CDRIVER-3277? actualKeyAltNames.Should().BeEquivalentTo(expectedKeyAltNames); (state, _, _) = ProcessState(context); diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs index 688daae072c..9feea3786ab 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs @@ -134,7 +134,7 @@ public void Concat_with_one_argument_should_work(string scenario, string expecte var queryable = scenario switch { "intproperty" => collection.AsQueryable().Select(x => string.Concat(x.I)), - "stringproperty" => collection.AsQueryable().Select(x => string.Concat(x.A)), + "stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A })), _ => throw new Exception() }; @@ -204,25 +204,25 @@ public void Concat_with_three_arguments_should_work(string scenario, string expe var queryable = scenario switch { - "intconstant+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(1 + x.B + 3)), - "intconstant+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(1 + x.B + x.K)), - "intconstant+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(1 + x.B + "Z")), - "intconstant+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(1 + x.B + x.C)), - "intproperty+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(x.I + x.B + 3)), - "intproperty+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(x.I + x.B + x.K)), - "intproperty+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(x.I + x.B + "Z")), - "intproperty+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(x.I + x.B + x.C)), - "stringconstant+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat("X" + x.B + 3)), - "stringconstant+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat("X" + x.B + x.K)), - "stringconstant+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat("X" + x.B + "Z")), - "stringconstant+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat("X" + x.B + x.C)), - "stringproperty+intconstant+stringproperty" => collection.AsQueryable().Select(x => string.Concat(x.A + 2 + x.C)), - "stringproperty+intproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(x.A + x.J + x.C)), - "stringproperty+stringconstant+stringproperty" => collection.AsQueryable().Select(x => string.Concat(x.A + "Y" + x.C)), - "stringproperty+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(x.A + x.B + 3)), - "stringproperty+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(x.A + x.B + x.K)), - "stringproperty+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(x.A + x.B + "Z")), - "stringproperty+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(x.A + x.B + x.C)), + "intconstant+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { 1 + x.B + 3 })), + "intconstant+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { 1 + x.B + x.K })), + "intconstant+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { 1 + x.B + "Z" })), + "intconstant+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { 1 + x.B + x.C })), + "intproperty+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { x.I + x.B + 3 })), + "intproperty+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.I + x.B + x.K })), + "intproperty+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { x.I + x.B + "Z" })), + "intproperty+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.I + x.B + x.C })), + "stringconstant+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { "X" + x.B + 3 })), + "stringconstant+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { "X" + x.B + x.K })), + "stringconstant+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { "X" + x.B + "Z" })), + "stringconstant+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { "X" + x.B + x.C })), + "stringproperty+intconstant+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + 2 + x.C })), + "stringproperty+intproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.J + x.C})), + "stringproperty+stringconstant+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + "Y" + x.C })), + "stringproperty+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.B + 3 })), + "stringproperty+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.B + x.K })), + "stringproperty+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.B + "Z" })), + "stringproperty+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.B + x.C })), _ => throw new Exception() }; diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs index 5015390b46e..cc8f12bf536 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs @@ -226,7 +226,8 @@ static Expression VisitContainsMethod(MethodCallExpression node, MethodInfo meth var readOnlySpan = ImplicitCastArrayToSpan(source, typeof(ReadOnlySpan<>), itemType); return Expression.Call( - MemoryExtensionsMethod.ContainsWithReadOnlySpanAndValueAndComparer.MakeGenericMethod(itemType), + MemoryExtensionsMethod.ContainsWithReadOnlySpanAndValueAndComparer.MakeGenericMethod( + itemType), [readOnlySpan, value, comparer]); } } @@ -235,7 +236,8 @@ static Expression VisitContainsMethod(MethodCallExpression node, MethodInfo meth return node; } - static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo method, ReadOnlyCollection arguments) + static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo method, + ReadOnlyCollection arguments) { if (method.Is(EnumerableMethod.SequenceEqual)) { @@ -249,7 +251,8 @@ static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo var secondReadOnlySpan = ImplicitCastArrayToSpan(second, typeof(ReadOnlySpan<>), itemType); return Expression.Call( - MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod(itemType), + MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod( + itemType), [firstReadOnlySpan, secondReadOnlySpan]); } } @@ -266,7 +269,8 @@ static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo var secondReadOnlySpan = ImplicitCastArrayToSpan(second, typeof(ReadOnlySpan<>), itemType); return Expression.Call( - MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod(itemType), + MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod( + itemType), [firstReadOnlySpan, secondReadOnlySpan, comparer]); } } diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/StringConcatMethodToAggregationExpressionTranslatorTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/StringConcatMethodToAggregationExpressionTranslatorTests.cs index fb94b8f9b83..fde98af08bb 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/StringConcatMethodToAggregationExpressionTranslatorTests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/StringConcatMethodToAggregationExpressionTranslatorTests.cs @@ -141,7 +141,7 @@ public void Filter_using_string_concat_with_params_array_should_work() { var collection = Fixture.Collection; var queryable = collection.AsQueryable() - .Where(i => string.Concat(i.A, ";", i.B, ";", i.C) == "A1;B1;C1"); + .Where(i => string.Concat(new[] { i.A, ";", i.B, ";", i.C }) == "A1;B1;C1"); var stages = Translate(collection, queryable); AssertStages( @@ -159,7 +159,7 @@ public void Projection_using_string_concat_with_params_array_should_work() var queryable = collection.AsQueryable() .Where(i => i.Id == 1) - .Select(i => new { T = string.Concat(i.A, ";", i.B, ";", i.C) }); + .Select(i => new { T = string.Concat(new[] { i.A, ";", i.B, ";", i.C }) }); var stages = Translate(collection, queryable); AssertStages( diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/AggregateProjectTranslatorTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/AggregateProjectTranslatorTests.cs index 439e5114bc4..9ddd71659d7 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/AggregateProjectTranslatorTests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/AggregateProjectTranslatorTests.cs @@ -1111,7 +1111,7 @@ public void Should_translate_reverse() { RequireServer.Check(); - var result = Project(x => new { Result = x.M.Reverse() }); + var result = Project(x => new { Result = ((IEnumerable)x.M).Reverse() }); result.Projection.Should().Be("{ Result: { \"$reverseArray\": \"$M\" }, _id: 0 }"); From c27455f2c09e8674c6c1966994b446e3546a7276 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Mon, 1 Dec 2025 12:20:17 -0800 Subject: [PATCH 2/4] - Cleanup --- tests/BuildProps/Tests.Build.props | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/BuildProps/Tests.Build.props b/tests/BuildProps/Tests.Build.props index 4a3a40684ef..39059d1879a 100644 --- a/tests/BuildProps/Tests.Build.props +++ b/tests/BuildProps/Tests.Build.props @@ -32,15 +32,6 @@ true - - - - - - - - - 0.0.0-local From b4e8d5b9a6fc5c80f5045d806e803b49d90699d4 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Fri, 5 Dec 2025 12:40:23 -0800 Subject: [PATCH 3/4] - Rebase + PR comments --- evergreen/evergreen.yml | 10 +- evergreen/execute-tests.sh | 3 +- evergreen/run-unit-tests.sh | 4 +- tests/BuildProps/Tests.Build.props | 2 +- tests/MongoDB.Bson.TestHelpers/Reflector.cs | 46 ++++---- .../IO/BsonChunkPoolTests.cs | 50 +-------- .../IO/BsonStreamAdapterTests.cs | 106 +++++------------- .../IO/BsonStreamExtensionsTests.cs | 45 +++----- .../IO/ByteArrayBufferTests.cs | 50 ++------- .../IO/ByteArrayChunkTests.cs | 57 +++------- .../IO/ByteBufferSliceTests.cs | 79 +++---------- .../IO/ByteBufferStreamTests.cs | 68 ++++------- .../IO/InputBufferChunkSourceTests.cs | 55 +++------ .../IO/MultiChunkBufferTests.cs | 92 +++++---------- .../IO/OutputBufferChunkSourceTests.cs | 65 ++--------- .../IO/SingleChunkBufferTests.cs | 71 +++--------- .../Jira/CSharp5071Tests.cs | 2 +- 17 files changed, 213 insertions(+), 592 deletions(-) diff --git a/evergreen/evergreen.yml b/evergreen/evergreen.yml index 2d9d2b18552..d8717938fa1 100644 --- a/evergreen/evergreen.yml +++ b/evergreen/evergreen.yml @@ -1124,7 +1124,7 @@ tasks: FRAMEWORK: net10.0 - func: run-unit-tests - - name: unit-tests-net100-v14 + - name: unit-tests-net100-csharp14 commands: - func: configure-framework vars: @@ -2401,7 +2401,7 @@ buildvariants: - name: unit-tests-netstandard21 - name: unit-tests-net60 - name: unit-tests-net100 - - name: unit-tests-net100-v14 + - name: unit-tests-net100-csharp14 - name: unit-tests-ubuntu display_name: Unit Tests on Ubuntu @@ -2412,7 +2412,7 @@ buildvariants: - name: unit-tests-netstandard21 - name: unit-tests-net60 - name: unit-tests-net100 - - name: unit-tests-net100-v14 + - name: unit-tests-net100-csharp14 - name: unit-tests-macos display_name: Unit Tests on MacOs @@ -2423,7 +2423,7 @@ buildvariants: - name: unit-tests-netstandard21 - name: unit-tests-net60 - name: unit-tests-net100 - - name: unit-tests-net100-v14 + - name: unit-tests-net100-csharp14 - name: unit-tests-macos-arm display_name: Unit Tests on MacOs Arm @@ -2433,7 +2433,7 @@ buildvariants: tasks: - name: unit-tests-net60 - name: unit-tests-net100 - - name: unit-tests-net100-v14 + - name: unit-tests-net100-csharp14 - matrix_name: stable-api-tests matrix_spec: { version: ["5.0", "6.0", "7.0", "8.0", "rapid", "latest"], topology: ["standalone", "sharded-cluster"], auth: "auth", ssl: "nossl", os: "windows-64" } diff --git a/evergreen/execute-tests.sh b/evergreen/execute-tests.sh index 85fec47e67a..252207e3d26 100644 --- a/evergreen/execute-tests.sh +++ b/evergreen/execute-tests.sh @@ -10,6 +10,7 @@ set -o errexit # Exit the script with error if any of the commands fail FRAMEWORK=${FRAMEWORK:-} TEST_CATEGORY=${TEST_CATEGORY:-Integration} TEST_PROJECT_PATH=${TEST_PROJECT_PATH:-./tests/**/[!AtlasConnectivity]*.Tests.csproj} +CONFIGURATION=${CONFIGURATION:-'Release'} if [ "$FRAMEWORK" = "netstandard2.1" ]; then FRAMEWORK="netcoreapp3.1" @@ -31,5 +32,5 @@ if [[ -n "${FRAMEWORK}" ]]; then fi for file in $TEST_PROJECT_PATH; do - dotnet test "${file}" -c Release --no-build ${FILTER_PARAMETER} ${FRAMEWORK_PARAMETER} --results-directory ./build/test-results --logger "junit;verbosity=detailed;LogFileName=TEST-{assembly}.xml;FailureBodyFormat=Verbose" --logger "console;verbosity=detailed" + dotnet test "${file}" -c ${CONFIGURATION} --no-build ${FILTER_PARAMETER} ${FRAMEWORK_PARAMETER} --results-directory ./build/test-results --logger "junit;verbosity=detailed;LogFileName=TEST-{assembly}.xml;FailureBodyFormat=Verbose" --logger "console;verbosity=detailed" done diff --git a/evergreen/run-unit-tests.sh b/evergreen/run-unit-tests.sh index 39738acf6f2..c258b7af11d 100644 --- a/evergreen/run-unit-tests.sh +++ b/evergreen/run-unit-tests.sh @@ -4,5 +4,5 @@ set -o errexit # Exit the script with error if any of the commands fail FRAMEWORK=${FRAMEWORK:-net6.0} CONFIGURATION=${CONFIGURATION:-'Release'} -./evergreen/compile-sources.sh -TEST_CATEGORY="!Integration" ./evergreen/execute-tests.sh +CONFIGURATION=${CONFIGURATION} ./evergreen/compile-sources.sh +CONFIGURATION=${CONFIGURATION} TEST_CATEGORY="!Integration" ./evergreen/execute-tests.sh diff --git a/tests/BuildProps/Tests.Build.props b/tests/BuildProps/Tests.Build.props index 39059d1879a..b678f12c637 100644 --- a/tests/BuildProps/Tests.Build.props +++ b/tests/BuildProps/Tests.Build.props @@ -21,7 +21,7 @@ - netcoreapp3.1;net6.0 + netcoreapp3.1;net6.0;net10.0 $(TargetFrameworks);net472 false true diff --git a/tests/MongoDB.Bson.TestHelpers/Reflector.cs b/tests/MongoDB.Bson.TestHelpers/Reflector.cs index df98a1515fc..a08dd6f8c69 100644 --- a/tests/MongoDB.Bson.TestHelpers/Reflector.cs +++ b/tests/MongoDB.Bson.TestHelpers/Reflector.cs @@ -1,17 +1,17 @@ -/* Copyright 2018-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ +/* Copyright 2010-present MongoDB Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; using System.Collections.Generic; @@ -40,13 +40,19 @@ public static object GetStaticFieldValue(Type type, string name, BindingFlags fl return fieldInfo.GetValue(null); } + public static TFieldType GetStaticFieldValue(string name, BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static) + { + var fieldInfo = GetDeclaredOrInheritedField(typeof(TObjectType), name, flags); + return (TFieldType)fieldInfo.GetValue(null); + } + public static object Invoke(object obj, string name, BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance) { try { - var methodInfo = obj.GetType().GetMethods(flags) - .Where(m => m.Name == name && m.GetParameters().Length == 0) - .Single(); + var methodInfo = obj.GetType() + .GetMethods(flags) + .Single(m => m.Name == name && m.GetParameters().Length == 0); return methodInfo.Invoke(obj, new object[] { }); } catch (TargetInvocationException exception) @@ -58,9 +64,9 @@ public static object Invoke(object obj, string name, BindingFlags flags = Bindin public static object Invoke(object obj, string name, T1 arg1) { var parameterTypes = new[] { typeof(T1) }; - var methodInfo = obj.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) - .Where(m => m.Name == name && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes)) - .Single(); + var methodInfo = obj.GetType() + .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) + .Single(m => m.Name == name && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes)); try { return methodInfo.Invoke(obj, new object[] { arg1 }); diff --git a/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs index 4fb4dc66897..e497e5daedc 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonChunkPoolTests.cs @@ -21,6 +21,7 @@ using System.Threading.Tasks; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Xunit; @@ -46,10 +47,9 @@ public void constructor_should_initialize_subject() var subject = new BsonChunkPool(maxChunkCount, chunkSize); - var reflector = new Reflector(subject); subject.MaxChunkCount.Should().Be(maxChunkCount); subject.ChunkSize.Should().Be(chunkSize); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Theory] @@ -122,8 +122,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -175,26 +174,6 @@ public void MaxChunkCount_get_should_return_expected_result() result.Should().Be(1); } - - // nested types - private class Reflector - { - private readonly BsonChunkPool _instance; - - public Reflector(BsonChunkPool instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = typeof(BsonChunkPool).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } public class BsonChunkPool_DisposableChunkTests @@ -242,8 +221,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -316,25 +294,5 @@ public void Fork_should_throw_when_subject_is_disposed() action.ShouldThrow().And.ObjectName.Should().Be("DisposableChunk"); } - - // nested types - private class Reflector - { - private readonly IBsonChunk _instance; - - public Reflector(IBsonChunk instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = _instance.GetType().GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs index 69611d93fb9..0a81105523f 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs @@ -1,22 +1,22 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; +using System.Buffers.Binary; using System.IO; using System.Linq; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using FluentAssertions; @@ -24,7 +24,7 @@ using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; -using System.Buffers.Binary; +using Reflector = MongoDB.Bson.TestHelpers.Reflector; namespace MongoDB.Bson.Tests { @@ -247,8 +247,7 @@ public void Close_can_be_called_multiple_times() subject.Close(); subject.Close(); - var subjectReflector = new Reflector(subject); - subjectReflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -259,8 +258,7 @@ public void Close_should_dispose_subject() subject.Close(); - var subjectReflector = new Reflector(subject); - subjectReflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -270,8 +268,7 @@ public void constructor_should_use_false_as_the_default_value_for_ownsStream() var subject = new BsonStreamAdapter(mockStream.Object); - var subjectReflector = new Reflector(subject); - subjectReflector._ownsStream.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_ownsStream").Should().Be(false); } [Theory] @@ -284,11 +281,10 @@ public void constructor_should_initialize_instance( var subject = new BsonStreamAdapter(mockStream.Object, ownsStream: ownsStream); - var subjectReflector = new Reflector(subject); - subjectReflector._disposed.Should().BeFalse(); - subjectReflector._ownsStream.Should().Be(ownsStream); - subjectReflector._stream.Should().Be(mockStream.Object); - subjectReflector._temp.Should().NotBeNull(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); + Reflector.GetFieldValue(subject, "_ownsStream").Should().Be(ownsStream); + Reflector.GetFieldValue(subject, "_stream").Should().Be(mockStream.Object); + Reflector.GetFieldValue(subject, "_temp").Should().NotBeNull(); } [Fact] @@ -342,8 +338,7 @@ public void Dispose_can_be_called_multiple_times() subject.Dispose(); subject.Dispose(); - var subjectReflector = new Reflector(subject); - subjectReflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -380,8 +375,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var subjectReflector = new Reflector(subject); - subjectReflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -635,7 +629,7 @@ public void ReadAsync_should_throw_when_subject_is_disposed() var cancellationToken = cancellationTokenSource.Token; subject.Dispose(); - Action action = () => subject.ReadAsync(buffer, offset, count, cancellationToken); + Action action = () => _ = subject.ReadAsync(buffer, offset, count, cancellationToken); action.ShouldThrow().And.ObjectName.Should().Be("BsonStreamAdapter"); } @@ -1683,55 +1677,5 @@ public void WriteTimeout_set_should_throw_when_subject_is_disposed() action.ShouldThrow().And.ObjectName.Should().Be("BsonStreamAdapter"); } - - // nested types - private class Reflector - { - // fields - private readonly BsonStreamAdapter _instance; - - // constructors - public Reflector(BsonStreamAdapter instance) - { - _instance = instance; - } - - // properties - public bool _disposed - { - get - { - var @field = typeof(BsonStreamAdapter).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - - public bool _ownsStream - { - get - { - var @field = typeof(BsonStreamAdapter).GetField("_ownsStream", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - - public Stream _stream - { - get - { - var @field = typeof(BsonStreamAdapter).GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance); - return (Stream)@field.GetValue(_instance); - } - } - - public byte[] _temp - { - get - { - var @field = typeof(BsonStreamAdapter).GetField("_temp", BindingFlags.NonPublic | BindingFlags.Instance); - return (byte[])@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs index a6b3cf4e83b..53e8e97fe0a 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonStreamExtensionsTests.cs @@ -1,28 +1,28 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; +using System.Buffers.Binary; using System.IO; using System.Linq; -using System.Reflection; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; -using System.Buffers.Binary; namespace MongoDB.Bson.Tests.IO { @@ -477,7 +477,7 @@ public void ReadBytes_with_count_should_throw_when_stream_is_null() [Fact] public void static_constructor_should_initialize_validBsonTypes() { - var validBsonTypes = Reflector.__validBsonTypes; + var validBsonTypes = Reflector.GetStaticFieldValue(typeof(BsonStreamExtensions), "__validBsonTypes") as bool[]; validBsonTypes.Should().HaveCount(256); for (var n = 0; n < 256; n++) @@ -766,18 +766,5 @@ public void WriteSlice_should_throw_when_stream_is_null() action.ShouldThrow().And.ParamName.Should().Be("stream"); } - - // nested types - private class Reflector - { - public static bool[] __validBsonTypes - { - get - { - var @field = typeof(BsonStreamExtensions).GetField("__validBsonTypes", BindingFlags.Static | BindingFlags.NonPublic); - return (bool[])@field.GetValue(null); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs index 29346799bdb..ed5fec73fe4 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteArrayBufferTests.cs @@ -18,6 +18,7 @@ using System.Reflection; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Xunit; @@ -188,11 +189,10 @@ public void constructor_with_bytes_and_length_should_initialize_subject( var subject = new ByteArrayBuffer(bytes, length, isReadOnly); - var reflector = new Reflector(subject); subject.IsReadOnly.Should().Be(isReadOnly); subject.Length.Should().Be(length); - reflector._bytes.Should().BeSameAs(bytes); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_bytes").Should().Be(bytes); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Fact] @@ -226,11 +226,10 @@ public void constructor_with_bytes_should_initialize_subject( var subject = new ByteArrayBuffer(bytes, isReadOnly); - var reflector = new Reflector(subject); subject.IsReadOnly.Should().Be(isReadOnly); subject.Length.Should().Be(bytes.Length); - reflector._bytes.Should().BeSameAs(bytes); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_bytes").Should().Be(bytes); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Fact] @@ -257,8 +256,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -276,10 +274,11 @@ public void EnsureCapacity_should_have_expected_effect(int size, int minimumCapa subject.EnsureCapacity(minimumCapacity); - var reflector = new Reflector(subject); subject.Capacity.Should().Be(expectedCapacity); - reflector._bytes.Take(size).Should().Equal(bytes); - reflector._bytes.Skip(size).Should().Equal(Enumerable.Repeat(0, expectedCapacity - size)); + + var _bytes = Reflector.GetFieldValue(subject, "_bytes") as byte[]; + _bytes.Take(size).Should().Equal(bytes); + _bytes.Skip(size).Should().Equal(Enumerable.Repeat(0, expectedCapacity - size)); } [Fact] @@ -867,34 +866,5 @@ private ByteArrayBuffer CreateSubject(int size = 0, int? length = null, bool isR var bytes = new byte[size]; return new ByteArrayBuffer(bytes, length ?? size, isReadOnly); } - - // nested types - private class Reflector - { - private readonly ByteArrayBuffer _instance; - - public Reflector(ByteArrayBuffer instance) - { - _instance = instance; - } - - public byte[] _bytes - { - get - { - var @field = typeof(ByteArrayBuffer).GetField("_bytes", BindingFlags.NonPublic | BindingFlags.Instance); - return (byte[])@field.GetValue(_instance); - } - } - - public bool _disposed - { - get - { - var @field = typeof(ByteArrayBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs index 9316e33c701..d438d4f48fd 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteArrayChunkTests.cs @@ -1,22 +1,22 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; -using System.Reflection; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Xunit; @@ -112,8 +112,7 @@ public void Dispose_forked_handle_should_not_dispose_subject() forked.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Fact] @@ -123,8 +122,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -135,8 +133,7 @@ public void Dispose_should_not_dispose_forked_handle() subject.Dispose(); - var reflector = new Reflector((ByteArrayChunk)forked); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(forked, "_disposed").Should().Be(false); } [Fact] @@ -164,25 +161,5 @@ public void Fork_should_throw_when_subject_is_disposed() action.ShouldThrow().And.ObjectName.Should().Be("ByteArrayChunk"); } - - // nested types - private class Reflector - { - private readonly ByteArrayChunk _instance; - - public Reflector(ByteArrayChunk instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = typeof(ByteArrayChunk).GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs index bd91eca4234..c809bbd153d 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteBufferSliceTests.cs @@ -1,26 +1,23 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; -using System.Collections.Generic; using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; @@ -168,11 +165,10 @@ public void constructor_should_initialize_subject() var subject = new ByteBufferSlice(buffer, 1, 2); - var reflector = new Reflector(subject); subject.Buffer.Should().BeSameAs(buffer); - reflector._disposed.Should().BeFalse(); - reflector._offset.Should().Be(1); - reflector._length.Should().Be(2); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); + Reflector.GetFieldValue(subject, "_offset").Should().Be(1); + Reflector.GetFieldValue(subject, "_length").Should().Be(2); } [Fact] @@ -249,8 +245,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Fact] @@ -487,43 +482,5 @@ public ByteBufferSlice CreateSubjectWithFakeBuffer(int size = 3, int offset = 1, mockBuffer.SetupGet(s => s.IsReadOnly).Returns(true); return new ByteBufferSlice(mockBuffer.Object, offset, length); } - - // nested types - private class Reflector - { - private readonly ByteBufferSlice _instance; - - public Reflector(ByteBufferSlice instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = typeof(ByteBufferSlice).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - - public int _length - { - get - { - var @field = typeof(ByteBufferSlice).GetField("_length", BindingFlags.NonPublic | BindingFlags.Instance); - return (int)@field.GetValue(_instance); - } - } - - public int _offset - { - get - { - var @field = typeof(ByteBufferSlice).GetField("_offset", BindingFlags.NonPublic | BindingFlags.Instance); - return (int)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs index 7275d88cc4f..f49ab710a90 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs @@ -1,29 +1,29 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; -using System.Buffers.Binary; namespace MongoDB.Bson.Tests.IO { @@ -131,12 +131,12 @@ public void constructor_should_initialize_subject( var subject = new ByteBufferStream(mockBuffer.Object, ownsBuffer); - var reflector = new Reflector(subject); subject.Buffer.Should().BeSameAs(mockBuffer.Object); subject.Length.Should().Be(length); subject.Position.Should().Be(0); - reflector._disposed.Should().BeFalse(); - reflector._ownsBuffer.Should().Be(ownsBuffer); + + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); + Reflector.GetFieldValue(subject, "_ownsBuffer").Should().Be(ownsBuffer); } [Fact] @@ -163,8 +163,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -1653,34 +1652,5 @@ private ByteBufferStream CreateSubject(int length, int numberOfChunks) { return CreateSubject(length, CalculateChunkSizes(length, numberOfChunks)); } - - // nested types - private class Reflector - { - private readonly ByteBufferStream _instance; - - public Reflector(ByteBufferStream instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = typeof(ByteBufferStream).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - - public bool _ownsBuffer - { - get - { - var @field = typeof(ByteBufferStream).GetField("_ownsBuffer", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs b/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs index 0acd9ea78cd..4467681e21f 100644 --- a/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs @@ -1,22 +1,22 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; -using System.Reflection; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; @@ -58,12 +58,11 @@ public void constructor_should_initialize_subject() var subject = new InputBufferChunkSource(mockBaseSource.Object, maxUnpooledChunkSize, minChunkSize, maxChunkSize); - var reflector = new Reflector(subject); subject.BaseSource.Should().BeSameAs(mockBaseSource.Object); subject.MaxChunkSize.Should().Be(maxChunkSize); subject.MaxUnpooledChunkSize.Should().Be(maxUnpooledChunkSize); subject.MinChunkSize.Should().Be(minChunkSize); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Fact] @@ -176,8 +175,8 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -285,25 +284,5 @@ public void MinChunkSize_get_should_return_expected_result() result.Should().Be(minChunkSize); } - - // nested types - private class Reflector - { - private readonly InputBufferChunkSource _instance; - - public Reflector(InputBufferChunkSource instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = typeof(InputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs b/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs index 7f9d98dc999..d59ad1da72c 100644 --- a/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/MultiChunkBufferTests.cs @@ -1,22 +1,21 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using FluentAssertions; using MongoDB.Bson.IO; using MongoDB.Bson.TestHelpers; @@ -235,8 +234,7 @@ public void constructor_with_chunks_should_compute_positions(int numberOfChunks, var subject = new MultiChunkBuffer(chunks); - var reflector = new Reflector(subject); - reflector._positions.Should().Equal(expectedPositions); + (Reflector.GetFieldValue(subject, "_positions") as List).Should().BeEquivalentTo(expectedPositions); } [Fact] @@ -270,14 +268,13 @@ public void constructor_with_chunks_should_initialize_subject() var subject = new MultiChunkBuffer(chunks, 0, false); - var reflector = new Reflector(subject); subject.Capacity.Should().Be(0); subject.ChunkSource.Should().BeNull(); subject.IsReadOnly.Should().BeFalse(); subject.Length.Should().Be(0); - reflector._chunks.Should().HaveCount(0); - reflector._disposed.Should().BeFalse(); - reflector._positions.Should().Equal(new[] { 0 }); + (Reflector.GetFieldValue(subject, "_chunks") as IList).Count.Should().Be(0); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); + (Reflector.GetFieldValue(subject, "_positions") as List).ShouldBeEquivalentTo(new[] { 0 }); } [Fact] @@ -285,7 +282,7 @@ public void constructor_with_chunks_should_throw_when_chunks_is_null() { IEnumerable chunks = null; - Action action = () => new MultiChunkBuffer(chunks); + Action action = () => _ = new MultiChunkBuffer(chunks); action.ShouldThrow().And.ParamName.Should().Be("chunks"); } @@ -311,14 +308,13 @@ public void constructor_with_chunkSource_should_initialize_subject() var subject = new MultiChunkBuffer(mockChunkSource.Object); - var reflector = new Reflector(subject); subject.Capacity.Should().Be(0); subject.ChunkSource.Should().BeSameAs(mockChunkSource.Object); subject.IsReadOnly.Should().BeFalse(); subject.Length.Should().Be(0); - reflector._chunks.Should().HaveCount(0); - reflector._disposed.Should().BeFalse(); - reflector._positions.Should().Equal(new[] { 0 }); + (Reflector.GetFieldValue(subject, "_chunks") as IList).Count.Should().Be(0); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); + (Reflector.GetFieldValue(subject, "_positions") as List).ShouldBeEquivalentTo(new[] { 0 }); } [Fact] @@ -365,8 +361,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -387,9 +382,10 @@ public void EnsureCapacity_should_have_expected_effect(int minimumCapacity, int[ subject.EnsureCapacity(minimumCapacity); - var reflector = new Reflector(subject); subject.Capacity.Should().BeGreaterOrEqualTo(minimumCapacity); - reflector._chunks.Select(c => c.Bytes.Count).Should().Equal(expectedChunkSizes); + + var _chunks = Reflector.GetFieldValue(subject, "_chunks") as List; + _chunks.Select(c => c.Bytes.Count).Should().Equal(expectedChunkSizes); } [Fact] @@ -1047,43 +1043,5 @@ private byte[] ToByteArray(IByteBuffer buffer) buffer.GetBytes(0, bytes, 0, buffer.Length); return bytes; } - - // nested types - private class Reflector - { - private readonly MultiChunkBuffer _instance; - - public Reflector(MultiChunkBuffer instance) - { - _instance = instance; - } - - public List _chunks - { - get - { - var @field = typeof(MultiChunkBuffer).GetField("_chunks", BindingFlags.NonPublic | BindingFlags.Instance); - return (List)@field.GetValue(_instance); - } - } - - public bool _disposed - { - get - { - var @field = typeof(MultiChunkBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - - public List _positions - { - get - { - var @field = typeof(MultiChunkBuffer).GetField("_positions", BindingFlags.NonPublic | BindingFlags.Instance); - return (List)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs b/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs index 9d638fe8f79..64d917bef90 100644 --- a/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/OutputBufferChunkSourceTests.cs @@ -21,6 +21,7 @@ using System.Threading.Tasks; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; @@ -62,12 +63,11 @@ public void constructor_should_initialize_subject() var subject = new OutputBufferChunkSource(mockBaseSource.Object, initialUnpooledChunkSize, minChunkSize, maxChunkSize); - var reflector = new Reflector(subject); - subject.BaseSource.Should().BeSameAs(mockBaseSource.Object); + subject.BaseSource.Should().Be(mockBaseSource.Object); subject.InitialUnpooledChunkSize.Should().Be(initialUnpooledChunkSize); subject.MaxChunkSize.Should().Be(maxChunkSize); subject.MinChunkSize.Should().Be(minChunkSize); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Fact] @@ -169,9 +169,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); - + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -262,7 +260,7 @@ public void GetChunk_should_throw_when_subject_is_disposed() public void InitialUnpooledChunkSize_get_should_return_expected_result() { var mockBaseSource = new Mock(); - var initialUnpooledChunkSize = Reflector.DefaultInitialUnpooledChunkSize * 2; + var initialUnpooledChunkSize = Reflector.GetStaticFieldValue("DefaultInitialUnpooledChunkSize") * 2; var subject = new OutputBufferChunkSource(mockBaseSource.Object, initialUnpooledChunkSize: initialUnpooledChunkSize); var result = subject.InitialUnpooledChunkSize; @@ -274,7 +272,7 @@ public void InitialUnpooledChunkSize_get_should_return_expected_result() public void MaxChunkSize_get_should_return_expected_result() { var mockBaseSource = new Mock(); - var maxChunkSize = Reflector.DefaultMaxChunkSize * 2; + var maxChunkSize = Reflector.GetStaticFieldValue("DefaultMaxChunkSize") * 2; var subject = new OutputBufferChunkSource(mockBaseSource.Object, maxChunkSize: maxChunkSize); var result = subject.MaxChunkSize; @@ -286,61 +284,12 @@ public void MaxChunkSize_get_should_return_expected_result() public void MinChunkSize_get_should_return_expected_result() { var mockBaseSource = new Mock(); - var minChunkSize = Reflector.DefaultMinChunkSize * 2; + var minChunkSize = Reflector.GetStaticFieldValue("DefaultMinChunkSize") * 2; var subject = new OutputBufferChunkSource(mockBaseSource.Object, minChunkSize: minChunkSize); var result = subject.MinChunkSize; result.Should().Be(minChunkSize); } - - // nested types - private class Reflector - { - #region static - public static int DefaultInitialUnpooledChunkSize - { - get - { - var @field = typeof(OutputBufferChunkSource).GetField("DefaultInitialUnpooledChunkSize", BindingFlags.NonPublic | BindingFlags.Static); - return (int)@field.GetValue(null); - } - } - - public static int DefaultMaxChunkSize - { - get - { - var @field = typeof(OutputBufferChunkSource).GetField("DefaultMaxChunkSize", BindingFlags.NonPublic | BindingFlags.Static); - return (int)@field.GetValue(null); - } - } - - public static int DefaultMinChunkSize - { - get - { - var @field = typeof(OutputBufferChunkSource).GetField("DefaultMinChunkSize", BindingFlags.NonPublic | BindingFlags.Static); - return (int)@field.GetValue(null); - } - } - #endregion - - private readonly OutputBufferChunkSource _instance; - - public Reflector(OutputBufferChunkSource instance) - { - _instance = instance; - } - - public bool _disposed - { - get - { - var @field = typeof(OutputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs b/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs index c2da92daca9..c16e7776d6a 100644 --- a/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/SingleChunkBufferTests.cs @@ -1,26 +1,22 @@ /* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; using FluentAssertions; using MongoDB.Bson.IO; +using MongoDB.Bson.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Moq; using Xunit; @@ -192,17 +188,16 @@ public void constructor_should_initialize_subject( var subject = new SingleChunkBuffer(chunk, length, isReadOnly); - var reflector = new Reflector(subject); subject.IsReadOnly.Should().Be(isReadOnly); subject.Length.Should().Be(length); - reflector._chunk.Should().BeSameAs(chunk); - reflector._disposed.Should().BeFalse(); + Reflector.GetFieldValue(subject, "_chunk").Should().BeSameAs(chunk); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(false); } [Fact] public void constructor_should_throw_when_chunk_is_null() { - Action action = () => new SingleChunkBuffer(null, 0); + Action action = () => _ = new SingleChunkBuffer(null, 0); action.ShouldThrow().And.ParamName.Should().Be("chunk"); } @@ -236,8 +231,7 @@ public void Dispose_should_dispose_subject() subject.Dispose(); - var reflector = new Reflector(subject); - reflector._disposed.Should().BeTrue(); + Reflector.GetFieldValue(subject, "_disposed").Should().Be(true); } [Theory] @@ -857,34 +851,5 @@ private SingleChunkBuffer CreateSubject(int size = 0, int? length = null, bool i var chunk = new ByteArrayChunk(size); return new SingleChunkBuffer(chunk, length ?? size, isReadOnly); } - - // nested types - private class Reflector - { - private readonly SingleChunkBuffer _instance; - - public Reflector(SingleChunkBuffer instance) - { - _instance = instance; - } - - public IBsonChunk _chunk - { - get - { - var @field = typeof(SingleChunkBuffer).GetField("_chunk", BindingFlags.NonPublic | BindingFlags.Instance); - return (IBsonChunk)@field.GetValue(_instance); - } - } - - public bool _disposed - { - get - { - var @field = typeof(SingleChunkBuffer).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance); - return (bool)@field.GetValue(_instance); - } - } - } } } diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs index 9feea3786ab..8eb0af8181f 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5071Tests.cs @@ -217,7 +217,7 @@ public void Concat_with_three_arguments_should_work(string scenario, string expe "stringconstant+stringproperty+stringconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { "X" + x.B + "Z" })), "stringconstant+stringproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { "X" + x.B + x.C })), "stringproperty+intconstant+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + 2 + x.C })), - "stringproperty+intproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.J + x.C})), + "stringproperty+intproperty+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.J + x.C })), "stringproperty+stringconstant+stringproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + "Y" + x.C })), "stringproperty+stringproperty+intconstant" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.B + 3 })), "stringproperty+stringproperty+intproperty" => collection.AsQueryable().Select(x => string.Concat(new[] { x.A + x.B + x.K })), From fa2b102bf53fd25a95dea4cca7c067f13d35c44a Mon Sep 17 00:00:00 2001 From: BorisDog Date: Fri, 5 Dec 2025 14:26:55 -0800 Subject: [PATCH 4/4] - Revert CSharp5749Tests --- .../Linq/Linq3Implementation/Jira/CSharp5749Tests.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs index cc8f12bf536..5015390b46e 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp5749Tests.cs @@ -226,8 +226,7 @@ static Expression VisitContainsMethod(MethodCallExpression node, MethodInfo meth var readOnlySpan = ImplicitCastArrayToSpan(source, typeof(ReadOnlySpan<>), itemType); return Expression.Call( - MemoryExtensionsMethod.ContainsWithReadOnlySpanAndValueAndComparer.MakeGenericMethod( - itemType), + MemoryExtensionsMethod.ContainsWithReadOnlySpanAndValueAndComparer.MakeGenericMethod(itemType), [readOnlySpan, value, comparer]); } } @@ -236,8 +235,7 @@ static Expression VisitContainsMethod(MethodCallExpression node, MethodInfo meth return node; } - static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo method, - ReadOnlyCollection arguments) + static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo method, ReadOnlyCollection arguments) { if (method.Is(EnumerableMethod.SequenceEqual)) { @@ -251,8 +249,7 @@ static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo var secondReadOnlySpan = ImplicitCastArrayToSpan(second, typeof(ReadOnlySpan<>), itemType); return Expression.Call( - MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod( - itemType), + MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod(itemType), [firstReadOnlySpan, secondReadOnlySpan]); } } @@ -269,8 +266,7 @@ static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo var secondReadOnlySpan = ImplicitCastArrayToSpan(second, typeof(ReadOnlySpan<>), itemType); return Expression.Call( - MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod( - itemType), + MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan.MakeGenericMethod(itemType), [firstReadOnlySpan, secondReadOnlySpan, comparer]); } }