|
| 1 | +usingCoder.Desktop.Vpn.Proto; |
| 2 | +usingNUnit.Framework.Constraints; |
| 3 | + |
| 4 | +namespaceCoder.Desktop.Tests.Vpn.Proto; |
| 5 | + |
| 6 | +[TestFixture] |
| 7 | +publicclassRpcVersionTest |
| 8 | +{ |
| 9 | +[Test(Description="Parse a variety of version strings")] |
| 10 | +publicvoidParse() |
| 11 | +{ |
| 12 | +Assert.That(RpcVersion.Parse("2.1"),Is.EqualTo(newRpcVersion(2,1))); |
| 13 | +Assert.That(RpcVersion.Parse("1.0"),Is.EqualTo(newRpcVersion(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 | +privatevoidIsCompatibleWithBothWays(RpcVersiona,RpcVersionb,IResolveConstraintc) |
| 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 | +publicvoidIsCompatibleWith() |
| 30 | +{ |
| 31 | +vartwoOne=newRpcVersion(2,1); |
| 32 | +Assert.That(twoOne.IsCompatibleWith(twoOne),Is.EqualTo(twoOne)); |
| 33 | + |
| 34 | +// 2.1 && 2.2 => 2.1 |
| 35 | +IsCompatibleWithBothWays(twoOne,newRpcVersion(2,2),Is.EqualTo(newRpcVersion(2,1))); |
| 36 | +// 2.1 && 2.0 => 2.0 |
| 37 | +IsCompatibleWithBothWays(twoOne,newRpcVersion(2,0),Is.EqualTo(newRpcVersion(2,0))); |
| 38 | +// 2.1 && 3.1 => null |
| 39 | +IsCompatibleWithBothWays(twoOne,newRpcVersion(3,1),Is.Null); |
| 40 | +// 2.1 && 1.1 => null |
| 41 | +IsCompatibleWithBothWays(twoOne,newRpcVersion(1,1),Is.Null); |
| 42 | +} |
| 43 | +} |
| 44 | + |
| 45 | +[TestFixture] |
| 46 | +publicclassRpcVersionListTest |
| 47 | +{ |
| 48 | +[Test(Description="Parse a variety of version list strings")] |
| 49 | +publicvoidParse() |
| 50 | +{ |
| 51 | +Assert.That(RpcVersionList.Parse("1.0"),Is.EqualTo(newRpcVersionList(newRpcVersion(1,0)))); |
| 52 | +Assert.That(RpcVersionList.Parse("1.3,2.1"), |
| 53 | +Is.EqualTo(newRpcVersionList(newRpcVersion(1,3),newRpcVersion(2,1)))); |
| 54 | + |
| 55 | +varex=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 | +publicvoidValidate() |
| 71 | +{ |
| 72 | +Assert.DoesNotThrow(()=> |
| 73 | +newRpcVersionList(newRpcVersion(1,3),newRpcVersion(2,4),newRpcVersion(3,0)).Validate()); |
| 74 | + |
| 75 | +varex=Assert.Throws<ArgumentException>(()=>newRpcVersionList(newRpcVersion(0,1)).Validate()); |
| 76 | +Assert.That(ex.Message,Does.Contain("Invalid major version")); |
| 77 | +ex=Assert.Throws<ArgumentException>(()=> |
| 78 | +newRpcVersionList(newRpcVersion(2,1),newRpcVersion(1,2)).Validate()); |
| 79 | +Assert.That(ex.Message,Does.Contain("sorted")); |
| 80 | +ex=Assert.Throws<ArgumentException>(()=> |
| 81 | +newRpcVersionList(newRpcVersion(1,1),newRpcVersion(1,2)).Validate()); |
| 82 | +Assert.That(ex.Message,Does.Contain("Duplicate major version")); |
| 83 | +} |
| 84 | + |
| 85 | +privatevoidIsCompatibleWithBothWays(RpcVersionLista,RpcVersionListb,IResolveConstraintc) |
| 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 | +publicvoidIsCompatibleWith() |
| 93 | +{ |
| 94 | +varlist1=RpcVersionList.Parse("1.2,2.4,3.2"); |
| 95 | +Assert.That(list1.IsCompatibleWith(list1),Is.EqualTo(newRpcVersion(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(newRpcVersion(2,3))); |
| 99 | +IsCompatibleWithBothWays(list1,RpcVersionList.Parse("2.3,3.3"),Is.EqualTo(newRpcVersion(3,2))); |
| 100 | +} |
| 101 | +} |