Skip to content

Commit 0b63609

Browse files
JayBazuziisidore4dsherwood
committed
- F add ConsoleOutput.verifyAll
Co-Authored-By: Llewellyn Falco <[email protected]> Co-Authored-By: 4dsherwood <[email protected]>
1 parent 22e5cf9 commit 0b63609

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.approvaltests.utils;
2+
3+
import org.approvaltests.core.Options;
4+
import org.approvaltests.reporters.AutoApproveReporter;
5+
import org.approvaltests.reporters.UseReporter;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class ConsoleOutputTests
11+
{
12+
@Test
13+
@UseReporter(AutoApproveReporter.class)
14+
void testVerifyOutput()
15+
{
16+
var expected = """
17+
Hello, World!
18+
No newline
19+
""";
20+
try (ConsoleOutput console = new ConsoleOutput())
21+
{
22+
System.out.println("Hello, World!");
23+
System.out.print("No ");
24+
System.out.print("newline");
25+
console.verifyOutput(new Options().inline(expected));
26+
}
27+
}
28+
@Test
29+
@UseReporter(AutoApproveReporter.class)
30+
void testVerifyError()
31+
{
32+
var expected = """
33+
Error message
34+
No newline
35+
""";
36+
try (ConsoleOutput console = new ConsoleOutput())
37+
{
38+
System.err.println("Error message");
39+
System.err.print("No ");
40+
System.err.print("newline");
41+
console.verifyError(new Options().inline(expected));
42+
}
43+
}
44+
@Test
45+
@UseReporter(AutoApproveReporter.class)
46+
void testVerifyAll()
47+
{
48+
var expected = """
49+
Output:
50+
Standard output
51+
52+
Error:
53+
Error output
54+
""";
55+
try (ConsoleOutput console = new ConsoleOutput())
56+
{
57+
System.out.println("Standard output");
58+
System.err.println("Error output");
59+
console.verifyAll(new Options().inline(expected));
60+
}
61+
}
62+
}

approvaltests/src/main/java/org/approvaltests/utils/ConsoleOutput.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ public void verifyError(Options options)
8383
{
8484
Approvals.verify(getError(), options);
8585
}
86+
/**
87+
* Verifies both captured standard output and error using ApprovalTests.
88+
* This is a convenience method that calls Approvals.verify() on both the output and error combined.
89+
*/
90+
public void verifyAll()
91+
{
92+
verifyAll(new Options());
93+
}
94+
/**
95+
* Verifies both captured standard output and error using ApprovalTests with options.
96+
* This is a convenience method that calls Approvals.verify() on both the output and error combined.
97+
* @param options The options to use for verification
98+
*/
99+
public void verifyAll(Options options)
100+
{
101+
String combined = "Output:\n" + getOutput() + "\nError:\n" + getError();
102+
Approvals.verify(combined, options);
103+
}
86104
/**
87105
* Restores the original System.out and System.err streams.
88106
* This method is automatically called when used in a try-with-resources block.

internal_documentation/micro_features/console_output.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Allows users to capture and verify console output (both System.out and System.er
1818
3. **getError()**: Returns captured standard error as a string
1919
4. **verifyOutput()**: Convenience method that calls Approvals.verify() on captured output
2020
5. **verifyError()**: Convenience method that calls Approvals.verify() on captured error
21+
5. **verifyAll()**: Convenience method that calls Approvals.verify() on both the output and error
22+
23+
2124
6. **close()**: Restores original System.out and System.err streams
2225

2326
## Usage Example
@@ -30,4 +33,9 @@ try (ConsoleOutput console = new ConsoleOutput()) {
3033

3134
## Integration
3235
- Works seamlessly with ApprovalTests
33-
- Automatically handles stream restoration even if exceptions occur
36+
- Automatically handles stream restoration even if exceptions occur
37+
38+
## Tests
39+
1. Test verifyOutput
40+
2. Test verifyError
41+
3. Test verifyAll

0 commit comments

Comments
 (0)