-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of round( number ) and number.round()
- Loading branch information
1 parent
7ab9dd4
commit 08e648f
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/ortus/boxlang/runtime/bifs/global/math/Round.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/test/java/ortus/boxlang/runtime/bifs/global/math/RoundTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
} |