代码之家  ›  专栏  ›  技术社区  ›  Erik Öjebo

是否有一个通用Lisp的模拟/存根框架?

  •  11
  • Erik Öjebo  · 技术社区  · 14 年前

    是否有一个通用Lisp的模拟/存根框架?

    emacslispmock看起来很好,但它是一个emacs-lisp框架,我正在寻找从普通lisp中使用的东西。

    有什么建议吗?

    7 回复  |  直到 7 年前
        1
  •  4
  •   6502    14 年前

    (defmacro with-replaced-function (fdef &rest body)
      (let ((oldf (gensym))
            (result (gensym))
            (name (car fdef))
            (args (cadr fdef))
            (rbody (cddr fdef)))
        `(let ((,oldf (symbol-function ',name)))
           (setf (symbol-function ',name) (lambda ,args ,@rbody))
           (let ((,result (progn ,@body)))
             (setf (symbol-function ',name) ,oldf)
             ,result))))
    
    (defmacro show (x)
      `(format t "~a --> ~a~%"
               ',x ,x))
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    (defun foo (x y) (+ x y))
    
    (defun bar (x) (foo x (* x 2)))
    
    (show (bar 42))
    
    (show (with-replaced-function (foo (x y) (* x y))
                                  (bar 42)))
    
    (show (bar 42))
    

    NOTINLINE

        2
  •  2
  •   antifuchs    14 年前
        3
  •  1
  •   Vijay Mathew Chor-ming Lung    14 年前

    > (defun b () 'original)
    B
    > (setf f #'b)
    #<Compiled-function B #xC2C1546>
    > (defun a () (funcall f))
    A
    > (a)
    ORIGINAL
    > (setf f #'(lambda () 'stub))
    #<Anonymous Function #xC2D990E>
    > (a)
    STUB
    > (setf f #'b)
    #<Compiled-function B #xC2C1546>
    > (a)
    ORIGINAL
    
        4
  •  1
  •   Thayne    9 年前

    with-mocked-functions with-added-methods https://github.com/bytecurry/bytecurry.mocks

        5
  •  1
  •   Bleeding Fingers    8 年前

        6
  •  0
  •   Marko    14 年前

    (defmacro with-fun (origfn mockfn &body body)
      `(let ((it ,origfn))
          (setf ,origfn ,mockfn)
         ,@body
          (setf ,origfn ,it)))
    

    profile

        7
  •  0
  •   Ehvince    7 年前

    cl-mock mockingbird

    (ql:quickload :mockingbird)