PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Bson.TestHelpers/BsonDocumentAssertions.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 103 lines | 72 code | 15 blank | 16 comment | 2 complexity | bc24d6fadc11a6cd4f0a14c400de0ce0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System.Diagnostics;
  16. using FluentAssertions;
  17. using FluentAssertions.Common;
  18. using FluentAssertions.Execution;
  19. using FluentAssertions.Primitives;
  20. namespace MongoDB.Bson.TestHelpers
  21. {
  22. [DebuggerStepThrough]
  23. public class BsonDocumentAssertions : ReferenceTypeAssertions<BsonDocument, BsonDocumentAssertions>
  24. {
  25. // constructors
  26. public BsonDocumentAssertions(BsonDocument value)
  27. {
  28. Subject = value;
  29. }
  30. // methods
  31. public AndConstraint<BsonDocumentAssertions> Be(BsonDocument expected, string because = "", params object[] reasonArgs)
  32. {
  33. Execute.Assertion
  34. .BecauseOf(because, reasonArgs)
  35. .ForCondition(Subject.IsSameOrEqualTo(expected))
  36. .FailWith("Expected {context:object} to be {0}{reason}, but found {1}.", expected,
  37. Subject);
  38. return new AndConstraint<BsonDocumentAssertions>(this);
  39. }
  40. public AndConstraint<BsonDocumentAssertions> BeEquivalentTo(BsonDocument expected, string because = "", params object[] reasonArgs)
  41. {
  42. Execute.Assertion
  43. .BecauseOf(because, reasonArgs)
  44. .ForCondition(BsonValueEquivalencyComparer.Compare(Subject, expected))
  45. .FailWith("Expected {context:object} to be {0}{reason}, but found {1}.", expected,
  46. Subject);
  47. return new AndConstraint<BsonDocumentAssertions>(this);
  48. }
  49. public AndConstraint<BsonDocumentAssertions> Be(string json, string because = "", params object[] reasonArgs)
  50. {
  51. var expected = json == null ? null : BsonDocument.Parse(json);
  52. return Be(expected, because, reasonArgs);
  53. }
  54. public AndConstraint<BsonDocumentAssertions> Contain(string name, string because = "", params object[] reasonArgs)
  55. {
  56. Execute.Assertion
  57. .BecauseOf(because, reasonArgs)
  58. .ForCondition(Subject.Contains(name))
  59. .FailWith("Expected {context:object} to contain element {0}{reason}.", name);
  60. return new AndConstraint<BsonDocumentAssertions>(this);
  61. }
  62. public AndConstraint<BsonDocumentAssertions> NotBe(BsonDocument unexpected, string because = "", params object[] reasonArgs)
  63. {
  64. Execute.Assertion
  65. .BecauseOf(because, reasonArgs)
  66. .ForCondition(!Subject.IsSameOrEqualTo(unexpected))
  67. .FailWith("Did not expect {context:object} to be equal to {0}{reason}.", unexpected);
  68. return new AndConstraint<BsonDocumentAssertions>(this);
  69. }
  70. public AndConstraint<BsonDocumentAssertions> NotBe(string json, string because = "", params object[] reasonArgs)
  71. {
  72. var expected = json == null ? null : BsonDocument.Parse(json);
  73. return NotBe(expected, because, reasonArgs);
  74. }
  75. public AndConstraint<BsonDocumentAssertions> NotContain(string name, string because = "", params object[] reasonArgs)
  76. {
  77. Execute.Assertion
  78. .BecauseOf(because, reasonArgs)
  79. .ForCondition(!Subject.Contains(name))
  80. .FailWith("Expected {context:object} to not contain element {0}{reason}.", name);
  81. return new AndConstraint<BsonDocumentAssertions>(this);
  82. }
  83. protected override string Context
  84. {
  85. get { return "BsonDocument"; }
  86. }
  87. }
  88. }