Skip to content

Commit

Permalink
unit testing: assert arguments, verify for external effects
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsandifer committed Jul 20, 2016
1 parent 69abefe commit b613283
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 97 deletions.
7 changes: 3 additions & 4 deletions RESET.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

; Resets system variables to my defaults

(defun C:Reset nil ( / systemVariables)
(setq systemVariables (("cmdecho" . 1)
("osmode" . 191)))
(JD:ResetAllVars 'systemVariables)
(defun C:Reset nil
(setvar "cmdecho" 1)
(setvar "osmode" 191)
(princ "Reset finished.")
(print))

Expand Down
149 changes: 79 additions & 70 deletions SYSTEM.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
;; ;;
;; 03/15/2016 ;;
;; - Added ChangeVar(iable). ;;
;; - Added Save&ChangeVar. ;;
;; - Added Save&ChangeVar. ;;
;; ;;
;; 02/04/2016 ;;
;; - Added SaveVar(iable). ;;
Expand All @@ -37,136 +37,145 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;; JD:ClearVar(iable)s - Copyright 2016 J.D. Sandifer
;;; Clears the list of system variables.
;;; variableList [symbol] - name of variable list to clear (nil value ok)

(defun JD:ClearVars ( variableList / )
(JD:ClearHash variableList)
(princ))
(defun JD:ClearVars (variableList /)
(JD:ClearHash variableList)
(princ))



;;; JD:SaveVar(iable) - Copyright 2016 J.D. Sandifer
;;; Saves the selected system variable if not already saved.
;;; variable [string] - system variable to store
;;; variableList [symbol] - name of the variable list to use

(defun JD:SaveVar ( variable variableList / )
(setq variable (strcase variable T))
(if (not (JD:GetHash variable variableList))
(JD:PutHash variable (getvar variable) variableList))
(princ))
(defun JD:SaveVar (variable variableList /)
(setq variable (strcase variable T))
(if (not (JD:GetHash variable variableList))
(JD:PutHash variable (getvar variable) variableList))
(princ))



;;; JD:ChangeVar(iable) - Copyright 2016 J.D. Sandifer
;;; Changes the selected system variable.
;;; variable [string] - system variable to store
;;; newValue [varies] - the value to which to set the variable

(defun JD:ChangeVar ( variable newValue / )
(setq variable (strcase variable T))
(setvar variable newValue)
(princ))

(defun JD:ChangeVar (variable newValue /)
(setq variable (strcase variable T))
(setvar variable newValue)
(princ))




;;; JD:Save&ChangeVar(iable) - Copyright 2016 J.D. Sandifer
;;; Saves and changes the selected system variable.
;;; variable [string] - system variable to store
;;; variableList [symbol] - name of the variable list to use
;;; newValue [varies] - the value to which to set the variable

(defun JD:Save&ChangeVar ( variable variableList newValue / )
(JD:SaveVar variable variableList)
(JD:ChangeVar variable newValue)
(princ))

(defun JD:Save&ChangeVar (variable variableList newValue /)
(JD:SaveVar variable variableList)
(JD:ChangeVar variable newValue)
(princ))




;;; JD:ResetVar(iable)s - Copyright 2016 J.D. Sandifer
;;; Restores a single system variable from the list (without removing it).
;;; variable [string] - system variable to store
;;; variableList [symbol] - name of the variable list to use

(defun JD:ResetVar ( variable variableList / )
(setq variable (strcase variable T))
(setvar variable (JD:GetHash variable variableList))
(princ))



(defun JD:ResetVar (variable variableList / oldValue didSucceed)
(setq variable (strcase variable T))
(cond
((setq oldValue (JD:GetHash variable variableList))
(setvar variable oldValue)
(setq didSucceed T))
(T
(setq didSucceed nil)))
didSucceed)



;;; JD:ResetAllVar(iable)s - Copyright 2016 J.D. Sandifer
;;; Restores all system variables in the list.
;;; variable [string] - system variable to store
;;; variableList [symbol] - name of the variable list to use (nil value ok)

(defun JD:ResetAllVars ( variableList / )
(foreach variable (eval variableList)
(JD:ResetVar (car variable) variableList))
(JD:ClearVars variableList)
(princ))


(defun JD:ResetAllVars (variableList /)
(foreach
variablePair (eval variableList)
(JD:ResetVar (car variablePair) variableList))
(JD:ClearVars variableList)
(princ))



;;; Error handling function - prints error message nicely and resets system variables

(defun ErrorHandler (errorMessage)
(if (not (member errorMessage '("Function cancelled" "quit / exit abort")))
(princ (strcat "\nThere's a slight problem: " errorMessage)))

(if (not (member
errorMessage
'("Function cancelled" "quit / exit abort")))
(princ (strcat "\nThere's a slight problem: " errorMessage)))

(JD:Save&ChangeVar "cmdecho" 'systemVariables 0)

(command-s "._UNDO" "_End") ; End UNDO group

(JD:ResetAllVars 'systemVariables)
; Relies on global variable, but nil works...so maybe ok

(command-s "._UNDO" "_End") ; End UNDO group
(JD:ResetAllVars 'systemVariables)
; Relies on global variable, but nil works...so maybe ok

(princ))



;; Start Undo - Lee Mac
;; Opens an Undo Group.

(defun LM:startundo ( doc )
(LM:endundo doc)
(vla-startundomark doc)
)
(defun
LM:startundo (doc)
(LM:endundo doc)
(vla-startundomark doc))



;; End Undo - Lee Mac
;; Closes an Undo Group.

(defun LM:endundo ( doc )
(while (= 8 (logand 8 (getvar 'undoctl)))
(vla-endundomark doc)
)
)
(defun
LM:endundo (doc)
(while (= 8 (logand 8 (getvar 'undoctl)))
(vla-endundomark doc)))



;; Active Document - Lee Mac
;; Returns the VLA Active Document Object

(defun LM:acdoc nil
(eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
(LM:acdoc)
)
(eval
(list
'defun
'LM:acdoc
'nil
(vla-get-activedocument (vlax-get-acad-object))))
(LM:acdoc))



;;----------------------------------------------------------------------;;

(vl-load-com)
(princ
(strcat
"\n:: SYSTEM.lsp loaded. | \\U+00A9 J.D. Sandifer "
(menucmd "m=$(edtime,0,yyyy)")
" ::"))
(strcat
"\n:: SYSTEM.lsp loaded. | \\U+00A9 J.D. Sandifer "
(menucmd "m=$(edtime,0,yyyy)")
" ::"))
(princ)

;;----------------------------------------------------------------------;;
Expand Down
97 changes: 74 additions & 23 deletions TEST.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,110 @@
;; 02/23/2016 ;;
;; - Started. ;;
;; ;;
;; 06/28/2016 ;;
;; - Added Verify for functions that don't ;;
;; return a value. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


; Runs all unit tests
; Output: T if all unit tests passed, else nil

(defun C:UnitTests ( / testList testResults)
(defun C:UnitTests ( / testList testResults varList)

;; Setup for tests
(princ "\n")
;(ReloadFile "TEST")
;(ReloadFile "TEST")
(setq varList '(("osmode" . 180)))

;; Actual tests
(princ "JD:ResetVar\n")
(Verify 'JD:ResetVar '("osmode" 'varList) '(= (getvar "osmode") 180))
(Assert 'JD:ResetVar '("cmdecho" 'varList) nil)
(setq varList nil)
(princ "Set varList to nil.\n")
(Assert 'JD:ResetVar '("cmdecho" 'varList) nil)

(princ "JD:ResetAllVars\n")


(princ "isInserted\n")
(Assert 'isInserted "not-a-block" nil)
(Assert 'isInserted "36-16-BLOCK-2" nil)
(Assert 'isInserted "BP" T)

(setq testResults (CountBooleans testList))
(PrintTestResults testResults))
;; Displaying the results of the tests
;(setq testResults (CountBooleans testList))
(PrintTestResults (CountBooleans testList)))



; Checks if function operates as expected and add result to testList
; Input: function (symbol), argument for the function, expected result
; Checks if function operates as expected and adds result to testList
; Input: function (symbol), argument (list) for function, expected result
; Output: result of test
; Return: T or nil


(defun Assert ( functionName argumentForFunction expectedResult /
actualResult passed)
(defun Assert ( functionName argumentList expectedReturn /
actualReturn passed)
(if (not (= (type argumentList) 'LIST))
(setq argumentList (list argumentList)))
(cond
((= (setq actualResult ((eval functionName) argumentForFunction))
expectedResult)
((= (setq actualReturn (eval (cons functionName argumentList)))
expectedReturn)
(princ "passed...(")
(setq passed T)
(setq actualResult nil))
(setq actualReturn nil))
(T
(princ "failed...(")
(setq passed nil)
(setq actualResult
(strcat (vl-symbol-name actualResult) " instead of "))))
(setq actualReturn
(strcat (vl-princ-to-string actualReturn) " instead of "))))

;; continue printing result...
(princ (strcase (vl-symbol-name functionName) T))
(princ " ")
(prin1 (eval argumentForFunction))
(prin1 argumentList)
(princ ") returned ")
(if actualResult (princ actualResult))
(princ expectedResult)
(if actualReturn (princ actualReturn))
(princ expectedReturn)
(princ "\n")
(setq testList (append testList (list passed)))

passed)




; Checks if function operates as expected and adds result to testList
; (for functions that don't return a value)
; Input: function (symbol), argument (list) for the function, expression
; that should return true if the function acts as expected
; Output: result of test
; Return: T or nil


(defun Verify ( functionName argumentList testForResult /
passed)

(eval (cons (eval functionName) argumentList)) ;run the test
(cond
((eval testForResult)
(princ "passed...(")
(setq passed T))
(T
(princ "failed...(")
(setq passed nil)))

;; continue printing result...
(princ (strcase (vl-symbol-name functionName) T))
(princ " ")
(prin1 argumentList)
(princ ") functioned ")
(if passed
(princ "as expected")
(princ "erroneously"))
(princ "\n")
(setq testList (append testList (list passed)))

passed)



; Counts boolean values
Expand Down Expand Up @@ -105,8 +156,8 @@
(setq falses 0))


; add code to count digits in numbers and add correct space before
; and use test vs. tests correctly?
; add code to count digits in numbers and add correct space before.
; also, choose between "test" and "tests" correctly?

(princ "\n ")(princ trues)(princ " tests passed")
(princ "\n ")(princ falses)(princ " tests failed")
Expand Down

0 comments on commit b613283

Please sign in to comment.