From b1fd37664f993c7fe51c2ab92de58dd08e8a7b2c Mon Sep 17 00:00:00 2001 From: minhna1112 Date: Wed, 12 Jul 2023 17:43:25 +0700 Subject: [PATCH 1/3] Add PHP interface --- src/codetext/parser/php_parser.py | 4 +++- tests/test_parser/test_php.py | 18 ++++++++++----- .../test_sample/php_test_sample.php | 22 +++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/codetext/parser/php_parser.py b/src/codetext/parser/php_parser.py index b2a8ef2..9068f68 100644 --- a/src/codetext/parser/php_parser.py +++ b/src/codetext/parser/php_parser.py @@ -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 diff --git a/tests/test_parser/test_php.py b/tests/test_parser/test_php.py index 2d2d526..1e2058a 100755 --- a/tests/test_parser/test_php.py +++ b/tests/test_parser/test_php.py @@ -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 = """ @@ -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__': diff --git a/tests/test_parser/test_sample/php_test_sample.php b/tests/test_parser/test_sample/php_test_sample.php index 716ee0a..6301e17 100755 --- a/tests/test_parser/test_sample/php_test_sample.php +++ b/tests/test_parser/test_sample/php_test_sample.php @@ -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; + } + +} + From b8a87b64c79c21723d02c01f66ecd0479ce99f10 Mon Sep 17 00:00:00 2001 From: minhna1112 Date: Thu, 13 Jul 2023 10:17:19 +0700 Subject: [PATCH 2/3] Add singleton method --- src/codetext/parser/ruby_parser.py | 5 +++-- tests/test_parser/test_ruby.py | 14 +++++++++++--- tests/test_parser/test_sample/ruby_test_sample.rb | 6 ++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/codetext/parser/ruby_parser.py b/src/codetext/parser/ruby_parser.py index 93ee79d..bbb3cd1 100644 --- a/src/codetext/parser/ruby_parser.py +++ b/src/codetext/parser/ruby_parser.py @@ -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 @@ -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': diff --git a/tests/test_parser/test_ruby.py b/tests/test_parser/test_ruby.py index 848d4a2..95fe92e 100755 --- a/tests/test_parser/test_ruby.py +++ b/tests/test_parser/test_ruby.py @@ -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 @@ -76,8 +76,8 @@ 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()) @@ -85,6 +85,14 @@ def test_get_function_metadata(self): 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'], '') + def test_metadata_without_return_statement(self): code_sample = ''' diff --git a/tests/test_parser/test_sample/ruby_test_sample.rb b/tests/test_parser/test_sample/ruby_test_sample.rb index 1c82cc8..381dbf4 100755 --- a/tests/test_parser/test_sample/ruby_test_sample.rb +++ b/tests/test_parser/test_sample/ruby_test_sample.rb @@ -32,6 +32,12 @@ 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 From 40c2a67ff6d2ac50afd787b1d058e7c66eb76d77 Mon Sep 17 00:00:00 2001 From: minhna1112 Date: Thu, 13 Jul 2023 11:27:27 +0700 Subject: [PATCH 3/3] Add Ruby actions with block parameters --- src/codetext/parser/ruby_parser.py | 36 +++++++++++++++++ tests/test_parser/test_ruby.py | 20 ++++++++++ .../test_sample/ruby_test_sample.rb | 40 +++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/src/codetext/parser/ruby_parser.py b/src/codetext/parser/ruby_parser.py index bbb3cd1..e9b5823 100644 --- a/src/codetext/parser/ruby_parser.py +++ b/src/codetext/parser/ruby_parser.py @@ -134,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 + diff --git a/tests/test_parser/test_ruby.py b/tests/test_parser/test_ruby.py index 95fe92e..dc898a8 100755 --- a/tests/test_parser/test_ruby.py +++ b/tests/test_parser/test_ruby.py @@ -122,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() diff --git a/tests/test_parser/test_sample/ruby_test_sample.rb b/tests/test_parser/test_sample/ruby_test_sample.rb index 381dbf4..acaf425 100755 --- a/tests/test_parser/test_sample/ruby_test_sample.rb +++ b/tests/test_parser/test_sample/ruby_test_sample.rb @@ -42,4 +42,44 @@ def self.my_method(a) 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 \ No newline at end of file