scopes/core: some minor improvements, e.g. optionally provide data in message creation
This commit is contained in:
parent
8741f50ef6
commit
993bab0923
3 changed files with 13 additions and 22 deletions
|
@ -4,8 +4,7 @@
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:core :scopes/core))
|
(:local-nicknames (:core :scopes/core))
|
||||||
(:export #:message #:simple-message
|
(:export #:message #:simple-message
|
||||||
#:head #:data
|
#:head #:data #:head-as-list))
|
||||||
#:as-list))
|
|
||||||
|
|
||||||
(in-package :scopes/core/message)
|
(in-package :scopes/core/message)
|
||||||
|
|
||||||
|
@ -19,29 +18,23 @@
|
||||||
(class)
|
(class)
|
||||||
(item)))
|
(item)))
|
||||||
|
|
||||||
(defmethod print-object ((head message-head) stream)
|
|
||||||
(with-slots (domain action class item) head
|
|
||||||
(format stream
|
|
||||||
"<message-head (~a ~a ~a ~a)>"
|
|
||||||
domain action class item)))
|
|
||||||
|
|
||||||
(defmethod as-list ((h message-head))
|
|
||||||
(with-slots (domain action class item) h
|
|
||||||
(list domain action class item)))
|
|
||||||
|
|
||||||
;;;; message
|
;;;; message
|
||||||
|
|
||||||
(defclass message ()
|
(defclass message ()
|
||||||
((head :reader head :initarg :head)
|
((head :reader head :initarg :head)
|
||||||
(sender)
|
(sender)
|
||||||
(timestamp)
|
(timestamp)
|
||||||
(data :accessor data :initform nil)))
|
(data :accessor data :initarg :data :initform nil)))
|
||||||
|
|
||||||
(defun simple-message (&rest head-vals)
|
(defun simple-message (head-vals &optional data)
|
||||||
(let ((h (make-instance 'message-head)))
|
(let ((h (make-instance 'message-head)))
|
||||||
(dolist (sl '(domain action class item))
|
(dolist (sl '(domain action class item))
|
||||||
(setf (slot-value h sl) (pop head-vals)))
|
(setf (slot-value h sl) (pop head-vals)))
|
||||||
(make-instance 'message :head h)))
|
(make-instance 'message :head h :data data)))
|
||||||
|
|
||||||
|
(defmethod head-as-list ((msg message))
|
||||||
|
(with-slots (domain action class item) (head msg)
|
||||||
|
(list domain action class item)))
|
||||||
|
|
||||||
(defmethod print-object ((msg message) stream)
|
(defmethod print-object ((msg message) stream)
|
||||||
(with-slots (domain action class item) (head msg)
|
(with-slots (domain action class item) (head msg)
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
:initform (make-hash-table :test #'equalp))))
|
:initform (make-hash-table :test #'equalp))))
|
||||||
|
|
||||||
(defun check-message (ctx msg)
|
(defun check-message (ctx msg)
|
||||||
(let ((key (message:as-list (message:head msg))))
|
(let ((key (message:head-as-list msg)))
|
||||||
(multiple-value-bind (val found) (gethash key (expected ctx))
|
(multiple-value-bind (val found) (gethash key (expected ctx))
|
||||||
(if found
|
(if found
|
||||||
(progn
|
(progn
|
||||||
|
@ -37,7 +37,7 @@
|
||||||
(t:failure "unexpected: ~s" msg)))))
|
(t:failure "unexpected: ~s" msg)))))
|
||||||
|
|
||||||
(defun expect (ctx msg)
|
(defun expect (ctx msg)
|
||||||
(setf (gethash (message:as-list (message:head msg)) (expected ctx))
|
(setf (gethash (message:head-as-list msg) (expected ctx))
|
||||||
(message:data msg)))
|
(message:data msg)))
|
||||||
|
|
||||||
(defun check-expected ()
|
(defun check-expected ()
|
||||||
|
@ -62,10 +62,8 @@
|
||||||
|
|
||||||
(t:deftest test-send ()
|
(t:deftest test-send ()
|
||||||
(let ((rcvr (receiver t:*test-suite*))
|
(let ((rcvr (receiver t:*test-suite*))
|
||||||
(msg (message:simple-message :test :dummy))
|
(msg (message:simple-message '(:test :dummy) "dummy payload"))
|
||||||
(msg-exp (message:simple-message :test :dummy)))
|
(msg-exp (message:simple-message '(:test :dummy) "dummy payload")))
|
||||||
(setf (message:data msg) "dummy payload")
|
|
||||||
(setf (message:data msg-exp) "dummy payload")
|
|
||||||
(expect rcvr msg-exp)
|
(expect rcvr msg-exp)
|
||||||
(== (core:name rcvr) :test-rcvr)
|
(== (core:name rcvr) :test-rcvr)
|
||||||
(core:send rcvr msg)
|
(core:send rcvr msg)
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:export #:*test-suite*
|
(:export #:*test-suite*
|
||||||
#:test-suite #:deftest #:show-result
|
#:test-suite #:deftest #:show-result
|
||||||
#:failure #:errors #:test #:==
|
#:failure #:test #:==
|
||||||
#:test-path #:*current-system*))
|
#:test-path #:*current-system*))
|
||||||
|
|
||||||
(in-package :scopes/testing)
|
(in-package :scopes/testing)
|
||||||
|
|
Loading…
Add table
Reference in a new issue