Skip to content

Commit

Permalink
Remove temp code in favor of actual BIFs
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Jan 1, 2024
1 parent 7e9d014 commit e73b606
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* [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.string;

import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.bifs.BoxMember;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.BoxLangType;

@BoxBIF
@BoxMember( type = BoxLangType.STRING )
public class YesNoFormat extends BIF {

/**
* Constructor
*/
public YesNoFormat() {
super();
declaredArguments = new Argument[] {
new Argument( true, "boolean", Key.value ),
};
}

/**
*
* Return Yes/No based on whether the input is true/false
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.value The value to check for true/false and return Yes/No
*
*/
public Object invoke( IBoxContext context, ArgumentsScope arguments ) {
return arguments.getAsBoolean( Key.value ) ? "Yes" : "No";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1138,16 +1138,6 @@ public static Object dereferenceAndInvoke( Class<?> targetClass, Object targetIn
}
}

// member functions on java objects
// temp workaround for test src\test\java\TestCases\phase2\ObjectLiteralTest.java
if ( targetInstance instanceof Boolean bool && name.equals( Key.of( "yesNoFormat" ) ) ) {
return bool ? "Yes" : "No";
}
// temp workaround for test src\test\java\TestCases\phase2\ObjectLiteralTest.java
if ( targetInstance instanceof String str && name.equals( Key.of( "len" ) ) ) {
return str.length();
}

if ( safe && !hasMethod( targetClass, name.getName() ) ) {
return null;
}
Expand Down Expand Up @@ -1190,16 +1180,6 @@ public static Object dereferenceAndInvoke( Class<?> targetClass, Object targetIn
}
}

// member functions on java objects
// temp workaround for test src\test\java\TestCases\phase2\ObjectLiteralTest.java
if ( targetInstance instanceof Boolean bool && name.equals( Key.of( "yesNoFormat" ) ) ) {
return bool ? "Yes" : "No";
}
// temp workaround for test src\test\java\TestCases\phase2\ObjectLiteralTest.java
if ( targetInstance instanceof String str && name.equals( Key.of( "len" ) ) ) {
return str.length();
}

throw new BoxRuntimeException( "Java objects cannot be called with named argumments" );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* [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.string;

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.ScriptingBoxContext;
import ortus.boxlang.runtime.scopes.VariablesScope;

public class YesNoFormatTest {

static BoxRuntime instance;
static IBoxContext context;
static VariablesScope variables;

@BeforeAll
public static void setUp() {
instance = BoxRuntime.getInstance( true );
context = new ScriptingBoxContext( instance.getRuntimeContext() );
variables = ( VariablesScope ) context.getScopeNearby( VariablesScope.name );
}

@AfterAll
public static void teardown() {
instance.shutdown();
}

@BeforeEach
public void setupEach() {
variables.clear();
}

@DisplayName( "It works" )
@Test
public void testItWorks() {
assertThat( instance.executeStatement( "yesNoFormat( 'true' )", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "yesNoFormat( true )", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "yesNoFormat( 1 )", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "yesNoFormat( 999 )", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "yesNoFormat( 'yes' )", context ) ).isEqualTo( "Yes" );

assertThat( instance.executeStatement( "'true'.yesNoFormat()", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "true.yesNoFormat()", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "(1).yesNoFormat()", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "(999).yesNoFormat()", context ) ).isEqualTo( "Yes" );
assertThat( instance.executeStatement( "'yes'.yesNoFormat()", context ) ).isEqualTo( "Yes" );

assertThat( instance.executeStatement( "yesNoFormat( 'false' )", context ) ).isEqualTo( "No" );
assertThat( instance.executeStatement( "yesNoFormat( false )", context ) ).isEqualTo( "No" );
assertThat( instance.executeStatement( "yesNoFormat( 0 )", context ) ).isEqualTo( "No" );
assertThat( instance.executeStatement( "yesNoFormat( 'no' )", context ) ).isEqualTo( "No" );

assertThat( instance.executeStatement( "'false'.yesNoFormat()", context ) ).isEqualTo( "No" );
assertThat( instance.executeStatement( "false.yesNoFormat()", context ) ).isEqualTo( "No" );
assertThat( instance.executeStatement( "(0).yesNoFormat()", context ) ).isEqualTo( "No" );
assertThat( instance.executeStatement( "'no'.yesNoFormat()", context ) ).isEqualTo( "No" );
}

}

0 comments on commit e73b606

Please sign in to comment.