When I run a test in Spacemacs with Clojure mode via <SPC> m t t it does not show a failure, even when the test clearly fails. See:
1 is not equal to 2, but still there are 0 test failures.
How can I make the test fail?
There is an issue in your test: it lacks comparison operator. The correct version is:
(deftest test-exercise-1-4
(testing "count-anywhere"
(is (= 2 1))))
is macro has following definition (edited source for brevity):
(defmacro is
"Generic assertion macro. 'form' is any predicate test.
'msg' is an optional message to attach to the assertion.
Example: (is (= 4 (+ 2 2)) \"Two plus two should be 4\")
([form] `(is ~form nil))
([form msg] `(try-expr ~msg ~form)))
As you can see with (is 2 1) you are calling the second arity where form is 2 and the message for assertion is 1. As 2 is truthy it makes the assertion to pass:
(is 2)
;; => 2
(is false)
FAIL in () (boot.user5045373352931487641.clj:1)
expected: false
actual: false
;; => false
Related
Emacs version: 26.3
Slime version: 2.26.1
I start up Emacs.
I open up a simple .lisp file.
(defun testfn (x y)
(+ x y))
(defmacro testmc (form)
form
`(list 1 2 3))
I place my cursor over the symbol defun and issue the keyboard-command M-. (slime-edit-definition).
This should bring me to the definition of defun.
But it doesn't.
It brings me here:
I place my cursor over the symbol defmacro and issue the keyboard-command M-. (slime-edit-definition).
This should bring me to the definition of defmacro.
But it doesn't.
It brings me here:
Why does it do this & how do I fix this
Notice there is a warning in the REPL when trying to find the source of DEFUN:
WARNING: inconsistent 2 form-number-translations
You can replicate it yourself in the REPL:
CL-USER> (let ((slynk::*buffer-package* (find-package :cl))
(slynk::*buffer-readtable* *readtable*))
(slynk:find-definitions-for-emacs "DEFUN"))
WARNING: inconsistent 2 form-number-translations
(("(DEFMACRO DEFUN)"
(:LOCATION (:FILE "/home/chris/data/src/sbcl/src/code/macros.lisp")
(:POSITION 4140)
(:SNIPPET "(setq doc nil)
(let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
(lambda-guts `(,#decls (block ,(fun-name-block-name name) ,#forms)))
(lambda `(lambda ,lambda-list ,#lambda-guts))
(named-lambda `("))))
To find where the warning comes from, you could do as I did first and do a textual search on the repository, or you could use the following alternate method that works better, namely invoke the debugger on warnings:
(handler-bind ((warning (lambda (c) (invoke-debugger c))))
(let ((slynk::*buffer-package* (find-package :cl))
(slynk::*buffer-readtable* *readtable*))
(slynk:find-definitions-for-emacs "DEFUN")))
This comes from SLYNK-SBCL::FORM-NUMBER-POSITION, and the interesting value in the debugger is the source location obtained from SBCL:
#<SB-INTROSPECT:DEFINITION-SOURCE {10369C50F3}>
--------------------
The object is a STRUCTURE-OBJECT of type SB-INTROSPECT:DEFINITION-SOURCE.
PATHNAME: #P"SYS:SRC;CODE;MACROS.LISP"
FORM-PATH: (5)
FORM-NUMBER: 89
CHARACTER-OFFSET: 3917
FILE-WRITE-DATE: 3825178034
PLIST: NIL
DESCRIPTION: NIL
It says the source is the fifth toplevel form in the file (which corresponds to the character offset), and from here, the FORM-NUMBER is the 89th form in a depth-first search walk of the form (this comes from the structure's docstring).
But, if I recompile the function FORM-NUMBER-POSITION with DEBUG set to 3, the toplevel form read at this position, TLF is NIL:
1: (SLYNK-SBCL::FORM-NUMBER-POSITION #S(SB-INTROSPECT:DEFINITION-SOURCE :PATHNAME #P"SYS:SRC;CODE;MACROS.LISP" :FORM-PATH (5) :FORM-NUMBER 89 :CHARACTER-OFFSET 3917 :FILE-WRITE-DATE 3825178034 :PLIST NIL..
Locals:
DEFINITION-SOURCE = #S(SB-INTROSPECT:DEFINITION-SOURCE :PATHNAME #P"SYS:SRC;CODE;MACROS.LISP" :FORM-PATH (5) :FORM-NUMBER 89 :CHARACTER-OFFSET 3917 :FILE-WRITE-DATE 3825178034 :PLIST NIL :DESCRIPTION NIL)
FORM-NUMBER = 89
PATH-TABLE = #((0 0))
POS-MAP = #<HASH-TABLE :TEST EQ :COUNT 126 {103B227EA3}>
POS-MAP#1 = #<HASH-TABLE :TEST EQ :COUNT 126 {103B227EA3}>
STREAM = #<SB-IMPL::STRING-INPUT-STREAM {7F3E0350D953}>
TLF = NIL
TLF#1 = NIL
TLF-NUMBER = 5
In read-source-form, you can see that the form is being read inside a (ignore-errors (read ...)) form, which returns NIL in case of error. I tried calling (read ...) only but this somehow did not invoke the debugger, so I did the same thing as above and explicitly invoked it on any condition.
There is an error, namely that the package "SB-XC" does not exist, which is expected since, if I am not mistaken, this is a package that only exists during the compilation of SBCL itself.
I think you should contact the SBCL SLY developers and file a bug for this directly, they would certainly have a better idea of how to fix the behaviour (feel free to link to your question in addition to giving the usual details of the bug report).
I'm seeing pretty much what you're seeing.
Defun (line 280 of defboot.lisp) is a macro, which is defined in terms of defun-expander (line 230 of defboot.lisp) which is what you're seeing.
Whereas, defmacro takes you directly to its definition (line 15 of defmacro.lisp) which is what you're seeing.
It seems to be doing useful things.
I defined a new function 'addmore'
(defun addmore (x y z)
(testfn x (testfn y z)))
I compiled it all, and M-. on 'addmore' takes me to the definition of testfn.
So I think it's all working.
Given Unit1 standard ACTR documentary code:
(add-dm
(b ISA count-order first 1 second 2)
(c ISA count-order first 2 second 3)
(d ISA count-order first 3 second 4)
(e ISA count-order first 4 second 5)
(firstgoal ISA count-from start 0 end 5)
)
question:looking at the 0 and the 5 in den firstgoal chunk. Is it possible to declare that more generic like:
(firstgoal ISA count-from start *global_start_var* end *global_end_var*)
Is it possible to output a specific value in an chunk?
Speaking of the counter I want a code running the model and then evaluate the number which the counter model counted to
Example how a lisp code might look like with proper value infiltration in the lisp model
;; load the model
(load "counter.lisp")
;; set the start and end variables
(setvar *global_start_var* 0)
(setvar *global_start_end* 9)
;; run the model
(run 9999)
;; safe the result of the counter in the (local) variable result using the function get_val
(result (get_val))
To set the chunk contents using Lisp variables you can use the add-dm-fct function which takes a list of chunk description lists:
(add-dm-fct (list (list 'firstgoal 'ISA 'count-from 'start *global_start_var* 'end *global_end_var*)))
To get the current value from the slot of a chunk you need to use the macro chunk-slot-value or the function chunk-slot-value-fct. Those both take two parameters: the name of the chunk and the name of the slot, and then return the value of that slot in that chunk.
With the chunks you've shown there, this:
(chunk-slot-value b first)
will return 1.
I refer to 17.5.7.4 Predicates and see the demonstration:
— Function: zerop x
Returns true if x is numerically zero, in any of the Calc data types. (Note that for some types, such as error forms and intervals,
it never makes sense to return true.) In defmath, the expression ‘(= x
0)’ will automatically be converted to ‘(math-zerop x)’, and ‘(/= x
0)’ will be converted to ‘(not (math-zerop x))’.
However, it report error when apply it
ELISP> (math-zerop 0)
*** Eval error *** Symbol’s function definition is void: math-zerop
ELISP> (math-zerop 1)
*** Eval error *** Symbol’s function definition is void: math-zerop
What's the problem?
Emacs tries to lazy load (called autoloading) some of its features. math-zerop is defined as part of the calc-misc feature (in calc-misc.el).
You can load it by (require 'calc) which loads the calc-misc feature.
I'm a ClojureScript newbie using emacs, cider, cljsbuild, and austin with slimerjs on a Windows machine. I've noted that sometimes when I type into the clojurescript repl, an extra ^M character and nil are appended to the output (but the return value is not nil, as indicated by the output of the repl enclosed below). The output before the ^M is colored red, while the nil output is black. What could be causing this, and how can I fix this? (Should I be reporting this as an issue to one of the project's trackers?)
cljs.user> 1
1
cljs.user> true
true
cljs.user> (reduce + [1 2 3])
6
cljs.user> (list [1 2 3])
([1 2 3])
cljs.user> reduce
#<function (a,e,f){switch(arguments.length){case 2:return b.call(this,
a,e);case 3:return c.call(this,a,e,f)}throw Error("Invalid arity: "+arguments.length);}>^M
nil
cljs.user> js/document
#<[object HTMLDocument]>^M
nil
cljs.user> (def d js/document)
#<[object HTMLDocument]>^M
nil
cljs.user> (nil? d)
false
^M is windows line ending. Answer how ho hide it in Emacs can be found here.
Printing of nil in general is normal behaviour and it's not specific to Clojurescript or Emacs. All forms return a value (which may be nil) and also may produce side-effects:
cljs.user> (println 1)
1 ; Side effect - printed value
nil ; Result of evaluation
However returning nil for js/document, (def d js/document) and similar "not-nil" forms is Austin specific behaviour and probably can be treated as minor bug.
I'm in the process of learning Clojure macros, and I'm getting a NullPointerException when trying to use macroexpand-1 on this macro:
(def config {:ns 'bulbs.neo4jserver.client,
:root-uri "http://localhost:7474/db/data/"})
(def data {:name "James"})
(defmacro create
[config data]
`(~(ns-resolve (:ns config) 'create-vertex) config data))
(macroexpand-1 '(create config data))
Trying to compile this returns:
Unknown location:
error: java.lang.NullPointerException
Compilation failed.
But evaluating the macro's body...
`(~(ns-resolve (:ns config) 'create-vertex) config data)
...returns this...
(#'bulbs.neo4jserver.client/create-vertex bulbs.vertices/config bulbs.vertices/data)
...which is what I think I want.
UPDATE: If I manually replace (:ns config) with 'bulbs.neo4jserver.client then the error goes away -- how do you make (:ns config) play nice?
You're trying to mix macroexpand-time and runtime information. The local "config" does not contain the contents of the #'config var, but instead is the symbol 'config.
If you look at the full stack trace, not just the error message, you'll see that ns-resolve is being passed a nil:
user=> (pst)
NullPointerException
java.util.concurrent.ConcurrentHashMap.get (ConcurrentHashMap.java:796)
clojure.lang.Namespace.find (Namespace.java:188)
clojure.core/find-ns (core.clj:3657)
clojure.core/the-ns (core.clj:3689)
clojure.core/ns-resolve (core.clj:3879)
clojure.core/ns-resolve (core.clj:3876)
clj.core/create (NO_SOURCE_FILE:7)
Once you understand the following you will understand your original problem:
user=> (def bar [1 2 3])
user=> (defmacro foo [x] [(class x) (pr-str x)])
user=> (foo (get bar 2))
[clojure.lang.PersistentList "(get bar 2)"]
Why is this a macro in the first place? It seems a normal function would do in this case.
Remember that config is bound to the literal value you entered, so if you do
(def c {:ns 'foo})
(create c 1)
config is going to be just 'c, not the map referenced by c at runtime.