Skip to content

Commit

Permalink
BL-719 #resolve
Browse files Browse the repository at this point in the history
GetTagData(), GetFunctionData() function not implemented, implement in the COMPAT module
  • Loading branch information
lmajano committed Oct 31, 2024
1 parent 805087f commit 26dc4e3
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `getTagData()` and `getFunctionData()` lucee compats

## [1.10.0] - 2024-10-28

### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* [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.modules.compat.bifs.system;

import ortus.boxlang.modules.compat.util.KeyDictionary;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BIFDescriptor;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.Array;
import ortus.boxlang.runtime.types.Struct;
import ortus.boxlang.runtime.types.exceptions.BoxValidationException;

@BoxBIF
public class GetFunctionData extends BIF {

/**
* Constructor
*/
public GetFunctionData() {
super();
declaredArguments = new Argument[] {
new Argument( true, Argument.STRING, KeyDictionary.functionName )
};
}

/**
* Lucee compat: Return information to of a Function as Struct
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.functionName The BIF function you want information for (Ex: listLen)
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
// Namespace is ignored, they never completed the other ones in lucee
String functionName = arguments.getAsString( KeyDictionary.functionName );
BIFDescriptor descriptor = functionService.getGlobalFunction( functionName );

if ( descriptor == null ) {
// Throw Validation Exception
throw new BoxValidationException( "Function not found: " + functionName );
}

return Struct.of(
"name", functionName,
"status", "implemented",
"description", "",
"keywords", new Array(),
"returnType", "Any",
"argumentType", "fixed",
"argMin", 0,
"argMax", -1,
"type", "java",
"memeber", Struct.of(),
"arguments", descriptor.getArguments()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* [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.modules.compat.bifs.system;

import ortus.boxlang.modules.compat.util.KeyDictionary;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.components.ComponentDescriptor;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.types.Struct;
import ortus.boxlang.runtime.types.exceptions.BoxValidationException;

@BoxBIF
public class GetTagData extends BIF {

/**
* Constructor
*/
public GetTagData() {
super();
declaredArguments = new Argument[] {
new Argument( true, Argument.STRING, KeyDictionary.nameSpaceWithSeperator ),
new Argument( true, Argument.STRING, KeyDictionary.tagName )
};
}

/**
* Lucee compat: Return information to a Tag as Struct
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
* @argument.namespaceWithSeperator The namespace of the tag (Ex: cf)
*
* @argument.tagName The name of the tag (Ex: mail, http, dbinfo, etc)
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
// Namespace is ignored, they never completed the other ones in lucee
String tagName = arguments.getAsString( KeyDictionary.tagName );
ComponentDescriptor descriptor = componentService.getComponent( tagName );

if ( descriptor == null ) {
// Throw Validation Exception
throw new BoxValidationException( "Tag not found: " + tagName );
}

return Struct.of(
"nameSpaceSeperator", "",
"nameSpace", "cf",
"name", tagName,
"description", "",
"status", "implemented",
"attributeType", "fixed",
"parseBody", descriptor.allowsBody(),
"bodyType", descriptor.requiresBody() ? "required" : "free",
"attrMin", 0,
"attrMax", 0,
"hasNameAppendix", false,
"attributeCollection", true,
"type", "java",
"attributes", descriptor.getAttributes()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public class KeyDictionary {
public static final Key objectType = Key.of( "objectType" );
public static final Key moduleName = Key.of( "compat-cfml" );
public static final Key jsonEscapeControlCharacters = Key.of( "jsonEscapeControlCharacters" );
public static final Key nameSpaceWithSeperator = Key.of( "nameSpaceWithSeperator" );
public static final Key tagName = Key.of( "tagName" );
public static final Key functionName = Key.of( "functionName" );

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* [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.modules.compat.bifs.system;

import static com.google.common.truth.Truth.assertThat;

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.IStruct;

public class GetFunctionDataTest {

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

@BeforeAll
public static void setUp() {
instance = BoxRuntime.getInstance( true );
}

@BeforeEach
public void setupEach() {
context = new ScriptingRequestBoxContext( instance.getRuntimeContext() );
variables = context.getScopeNearby( VariablesScope.name );
}

@DisplayName( "It returns tag data" )
@Test
public void testBIF() {
instance.executeSource(
"""
result = getFunctionData( "listLen" )
println( result )
""",
context );
IStruct data = variables.getAsStruct( result );
assertThat( data.get( "name" ) ).isEqualTo( "listLen" );
assertThat( data.get( "description" ) ).isEqualTo( "" );
assertThat( data.get( "status" ) ).isEqualTo( "implemented" );
assertThat( data.get( "argMin" ) ).isEqualTo( 0 );
assertThat( data.get( "argMax" ) ).isEqualTo( -1 );
assertThat( data.get( "type" ) ).isEqualTo( "java" );
assertThat( data.getAsStruct( Key.of( "arguments" ) ) ).isNotEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* [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.modules.compat.bifs.system;

import static com.google.common.truth.Truth.assertThat;

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.IStruct;

public class GetTagDataTest {

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

@BeforeAll
public static void setUp() {
instance = BoxRuntime.getInstance( true );
}

@BeforeEach
public void setupEach() {
context = new ScriptingRequestBoxContext( instance.getRuntimeContext() );
variables = context.getScopeNearby( VariablesScope.name );
}

@DisplayName( "It returns tag data" )
@Test
public void testBIF() {
instance.executeSource(
"""
result = getTagData( "cf", "dbinfo" )
println( result )
""",
context );
IStruct data = variables.getAsStruct( result );
assertThat( data.get( "nameSpaceSeperator" ) ).isEqualTo( "" );
assertThat( data.get( "nameSpace" ) ).isEqualTo( "cf" );
assertThat( data.get( "name" ) ).isEqualTo( "dbinfo" );
assertThat( data.get( "description" ) ).isEqualTo( "" );
assertThat( data.get( "status" ) ).isEqualTo( "implemented" );
assertThat( data.get( "attributeType" ) ).isEqualTo( "fixed" );
assertThat( data.get( "parseBody" ) ).isEqualTo( false );
assertThat( data.get( "bodyType" ) ).isEqualTo( "free" );
assertThat( data.get( "attrMin" ) ).isEqualTo( 0 );
assertThat( data.get( "attrMax" ) ).isEqualTo( 0 );
assertThat( data.get( "hasNameAppendix" ) ).isEqualTo( false );
assertThat( data.get( "attributeCollection" ) ).isEqualTo( true );
assertThat( data.get( "type" ) ).isEqualTo( "java" );
assertThat( data.getAsStruct( Key.of( "attributes" ) ) ).isNotEmpty();
}

}

0 comments on commit 26dc4e3

Please sign in to comment.