• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
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 android.tools.flicker.assertions
18 
19 import android.tools.function.AssertionPredicate
20 
21 /**
22  * Utility class to store assertions with an identifier to help generate more useful debug data when
23  * dealing with multiple assertions.
24  *
25  * @param predicate Assertion to execute
26  * @param name Assertion name
27  * @param isOptional If the assertion is optional (can fail) or not (must pass)
28  */
29 open class NamedAssertion<T>(
30     val predicate: AssertionPredicate<T>,
31     override val name: String,
32     override val isOptional: Boolean = false,
33 ) : Assertion<T> {
invokenull34     override operator fun invoke(target: T) = predicate.verify(target)
35 
36     override fun toString(): String = "Assertion($name)${if (isOptional) "[optional]" else ""}"
37 
38     /**
39      * We can't check the actual assertion is the same. We are checking for the name, which should
40      * have a 1:1 correspondence with the assertion, but there is no actual guarantee of the same
41      * execution of the assertion even if isEqual() is true.
42      */
43     override fun equals(other: Any?): Boolean {
44         if (other !is NamedAssertion<*>) {
45             return false
46         }
47         if (name != other.name) {
48             return false
49         }
50         if (isOptional != other.isOptional) {
51             return false
52         }
53         return true
54     }
55 
hashCodenull56     override fun hashCode(): Int {
57         var result = predicate.hashCode()
58         result = 31 * result + name.hashCode()
59         result = 31 * result + isOptional.hashCode()
60         return result
61     }
62 }
63