From 8a7923cba738391734815a7c4ea96aa793273555 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.codeberg.org> Date: Tue, 14 Jul 2020 18:20:00 +0200 Subject: [PATCH] Fix VersionCheck (#591) code dedub Bugfix & Test Correction Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/591 Reviewed-by: opyale --- .../java/org/mian/gitnex/helpers/Version.java | 32 ++++--------------- .../org/mian/gitnex/helpers/VersionTest.java | 9 ++++-- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/helpers/Version.java b/app/src/main/java/org/mian/gitnex/helpers/Version.java index 551e4df0..cc62bd69 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/Version.java +++ b/app/src/main/java/org/mian/gitnex/helpers/Version.java @@ -103,23 +103,7 @@ public class Version { */ public boolean less(@NonNull Version v) { - int rounds = Math.min(this.values.size(), v.values.size()); - for(int i = 0; i < rounds; i++) { - if(i + 1 == rounds) { - if(this.values.get(i) >= v.values.get(i)) { - return false; - } - } - else { - if(this.values.get(i) > v.values.get(i)) { - return false; - } - else if(this.values.get(i) < v.values.get(i)) { - return true; - } - } - } - return true; + return v.higher(this); } @@ -184,13 +168,7 @@ public class Version { */ public boolean lessOrEqual(@NonNull Version v) { - int rounds = Math.min(this.values.size(), v.values.size()); - for(int i = 0; i < rounds; i++) { - if(this.values.get(i) > v.values.get(i)) { - return false; - } - } - return true; + return v.higherOrEqual(this); } @@ -217,9 +195,13 @@ public class Version { int rounds = Math.min(this.values.size(), v.values.size()); for(int i = 0; i < rounds; i++) { - if(this.values.get(i) < v.values.get(i)) { + if(this.values.get(i) > v.values.get(i)) { + return true; + } + else if(this.values.get(i) < v.values.get(i)) { return false; } + } return true; diff --git a/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java b/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java index 2909c756..33c7e955 100644 --- a/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java +++ b/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java @@ -28,6 +28,7 @@ public class VersionTest { assertFalse(new Version("2").equal("1")); assertFalse(new Version("1.2").equal("2.1")); assertFalse(new Version("2.2").equal("2.1.120")); + assertFalse(new Version("1.12.3").equal("1.13.0+dev-307-g633f52c22")); } @@ -41,6 +42,7 @@ public class VersionTest { assertTrue(new Version("1.2.3").less("1.2.4")); assertTrue(new Version("1.2.4").less("1.3.1")); assertTrue(new Version("1.2").less("2.1")); + assertTrue(new Version("1.12.3").less("1.13.0+dev-307-g633f52c22")); assertFalse(new Version("1").less("1.1.10")); assertFalse(new Version("1.12.1").less("1.12.0+dev-211-g316db0fe7")); @@ -62,12 +64,13 @@ public class VersionTest { assertTrue(new Version("1.2.3").lessOrEqual("1.2.4")); assertTrue(new Version("1").lessOrEqual("1.1.10")); assertTrue(new Version("1.12.0").lessOrEqual("1.12.0")); + assertTrue(new Version("1.12.3").lessOrEqual("1.13.0+dev-307-g633f52c22")); assertFalse(new Version("1.12.1").lessOrEqual("1.12.0+dev-211-g316db0fe7")); assertFalse(new Version("v1.12.0").lessOrEqual("1.10.0")); assertFalse(new Version("2.12.0").lessOrEqual("v1.12")); assertFalse(new Version("2").lessOrEqual("1")); - assertFalse(new Version("1.2").lessOrEqual("2.1")); + assertFalse(new Version("2.1").lessOrEqual("1.2")); assertFalse(new Version("2.2").lessOrEqual("2.1.120")); } @@ -83,6 +86,7 @@ public class VersionTest { assertTrue(new Version("1.2.4").higher("1.2.3")); assertTrue(new Version("1.13.0+dev-30-gb02d2c377").higher("1.11.4")); assertTrue(new Version("2.1").higher("1.2")); + assertTrue(new Version("1.13.0+dev-307-g633f52c22").higher("1.12.3")); assertFalse(new Version("1").higher("1.1.10")); assertFalse(new Version("1.12.0+dev-211-g316db0fe7").higher("1.12.1")); @@ -104,12 +108,13 @@ public class VersionTest { assertTrue(new Version("1.2.4").higherOrEqual("1.2.3")); assertTrue(new Version("1").higherOrEqual("1.1.10")); assertTrue(new Version("1.12.0").higherOrEqual("1.12.0")); + assertTrue(new Version("1.13.0+dev-307-g633f52c22").higherOrEqual("1.12.3")); assertFalse(new Version("1.12.0+dev-211-g316db0fe7").higherOrEqual("1.12.1")); assertFalse(new Version("1.10.0").higherOrEqual("v1.12.0")); assertFalse(new Version("v1.12").higherOrEqual("2.12.0")); assertFalse(new Version("1").higherOrEqual("2")); - assertFalse(new Version("2.1").higherOrEqual("1.2")); + assertFalse(new Version("1.2").higherOrEqual("2.1")); assertFalse(new Version("2.1.120").higherOrEqual("2.2")); }