FiveAM 库的 Lisp 单元测试错误:无法导入函数

问题描述 投票:0回答:1

我目前正在开发一个 Common Lisp 项目,并面临单元测试的问题。我有一个名为

lec_2.lisp
的文件,其中包含一个函数
COUNT-VC-DO
,用于计算给定字符串中的元音和辅音。但是,当我运行单元测试时,遇到错误,指出
COUNT-VC-DO
函数未定义。我已确认文件路径正确并且该函数已在文件中定义。我正在使用
FiveAM
库通过 Quicklisp 进行单元测试。

目录树:

src/
├── lec_2.lisp
└── tests/
     └── test_lec_2.lisp

这是来自

lec_2.lisp
的函数定义:

(defun count-vc-do (s)
    ; Initialize loop variables i, accv, and accc
    (do ((i 0 (1+ i))
         (accv 0)
         (accc 0))
        ; Terminate loop when i reaches the length of s
        ((equal i (length s)) 
         (values accv accc)) ; Return the values of accv and accc
        ; Check if the current character is a vowel
        (if (vowelp (aref s i))
            ; Increment accv if the character is a vowel
            (incf accv)
            ; Check if the current character is a consonant
            (if (consonantp (aref s i))
                ; Increment accc if the character is a consonant
                (incf accc)))))

这是失败的单元测试:

;; Load the FiveAM library
(asdf:test-system "fiveam")

;; Load the file with the functions that need testing
(load "/src/lec_2.lisp")  ; Replace with the actual file path

;; Define a test suite
(defpackage :my-tests
  (:use :cl :fiveam))

(in-package :my-tests)

(test count-vc-do-test
    (is (= (count-vc-do "example") '(3 4)))) ; 3 vowels and 4 consonants in "example"

(run! 'count-vc-do-test)

这是我收到的错误消息:

| Running test COUNT-VC-DO-TEST X
| Did 1 check.
|    Pass: 0 ( 0%)
|    Skip: 0 ( 0%)
|    Fail: 1 (100%)
|
| Failure Details:
| --------------------------------
| COUNT-VC-DO-TEST [A test for the count-vc-do function.]:
|      Unexpected Error: #<UNDEFINED-FUNCTION COUNT-VC-DO {1002E45DE3}>
| The function COUNT-VC-DO is undefined..
| --------------------------------

我已经尝试重新加载文件并确保函数名称或测试中没有拼写错误。文件已正确加载,我可以毫无问题地调用 REPL 中的函数。然而,单元测试仍然失败,并出现同样的错误。

testing lisp common-lisp
1个回答
0
投票

在测试中,尝试拨打

cl-user::count-vc-do

如果您向我们展示了包含函数定义的文件

lec_2.lisp
的完整内容,则该函数将在 CL-USER 包(默认包)中创建。

但是在您的测试文件中,您创建了一个包 - 这很好,它有助于封装。

;; here the current package is CL-USER

(defpackage :my-tests
  (:use :cl :fiveam)) ;; <-- you create a package: good idea.

(in-package :my-tests) 

;; now the current package is this one and not CL-USER

(test count-vc-do-test
    (is (= (count-vc-do "example") '(3 4))))
;;          ^^^^
;;          Your Lisp is looking for this function in the my-tests package
;;          and it, rightly, doesn't exist.

请使用包前缀 (cl-user) 和双冒号 (::) 引用该函数,因为您的函数不是

export
-ed,

要么在第一个文件中创建一个包,又有两个选择:

  • 导出函数和
    use
    这个包,就像你对 Fiveam 所做的那样
  • 或者也使用包前缀(带或不带导出函数)。

您不导出的函数不会通过

:use
“导入”,但您仍然可以使用 :: 符号来引用它们。


https://lispcookbook.github.io/cl-cookbook/packages.html

© www.soinside.com 2019 - 2024. All rights reserved.