From 01faa3c5e10e329e25e7be9bb6da13ef9429cbf1 Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Fri, 19 Apr 2024 11:47:42 +0200 Subject: [PATCH] Lua tests and fix for excessive binary_expression calculations (#16) * Basic tests for Lua and commenting out of rule not working properly * Verify implementation of nesting logic for Lua * Reintroduce binary_expression due to regular logical operations calc not working * Add simple Lua test for logical operators * Rewrite Lua binary expression code to make tests pass --- Eask | 1 + codemetrics.el | 19 +++++--- test/lua-test.el | 91 +++++++++++++++++++++++++++++++++++ test/lua/LogicalOperators.lua | 5 ++ test/lua/Nesting.lua | 23 +++++++++ test/lua/Recursion.lua | 7 +++ test/lua/Simple.lua | 10 ++++ 7 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 test/lua-test.el create mode 100644 test/lua/LogicalOperators.lua create mode 100644 test/lua/Nesting.lua create mode 100644 test/lua/Recursion.lua create mode 100644 test/lua/Simple.lua diff --git a/Eask b/Eask index 4f21d1b..7aa9e6d 100644 --- a/Eask +++ b/Eask @@ -22,6 +22,7 @@ (development (depends-on "kotlin-mode") (depends-on "go-mode") + (depends-on "lua-mode") (depends-on "rust-mode") (depends-on "typescript-mode") (depends-on "tree-sitter-langs")) diff --git a/codemetrics.el b/codemetrics.el index b2fd724..c107267 100644 --- a/codemetrics.el +++ b/codemetrics.el @@ -491,15 +491,22 @@ For argument NODE, see function `codemetrics-analyze' for more information." '(0 nil))) (defun codemetrics-rules--lua-binary-expressions (node &rest _) - "Define rule for Lua binary expressions. + "Define rule for Lua binary expressions, which includes logical operators. For argument NODE, see function `codemetrics-analyze' for more information." (codemetrics-with-complexity - (let ((matches (codemetrics--tsc-find-children-traverse node "binary_expression")) - (sequence nil)) - (when (<= 2 (length matches)) - (setq sequence t)) - (list (if sequence 1 0) nil)) + (let* ((node-is-logical-operator (lambda (node) + (-contains? '("and" "or") + ;; binary_expressions contain 3 elements; two expressions and one middle string + (tsc-node-text (tsc-get-nth-child node 1))))) + (matches (codemetrics--tsc-find-children node "binary_expression")) + (has-child-logical-operator (-any (lambda (x) (funcall node-is-logical-operator x)) + matches)) + (self-is-logical-operator (funcall node-is-logical-operator node))) + (list (if (and self-is-logical-operator has-child-logical-operator) + 1 + 0) + nil)) '(1 nil))) (defun codemetrics-rules--ruby-binary (node &rest _) diff --git a/test/lua-test.el b/test/lua-test.el new file mode 100644 index 0000000..2347ad1 --- /dev/null +++ b/test/lua-test.el @@ -0,0 +1,91 @@ +;;; lua-test.el --- Lua language tests for codemetrics.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Marie Katrine Ekeberg + +;; Author: Marie Katrine Ekeberg + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: +;; + +;;; Code: +(require 'codemetrics) +(require 'lua-mode) + +(codemetrics-test lua-simple + "test/lua/Simple.lua" + lua-mode + '(3 + (function_declaration . 0) + (binary_expression . 0) + (function_call . 0) + (binary_expression . 0) + (function_call . 0) + (for_statement . 1) + (if_statement . 2) + (binary_expression . 0))) + +(codemetrics-test lua-recursion + "test/lua/Recursion.lua" + lua-mode + '(2 + (function_declaration . 0) + (if_statement . 1) + (binary_expression . 0) + (binary_expression . 0) + (function_call . 1) + (binary_expression . 0))) + +(codemetrics-test lua-nesting + "test/lua/Nesting.lua" + lua-mode + '(7 + (while_statement . 1) + (binary_expression . 0) + (if_statement . 2) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (repeat_statement . 2) + (binary_expression . 0) + (binary_expression . 0) + (if_statement . 1) + (binary_expression . 0) + (goto_statement . 1))) + +(codemetrics-test lua-logical-operators + "test/lua/LogicalOperators.lua" + lua-mode + '(2 + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 1) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 1) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0) + (binary_expression . 0))) + + + +;;; lua-test.el ends here diff --git a/test/lua/LogicalOperators.lua b/test/lua/LogicalOperators.lua new file mode 100644 index 0000000..5dae2b2 --- /dev/null +++ b/test/lua/LogicalOperators.lua @@ -0,0 +1,5 @@ +local simple_and = 1 == 1 and 1 == 2 +local simple_or = 1 == 1 or 1 == 2 + +local mix_one = 2 == 2 and 1 == 1 or 2 == 3 +local mix_two = 1 == 2 or 2 == 3 and 3 == 3 diff --git a/test/lua/Nesting.lua b/test/lua/Nesting.lua new file mode 100644 index 0000000..4f8e172 --- /dev/null +++ b/test/lua/Nesting.lua @@ -0,0 +1,23 @@ +-- Testing various statements and nesting of those +local my_arr = { 1, 3, 5, 10, 23 } +local i = 1 +local some_value = 0 + +::beginning_flow:: +while i < 6 do + if my_arr[i] % 2 == 0 then + some_value = some_value + 1 + break + end + + local j = 0 + repeat + j = j + 1 + until j == 10 +end + + + +if 10 < some_value then + goto beginning_flow +end diff --git a/test/lua/Recursion.lua b/test/lua/Recursion.lua new file mode 100644 index 0000000..913af5c --- /dev/null +++ b/test/lua/Recursion.lua @@ -0,0 +1,7 @@ +function factorial(n) + if 1 <= n then + return 1 + end + + return n * factorial(n - 1) +end diff --git a/test/lua/Simple.lua b/test/lua/Simple.lua new file mode 100644 index 0000000..c6d5cdb --- /dev/null +++ b/test/lua/Simple.lua @@ -0,0 +1,10 @@ +function square(n) + return n * n +end + +print("Square of 3: " .. square(3)) + +for i=1,10 do + if 1 == i then + end +end