Skip to content

Commit 41a680f

Browse files
committed
t Added docs and examples for namers
1 parent 33a37a6 commit 41a680f

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data

approvaltests-tests/src/test/java/org/approvaltests/namer/NamerFactoryForOptionsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,33 @@ void testOsSpecificTest()
2121
String name = namer.getApprovedFile(".txt").getName();
2222
assertEquals("NamerFactoryForOptionsTest.testOsSpecificTest.Mac_OS_X.approved.txt", name);
2323
}
24+
@Test
25+
void oldExampleForDocs()
26+
{
27+
// begin-snippet: namer_factory_example
28+
try (NamedEnvironment namer = NamerFactory.withParameters("title", "chapter")) {
29+
Approvals.verify("data");
30+
}
31+
// end-snippet
32+
}
33+
@Test
34+
void exampleForDocs()
35+
{
36+
// begin-snippet: additional_information_example
37+
Approvals.verify("data", Approvals.NAMES.withParameters("title", "chapter"));
38+
// end-snippet
39+
}
2440
@Disabled("TODO: only on Lars' laptop")
2541
// @EnabledOnOs(OS.MAC)
2642
@Test
2743
void testMachineSpecificTestAndOsSPecific()
2844
{
45+
// begin-snippet: options_and
2946
Options options = Approvals.NAMES.asOsSpecificTest().and(Approvals.NAMES::asMachineNameSpecificTest);
3047
ApprovalNamer namer = options.forFile().getNamer();
3148
String name = namer.getApprovedFile(".txt").getName();
3249
assertEquals("NamerFactoryForOptionsTest.testMachineSpecificTest.Mac_OS_X.lars-mbp-14.approved.txt", name);
50+
// end-snippet
3351
}
3452
@Test
3553
void allHaveOptions()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<a id="top"></a>
2+
3+
# Naming
4+
5+
<!-- toc -->
6+
## Contents
7+
8+
* [Paintables](#paintables)
9+
* [Why](#why)
10+
* [How To](#how-to)
11+
* [create an animated gif](#create-an-animated-gif)<!-- endToc -->
12+
13+
## Additional Naming Parameters
14+
15+
By default an approval file will take the form of:
16+
`classname.methodname.approved.txt`
17+
18+
There are a few cases where this is not sufficient:
19+
* You need more than one approval in a test method
20+
* You are using parameterized tests
21+
* You need different approvals per computer/OS/platform
22+
23+
The way this is handled is through the naming factories. This will give you approval tests with the following:
24+
`classname.methodname.additionalInformation.approved.txt`
25+
26+
## How to do this
27+
28+
### The old way
29+
There is a global namer factory that can be accessed with a try/catch. While this way works, it is not thread-safe
30+
and can cause trouble when running tests in parallel. Here is an example of how to do it:
31+
snippet: namer_factory_example
32+
33+
This will result in a file named:
34+
`ClassName.methodName.firstString.secondString.approved.txt`
35+
36+
### The new way
37+
With the addition of `Options` you can now set additional naming information in a thread-safe way. Here is an example of how to do that:
38+
39+
snippet: additional_information_example
40+
41+
This will result in a file named:
42+
`ClassName.methodName.firstString.secondString.approved.txt`
43+
44+
#### Adding to an existing scenario
45+
If there is already an `Options` created, you can add to it using the `.and` keyword. Here is an example:
46+
47+
snippet: options_and
48+
49+
This will result in a file named:
50+
`ClassName.methodName.OSName.machineName.approved.txt`
51+
52+
## Parameterized Tests
53+
If you are using parameterized tests you will need to use additional names. See [How to use Approvals.verify in Parameterized Tests](../how_to/ParameterizedTest.md).
54+
55+
## Only running on a specific machine
56+
57+
Sometimes you have tests that cannot run on all of your machines. See [How to run tests only on specific machines](../how_to/MachineNameSpecificTest.md)
58+
59+
## Capturing approvals from CI
60+
If you are using special names per OS/platform your CI machines might not have a corresponding dev box. To handle this see [How to capture .received. files from CI](../how_to/CaptureFilesFromCI.md)

approvaltests/src/main/java/org/approvaltests/namer/NamerFactoryForOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ public Options asMachineNameSpecificTest(Options options)
2525
{
2626
return options.forFile().withAdditionalInformation(SystemUtils.getComputerName());
2727
}
28+
public Options withParameters(String... parameters) {
29+
return withParameters(new Options(), parameters);
30+
}
31+
public Options withParameters(Options options, String... parameters) {
32+
for (String parameter : parameters) {
33+
options = options.forFile().withAdditionalInformation(parameter);
34+
}
35+
return options;
36+
}
2837
}

0 commit comments

Comments
 (0)