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

Limit docs code snippets line length to max 60 chars (#317 / #318) #318

Merged
merged 17 commits into from
Sep 29, 2020
Merged
39 changes: 26 additions & 13 deletions docs/default-locale-timezone.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ The default `Locale` can be specified using an https://docs.oracle.com/javase/8/
@Test
@DefaultLocale("zh-Hant-TW")
void test_with_lanuage() {
assertThat(Locale.getDefault()).isEqualTo(Locale.forLanguageTag("zh-Hant-TW"));
assertThat(Locale.getDefault())
.isEqualTo(Locale.forLanguageTag("zh-Hant-TW"));
}
----

Expand All @@ -29,19 +30,25 @@ Alternatively the default `Locale` can be specified according to https://docs.or
@Test
@DefaultLocale(language = "en")
void test_with_lanuage() {
assertThat(Locale.getDefault()).isEqualTo(new Locale("en"));
assertThat(Locale.getDefault())
.isEqualTo(new Locale("en"));
}

@Test
@DefaultLocale(language = "en", country = "EN")
void test_with_lanuage_and_country() {
assertThat(Locale.getDefault()).isEqualTo(new Locale("en", "EN"));
assertThat(Locale.getDefault())
.isEqualTo(new Locale("en", "EN"));
}

@Test
@DefaultLocale(language = "en", country = "EN", variant = "gb")
@DefaultLocale(
language = "en",
country = "EN",
variant = "gb")
void test_with_lanuage_and_country_and_vairant() {
assertThat(Locale.getDefault()).isEqualTo(new Locale("en", "EN", "gb"));
assertThat(Locale.getDefault())
.isEqualTo(new Locale("en", "EN", "gb"));
}
----

Expand All @@ -58,13 +65,15 @@ class MyLocaleTests {

@Test
void test_with_class_level_configuration() {
assertThat(Locale.getDefault()).isEqualTo(new Locale("fr"));
assertThat(Locale.getDefault())
.isEqualTo(new Locale("fr"));
}

@Test
@DefaultLocale(language = "en")
void test_with_method_level_configuration() {
assertThat(Locale.getDefault()).isEqualTo(new Locale("en"));
assertThat(Locale.getDefault())
.isEqualTo(new Locale("en"));
}
}
----
Expand All @@ -78,13 +87,15 @@ The default `TimeZone` is specified according to the https://docs.oracle.com/jav
@Test
@DefaulTimeZone("CET")
void test_with_short_zone_id() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("CET"));
assertThat(TimeZone.getDefault()).
isEqualTo(TimeZone.getTimeZone("CET"));
}

@Test
@DefaultTimeZone("America/Los_Angeles")
@DefaultTimeZone("Africa/Juba")
void test_with_long_zone_id() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("America/Los_Angeles"));
assertThat(TimeZone.getDefault()).
isEqualTo(TimeZone.getTimeZone("Africa/Juba"));
}
----

Expand All @@ -97,13 +108,15 @@ class MyTimeZoneTests {

@Test
void test_with_class_level_configuration() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("CET"));
assertThat(TimeZone.getDefault()).
isEqualTo(TimeZone.getTimeZone("CET"));
}

@Test
@DefaultTimeZone("America/Los_Angeles")
@DefaultTimeZone("Africa/Juba")
void test_with_method_level_configuration() {
assertThat(TimeZone.getDefault()).isEqualTo(TimeZone.getTimeZone("America/Los_Angeles"));
assertThat(TimeZone.getDefault()).
isEqualTo(TimeZone.getTimeZone("Africa/Juba"));
}
}
----
Expand Down
43 changes: 24 additions & 19 deletions docs/disable-if-display-name.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ As a consequence, instead of disabling the entire set of parameterized tests, ea
@DisableIfDisplayName(contains = "disable")
@ParameterizedTest(name = "See if enabled with {0}")
@ValueSource(
strings = {
"disable who", // ~> disabled
"you, disable you", // ~> disabled
"why am I disabled", // ~> disabled
"what has been disabled must stay disabled", // ~> disabled
"fine disable me all you want", // ~> disabled
"not those one, though!" // ~> NOT disabled
}
// Disabled: 1,2,3,4,5
// Not disabled: 6
strings = {
"disable who", // 1
"you, disable you", // 2
"why am I disabled", // 3
"what has been disabled must stay disabled", // 4
"fine disable me all you want", // 5
"not those one, though!" // 6
}
)
void testExecutionDisabled(String reason) {
if (reason.contains("disable"))
Expand All @@ -44,18 +46,21 @@ If substrings are not powerful enough, you can also use regular expressions:

[source,java]
----
// disable invocations whose display name contains "disable " or "disabled "
// disable invocations whose display name
// contains "disable " or "disabled "
@DisableIfDisplayName(matches = ".*disabled?\\s.*")
@ParameterizedTest(name = "See if enabled with {0}")
@ValueSource(
strings = {
"disable who", // ~> disabled
"you, disable you", // ~> disabled
"why am I disabled", // ~> NOT disabled
"what has been disabled must stay disabled", // ~> disabled
"fine disable me all you want", // ~> disabled
"not those one, though!" // ~> NOT disabled
}
// Disabled: 1,2,4,5
// Not disabled: 3,6
strings = {
"disable who", // 1
"you, disable you", // 2
"why am I disabled", // 3
"what has been disabled must stay disabled", // 4
"fine disable me all you want", // 5
"not those one, though!" // 6
}
)
void single(String reason) {
// ...
Expand All @@ -66,11 +71,11 @@ You can even use both, in which case a test is disabled if contains a substring

[source,java]
----
@DisableIfDisplayName(contains = "000", matches = ".*10?" )
@DisableIfDisplayName(contains = "000", matches = ".*10?")
@ParameterizedTest(name = "See if enabled with {0}")
@ValueSource(ints = { 1, 10, 100, 1_000, 10_000 })
void containsAndMatches_containsAndMatches(int number) {
if (number != 100)
fail("Test should've been disabled for " + number);
}
----
----
22 changes: 16 additions & 6 deletions docs/environment-variables.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ And setting a environment variable for a test execution:
[source,java]
----
@Test
@SetEnvironmentVariable(key = "some variable", value = "new value")
@SetEnvironmentVariable(
key = "some variable",
value = "new value")
void test() {
assertThat(System.getenv("some variable")).isEqualTo("new value");
assertThat(System.getenv("some variable")).
isEqualTo("new value");
}
----

Expand All @@ -40,11 +43,14 @@ As mentioned before, both annotations are repeatable and they can also be combin
@Test
@ClearEnvironmentVariable(key = "1st variable")
@ClearEnvironmentVariable(key = "2nd variable")
@SetEnvironmentVariable(key = "3rd variable", value = "new value")
@SetEnvironmentVariable(
key = "3rd variable",
value = "new value")
void test() {
assertThat(System.getenv("1st variable")).isNull();
assertThat(System.getenv("2nd variable")).isNull();
assertThat(System.getenv("3rd variable")).isEqualTo("new value");
assertThat(System.getenv("3rd variable"))
.isEqualTo("new value");
}
----

Expand All @@ -54,11 +60,15 @@ Note that class level configurations are overwritten by method level configurati
----
@ClearEnvironmentVariable(key = "some variable")
class MyEnvironmentVariableTest {

@Test
@SetEnvironmentVariable(key = "some variable", value = "new value")
@SetEnvironmentVariable(key = "some variable",
value = "new value")
void test() {
assertThat(System.getenv("some variable")).isEqualTo("new value");
assertThat(System.getenv("some variable"))
.isEqualTo("new value");
}

}
----

Expand Down
6 changes: 3 additions & 3 deletions docs/range-sources.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ It can be positive or negative:
@ParameterizedTest
@DoubleRangeSource(from = -0.1, to = -10, step = -0.1)
void howColdIsIt(double d) {
System.out.println(d + " degrees Celsius is cold");
System.out.println(d + " degrees Fahrenheit is REALY cold");
System.out.println(d + " degrees Kelvin is too cold to be true");
System.out.println(d + " °C is cold");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, non-ASCII chars in code snippets? 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Extended) ASCII-Code 248 :p http://www.asciitable.com/

I havn't come up with the short limit of 60 chars where about 40 are for intenden and System.out.println(..); 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every day's a school say. 👍

System.out.println(d + " °F is REALY cold");
System.out.println(d + " K is too cold to be true");
}
----

Expand Down
11 changes: 8 additions & 3 deletions docs/report-entries.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ Just like `TestReporter::publishEntry` accepts a single string as value or a key
[source,java]
----
@Test
@ReportEntry(key = "line1", value = "Once upon a midnight dreary")
@ReportEntry(key = "line2", value = "While I pondered weak and weary")
@ReportEntry(key = "line1",
value = "Once upon a midnight dreary")
@ReportEntry(key = "line2",
value = "While I pondered weak and weary")
void edgarAllanPoe() {
// YOUR TEST CODE HERE
}
Expand All @@ -89,7 +91,10 @@ Just so:
[source,java]
----
@Test
@ReportEntry(key = "line", value = "success entry", when = ReportEntry.PublishCondition.ON_SUCCESS)
@ReportEntry(
key = "line",
value = "success entry",
when = ReportEntry.PublishCondition.ON_SUCCESS)
void sufferingFromSuccess() {
// YOUR TEST CODE HERE
}
Expand Down
23 changes: 14 additions & 9 deletions docs/retrying-test.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,47 @@ Such tests make a suite fragile and it makes sense to try and avoid them, but if
`@RetryingTest(n)` is used _instead_ of `@Test` or other such annotations (e.g. `@RepeatedTest`).
The attribute `n` specifies how often the test is executed before giving up.

```java
[source,java]
----
@RetryingTest(3)
void failsNever() {
// passing test code
}
```
----

The test `failsNever` is executed once (which succeeds) and marked as passed.

```java
[source,java]
----
@RetryingTest(3)
void failsOnlyOnFirstInvocation() {
// test code that fails on first execution
// but passes on the second
}
```
----

The test `failsOnlyOnFirstInvocation` is executed once (which fails) and then once more (which succeeds).
To allow the entire test suite to pass, the first execution is marked as ignored/aborted, which includes the underlying exception - the second is of course marked as passing.

```java
[source,java]
----
@RetryingTest(3)
void failsAlways() {
// test code that always fails
}
```
----

The test `failsAlways` is executed three times (all of which fail).
The first two executions are marked as ignored/aborted, while the last as failed - each contains the underlying exception.

```java
[source,java]
----
@RetryingTest(3)
void aborted() {
// test code that is aborted e.g. because of an Assumption.
// test code that is aborted,
// e.g. because of an `Assumption`.
}
```
----

If a test is aborted (e.g.: because of a failed Assumption) `@RetryingTest` won't try again after that.
The test `aborted` is aborted before (or during) its first execution.
Expand Down
16 changes: 11 additions & 5 deletions docs/standard-input-output.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class ExampleConsoleReader {
private List<String> lines = new ArrayList<>();

public void readLines() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(is);
for (int i = 0; i < 2; i++) {
String line = reader.readLine();
lines.add(line);
Expand All @@ -158,17 +159,22 @@ This is the unit test for this class, using `StdIoExtension`:

[source, java]
----
class ExampleConsoleReaderTest {
class ConsoleReaderTest {

@Test
@StdIo({ "line1", "line2", "line3" })
void testReadLines(StdIn in) {
ExampleConsoleReader consoleReader = new ExampleConsoleReader();
ConsoleReader consoleReader = new ConsoleReader();

consoleReader.readLines();

// assertEquals(in.capturedLines(), "line1", "line2"); // This is failing
// assertEquals(in.capturedLines(), "line1", "line2", "line3"); // This is passing
String[] lines = in.capturedLines();

// This is failing
// assertEquals(lines, "line1", "line2");
Bukama marked this conversation as resolved.
Show resolved Hide resolved

// This is passing
// assertEquals(lines, "line1", "line2", "line3");
Bukama marked this conversation as resolved.
Show resolved Hide resolved
}

}
Expand Down