PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Bson.TestHelpers/EqualityComparers/ObjectAssertionsExtensions.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 61 lines | 39 code | 7 blank | 15 comment | 2 complexity | cd2095b6c7c6174b79c0fe6c203f294f MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-2014 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.Collections;
  16. using FluentAssertions;
  17. using FluentAssertions.Execution;
  18. using FluentAssertions.Primitives;
  19. namespace MongoDB.Bson.TestHelpers.EqualityComparers
  20. {
  21. public static class ObjectAssertionsExtensions
  22. {
  23. // static methods
  24. public static AndConstraint<ObjectAssertions> BeUsing(this ObjectAssertions assertions, object expected, IEqualityComparer comparer, string because = "", params object[] reasonArgs)
  25. {
  26. Execute.Assertion
  27. .BecauseOf(because, reasonArgs)
  28. .ForCondition(comparer.Equals(assertions.Subject, expected))
  29. .FailWith("Expected {context:object} to be {0}{reason}, but found {1}.", expected,
  30. assertions.Subject);
  31. return new AndConstraint<ObjectAssertions>(assertions);
  32. }
  33. public static AndConstraint<ObjectAssertions> BeUsing(this ObjectAssertions assertions, object expected, IEqualityComparerSource source, string because = "", params object[] reasonArgs)
  34. {
  35. var actual = assertions.Subject;
  36. var comparer = actual == null ? ReferenceEqualsEqualityComparer.Instance : source.GetComparer(actual.GetType());
  37. return assertions.BeUsing(expected, comparer, because, reasonArgs);
  38. }
  39. public static AndConstraint<ObjectAssertions> NotBeUsing(this ObjectAssertions assertions, object unexpected, IEqualityComparer comparer, string because = "", params object[] reasonArgs)
  40. {
  41. Execute.Assertion
  42. .BecauseOf(because, reasonArgs)
  43. .ForCondition(!comparer.Equals(assertions.Subject, unexpected))
  44. .FailWith("Did not expect {context:object} to be equal to {0}{reason}.", unexpected);
  45. return new AndConstraint<ObjectAssertions>(assertions);
  46. }
  47. public static AndConstraint<ObjectAssertions> NotBeUsing(this ObjectAssertions assertions, object unexpected, IEqualityComparerSource source, string because = "", params object[] reasonArgs)
  48. {
  49. var actual = assertions.Subject;
  50. var comparer = actual == null ? ReferenceEqualsEqualityComparer.Instance : source.GetComparer(actual.GetType());
  51. return assertions.NotBeUsing(unexpected, comparer, because, reasonArgs);
  52. }
  53. }
  54. }