Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Parameterized Tests #86

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.obvj</groupId>
<artifactId>junit-utils</artifactId>
Expand Down
33 changes: 24 additions & 9 deletions src/test/java/net/obvj/performetrics/ConversionModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package net.obvj.performetrics;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Unit tests for the {@link ConversionMode}.
Expand All @@ -32,16 +33,30 @@
class ConversionModeTest
{

@Test
void convert_fast999millisecondsToSeconds()
@ParameterizedTest
@CsvSource({
"1, MILLISECONDS, SECONDS, 0.0",
"999, MILLISECONDS, SECONDS, 0.0",
"1000, MILLISECONDS, SECONDS, 1.0",
"1500, MILLISECONDS, SECONDS, 1.0",
"2, HOURS, MINUTES, 120.0"
})
void convert_fast(long amount, TimeUnit source, TimeUnit target, double result)
{
assertThat(ConversionMode.FAST.convert(999, MILLISECONDS, SECONDS), is(0.0));
assertThat(ConversionMode.FAST.convert(amount, source, target), is(result));
}

@Test
void convert_doublePrecision999millisecondsToSeconds()
@ParameterizedTest
@CsvSource({
"1, MILLISECONDS, SECONDS, 0.001",
"999, MILLISECONDS, SECONDS, 0.999",
"1000, MILLISECONDS, SECONDS, 1.0",
"1500, MILLISECONDS, SECONDS, 1.5",
"2, HOURS, MINUTES, 120.0"
})
void convert_doublePrecision(long amount, TimeUnit source, TimeUnit target, double result)
{
assertThat(ConversionMode.DOUBLE_PRECISION.convert(999, MILLISECONDS, SECONDS), is(0.999));
assertThat(ConversionMode.DOUBLE_PRECISION.convert(amount, source, target), is(result));
}

}
38 changes: 18 additions & 20 deletions src/test/java/net/obvj/performetrics/util/DurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Unit tests for the {@link Duration}.
Expand Down Expand Up @@ -322,27 +325,22 @@ void compareTo_lowerDurations_negative()
assertThat(Duration.of(-4, MINUTES ).compareTo(Duration.of( -3, MINUTES )), isNegative());
}

@Test
void parse_stringsGeneratedByToString_success()
{
assertParseFromToString(Duration.of(123, NANOSECONDS));
assertParseFromToString(Duration.of(123, MILLISECONDS));
assertParseFromToString(Duration.of(123, SECONDS));
assertParseFromToString(Duration.of(123, MINUTES));
}

@Test
void parse_stringsGeneratedByToStringUsingNegativeDurations_success()
{
assertParseFromToString(Duration.of(-123, NANOSECONDS));
assertParseFromToString(Duration.of(-123, MILLISECONDS));
assertParseFromToString(Duration.of(-123, SECONDS));
assertParseFromToString(Duration.of(-123, MINUTES));
}

private static void assertParseFromToString(Duration duration)
@ParameterizedTest
@CsvSource({
" 123, NANOSECONDS",
" 123, MILLISECONDS",
" 123, SECONDS",
" 123, MINUTES",
"-123, NANOSECONDS",
"-123, MILLISECONDS",
"-123, SECONDS",
"-123, MINUTES"
})
void parse_stringsGeneratedByToString_success(long amount, TimeUnit timeUnit)
{
assertThat(Duration.parse(duration.toString()), equalTo(duration));
Duration duration = Duration.of(amount, timeUnit);
String durationAsString = duration.toString();
assertThat(Duration.parse(durationAsString), equalTo(duration));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Unit tests for the {@link TimeUnitConverter}.
Expand All @@ -39,64 +41,48 @@ void constructor_instantiationNotAllowed()
assertThat(TimeUnitConverter.class, instantiationNotAllowed().throwing(IllegalStateException.class));
}

@Test
void convertAndRound_2MinutesToMilliseconds()
{
assertThat(TimeUnitConverter.convertAndRound(2, TimeUnit.MINUTES, TimeUnit.MILLISECONDS), is(equalTo(2.0 * 60 * 1000)));
}

@Test
void convertAndRound_90SecondsToMinutes()
{
assertThat(TimeUnitConverter.convertAndRound(30, TimeUnit.SECONDS, TimeUnit.MINUTES), is(equalTo(0.5)));
}

@Test
void convertAndRound_999MillisecondsToSeconds()
{
assertThat(TimeUnitConverter.convertAndRound(999, TimeUnit.MILLISECONDS, TimeUnit.SECONDS), is(equalTo(0.999)));
}

@Test
void convertAndRound_988MillisecondsToSecondsAnd2DecimalPlaces()
{
assertThat(TimeUnitConverter.convertAndRound(988, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, 2), is(equalTo(0.99)));
}

@Test
void convertAndRound_988MillisecondsToSecondsAnd0DecimalPlaces()
{
assertThat(TimeUnitConverter.convertAndRound(988, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, 0), is(equalTo(1.0)));
}

@Test
void round_positiveDecimalPlaces()
@ParameterizedTest
@CsvSource({
" 2, MINUTES, MILLISECONDS, 120_000",
" 30, SECONDS, MINUTES, 0.5",
"999, MILLISECONDS, SECONDS, 0.999"
})
void convertAndRound_threeParameters(long amount, TimeUnit source, TimeUnit target, double expected)
{
assertThat(TimeUnitConverter.round(22.859, 2), is(equalTo(22.86)));
assertThat(TimeUnitConverter.convertAndRound(amount, source, target),
is(equalTo(expected)));
}

@Test
void round_zeroDecimalPlaces()
@ParameterizedTest
@CsvSource({
"988, MILLISECONDS, SECONDS, 2, 0.99",
"988, MILLISECONDS, SECONDS, 0, 1.0"
})
void convertAndRound_allParameters(long amount, TimeUnit source, TimeUnit target, int decimalPlaces, double expected)
{
assertThat(TimeUnitConverter.round(22.859, 0), is(equalTo(23.0)));
assertThat(TimeUnitConverter.convertAndRound(amount, source, target, decimalPlaces),
is(equalTo(expected)));
}

@Test
void round_negativeDecimalPlaces()
@ParameterizedTest
@CsvSource({
"22.859, 2, 22.86",
"22.859, 0, 23.0",
"22.859, -1, 20.0"
})
void round_twoParameters(double amount, int decimalPlaces, double expected)
{
assertThat(TimeUnitConverter.round(22.859, -1), is(equalTo(20.0)));
assertThat(TimeUnitConverter.round(amount, decimalPlaces), is(equalTo(expected)));
}

@Test
void convert_999MillisecondsToSeconds()
{
assertThat(TimeUnitConverter.convert(999, TimeUnit.MILLISECONDS, TimeUnit.SECONDS), is(equalTo(0.999)));
}

@Test
void convert_sameTimeUnit_success()
@ParameterizedTest
@CsvSource({
"999, MILLISECONDS, SECONDS, 0.999",
"999, NANOSECONDS, NANOSECONDS, 999.0",
})
void convert_threeParameters(long amount, TimeUnit source, TimeUnit target, double expected)
{
assertThat(TimeUnitConverter.convert(999, TimeUnit.NANOSECONDS, TimeUnit.NANOSECONDS), equalTo(999.0));
assertThat(TimeUnitConverter.convert(amount, source, target), is(equalTo(expected)));
}

}
Loading