Skip to content

Commit

Permalink
Initial implementation of round( number ) and number.round()
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcopley committed Jan 12, 2024
1 parent 7ab9dd4 commit 08e648f
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/main/java/ortus/boxlang/runtime/bifs/global/math/Round.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* [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.math;

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.NUMERIC, name = "Round" )
public class Round extends BIF {

/**
* Constructor
*/
public Round() {
super();
declaredArguments = new Argument[] {
new Argument( true, "numeric", Key.number )
};
}

/**
* Rounds a number to the closest integer.
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.number The number to be rounded.
*/
public Object invoke( IBoxContext context, ArgumentsScope arguments ) {
double value = arguments.getAsDouble( Key.number );
return StrictMath.round( value );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* [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.math;

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

public class RoundTest {

static BoxRuntime instance;
static IBoxContext context;
static IScope variables;
static Key result = new Key( "result" );

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

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

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

@DisplayName( "It rounds a number to the closest integer" )
@Test
public void testItRoundsToClosestInteger() {
instance.executeSource(
"""
result = round(0.3);
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.round( 0.3 ) );

instance.executeSource(
"""
result = round(1.7);
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.round( 1.7 ) );
}

@DisplayName( "It rounds a number to the closest integer as member" )
@Test
public void testItRoundsToClosestIntegerMember() {
instance.executeSource(
"""
result = (0.3).round();
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.round( 0.3 ) );

instance.executeSource(
"""
result = (1.7).round();
""",
context );
assertThat( variables.get( result ) ).isEqualTo( StrictMath.round( 1.7 ) );
}
}

0 comments on commit 08e648f

Please sign in to comment.