Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api;
12
13 import static java.util.stream.Collectors.joining;
14
15 import java.util.Deque;
16 import java.util.function.Supplier;
17
18 import org.junit.platform.commons.util.UnrecoverableExceptions;
19 import org.opentest4j.AssertionFailedError;
20
21 /**
22 * {@code AssertionUtils} is a collection of utility methods that are common to
23 * all assertion implementations.
24 *
25 * @since 5.0
26 */
27 class AssertionUtils {
28
29 private AssertionUtils() {
30 /* no-op */
31 }
32
33 static void fail() {
34 throw new AssertionFailedError();
35 }
36
37 static void fail(String message) {
38 throw new AssertionFailedError(message);
39 }
40
41 static void fail(String message, Throwable cause) {
42 throw new AssertionFailedError(message, cause);
43 }
44
45 static void fail(Throwable cause) {
46 throw new AssertionFailedError(null, cause);
47 }
48
49 static void fail(Supplier<String> messageSupplier) {
50 throw new AssertionFailedError(nullSafeGet(messageSupplier));
51 }
52
53 static String nullSafeGet(Supplier<String> messageSupplier) {
54 return (messageSupplier != null ? messageSupplier.get() : null);
55 }
56
57 static String getCanonicalName(Class<?> clazz) {
58 try {
59 String canonicalName = clazz.getCanonicalName();
60 return (canonicalName != null ? canonicalName : clazz.getName());
61 }
62 catch (Throwable t) {
63 UnrecoverableExceptions.rethrowIfUnrecoverable(t);
64 return clazz.getName();
65 }
66 }
67
68 static String formatIndexes(Deque<Integer> indexes) {
69 if (indexes == null || indexes.isEmpty()) {
70 return "";
71 }
72 String indexesString = indexes.stream().map(Object::toString).collect(joining("][", "[", "]"));
73 return " at index " + indexesString;
74 }
75
76 static boolean floatsAreEqual(float value1, float value2, float delta) {
77 assertValidDelta(delta);
78 return floatsAreEqual(value1, value2) || Math.abs(value1 - value2) <= delta;
79 }
80
81 static void assertValidDelta(float delta) {
82 if (Float.isNaN(delta) || delta < 0.0) {
83 failIllegalDelta(String.valueOf(delta));
84 }
85 }
86
87 static void assertValidDelta(double delta) {
88 if (Double.isNaN(delta) || delta < 0.0) {
89 failIllegalDelta(String.valueOf(delta));
90 }
91 }
92
93 static boolean floatsAreEqual(float value1, float value2) {
94 return Float.floatToIntBits(value1) == Float.floatToIntBits(value2);
95 }
96
97 static boolean doublesAreEqual(double value1, double value2, double delta) {
98 assertValidDelta(delta);
99 return doublesAreEqual(value1, value2) || Math.abs(value1 - value2) <= delta;
100 }
101
102 static boolean doublesAreEqual(double value1, double value2) {
103 return Double.doubleToLongBits(value1) == Double.doubleToLongBits(value2);
104 }
105
106 static boolean objectsAreEqual(Object obj1, Object obj2) {
107 if (obj1 == null) {
108 return (obj2 == null);
109 }
110 return obj1.equals(obj2);
111 }
112
113 private static void failIllegalDelta(String delta) {
114 fail("positive delta expected but was: <" + delta + ">");
115 }
116
117 }