1 /* 2 * Copyright 2023 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.code_intelligence.jazzer.mutation.api; 18 19 import static java.util.Collections.newSetFromMap; 20 import static java.util.Objects.requireNonNull; 21 22 import java.util.IdentityHashMap; 23 import java.util.Set; 24 import java.util.function.Predicate; 25 26 public interface Debuggable { 27 /** 28 * Returns a string representation of the object that is meant to be used to make assertions about 29 * its structure in tests. 30 * 31 * @param isInCycle evaluates to {@code true} if a cycle has been detected during recursive calls 32 * of this function. Must be called at most once with {@code this} as the single 33 * argument. Implementing classes that know that their current instance can never 34 * be contained in recursive substructures need not call this method. 35 */ toDebugString(Predicate<Debuggable> isInCycle)36 String toDebugString(Predicate<Debuggable> isInCycle); 37 38 /** 39 * Returns a string representation of the given {@link Debuggable} that is meant to be used to 40 * make assertions about its structure in tests. 41 */ getDebugString(Debuggable debuggable)42 static String getDebugString(Debuggable debuggable) { 43 Set<Debuggable> seen = newSetFromMap(new IdentityHashMap<>()); 44 return debuggable.toDebugString(child -> !seen.add(requireNonNull(child))); 45 } 46 } 47