diff --git a/src/main/java/ortus/boxlang/runtime/bifs/global/format/DollarFormat.java b/src/main/java/ortus/boxlang/runtime/bifs/global/format/DollarFormat.java deleted file mode 100644 index 6a4feb71f..000000000 --- a/src/main/java/ortus/boxlang/runtime/bifs/global/format/DollarFormat.java +++ /dev/null @@ -1,67 +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.format; - -import ortus.boxlang.runtime.bifs.BoxBIF; -import ortus.boxlang.runtime.bifs.BoxMember; -import ortus.boxlang.runtime.context.IBoxContext; -import ortus.boxlang.runtime.dynamic.casters.DoubleCaster; -import ortus.boxlang.runtime.dynamic.casters.StringCaster; -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 ) -public class DollarFormat extends NumberFormat { - - /** - * Constructor - */ - public DollarFormat() { - super(); - declaredArguments = new Argument[] { - new Argument( true, "any", Key.number ) - }; - } - - /** - * Formats a number as a U.S. Dollar string with two decimal places, thousands separator, and a dollar sign. - * If the number is negative, the return value is enclosed in parentheses. - * If the number is an empty string, the function returns "0.00". - * - * @param context The context in which the BIF is being invoked. - * @param arguments Argument scope for the BIF. - * - * @argument.number The number to format as a U.S. Dollar string. - */ - public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { - Object number = arguments.get( Key.number ); - Double value = null; - - if ( number == null || ( number instanceof String && StringCaster.cast( number ).isEmpty() ) ) { - value = 0D; - } else { - value = DoubleCaster.cast( number, true ); - } - arguments.put( Key.number, value ); - arguments.put( Key.mask, "dollarFormat" ); - arguments.put( Key.locale, "US" ); - return super._invoke( context, arguments ); - } - -} diff --git a/src/test/java/ortus/boxlang/runtime/bifs/global/format/DollarFormatTest.java b/src/test/java/ortus/boxlang/runtime/bifs/global/format/DollarFormatTest.java deleted file mode 100644 index 2ad1621c2..000000000 --- a/src/test/java/ortus/boxlang/runtime/bifs/global/format/DollarFormatTest.java +++ /dev/null @@ -1,143 +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.format; - -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 DollarFormatTest { - - 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 formats a positive number as a U.S. Dollar string" ) - @Test - void testItFormatsPositiveNumberAsDollarString() { - instance.executeSource( - """ - result = dollarFormat(12345.67); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "$12,345.67" ); - instance.executeSource( - """ - result = dollarFormat(0.5); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "$0.50" ); - } - - @DisplayName( "It tests the member function Numeric.dollarFormat" ) - @Test - void testNumericMemberFunction() { - instance.executeSource( - """ - number = 12345.67; - result = number.dollarFormat(); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "$12,345.67" ); - instance.executeSource( - """ - result = dollarFormat(0.5); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "$0.50" ); - } - - @DisplayName( "It formats a negative number as a U.S. Dollar string in parentheses" ) - @Test - void testItFormatsNegativeNumberAsDollarStringInParentheses() { - instance.executeSource( - """ - result = dollarFormat(-12345.67); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "($12,345.67)" ); - instance.executeSource( - """ - result = dollarFormat(-0.5); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "($0.50)" ); - } - - @DisplayName( "It formats zero as $0.00" ) - @Test - void testItFormatsZeroAsDollarString() { - instance.executeSource( - """ - result = dollarFormat(0); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "$0.00" ); - } - - @DisplayName( "It formats an empty string as $0.00" ) - @Test - void testItFormatsEmptyStringAsDollarString() { - instance.executeSource( - """ - result = dollarFormat(""); - """, - context ); - assertThat( variables.get( result ) ).isEqualTo( "$0.00" ); - } - - @DisplayName( "It throws an exception when the argument is not a number" ) - @Test - void testItThrowsExceptionWhenArgumentIsNotANumber() { - assertThrows( - BoxRuntimeException.class, - () -> instance.executeSource( - """ - dollarFormat( "foo" ); - """, - context ) - ); - } -}