Skip to content

Commit 80ea552

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 18c5c9f + 5cc8b8f commit 80ea552

File tree

5 files changed

+74
-50
lines changed

5 files changed

+74
-50
lines changed

.github/dependabot.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: maven
4+
directory: "/"
5+
schedule:
6+
interval: daily
7+
open-pull-requests-limit: 10

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* [Documentation](#documentation)
1010
* [Getting started](#getting-started)
1111
* [How to get it](#how-to-get-it)
12+
* [Maven](#maven)
13+
* [Gradle](#gradle)
1214
* [Podcasts](#podcasts)
1315
* [Examples](#examples)
1416
* [Approved File Artifacts](#approved-file-artifacts)
@@ -44,22 +46,34 @@ Approval Tests can be used for verifying objects that require more than a simple
4446

4547
## Getting started
4648

47-
The best way to get started is download and open the [Starter Project](https://github.com/approvals/approvaltests.java.starterproject).
48-
It is a maven project and can be imported into any editor.
49+
The best way to get started is download and open one of
50+
* [Maven Starter Project](https://github.com/approvals/approvaltests.java.starterproject)
51+
* [Gradle Starter Project](https://github.com/approvals/approvaltests.java.starterproject.gradle)
52+
It is a standard project and can be imported into any editor or IDE.
4953

5054

5155
## How to get it
52-
It's on Maven Central, search for 'approvaltests'. If you're using Maven,
53-
add this to your pom file:
56+
It's on Maven Central, search for 'approvaltests'.
57+
58+
### Maven
59+
If you're using Maven, add this to your pom file:
5460

5561
``` xml
5662
<dependency>
5763
<groupId>com.approvaltests</groupId>
5864
<artifactId>approvaltests</artifactId>
59-
<version>11.2.2</version>
65+
<version>11.2.3</version>
6066
</dependency>
6167
```
6268

69+
### Gradle
70+
71+
```gradle
72+
dependencies {
73+
testImplementation("com.approvaltests:approvaltests:11.2.3")
74+
}
75+
```
76+
6377
or [download the jars from maven central repository](https://repo1.maven.org/maven2/com/approvaltests/approvaltests/)
6478

6579
[Video Tutorials](https://www.youtube.com/playlist?list=PLFBA98F47156EFAA9&feature=view_all)

approvaltests-util/src/main/java/com/spun/util/ArrayUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import java.util.HashMap;
1111
import java.util.Iterator;
1212
import java.util.List;
13+
import java.util.Map;
1314
import java.util.Vector;
1415

16+
import org.lambda.functions.Function0;
1517
import org.lambda.functions.Function1;
1618
import org.lambda.query.Queryable;
1719

@@ -276,6 +278,18 @@ public static <T> T[] toArray(List<T> list, Class<T> type)
276278
{
277279
return Queryable.as(list, type).asArray();
278280
}
281+
public static <KEY, VALUES, SPECIFIC_VALUE extends VALUES> SPECIFIC_VALUE getOrElse(Map<KEY, VALUES> fields,
282+
KEY key, Function0<SPECIFIC_VALUE> defaultIfNotFound)
283+
{
284+
if (fields.containsKey(key))
285+
{
286+
return (SPECIFIC_VALUE) fields.get(key);
287+
}
288+
else
289+
{
290+
return defaultIfNotFound.call();
291+
}
292+
}
279293
public static class IterableWrapper<T> implements Iterable<T>
280294
{
281295
private final Iterator<T> iterator;
Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,96 @@
11
package org.approvaltests.core;
22

3-
import java.util.Optional;
3+
import java.util.HashMap;
4+
import java.util.Map;
45

56
import org.approvaltests.Approvals;
67
import org.approvaltests.ReporterFactory;
78
import org.approvaltests.namer.ApprovalNamer;
89
import org.approvaltests.namer.NamerWrapper;
910
import org.approvaltests.scrubbers.NoOpScrubber;
1011

12+
import com.spun.util.ArrayUtils;
13+
1114
public class Options
1215
{
13-
private Scrubber scrubber = NoOpScrubber.INSTANCE;
14-
private Optional<ApprovalFailureReporter> reporter = Optional.empty();
15-
private FileOptions fileOptions = new FileOptions();
16+
private enum Fields {
17+
SCRUBBER, REPORTER, FILE_OPTIONS_FILE_EXTENSION, FILE_OPTIONS_NAMER;
18+
}
19+
private final Map<Fields, Object> fields = new HashMap<>();
1620
public Options()
1721
{
1822
}
1923
public Options(Scrubber scrubber)
2024
{
21-
this.scrubber = scrubber;
25+
fields.put(Fields.SCRUBBER, scrubber);
2226
}
2327
public Options(ApprovalFailureReporter reporter)
2428
{
25-
this.reporter = Optional.ofNullable(reporter);
26-
}
27-
private Options(Scrubber scrubber, Optional<ApprovalFailureReporter> reporter, FileOptions fileOptions)
28-
{
29-
this.scrubber = scrubber;
30-
this.reporter = reporter;
31-
this.fileOptions = fileOptions;
29+
fields.put(Fields.REPORTER, reporter);
3230
}
33-
public Options(Options parent, FileOptions fileOptions)
31+
private Options(Map<Fields, Object> fields, Fields key, Object value)
3432
{
35-
this(parent.scrubber, parent.reporter, fileOptions);
33+
this.fields.putAll(fields);
34+
this.fields.put(key, value);
3635
}
3736
public ApprovalFailureReporter getReporter()
3837
{
39-
return this.reporter.orElseGet(ReporterFactory::get);
38+
return ArrayUtils.getOrElse(fields, Fields.REPORTER, ReporterFactory::get);
4039
}
4140
public Options withReporter(ApprovalFailureReporter reporter)
4241
{
43-
return new Options(this.scrubber, Optional.ofNullable(reporter), fileOptions);
42+
return new Options(fields, Fields.REPORTER, reporter);
4443
}
4544
public Options withScrubber(Scrubber scrubber)
4645
{
47-
return new Options(scrubber, this.reporter, fileOptions);
46+
return new Options(fields, Fields.SCRUBBER, scrubber);
4847
}
4948
public String scrub(String input)
5049
{
51-
return scrubber.scrub(input);
50+
return getScrubber().scrub(input);
51+
}
52+
private Scrubber getScrubber()
53+
{
54+
return ArrayUtils.getOrElse(fields, Fields.SCRUBBER, () -> NoOpScrubber.INSTANCE);
5255
}
5356
public FileOptions forFile()
5457
{
55-
fileOptions.setParent(this);
56-
return fileOptions;
58+
return new FileOptions(this.fields);
5759
}
5860
public static class FileOptions
5961
{
60-
private String fileExtension = ".txt";
61-
protected NamerWrapper approvalNamer = null;
62-
protected Options parent;
63-
public FileOptions()
64-
{
65-
}
66-
public FileOptions(String fileExtension)
67-
{
68-
this.fileExtension = fileExtension;
69-
}
70-
public FileOptions(NamerWrapper approvalNamer, String fileExtension)
71-
{
72-
this.approvalNamer = approvalNamer;
73-
this.fileExtension = fileExtension;
74-
}
75-
public void setParent(Options parent)
62+
private final Map<Fields, Object> fields;
63+
public FileOptions(Map<Fields, Object> fields)
7664
{
77-
this.parent = parent;
65+
this.fields = fields;
7866
}
7967
public Options withExtension(String fileExtensionWithDot)
8068
{
8169
if (!fileExtensionWithDot.startsWith("."))
8270
{
8371
fileExtensionWithDot = "." + fileExtensionWithDot;
8472
}
85-
FileOptions f = new FileOptions(this.approvalNamer, fileExtensionWithDot);
86-
return new Options(parent, f);
73+
return new Options(fields, Fields.FILE_OPTIONS_FILE_EXTENSION, fileExtensionWithDot);
8774
}
8875
public ApprovalNamer getNamer()
8976
{
90-
return approvalNamer == null ? Approvals.createApprovalNamer() : approvalNamer;
77+
return ArrayUtils.getOrElse(fields, Fields.FILE_OPTIONS_NAMER, Approvals::createApprovalNamer);
9178
}
9279
public String getFileExtension()
9380
{
94-
return fileExtension;
81+
return ArrayUtils.getOrElse(fields, Fields.FILE_OPTIONS_FILE_EXTENSION, () -> ".txt");
9582
}
9683
public Options withBaseName(String fileBaseName)
9784
{
9885
NamerWrapper approvalNamer = new NamerWrapper(() -> fileBaseName, getNamer());
99-
FileOptions f = new FileOptions(approvalNamer, this.fileExtension);
100-
return new Options(parent, f);
86+
return new Options(fields, Fields.FILE_OPTIONS_NAMER, approvalNamer);
10187
}
10288
public Options withName(String fileBaseName, String extension)
10389
{
10490
NamerWrapper approvalNamer = new NamerWrapper(() -> fileBaseName, getNamer());
105-
FileOptions f = new FileOptions(approvalNamer, extension);
106-
return new Options(parent, f);
91+
HashMap<Fields, Object> newFields = new HashMap<>(fields);
92+
newFields.put(Fields.FILE_OPTIONS_FILE_EXTENSION, extension);
93+
return new Options(newFields, Fields.FILE_OPTIONS_NAMER, approvalNamer);
10794
}
10895
}
10996
}

build/UpdateVersion.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public static void main(String[] args) throws IOException {
88
final String version = args[0];
99
replaceTextInFile("README.md", "<version>\\d+.\\d+.\\d+</version>",
1010
"<version>" + version + "</version>");
11+
replaceTextInFile("README.md", "com.approvaltests:approvaltests:\\d+.\\d+.\\d+",
12+
"com.approvaltests:approvaltests:" + version);
1113
}
1214

1315
private static void replaceTextInFile(String fileName, String regex, String replacement)

0 commit comments

Comments
 (0)