Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.index.query.testing; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : import static com.google.common.truth.Truth.assertAbout; 19 : import static java.util.Objects.requireNonNull; 20 : 21 : import com.google.common.truth.FailureMetadata; 22 : import com.google.common.truth.Subject; 23 : import com.google.gerrit.index.query.QueryParser; 24 : import org.antlr.runtime.tree.Tree; 25 : 26 : public class TreeSubject extends Subject { 27 : public static TreeSubject assertThat(Tree actual) { 28 1 : return assertAbout(TreeSubject::new).that(actual); 29 : } 30 : 31 : private final Tree tree; 32 : 33 : private TreeSubject(FailureMetadata failureMetadata, Tree tree) { 34 1 : super(failureMetadata, tree); 35 1 : this.tree = tree; 36 1 : } 37 : 38 : public void hasType(int expectedType) { 39 1 : isNotNull(); 40 1 : check("getType()").that(typeName(tree.getType())).isEqualTo(typeName(expectedType)); 41 1 : } 42 : 43 : public void hasText(String expectedText) { 44 1 : requireNonNull(expectedText); 45 1 : isNotNull(); 46 1 : check("getText()").that(tree.getText()).isEqualTo(expectedText); 47 1 : } 48 : 49 : public void hasNoChildren() { 50 1 : isNotNull(); 51 1 : check("getChildCount()").that(tree.getChildCount()).isEqualTo(0); 52 1 : } 53 : 54 : public void hasChildCount(int expectedChildCount) { 55 1 : checkArgument( 56 : expectedChildCount > 0, "expected child count must be positive: %s", expectedChildCount); 57 1 : isNotNull(); 58 1 : check("getChildCount()").that(tree.getChildCount()).isEqualTo(expectedChildCount); 59 1 : } 60 : 61 : public TreeSubject child(int childIndex) { 62 1 : isNotNull(); 63 1 : return check("getChild(%s)", childIndex) 64 1 : .about(TreeSubject::new) 65 1 : .that(tree.getChild(childIndex)); 66 : } 67 : 68 : private static String typeName(int type) { 69 1 : checkArgument( 70 : type >= 0 && type < QueryParser.tokenNames.length, 71 : "invalid token type %s, max is %s", 72 : type, 73 : QueryParser.tokenNames.length - 1); 74 1 : return QueryParser.tokenNames[type]; 75 : } 76 : }