From 29a1e155b609f82aaa23cdebc865cf7de0b84d91 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Fri, 22 Mar 2019 14:22:41 -0700 Subject: [PATCH] Add simple test of index-agnostic QueryBuilder Change-Id: I570410f5e32a7306017330c7455d5d19bcc530eb --- .../gerrit/index/query/QueryBuilderTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 javatests/com/google/gerrit/index/query/QueryBuilderTest.java diff --git a/javatests/com/google/gerrit/index/query/QueryBuilderTest.java b/javatests/com/google/gerrit/index/query/QueryBuilderTest.java new file mode 100644 index 0000000000..10fa0e8b91 --- /dev/null +++ b/javatests/com/google/gerrit/index/query/QueryBuilderTest.java @@ -0,0 +1,90 @@ +// Copyright (C) 2019 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.index.query; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.truth.ThrowableSubject; +import com.google.gerrit.testing.GerritBaseTests; +import java.util.Collection; +import java.util.Objects; +import org.junit.Test; + +public class QueryBuilderTest extends GerritBaseTests { + private static class TestPredicate extends Predicate { + private final String field; + private final String value; + + TestPredicate(String field, String value) { + this.field = field; + this.value = value; + } + + @Override + public Predicate copy(Collection> children) { + throw new UnsupportedOperationException(); + } + + @Override + public int hashCode() { + return Objects.hash(field, value); + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof TestPredicate)) { + return false; + } + TestPredicate p = (TestPredicate) o; + return Objects.equals(field, p.field) && Objects.equals(value, p.value); + } + } + + private static class TestQueryBuilder extends QueryBuilder { + TestQueryBuilder() { + super(new QueryBuilder.Definition<>(TestQueryBuilder.class)); + } + + @Operator + public Predicate a(String value) { + return new TestPredicate("a", value); + } + } + + @Test + public void fieldNameAndValue() throws Exception { + assertThat(parse("a:foo")).isEqualTo(new TestPredicate("a", "foo")); + } + + @Test + public void fieldWithParenthesizedValues() throws Exception { + assertThatParseException("a:(foo bar)") + .hasMessageThat() + .isEqualTo("line 1:2 no viable alternative at input '('"); + } + + private static Predicate parse(String query) throws Exception { + return new TestQueryBuilder().parse(query); + } + + private static ThrowableSubject assertThatParseException(String query) { + try { + new TestQueryBuilder().parse(query); + throw new AssertionError("expected QueryParseException for " + query); + } catch (QueryParseException e) { + return assertThat(e); + } + } +}