|
| 1 | +using Coder.Desktop.Vpn.Proto; |
| 2 | +using NUnit.Framework.Constraints; |
| 3 | + |
| 4 | +namespace Coder.Desktop.Tests.Vpn.Proto; |
| 5 | + |
| 6 | +[TestFixture] |
| 7 | +public class RpcVersionTest |
| 8 | +{ |
| 9 | + [Test(Description = "Parse a variety of version strings")] |
| 10 | + public void Parse() |
| 11 | + { |
| 12 | + Assert.That(RpcVersion.Parse("2.1"), Is.EqualTo(new RpcVersion(2, 1))); |
| 13 | + Assert.That(RpcVersion.Parse("1.0"), Is.EqualTo(new RpcVersion(1, 0))); |
| 14 | + |
| 15 | + Assert.Throws<ArgumentException>(() => RpcVersion.Parse("cats")); |
| 16 | + Assert.Throws<ArgumentException>(() => RpcVersion.Parse("cats.dogs")); |
| 17 | + Assert.Throws<ArgumentException>(() => RpcVersion.Parse("1.dogs")); |
| 18 | + Assert.Throws<ArgumentException>(() => RpcVersion.Parse("1.0.1")); |
| 19 | + Assert.Throws<ArgumentException>(() => RpcVersion.Parse("11")); |
| 20 | + } |
| 21 | + |
| 22 | + private void IsCompatibleWithBothWays(RpcVersion a, RpcVersion b, IResolveConstraint c) |
| 23 | + { |
| 24 | + Assert.That(a.IsCompatibleWith(b), c); |
| 25 | + Assert.That(b.IsCompatibleWith(a), c); |
| 26 | + } |
| 27 | + |
| 28 | + [Test(Description = "Test that versions are compatible")] |
| 29 | + public void IsCompatibleWith() |
| 30 | + { |
| 31 | + var twoOne = new RpcVersion(2, 1); |
| 32 | + Assert.That(twoOne.IsCompatibleWith(twoOne), Is.EqualTo(twoOne)); |
| 33 | + |
| 34 | + // 2.1 && 2.2 => 2.1 |
| 35 | + IsCompatibleWithBothWays(twoOne, new RpcVersion(2, 2), Is.EqualTo(new RpcVersion(2, 1))); |
| 36 | + // 2.1 && 2.0 => 2.0 |
| 37 | + IsCompatibleWithBothWays(twoOne, new RpcVersion(2, 0), Is.EqualTo(new RpcVersion(2, 0))); |
| 38 | + // 2.1 && 3.1 => null |
| 39 | + IsCompatibleWithBothWays(twoOne, new RpcVersion(3, 1), Is.Null); |
| 40 | + // 2.1 && 1.1 => null |
| 41 | + IsCompatibleWithBothWays(twoOne, new RpcVersion(1, 1), Is.Null); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +[TestFixture] |
| 46 | +public class RpcVersionListTest |
| 47 | +{ |
| 48 | + [Test(Description = "Parse a variety of version list strings")] |
| 49 | + public void Parse() |
| 50 | + { |
| 51 | + Assert.That(RpcVersionList.Parse("1.0"), Is.EqualTo(new RpcVersionList(new RpcVersion(1, 0)))); |
| 52 | + Assert.That(RpcVersionList.Parse("1.3,2.1"), |
| 53 | + Is.EqualTo(new RpcVersionList(new RpcVersion(1, 3), new RpcVersion(2, 1)))); |
| 54 | + |
| 55 | + var ex = Assert.Throws<ArgumentException>(() => RpcVersionList.Parse("0.1")); |
| 56 | + Assert.That(ex.InnerException, Is.Not.Null); |
| 57 | + Assert.That(ex.InnerException.Message, Does.Contain("Invalid major version")); |
| 58 | + ex = Assert.Throws<ArgumentException>(() => RpcVersionList.Parse("")); |
| 59 | + Assert.That(ex.InnerException, Is.Not.Null); |
| 60 | + Assert.That(ex.InnerException.Message, Does.Contain("Invalid version string")); |
| 61 | + ex = Assert.Throws<ArgumentException>(() => RpcVersionList.Parse("2.1,1.1")); |
| 62 | + Assert.That(ex.InnerException, Is.Not.Null); |
| 63 | + Assert.That(ex.InnerException.Message, Does.Contain("sorted")); |
| 64 | + ex = Assert.Throws<ArgumentException>(() => RpcVersionList.Parse("1.1,1.2")); |
| 65 | + Assert.That(ex.InnerException, Is.Not.Null); |
| 66 | + Assert.That(ex.InnerException.Message, Does.Contain("Duplicate major version")); |
| 67 | + } |
| 68 | + |
| 69 | + [Test(Description = "Validate a variety of version lists to test every error")] |
| 70 | + public void Validate() |
| 71 | + { |
| 72 | + Assert.DoesNotThrow(() => |
| 73 | + new RpcVersionList(new RpcVersion(1, 3), new RpcVersion(2, 4), new RpcVersion(3, 0)).Validate()); |
| 74 | + |
| 75 | + var ex = Assert.Throws<ArgumentException>(() => new RpcVersionList(new RpcVersion(0, 1)).Validate()); |
| 76 | + Assert.That(ex.Message, Does.Contain("Invalid major version")); |
| 77 | + ex = Assert.Throws<ArgumentException>(() => |
| 78 | + new RpcVersionList(new RpcVersion(2, 1), new RpcVersion(1, 2)).Validate()); |
| 79 | + Assert.That(ex.Message, Does.Contain("sorted")); |
| 80 | + ex = Assert.Throws<ArgumentException>(() => |
| 81 | + new RpcVersionList(new RpcVersion(1, 1), new RpcVersion(1, 2)).Validate()); |
| 82 | + Assert.That(ex.Message, Does.Contain("Duplicate major version")); |
| 83 | + } |
| 84 | + |
| 85 | + private void IsCompatibleWithBothWays(RpcVersionList a, RpcVersionList b, IResolveConstraint c) |
| 86 | + { |
| 87 | + Assert.That(a.IsCompatibleWith(b), c); |
| 88 | + Assert.That(b.IsCompatibleWith(a), c); |
| 89 | + } |
| 90 | + |
| 91 | + [Test(Description = "Check a variety of lists against each other to determine compatible version")] |
| 92 | + public void IsCompatibleWith() |
| 93 | + { |
| 94 | + var list1 = RpcVersionList.Parse("1.2,2.4,3.2"); |
| 95 | + Assert.That(list1.IsCompatibleWith(list1), Is.EqualTo(new RpcVersion(3, 2))); |
| 96 | + |
| 97 | + IsCompatibleWithBothWays(list1, RpcVersionList.Parse("4.1,5.2"), Is.Null); |
| 98 | + IsCompatibleWithBothWays(list1, RpcVersionList.Parse("1.2,2.3"), Is.EqualTo(new RpcVersion(2, 3))); |
| 99 | + IsCompatibleWithBothWays(list1, RpcVersionList.Parse("2.3,3.3"), Is.EqualTo(new RpcVersion(3, 2))); |
| 100 | + } |
| 101 | +} |
0 commit comments