package jetbrains.mps.lang.test.junit5;

/*Generated by MPS */

import org.junit.platform.launcher.TestExecutionListener;
import java.util.List;
import java.util.ArrayList;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.engine.TestExecutionResult;
import jetbrains.mps.lang.test.launcher.WorkerCallback;

public class FailureDetector implements TestExecutionListener {
  private List<String> errors = new ArrayList<>();

  @Override
  public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
    if (testExecutionResult.getStatus() == TestExecutionResult.Status.FAILED) {
      errors.add("Test failure: " + testIdentifier.getDisplayName());
    }
  }

  public boolean hasFailures() {
    return !(errors.isEmpty());
  }

  public int failuresCount() {
    return errors.size();
  }

  public void flushErrors(WorkerCallback callback) {
    for (String err : errors) {
      callback.error(err);
    }
  }
}
