PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Misc/SemanticVersionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 99 lines | 77 code | 8 blank | 14 comment | 8 complexity | 6fa3e7ae0fac3fea0cb20b85086cd675 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-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;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Driver.Core.Misc;
  22. using Xunit;
  23. namespace MongoDB.Driver.Core.Misc
  24. {
  25. public class SemanticVersionTests
  26. {
  27. [Theory]
  28. [InlineData("1.0.0", null, 1)]
  29. [InlineData("1.0.0", "1.0.0", 0)]
  30. [InlineData("1.1.0", "1.1.0", 0)]
  31. [InlineData("1.1.1", "1.1.1", 0)]
  32. [InlineData("1.1.1-rc2", "1.1.1-rc2", 0)]
  33. [InlineData("1.0.0", "2.0.0", -1)]
  34. [InlineData("2.0.0", "1.0.0", 1)]
  35. [InlineData("1.0.0", "1.1.0", -1)]
  36. [InlineData("1.1.0", "1.0.0", 1)]
  37. [InlineData("1.0.0", "1.0.1", -1)]
  38. [InlineData("1.0.1", "1.0.0", 1)]
  39. [InlineData("1.0.0-alpha", "1.0.0-beta", -1)]
  40. [InlineData("1.0.0-beta", "1.0.0-alpha", 1)]
  41. public void Comparisons_should_be_correct(string a, string b, int comparison)
  42. {
  43. var subject = SemanticVersion.Parse(a);
  44. var comparand = b == null ? null : SemanticVersion.Parse(b);
  45. subject.Equals(comparand).Should().Be(comparison == 0);
  46. subject.CompareTo(comparand).Should().Be(comparison);
  47. (subject == comparand).Should().Be(comparison == 0);
  48. (subject != comparand).Should().Be(comparison != 0);
  49. (subject > comparand).Should().Be(comparison == 1);
  50. (subject >= comparand).Should().Be(comparison >= 0);
  51. (subject < comparand).Should().Be(comparison == -1);
  52. (subject <= comparand).Should().Be(comparison <= 0);
  53. }
  54. [Theory]
  55. [InlineData("1.0.0", 1, 0, 0, null)]
  56. [InlineData("1.2.0", 1, 2, 0, null)]
  57. [InlineData("1.0.3", 1, 0, 3, null)]
  58. [InlineData("1.0.3-rc", 1, 0, 3, "rc")]
  59. [InlineData("1.0.3-rc1", 1, 0, 3, "rc1")]
  60. [InlineData("1.0.3-rc.2.3", 1, 0, 3, "rc.2.3")]
  61. public void Parse_should_handle_valid_semantic_version_strings(string versionString, int major, int minor, int patch, string preRelease)
  62. {
  63. var subject = SemanticVersion.Parse(versionString);
  64. subject.Major.Should().Be(major);
  65. subject.Minor.Should().Be(minor);
  66. subject.Patch.Should().Be(patch);
  67. subject.PreRelease.Should().Be(preRelease);
  68. }
  69. [Theory]
  70. [InlineData("1")]
  71. [InlineData("1-rc2")]
  72. [InlineData("alpha")]
  73. public void Parse_should_throw_a_FormatException_when_the_version_string_is_invalid(string versionString)
  74. {
  75. Action act = () => SemanticVersion.Parse(versionString);
  76. act.ShouldThrow<FormatException>();
  77. }
  78. [Theory]
  79. [InlineData("1.0.0", 1, 0, 0, null)]
  80. [InlineData("1.2.0", 1, 2, 0, null)]
  81. [InlineData("1.0.3", 1, 0, 3, null)]
  82. [InlineData("1.0.3-rc", 1, 0, 3, "rc")]
  83. [InlineData("1.0.3-rc1", 1, 0, 3, "rc1")]
  84. [InlineData("1.0.3-rc.2.3", 1, 0, 3, "rc.2.3")]
  85. public void ToString_should_render_a_correct_semantic_version_string(string versionString, int major, int minor, int patch, string preRelease)
  86. {
  87. var subject = new SemanticVersion(major, minor, patch, preRelease);
  88. subject.ToString().Should().Be(versionString);
  89. }
  90. }
  91. }