001/*
002 * Copyright (C) 2022 - 2024, the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.github.ascopes.jct.assertions;
017
018import io.github.ascopes.jct.repr.StackTraceRepresentation;
019import java.util.ArrayList;
020import java.util.List;
021import org.assertj.core.api.AbstractListAssert;
022import org.jspecify.annotations.Nullable;
023
024/**
025 * Assertions for a list of {@link StackTraceElement stack trace frames}.
026 *
027 * <p>This type is a placeholder and will be replaced when AssertJ releases changes to
028 * support assertions on stack traces.
029 *
030 * @author Ashley Scopes
031 * @since 0.0.1
032 */
033public final class StackTraceAssert
034    extends AbstractListAssert<StackTraceAssert, List<? extends StackTraceElement>, StackTraceElement, StackTraceElementAssert> {
035
036  /**
037   * Initialize a new assertions object.
038   *
039   * @param actual the list of stack trace elements to assert upon.
040   */
041  @SuppressWarnings("DataFlowIssue")
042  public StackTraceAssert(@Nullable List<? extends StackTraceElement> actual) {
043    super(actual, StackTraceAssert.class);
044    info.useRepresentation(StackTraceRepresentation.getInstance());
045  }
046
047  @Override
048  protected StackTraceElementAssert toAssert(StackTraceElement value, String description) {
049    return new StackTraceElementAssert(value).describedAs(description);
050  }
051
052  @Override
053  protected StackTraceAssert newAbstractIterableAssert(
054      Iterable<? extends StackTraceElement> iterable
055  ) {
056    var list = new ArrayList<StackTraceElement>();
057    iterable.forEach(list::add);
058    return new StackTraceAssert(list);
059  }
060}
061