Skip to content

Commit

Permalink
Merge pull request #4 from FSoft-AI4Code/dev/extended
Browse files Browse the repository at this point in the history
Dev/extended
  • Loading branch information
minhna1112 authored Jul 13, 2023
2 parents 026a807 + 40c2a67 commit 66616c4
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/codetext/parser/php_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def get_comment_node(function_node):

@staticmethod
def get_class_list(node):
res = get_node_by_kind(node, ['class_declaration', 'trait_declaration'])
res = get_node_by_kind(node, ['class_declaration',
'trait_declaration',
'interface_declaration'])
return res

@staticmethod
Expand Down
41 changes: 39 additions & 2 deletions src/codetext/parser/ruby_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class RubyParser(LanguageParser):

@staticmethod
def get_function_list(node):
res = get_node_by_kind(node, ['method'])
res = get_node_by_kind(node, ['method',
'singleton_method'])
return res

@staticmethod
Expand Down Expand Up @@ -88,7 +89,7 @@ def get_function_metadata(function_node, blob=None) -> Dict[str, str]:
}

assert type(function_node) == tree_sitter.Node
assert function_node.type == 'method'
assert function_node.type in ['method', 'singleton_method']

for child in function_node.children:
if child.type == 'identifier':
Expand Down Expand Up @@ -133,3 +134,39 @@ def get_class_metadata(class_node, blob=None):
def get_comment_node(function_node):
comment_node = get_node_by_kind(function_node, kind='comment')
return comment_node

@staticmethod
def get_action_list(action_node):
call_nodes = get_node_by_kind(action_node, ['call'])
res = []
for call_node in call_nodes:
if get_node_by_kind(call_node, ["do_block"]):
res.append(call_node)
# print(res)
return res

@staticmethod
def get_action_metadata(action_node):
metadata = {
'identifier': '',
'parameters': {},
'return_type': None,
}

for child in action_node.children:
if child.type in ["identifier"]:
metadata['identifier'] = get_node_text(child)
if child.type in ["argument_list"]:
symbol = get_node_by_kind(child, ["simple_symbol"])
if symbol:
metadata['identifier'] += get_node_text(symbol[0])

parameters = get_node_by_kind(action_node, ["block_parameters"])

if parameters:
for param in get_node_by_kind(parameters[0], ["identifier"]):
param_name = get_node_text(param)
metadata['parameters'].update({param_name : None})

return metadata

18 changes: 12 additions & 6 deletions tests/test_parser/test_php.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def test_get_function_list(self):

function_list = PhpParser.get_function_list(root)

self.assertEqual(len(function_list), 3)
self.assertEqual(len(function_list), 5)

def test_get_class_list(self):
root = self.root_node

class_list = PhpParser.get_class_list(root)

self.assertEqual(len(class_list), 1)
self.assertEqual(len(class_list), 3)

def test_get_docstring(self):
code_sample = """
Expand Down Expand Up @@ -104,11 +104,17 @@ def test_metadata_without_return_statement(self):
def test_get_class_metadata(self):
root = self.root_node

classes = list(PhpParser.get_class_list(root))[0]
metadata = PhpParser.get_class_metadata(classes)
_class, interface, trait = list(PhpParser.get_class_list(root))
class_metadata = PhpParser.get_class_metadata(_class)

self.assertEqual(metadata['parameters'], {'AbstractSQLServerDriver': None})
self.assertEqual(metadata['identifier'], 'Driver')
self.assertEqual(class_metadata['parameters'], {'AbstractSQLServerDriver': None})
self.assertEqual(class_metadata['identifier'], 'Driver')

interface_metadata = PhpParser.get_class_metadata(interface)
self.assertEqual(interface_metadata['identifier'], 'MyInterface')

trait_metadata = PhpParser.get_class_metadata(trait)
self.assertEqual(trait_metadata['identifier'], 'MyTrait')


if __name__ == '__main__':
Expand Down
34 changes: 31 additions & 3 deletions tests/test_parser/test_ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_get_function_list(self):

function_list = RubyParser.get_function_list(root)

self.assertEqual(len(function_list), 1)
self.assertEqual(len(function_list), 2)

def test_get_class_list(self):
root = self.root_node
Expand Down Expand Up @@ -76,15 +76,23 @@ def squeeze
def test_get_function_metadata(self):
root = self.root_node

function = RubyParser.get_function_list(root)[0]
metadata = RubyParser.get_function_metadata(function)
_function = RubyParser.get_function_list(root)[0]
metadata = RubyParser.get_function_metadata(_function)

for key in ['identifier', 'parameters', 'return_type']:
self.assertTrue(key in metadata.keys())
self.assertEqual(metadata['identifier'], 'search')
self.assertEqual(metadata['parameters'], {'query': None, 'options': None})
self.assertEqual(metadata['return_type'], None)

_singleton = RubyParser.get_function_list(root)[1]
metadata = RubyParser.get_function_metadata(_singleton)
for key in ['identifier', 'parameters', 'return_type']:
self.assertTrue(key in metadata.keys())
self.assertEqual(metadata['identifier'], 'my_method')
self.assertEqual(metadata['parameters'], {'a': None})
self.assertEqual(metadata['return_type'], '<not_specific>')


def test_metadata_without_return_statement(self):
code_sample = '''
Expand Down Expand Up @@ -114,6 +122,26 @@ def test_get_class_metadata(self):
self.assertEqual(metadata['identifier'], 'Client')
self.assertEqual(metadata['parameters'], {'API': None})

def test_get_action_list(self):
root = self.root_node
actions = RubyParser.get_action_list(root)

self.assertEqual(len(actions), 5)

def test_get_action_metadata(self):
root = self.root_node
actions = RubyParser.get_action_list(root)
metadatas = [ RubyParser.get_action_metadata(action) for action in actions]
self.assertEqual(metadatas[0]["identifier"], "load_current_value")
self.assertEqual(metadatas[1]["identifier"], "action:install")
self.assertEqual(metadatas[2]["identifier"], "converge_by")

self.assertEqual(metadatas[3]["identifier"], "action:reinstall")
self.assertEqual(metadatas[4]["identifier"], "converge_by")

self.assertEqual(metadatas[0]["parameters"]["new_resource"], None)
self.assertEqual(metadatas[0]["parameters"]["old_resource"], None)


if __name__ == '__main__':
unittest.main()
22 changes: 22 additions & 0 deletions tests/test_parser/test_sample/php_test_sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ private function getConnectionOptionsDsn(array $connectionOptions): string
return $connectionOptionsDsn;
}
}

interface MyInterface {
public function myMethod() {
// Method implementation
}

}

trait MyTrait {

public function setBackgroundImage(Drawing $objDrawing): self
{
if (!array_key_exists($objDrawing->getType(), Drawing::IMAGE_TYPES_CONVERTION_MAP)) {
throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
}
$this->backgroundImage = $objDrawing;

return $this;
}

}

46 changes: 46 additions & 0 deletions tests/test_parser/test_sample/ruby_test_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,54 @@ def search(query, options = {})

objects_from_response(:get, path, parameters)
end

def self.my_method(a)
# Method implementation
puts(a)
return a
end

end
end
end

load_current_value do |new_resource, old_resource|
unless current_installed_version(new_resource).nil?
version(current_installed_version(new_resource))
Chef::Log.debug("Current version is #{version}") if version
return a
end
end

action :install do
build_essential

install_version = new_resource.version unless new_resource.version.nil? || new_resource.version == current_resource.version
versions_match = candidate_version == current_installed_version(new_resource)

if install_version || new_resource.version.nil? && !versions_match
converge_by("install package #{new_resource.package_name} #{install_version}") do
info_output = "Installing #{new_resource.package_name}"
info_output << " version #{install_version}" if install_version && !install_version.empty?
Chef::Log.info(info_output)
install_package(new_resource.package_name, install_version)
end
end
end

action :reinstall do
build_essential

install_version = new_resource.version unless new_resource.version.nil?
converge_by("reinstall package #{new_resource.package_name} #{install_version}") do
info_output = "Installing #{new_resource.package_name}"
info_output << " version #{install_version}" if install_version && !install_version.empty?
Chef::Log.info(info_output)
install_package(new_resource.package_name, install_version, force: true)
end
end

a = 1

reinstall

0 comments on commit 66616c4

Please sign in to comment.