1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .nio .file .Paths ;
23
23
import java .util .Arrays ;
24
24
import java .util .Objects ;
25
- import java .util .function .Consumer ;
26
25
27
- import org .assertj .core .api .AbstractThrowableAssert ;
26
+ import org .assertj .core .api .AbstractStringAssert ;
28
27
import org .assertj .core .api .Assertions ;
29
28
30
29
/** An api for testing a {@code FormatterStep} that doesn't depend on the File path. DO NOT ADD FILE SUPPORT TO THIS, use {@link StepHarnessWithFile} if you need that. */
31
30
public class StepHarness implements AutoCloseable {
32
- private final FormatterFunc formatter ;
31
+ private final Formatter formatter ;
33
32
34
- private StepHarness (FormatterFunc formatter ) {
33
+ private StepHarness (Formatter formatter ) {
35
34
this .formatter = Objects .requireNonNull (formatter );
36
35
}
37
36
38
37
/** Creates a harness for testing steps which don't depend on the file. */
39
38
public static StepHarness forStep (FormatterStep step ) {
40
- // We don't care if an individual FormatterStep is misbehaving on line-endings, because
41
- // Formatter fixes that. No reason to care in tests either. It's likely to pop up when
42
- // running tests on Windows from time-to-time
43
- return new StepHarness (FormatterFunc .Closeable .ofDangerous (
44
- () -> {
45
- if (step instanceof FormatterStepImpl .Standard ) {
46
- ((FormatterStepImpl .Standard <?>) step ).cleanupFormatterFunc ();
47
- }
48
- },
49
- input -> LineEnding .toUnix (step .format (input , new File ("" )))));
39
+ return forSteps (step );
50
40
}
51
41
52
42
/** Creates a harness for testing steps which don't depend on the file. */
@@ -56,60 +46,68 @@ public static StepHarness forSteps(FormatterStep... steps) {
56
46
.lineEndingsPolicy (LineEnding .UNIX .createPolicy ())
57
47
.encoding (StandardCharsets .UTF_8 )
58
48
.rootDir (Paths .get ("" ))
49
+ .exceptionPolicy (new FormatExceptionPolicyStrict ())
59
50
.build ());
60
51
}
61
52
62
53
/** Creates a harness for testing a formatter whose steps don't depend on the file. */
63
54
public static StepHarness forFormatter (Formatter formatter ) {
64
- return new StepHarness (FormatterFunc .Closeable .ofDangerous (
65
- formatter ::close ,
66
- input -> formatter .compute (input , new File ("" ))));
55
+ return new StepHarness (formatter );
67
56
}
68
57
69
58
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */
70
- public StepHarness test (String before , String after ) throws Exception {
71
- String actual = formatter .apply ( before );
59
+ public StepHarness test (String before , String after ) {
60
+ String actual = formatter .compute ( LineEnding . toUnix ( before ), new File ( "" ) );
72
61
assertEquals (after , actual , "Step application failed" );
73
62
return testUnaffected (after );
74
63
}
75
64
76
65
/** Asserts that the given element is idempotent w.r.t the step under test. */
77
- public StepHarness testUnaffected (String idempotentElement ) throws Exception {
78
- String actual = formatter .apply ( idempotentElement );
66
+ public StepHarness testUnaffected (String idempotentElement ) {
67
+ String actual = formatter .compute ( LineEnding . toUnix ( idempotentElement ), new File ( "" ) );
79
68
assertEquals (idempotentElement , actual , "Step is not idempotent" );
80
69
return this ;
81
70
}
82
71
83
72
/** Asserts that the given elements in the resources directory are transformed as expected. */
84
- public StepHarness testResource (String resourceBefore , String resourceAfter ) throws Exception {
73
+ public StepHarness testResource (String resourceBefore , String resourceAfter ) {
85
74
String before = ResourceHarness .getTestResource (resourceBefore );
86
75
String after = ResourceHarness .getTestResource (resourceAfter );
87
76
return test (before , after );
88
77
}
89
78
90
79
/** Asserts that the given elements in the resources directory are transformed as expected. */
91
- public StepHarness testResourceUnaffected (String resourceIdempotent ) throws Exception {
80
+ public StepHarness testResourceUnaffected (String resourceIdempotent ) {
92
81
String idempotentElement = ResourceHarness .getTestResource (resourceIdempotent );
93
82
return testUnaffected (idempotentElement );
94
83
}
95
84
96
- /** Asserts that the given elements in the resources directory are transformed as expected. */
97
- public StepHarness testResourceException (String resourceBefore , Consumer <AbstractThrowableAssert <?, ? extends Throwable >> exceptionAssertion ) throws Exception {
98
- return testException (ResourceHarness .getTestResource (resourceBefore ), exceptionAssertion );
85
+ public AbstractStringAssert <?> testResourceExceptionMsg (String resourceBefore ) {
86
+ return testExceptionMsg (ResourceHarness .getTestResource (resourceBefore ));
99
87
}
100
88
101
- /** Asserts that the given elements in the resources directory are transformed as expected. */
102
- public StepHarness testException (String before , Consumer <AbstractThrowableAssert <?, ? extends Throwable >> exceptionAssertion ) throws Exception {
103
- Throwable t = assertThrows (Throwable .class , () -> formatter .apply (before ));
104
- AbstractThrowableAssert <?, ? extends Throwable > abstractAssert = Assertions .assertThat (t );
105
- exceptionAssertion .accept (abstractAssert );
106
- return this ;
89
+ public AbstractStringAssert <?> testExceptionMsg (String before ) {
90
+ try {
91
+ formatter .compute (LineEnding .toUnix (before ), FormatterStepImpl .SENTINEL );
92
+ throw new SecurityException ("Expected exception" );
93
+ } catch (Throwable e ) {
94
+ if (e instanceof SecurityException ) {
95
+ throw new AssertionError (e .getMessage ());
96
+ } else {
97
+ Throwable rootCause = e ;
98
+ while (rootCause .getCause () != null ) {
99
+ if (rootCause instanceof IllegalStateException ) {
100
+ break ;
101
+ }
102
+ rootCause = rootCause .getCause ();
103
+ }
104
+ return Assertions .assertThat (rootCause .getMessage ());
105
+ }
106
+ }
107
107
}
108
108
109
109
@ Override
110
110
public void close () {
111
- if (formatter instanceof FormatterFunc .Closeable ) {
112
- ((FormatterFunc .Closeable ) formatter ).close ();
113
- }
111
+ formatter .close ();
114
112
}
115
113
}
0 commit comments