|
| 1 | +;;; test_ai-code-change.el --- Tests for ai-code-change.el -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Author: Kang Tu <tninja@gmail.com> |
| 4 | +;; SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +;;; Commentary: |
| 7 | +;; Tests for the ai-code-change module, specifically testing |
| 8 | +;; the function detection logic for TODO comments. |
| 9 | + |
| 10 | +;;; Code: |
| 11 | + |
| 12 | +(require 'ert) |
| 13 | +(require 'ai-code-change) |
| 14 | + |
| 15 | +(ert-deftest test-ai-code--get-function-name-for-comment-basic () |
| 16 | + "Test function name detection when on a comment line before function body. |
| 17 | +This simulates the Ruby example from the issue where a TODO comment |
| 18 | +is between the function definition and its body." |
| 19 | + (with-temp-buffer |
| 20 | + ;; Simulate Ruby mode comment syntax |
| 21 | + (setq-local comment-start "# ") |
| 22 | + (insert "module Foo\n") |
| 23 | + (insert " class Bar\n") |
| 24 | + (insert " def baz\n") |
| 25 | + (insert " end\n") |
| 26 | + (insert "\n") |
| 27 | + (insert " # TODO remove this function\n") ;; Line 6 - cursor will be here |
| 28 | + (insert " def click_first_available(driver, selectors)\n") |
| 29 | + (insert " wait = Selenium::WebDriver::Wait.new(timeout: 10)\n") |
| 30 | + (insert " end\n") |
| 31 | + (insert " end\n") |
| 32 | + (insert "end\n") |
| 33 | + ;; Move cursor to the TODO comment line (line 6) |
| 34 | + (goto-char (point-min)) |
| 35 | + (forward-line 5) ;; Move 5 lines forward from line 1 to reach line 6 |
| 36 | + ;; Mock which-function to simulate the actual behavior |
| 37 | + ;; When on line 6, which-function might return "Bar" (class) |
| 38 | + ;; When on line 7 (def line), it should return "Bar#click_first_available" |
| 39 | + (cl-letf (((symbol-function 'which-function) |
| 40 | + (lambda () |
| 41 | + (save-excursion |
| 42 | + (let ((line-num (line-number-at-pos (point)))) |
| 43 | + (cond |
| 44 | + ((= line-num 6) "Bar") ;; On comment, returns class |
| 45 | + ((= line-num 7) "Bar") ;; On def, still returns class |
| 46 | + ((>= line-num 8) "Bar#click_first_available") ;; Inside method body |
| 47 | + (t nil))))))) |
| 48 | + ;; Test that on the comment line, we get the correct function name |
| 49 | + (let ((result (ai-code--get-function-name-for-comment))) |
| 50 | + (should (string= result "Bar#click_first_available")))))) |
| 51 | + |
| 52 | +(ert-deftest test-ai-code--get-function-name-for-comment-no-function () |
| 53 | + "Test function name detection when comment is not followed by a function." |
| 54 | + (with-temp-buffer |
| 55 | + (setq-local comment-start "# ") |
| 56 | + (insert "# TODO some task\n") |
| 57 | + (insert "x = 1\n") |
| 58 | + (goto-char (point-min)) |
| 59 | + (cl-letf (((symbol-function 'which-function) (lambda () nil))) |
| 60 | + (let ((result (ai-code--get-function-name-for-comment))) |
| 61 | + (should (null result)))))) |
| 62 | + |
| 63 | +(ert-deftest test-ai-code--get-function-name-for-comment-multiple-comments () |
| 64 | + "Test function name detection with multiple comment lines before function." |
| 65 | + (with-temp-buffer |
| 66 | + (setq-local comment-start "# ") |
| 67 | + (insert " # TODO task 1\n") ;; Line 1 - cursor here |
| 68 | + (insert " # TODO task 2\n") ;; Line 2 |
| 69 | + (insert " def my_function()\n") ;; Line 3 |
| 70 | + (insert " x = 1\n") |
| 71 | + (insert " end\n") |
| 72 | + (goto-char (point-min)) |
| 73 | + ;; Mock which-function |
| 74 | + (cl-letf (((symbol-function 'which-function) |
| 75 | + (lambda () |
| 76 | + (save-excursion |
| 77 | + (let ((line-num (line-number-at-pos (point)))) |
| 78 | + (cond |
| 79 | + ((<= line-num 2) nil) ;; On comments, no function context |
| 80 | + ((>= line-num 3) "my_function") ;; On/in function |
| 81 | + (t nil))))))) |
| 82 | + (let ((result (ai-code--get-function-name-for-comment))) |
| 83 | + (should (string= result "my_function")))))) |
| 84 | + |
| 85 | +(ert-deftest test-ai-code--get-function-name-for-comment-same-function () |
| 86 | + "Test that when comment and next line are in same function, we get that function." |
| 87 | + (with-temp-buffer |
| 88 | + (setq-local comment-start "# ") |
| 89 | + (insert " def my_function()\n") ;; Line 1 |
| 90 | + (insert " # TODO implement this\n") ;; Line 2 - cursor here |
| 91 | + (insert " x = 1\n") ;; Line 3 |
| 92 | + (insert " end\n") |
| 93 | + (goto-char (point-min)) |
| 94 | + (forward-line 1) ;; Move 1 line forward from line 1 to reach line 2 (the comment) |
| 95 | + ;; Mock which-function - both lines return same function |
| 96 | + (cl-letf (((symbol-function 'which-function) (lambda () "my_function"))) |
| 97 | + (let ((result (ai-code--get-function-name-for-comment))) |
| 98 | + (should (string= result "my_function")))))) |
| 99 | + |
| 100 | +(ert-deftest test-ai-code--is-comment-line () |
| 101 | + "Test comment line detection." |
| 102 | + ;; Test with hash comment |
| 103 | + (let ((comment-start "# ")) |
| 104 | + (should (ai-code--is-comment-line "# This is a comment")) |
| 105 | + (should (ai-code--is-comment-line " # This is an indented comment")) |
| 106 | + (should (ai-code--is-comment-line "## Multiple hashes")) |
| 107 | + (should-not (ai-code--is-comment-line "This is not a comment")) |
| 108 | + (should-not (ai-code--is-comment-line " x = 1 # inline comment"))) |
| 109 | + ;; Test with semicolon comment (Lisp) |
| 110 | + (let ((comment-start "; ")) |
| 111 | + (should (ai-code--is-comment-line "; This is a comment")) |
| 112 | + (should (ai-code--is-comment-line " ;; This is a comment")) |
| 113 | + (should-not (ai-code--is-comment-line "This is not a comment"))) |
| 114 | + ;; Test with double slash comment (C/Java) |
| 115 | + (let ((comment-start "// ")) |
| 116 | + (should (ai-code--is-comment-line "// This is a comment")) |
| 117 | + (should (ai-code--is-comment-line " // This is an indented comment")) |
| 118 | + (should-not (ai-code--is-comment-line "This is not a comment")))) |
| 119 | + |
| 120 | +(provide 'test_ai-code-change) |
| 121 | + |
| 122 | +;;; test_ai-code-change.el ends here |
0 commit comments