Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests to verify JavaScript, (some) TypeScript, and Rust calculations #15

Merged
merged 5 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Eask
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
(development
(depends-on "kotlin-mode")
(depends-on "go-mode")
(depends-on "rust-mode")
(depends-on "typescript-mode")
(depends-on "tree-sitter-langs"))
2 changes: 1 addition & 1 deletion codemetrics-rules.el
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
(defun codemetrics-rules-javascript ()
"Return rules for JavaScript."
`((function_declaration . codemetrics-rules--method-declaration)
(function . (0 t)) ; traditional anonymous function
(function_expression . (0 t)) ; traditional anonymous function
(arrow_function . (0 t)) ; don't score, but increase nested level
(if_statement . (1 t))
(switch_statement . (1 t))
Expand Down
69 changes: 69 additions & 0 deletions test/javascript-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
;;; javascript-test.el --- JavaScript language tests for codemetrics.el -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Marie Katrine Ekeberg

;; Author: Marie Katrine Ekeberg <[email protected]>

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:
;;

;;; Code:
(require 'codemetrics)

(codemetrics-test javascript-simple
"test/javascript/Simple.js"
js-mode
'(3
(function_declaration . 0)
(call_expression . 0)
(call_expression . 0)
(for_statement . 1)
(if_statement . 2)))

(codemetrics-test javascript-recursion
"test/javascript/Recursion.js"
js-mode
'(2
(function_declaration . 0)
(if_statement . 1)
(call_expression . 1)))

(codemetrics-test javascript-nesting
"test/javascript/Nesting.js"
js-mode
'(4
(call_expression . 0)
(arrow_function . 0)
(call_expression . 0)
(call_expression . 0)
(function_expression . 0)
(if_statement . 2)
(arrow_function . 0)
(if_statement . 2)
(call_expression . 0)))

(codemetrics-test javascript-logical-operators
"test/javascript/LogicalOperators.js"
js-mode
'(2
("&&" . 0)
("||" . 0)
("&&" . 0)
("||" . 1)
("||" . 1)
("&&" . 0)))

;;; javascript-test.el ends here
5 changes: 5 additions & 0 deletions test/javascript/LogicalOperators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let importantCheck = (1 == 1) && (3 != 3);
let otherCheck = 1 || 2;

let anotherCheck = 1 && 3 || 4;
let lastCheck = 2 || 3 && 4;
14 changes: 14 additions & 0 deletions test/javascript/Nesting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let myArr = [1, 3, 5, 10, 23];
let doubledArr = myArr.map(elem => elem * 2);

// Some bogus code to check combination of anonymous functions and nesting
doubledArr.filter(function(elem) {
if(elem % 3 == 0) {
return elem < 2;
}
return elem >= 10;
}).forEach(elem => {
if(elem % 10 == 0) {
console.log("Condition met");
}
});
7 changes: 7 additions & 0 deletions test/javascript/Recursion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function factorial(num) {
if (num <= 1) {
return 1;
}

return num * factorial(num - 1);
}
9 changes: 9 additions & 0 deletions test/javascript/Simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function sayHi(name) {
console.log(`Hi ${name}`);
}

sayHi("Dude");

for (var i = 0; i < 10; i++) {
if(i == 0) {}
}
95 changes: 95 additions & 0 deletions test/rust-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
;;; rust-test.el --- Rust language tests for codemetrics.el -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Marie Katrine Ekeberg

;; Author: Marie Katrine Ekeberg <[email protected]>

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:
;;

;;; Code:
(require 'codemetrics)
(require 'rust-mode)

(codemetrics-test rust-simple
"test/rust/Simple.rs"
rust-mode
'(2
(function_item . 0)
(function_item . 0)
(call_expression . 0)
(macro_invocation . 0)
(if_expression . 1)
(for_expression . 1)))

(codemetrics-test rust-recursion
"test/rust/Recursion.rs"
rust-mode
'(2
(function_item . 0)
(if_expression . 1)
(call_expression . 1)))

(codemetrics-test rust-nesting
"test/rust/Nesting.rs"
rust-mode
'(5
(function_item . 0)
(macro_invocation . 0)
(call_expression . 0)
(call_expression . 0)
(call_expression . 0)
(closure_expression . 0)
(closure_expression . 0)
(match_expression . 2)
(loop_expression . 1)
(if_expression . 2)))

(codemetrics-test rust-logical-operators
"test/rust/LogicalOperators.rs"
rust-mode
'(6
(function_item . 0)
(if_expression . 1)
("&&" . 0)
(if_expression . 1)
("||" . 0)
(if_expression . 1)
("&&" . 0)
("||" . 1)
(if_expression . 1)
("||" . 1)
("&&" . 0)))

(codemetrics-test rust-control-flow
"test/rust/ControlFlow.rs"
rust-mode
'(17
(function_item . 0)
(loop_expression . 1)
(loop_expression . 2)
(if_expression . 3)
(break_expression . 1)
(if_expression . 3)
(continue_expression . 0)
(macro_invocation . 0)
(for_expression . 1)
(loop_expression . 2)
(if_expression . 3)
(continue_expression . 1)
(break_expression . 0)))

;;; rust-test.el ends here
26 changes: 26 additions & 0 deletions test/rust/ControlFlow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn main() {
let mut my_val = 0;
'outer: loop {
loop {
my_val += 1;
if 15 == my_val {
break 'outer;
}

if 5 == my_val {
continue;
}
}
}

let my_vec = vec![1, 2, 3, 4];
'outer_for: for some_val in my_vec {
loop {
if some_val == 2 {
continue 'outer_for;
} else {
break;
}
}
}
}
7 changes: 7 additions & 0 deletions test/rust/LogicalOperators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
if 1 && 1 {}
if 2 || 1 {}

if 1 && 1 || 2 {}
if 2 || 1 && 1 {}
}
13 changes: 13 additions & 0 deletions test/rust/Nesting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
let nums = vec![1, 2, 3, 4, 128, 1024];
nums.iter()
.map(|elem| elem * 3)
.for_each(|elem| match elem {
3 | 9 => {}
_ => {}
});

loop {
if 1 == 3 {}
}
}
7 changes: 7 additions & 0 deletions test/rust/Recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn factorial(n: u32) -> u32 {
if n <= 1 {
return 1;
}

n * factorial(n - 1)
}
12 changes: 12 additions & 0 deletions test/rust/Simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn square(x: i32) -> i32 {
x * x
}

fn main() {
let three_squared = square(3);
println!("Here goes some text - {three_squared}");

if 9 == three_squared {}

for i in 0..10 {}
}
37 changes: 37 additions & 0 deletions test/typescript-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;;; typescript-test.el --- TypeScript language tests for codemetrics.el -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Marie Katrine Ekeberg

;; Author: Marie Katrine Ekeberg <[email protected]>

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:
;;

;;; Code:
(require 'codemetrics)
(require 'typescript-mode)

(codemetrics-test typescript-simple
"test/typescript/Simple.ts"
typescript-mode
'(1
(class_declaration . 0)
(method_definition . 0)
(call_expression . 0)
(method_definition . 0)
(if_statement . 1)))

;;; typescript-test.el ends here
13 changes: 13 additions & 0 deletions test/typescript/Simple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class MyClass {
constructor(arg) {
console.log(`Initializing ${arg}`);
}

myMethod(n: number): number {
if(n > 10) {
return 10;
}

return n;
}
}
Loading