From 66833fc61ff9e2014f2639031e77aa8cfc9d4278 Mon Sep 17 00:00:00 2001 From: Luis Majano Date: Mon, 20 May 2024 22:07:13 +0200 Subject: [PATCH] moving contains arrays and lists to the compat module --- .../runtime/bifs/global/array/ArrayFind.java | 36 +---- .../runtime/bifs/global/list/ListFind.java | 4 - .../ortus/boxlang/runtime/scopes/Key.java | 6 - src/test/java/TestCases/ScratchPad.java | 9 +- .../global/array/ArrayContainsNoCaseTest.java | 114 --------------- .../bifs/global/array/ArrayContainsTest.java | 136 ------------------ 6 files changed, 11 insertions(+), 294 deletions(-) delete mode 100644 src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsNoCaseTest.java delete mode 100644 src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsTest.java diff --git a/src/main/java/ortus/boxlang/runtime/bifs/global/array/ArrayFind.java b/src/main/java/ortus/boxlang/runtime/bifs/global/array/ArrayFind.java index 671083c15..73f950dc3 100644 --- a/src/main/java/ortus/boxlang/runtime/bifs/global/array/ArrayFind.java +++ b/src/main/java/ortus/boxlang/runtime/bifs/global/array/ArrayFind.java @@ -31,26 +31,8 @@ @BoxBIF( alias = "ArrayFindNoCase" ) @BoxMember( type = BoxLangType.ARRAY ) @BoxMember( type = BoxLangType.ARRAY, name = "findNoCase" ) -@BoxBIF( alias = "ArrayContains" ) -@BoxBIF( alias = "ArrayContainsNoCase" ) -@BoxMember( type = BoxLangType.ARRAY, name = "contains" ) -@BoxMember( type = BoxLangType.ARRAY, name = "containsNoCase" ) public class ArrayFind extends BIF { - /** - * These are the functions that return boolean instead of the index - */ - private static final Array booleanReturnFunctions = new Array( - new Object[] { - Key.arrayContains, - Key.arrayContainsNoCase, - Key.contains, - Key.containsNoCase, - Key.listContains, - Key.listContainsNoCase - } - ); - /** * Constructor */ @@ -73,11 +55,6 @@ public ArrayFind() { * @function.arrayFindNoCase This function searches the array for the specified value. Returns the index in the array of the first match, or 0 if * there is no match. The search is case insensitive. * - * @function.arrayContains This function searches the array for the specified value. Returns a boolean indicating if the value was found or not. - * - * @function.arrayContainsNoCase This function searches the array for the specified value. Returns a boolean indicating if the value was found or not. - * The search is case insensitive. - * * @param context The context in which the BIF is being invoked. * @param arguments Argument scope for the BIF. * @@ -95,17 +72,18 @@ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { Object value = arguments.get( Key.value ); Boolean substringMatch = arguments.getAsBoolean( Key.substringMatch ); + // This case might exist. If it does, we need to set it to false + if ( substringMatch == null ) { + substringMatch = false; + } + // Go search by function or by value - Integer indexFound = value instanceof Function + return value instanceof Function castedValueFunction // Search by function - ? actualArray.findIndex( ( Function ) value, context ) + ? actualArray.findIndex( castedValueFunction, context ) // Search by value or by substring : ( substringMatch ? actualArray.findIndexWithSubstring( value, isCaseSensitive( bifMethodKey ) ) : actualArray.findIndex( value, isCaseSensitive( bifMethodKey ) ) ); - - // If the function is a boolean return function, return a boolean - // Else the index - return booleanReturnFunctions.contains( bifMethodKey ) ? indexFound > 0 : indexFound; } /** diff --git a/src/main/java/ortus/boxlang/runtime/bifs/global/list/ListFind.java b/src/main/java/ortus/boxlang/runtime/bifs/global/list/ListFind.java index e84ab4960..91b75a8fa 100644 --- a/src/main/java/ortus/boxlang/runtime/bifs/global/list/ListFind.java +++ b/src/main/java/ortus/boxlang/runtime/bifs/global/list/ListFind.java @@ -31,10 +31,6 @@ @BoxBIF( alias = "ListFindNoCase" ) @BoxMember( type = BoxLangType.STRING, name = "listFind" ) @BoxMember( type = BoxLangType.STRING, name = "listFindNoCase" ) -@BoxBIF( alias = "ListContains" ) -@BoxBIF( alias = "ListContainsNoCase" ) -@BoxMember( type = BoxLangType.STRING, name = "listContains" ) -@BoxMember( type = BoxLangType.STRING, name = "listContainsNoCase" ) public class ListFind extends ArrayFind { /** diff --git a/src/main/java/ortus/boxlang/runtime/scopes/Key.java b/src/main/java/ortus/boxlang/runtime/scopes/Key.java index 555d3289d..6c76f8c26 100644 --- a/src/main/java/ortus/boxlang/runtime/scopes/Key.java +++ b/src/main/java/ortus/boxlang/runtime/scopes/Key.java @@ -90,8 +90,6 @@ public class Key implements Comparable, Serializable { public static final Key array2 = Key.of( "array2" ); public static final Key arrayFind = Key.of( "arrayFind" ); public static final Key arrayFindAll = Key.of( "arrayFindAll" ); - public static final Key arrayContains = Key.of( "arrayContains" ); - public static final Key arrayContainsNoCase = Key.of( "arrayContainsNoCase" ); public static final Key asOptional = Key.of( "asOptional" ); public static final Key assocAttribs = Key.of( "assocAttribs" ); public static final Key asyncService = Key.of( "asyncService" ); @@ -152,8 +150,6 @@ public class Key implements Comparable, Serializable { public static final Key component = Key.of( "component" ); public static final Key condition = Key.of( "condition" ); public static final Key configure = Key.of( "configure" ); - public static final Key contains = Key.of( "contains" ); - public static final Key containsNoCase = Key.of( "containsNoCase" ); public static final Key content = Key.of( "content" ); public static final Key context = Key.of( "context" ); public static final Key contextual = Key.of( "contextual" ); @@ -333,8 +329,6 @@ public class Key implements Comparable, Serializable { public static final Key limit = Key.of( "limit" ); public static final Key line = Key.of( "line" ); public static final Key list = Key.of( "list" ); - public static final Key listContains = Key.of( "listContains" ); - public static final Key listContainsNoCase = Key.of( "listContainsNoCase" ); public static final Key listInfo = Key.of( "listInfo" ); public static final Key listToJSON = Key.of( "listToJSON" ); public static final Key lJustify = Key.of( "lJustify" ); diff --git a/src/test/java/TestCases/ScratchPad.java b/src/test/java/TestCases/ScratchPad.java index 974d6cebc..41dc8b497 100644 --- a/src/test/java/TestCases/ScratchPad.java +++ b/src/test/java/TestCases/ScratchPad.java @@ -59,13 +59,12 @@ void testIt() { // @formatter:off instance.executeSource( """ - cl = createObject("java","java.net.URLClassLoader"); - test = cl.getClass().getName(); + test = [ 1, 2 ,3 ] + for( item, index in test ) { + println( item ) + } """, context); // @formatter:on - - var result = variables.get( resultKey ); - System.out.println( result ); } } diff --git a/src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsNoCaseTest.java b/src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsNoCaseTest.java deleted file mode 100644 index 5a9249d1a..000000000 --- a/src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsNoCaseTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/** - * [BoxLang] - * - * Copyright [2023] [Ortus Solutions, Corp] - * - * 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 ortus.boxlang.runtime.bifs.global.array; - -import static com.google.common.truth.Truth.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import ortus.boxlang.runtime.BoxRuntime; -import ortus.boxlang.runtime.context.IBoxContext; -import ortus.boxlang.runtime.context.ScriptingRequestBoxContext; -import ortus.boxlang.runtime.scopes.IScope; -import ortus.boxlang.runtime.scopes.Key; -import ortus.boxlang.runtime.scopes.VariablesScope; -import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException; - -public class ArrayContainsNoCaseTest { - - static BoxRuntime instance; - IBoxContext context; - IScope variables; - static Key result = new Key( "result" ); - - @BeforeAll - public static void setUp() { - instance = BoxRuntime.getInstance( true ); - } - - @AfterAll - public static void teardown() { - - } - - @BeforeEach - public void setupEach() { - context = new ScriptingRequestBoxContext( instance.getRuntimeContext() ); - variables = context.getScopeNearby( VariablesScope.name ); - } - - @DisplayName( "It can search" ) - @Test - public void testCanSearch() { - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arrayContainsNoCase( arr, 'b' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arrayContainsNoCase( arr, 'B' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - } - - @DisplayName( "Can now search with a function" ) - @Test - public void testCanSearchFunction() { - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arrayContainsNoCase( arr, i->i=="b" ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - } - - @DisplayName( "It can search member" ) - @Test - public void testCanSearchMember() { - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arr.containsNoCase( 'b' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arr.containsNoCase( 'B' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - } -} diff --git a/src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsTest.java b/src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsTest.java deleted file mode 100644 index 5a5369d25..000000000 --- a/src/test/java/ortus/boxlang/runtime/bifs/global/array/ArrayContainsTest.java +++ /dev/null @@ -1,136 +0,0 @@ -/** - * [BoxLang] - * - * Copyright [2023] [Ortus Solutions, Corp] - * - * 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 ortus.boxlang.runtime.bifs.global.array; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import ortus.boxlang.runtime.BoxRuntime; -import ortus.boxlang.runtime.context.IBoxContext; -import ortus.boxlang.runtime.context.ScriptingRequestBoxContext; -import ortus.boxlang.runtime.scopes.IScope; -import ortus.boxlang.runtime.scopes.Key; -import ortus.boxlang.runtime.scopes.VariablesScope; - -public class ArrayContainsTest { - - static BoxRuntime instance; - IBoxContext context; - IScope variables; - static Key result = new Key( "result" ); - - @BeforeAll - public static void setUp() { - instance = BoxRuntime.getInstance( true ); - } - - @AfterAll - public static void teardown() { - - } - - @BeforeEach - public void setupEach() { - context = new ScriptingRequestBoxContext( instance.getRuntimeContext() ); - variables = context.getScopeNearby( VariablesScope.name ); - } - - @DisplayName( "It can search" ) - @Test - public void testCanSearch() { - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arrayContains( arr, 'b' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arrayContains( arr, 'B' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( false ); - } - - @DisplayName( "Can now search with a function" ) - @Test - public void testCanSearchFunction() { - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arrayContains( arr, i->i=="b" ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - } - - @DisplayName( "It can search member" ) - @Test - public void testCanSearchMember() { - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arr.contains( 'b' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( true ); - - instance.executeSource( - """ - arr = [ 'a', 'b', 'c' ]; - result = arr.contains( 'B' ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( false ); - } - - @DisplayName( "Can find a substring" ) - @Test - public void testCanFindSubstring() { - instance.executeSource( - """ - arr = [ "hello", "world" ]; - result = arr.find( "el", true ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( 1 ); - } - - @DisplayName( "Can find a substring with case-insensitivity" ) - @Test - public void testCanFindSubstringCaseInsensitive() { - instance.executeSource( - """ - arr = [ "hello", "world" ]; - result = arr.findNoCase( "EL", true ); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( 1 ); - } - -}