Console.java
/*
* Copyright (c) 2020, Stein Eldar Johnsen
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package net.morimekta.testing.console;
import net.morimekta.io.tty.TTY;
import net.morimekta.io.tty.TTYSize;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* Interface for controlling and inspecting console I/O during testing. Provides access
* to a fake {@link TTY}, captured stdout/stderr content, and allows setting stdin
* programmatically. Framework-specific implementations are provided via
* {@link ConsoleManager} and the JUnit 4/5 integration modules.
*/
public interface Console {
/**
* Get the fake TTY that reflects the state of the testing console (terminal size,
* interactivity, and TTY mode).
*
* @return The testing TTY.
*/
TTY tty();
/**
* Get everything written to stdout since the last reset.
*
* @return The captured standard output as a string.
*/
String output();
/**
* Get everything written to stderr since the last reset.
*
* @return The captured error output as a string.
*/
String error();
/**
* Set input to return the given bytes.
*
* @param in The bytes for input.
*/
void setInput(byte[] in);
/**
* Set input with dynamic content.
*
* @param in The input values.
*/
void setInput(Object... in);
/**
* Create an output stream that will feed the input stream. Handy
* for testing interactive input.
*
* @return The controlling output stream.
*/
OutputStream createInputSource();
/**
* Get the input stream that replaces {@link System#in} during testing.
*
* @return The fake stdin stream.
*/
InputStream getConsoleIn();
/**
* Get the print stream that replaces {@link System#out} during testing.
*
* @return The fake stdout stream.
*/
PrintStream getConsoleOut();
/**
* Get the print stream that replaces {@link System#err} during testing.
*
* @return The fake stderr stream.
*/
PrintStream getConsoleErr();
/**
* Reset all the streams for the console.
*/
void reset();
/** The default number of rows in a fake TTY. */
int DEFAULT_ROWS = 25;
/** The default number of cols in a fake TTY. */
int DEFAULT_COLS = 144;
/** The default terminal size of a fake TTY. */
TTYSize DEFAULT_TERMINAL_SIZE = new TTYSize(DEFAULT_ROWS, DEFAULT_COLS);
}