|
@@ -0,0 +1,130 @@
|
|
|
+/*
|
|
|
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
+ * or more contributor license agreements. Licensed under the Elastic License
|
|
|
+ * 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
+ * 2.0.
|
|
|
+ */
|
|
|
+
|
|
|
+package org.elasticsearch.xpack.esql.expression.function.scalar.string;
|
|
|
+
|
|
|
+import com.carrotsearch.randomizedtesting.annotations.Name;
|
|
|
+import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
|
|
+
|
|
|
+import org.apache.lucene.util.BytesRef;
|
|
|
+import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
|
|
|
+import org.elasticsearch.xpack.esql.expression.function.scalar.AbstractScalarFunctionTestCase;
|
|
|
+import org.elasticsearch.xpack.ql.expression.Expression;
|
|
|
+import org.elasticsearch.xpack.ql.tree.Source;
|
|
|
+import org.elasticsearch.xpack.ql.type.DataType;
|
|
|
+import org.elasticsearch.xpack.ql.type.DataTypes;
|
|
|
+import org.hamcrest.Matcher;
|
|
|
+
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
+
|
|
|
+public class EndsWithTests extends AbstractScalarFunctionTestCase {
|
|
|
+ public EndsWithTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
|
|
|
+ this.testCase = testCaseSupplier.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ParametersFactory
|
|
|
+ public static Iterable<Object[]> parameters() {
|
|
|
+ List<TestCaseSupplier> suppliers = new LinkedList<>();
|
|
|
+ suppliers.add(new TestCaseSupplier("ends_with empty suffix", () -> {
|
|
|
+ String str = randomAlphaOfLength(5);
|
|
|
+ String suffix = "";
|
|
|
+ return new TestCaseSupplier.TestCase(
|
|
|
+ List.of(
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(str), DataTypes.KEYWORD, "str"),
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(suffix), DataTypes.KEYWORD, "suffix")
|
|
|
+ ),
|
|
|
+ "EndsWithEvaluator[str=Attribute[channel=0], suffix=Attribute[channel=1]]",
|
|
|
+ DataTypes.BOOLEAN,
|
|
|
+ equalTo(str.endsWith(suffix))
|
|
|
+ );
|
|
|
+ }));
|
|
|
+ suppliers.add(new TestCaseSupplier("ends_with empty str", () -> {
|
|
|
+ String str = "";
|
|
|
+ String suffix = randomAlphaOfLength(5);
|
|
|
+ return new TestCaseSupplier.TestCase(
|
|
|
+ List.of(
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(str), DataTypes.KEYWORD, "str"),
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(suffix), DataTypes.KEYWORD, "suffix")
|
|
|
+ ),
|
|
|
+ "EndsWithEvaluator[str=Attribute[channel=0], suffix=Attribute[channel=1]]",
|
|
|
+ DataTypes.BOOLEAN,
|
|
|
+ equalTo(str.endsWith(suffix))
|
|
|
+ );
|
|
|
+ }));
|
|
|
+ suppliers.add(new TestCaseSupplier("ends_with one char suffix", () -> {
|
|
|
+ String str = randomAlphaOfLength(5);
|
|
|
+ String suffix = randomAlphaOfLength(1);
|
|
|
+ str = str + suffix;
|
|
|
+
|
|
|
+ return new TestCaseSupplier.TestCase(
|
|
|
+ List.of(
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(str), DataTypes.KEYWORD, "str"),
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(suffix), DataTypes.KEYWORD, "suffix")
|
|
|
+ ),
|
|
|
+ "EndsWithEvaluator[str=Attribute[channel=0], suffix=Attribute[channel=1]]",
|
|
|
+ DataTypes.BOOLEAN,
|
|
|
+ equalTo(str.endsWith(suffix))
|
|
|
+ );
|
|
|
+ }));
|
|
|
+ suppliers.add(new TestCaseSupplier("ends_with no match suffix", () -> {
|
|
|
+ String str = randomAlphaOfLength(5);
|
|
|
+ String suffix = "no_match_suffix";
|
|
|
+ str = suffix + str;
|
|
|
+
|
|
|
+ return new TestCaseSupplier.TestCase(
|
|
|
+ List.of(
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(str), DataTypes.KEYWORD, "str"),
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(suffix), DataTypes.KEYWORD, "suffix")
|
|
|
+ ),
|
|
|
+ "EndsWithEvaluator[str=Attribute[channel=0], suffix=Attribute[channel=1]]",
|
|
|
+ DataTypes.BOOLEAN,
|
|
|
+ equalTo(str.endsWith(suffix))
|
|
|
+ );
|
|
|
+ }));
|
|
|
+ suppliers.add(new TestCaseSupplier("ends_with randomized test", () -> {
|
|
|
+ String str = randomRealisticUnicodeOfLength(5);
|
|
|
+ String suffix = randomRealisticUnicodeOfLength(5);
|
|
|
+ str = str + suffix;
|
|
|
+
|
|
|
+ return new TestCaseSupplier.TestCase(
|
|
|
+ List.of(
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(str), DataTypes.KEYWORD, "str"),
|
|
|
+ new TestCaseSupplier.TypedData(new BytesRef(suffix), DataTypes.KEYWORD, "suffix")
|
|
|
+ ),
|
|
|
+ "EndsWithEvaluator[str=Attribute[channel=0], suffix=Attribute[channel=1]]",
|
|
|
+ DataTypes.BOOLEAN,
|
|
|
+ equalTo(str.endsWith(suffix))
|
|
|
+ );
|
|
|
+ }));
|
|
|
+ return parameterSuppliersFromTypedData(suppliers);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected DataType expectedType(List<DataType> argTypes) {
|
|
|
+ return DataTypes.BOOLEAN;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Matcher<Object> resultsMatcher(List<TestCaseSupplier.TypedData> typedData) {
|
|
|
+ String str = ((BytesRef) typedData.get(0).data()).utf8ToString();
|
|
|
+ String prefix = ((BytesRef) typedData.get(1).data()).utf8ToString();
|
|
|
+ return equalTo(str.endsWith(prefix));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected List<ArgumentSpec> argSpec() {
|
|
|
+ return List.of(required(strings()), required(strings()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Expression build(Source source, List<Expression> args) {
|
|
|
+ return new EndsWith(source, args.get(0), args.get(1));
|
|
|
+ }
|
|
|
+}
|