### Question:
VersionParser { public static double toDouble(String version) { if (version != null) { try { return Double.parseDouble(version); } catch (NumberFormatException ex) { if (version.contains(".")) { return parseAsVersionName(version); } else { return parseAsNumber(version); } } } return INVALID_VERSION; } private  VersionParser(); static double toDouble(String version);  }### Answer:
@Test @Parameters({ "5.0.0, 5", "5.1.0, 5.1", "5.2.0, 5.2", "5.5.0, 5.5", "5.6.0, 5.6", "5.6.1, 5.61", "6.0, 6", "6.0.1, 6.01", "6.1, 6.1", "6.1.1, 6.11", }) public void shouldParseSemanticVersioning(String versionCode, String expected) { double expectedCode = Double.valueOf(expected); double resultCode = VersionParser.toDouble(versionCode); assertThat(resultCode, Is.is(expectedCode)); }
@Test @Parameters({ "5.61, 5.61", "6.01, 6.01" }) public void shouldParseValidDouble(String versionCode, String expected) { double expectedCode = Double.valueOf(expected); double resultCode = VersionParser.toDouble(versionCode); assertThat(resultCode, Is.is(expectedCode)); }
@Test @Parameters({ "5.6.1.2, 5.612", "5.6.1.2.0, 5.612", "5.5.6.1.2, 5.5612", "5.5.6.1.2.0, 5.5612", "5.5.6.1.2.3, 5.56123", "5.5.6.1.2.3.0, 5.56123", }) public void shouldParseLongSemanticVersioning(String versionCode, String expected) { double expectedCode = Double.valueOf(expected); double resultCode = VersionParser.toDouble(versionCode); assertThat(resultCode, Is.is(expectedCode)); }
@Test @Parameters({ "0, 0", "0.0, 0", "1-asdasdsad, 1", "1-asdasdsad2, 1", "12-asdasdsad2, 12", }) public void shouldParseIfHasNumber(String version, String result) { double resultCode = VersionParser.toDouble(version); assertThat(resultCode, Is.is(Double.valueOf(result))); }
@Test @Parameters({ "invalid", ".-", "" }) public void shouldReturnZeroForIncorrectVersion(String invalidVersion) { assertThat(String.format("Version '%s' should be treated as zero", invalidVersion), VersionParser.toDouble(invalidVersion), is(-1d)); }