Skip to content

Commit

Permalink
Merge branch 'integration-test-replayer-122'. Close #122
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 23, 2015
2 parents 6a4bf8c + daca08b commit b73dd82
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 16 deletions.
10 changes: 10 additions & 0 deletions it-cases/it-case01.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
((pacmacs-down . 7)
(pacmacs-right . 10)
(pacmacs-down . 14)
(pacmacs-right . 17)
(pacmacs-down . 21)
(pacmacs-right . 26)
(pacmacs-up . 30)
(pacmacs-left . 32)
(pacmacs-up . 34)
(pacmacs-left . 37))
139 changes: 139 additions & 0 deletions it-cases/it-case02.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
((pacmacs-down . 8)
(pacmacs-right . 11)
(pacmacs-down . 15)
(pacmacs-right . 17)
(pacmacs-down . 21)
(pacmacs-right . 26)
(pacmacs-up . 31)
(pacmacs-left . 33)
(pacmacs-up . 36)
(pacmacs-left . 38)
(pacmacs-right . 41)
(pacmacs-down . 42)
(pacmacs-left . 46)
(pacmacs-up . 50)
(pacmacs-left . 52)
(pacmacs-down . 56)
(pacmacs-left . 59)
(pacmacs-right . 62)
(pacmacs-up . 64)
(pacmacs-left . 68)
(pacmacs-up . 71)
(pacmacs-left . 73)
(pacmacs-up . 91)
(pacmacs-up . 95)
(pacmacs-up . 98)
(pacmacs-up . 100)
(pacmacs-right . 102)
(pacmacs-down . 105)
(pacmacs-right . 110)
(pacmacs-up . 113)
(pacmacs-right . 118)
(pacmacs-down . 120)
(pacmacs-right . 125)
(pacmacs-up . 128)
(pacmacs-left . 133)
(pacmacs-down . 136)
(pacmacs-up . 140)
(pacmacs-right . 144)
(pacmacs-down . 147)
(pacmacs-right . 171)
(pacmacs-down . 182)
(pacmacs-left . 184)
(pacmacs-up . 190)
(pacmacs-right . 192)
(pacmacs-up . 206)
(pacmacs-left . 208)
(pacmacs-down . 215)
(pacmacs-up . 221)
(pacmacs-right . 224)
(pacmacs-up . 235)
(pacmacs-left . 240)
(pacmacs-right . 254)
(pacmacs-left . 272)
(pacmacs-down . 279)
(pacmacs-up . 288)
(pacmacs-right . 290)
(pacmacs-up . 297)
(pacmacs-left . 304)
(pacmacs-right . 322)
(pacmacs-down . 330)
(pacmacs-left . 335)
(pacmacs-up . 342)
(pacmacs-right . 344)
(pacmacs-up . 352)
(pacmacs-left . 356)
(pacmacs-right . 367)
(pacmacs-left . 379)
(pacmacs-right . 398)
(pacmacs-down . 407)
(pacmacs-left . 410)
(pacmacs-down . 443)
(pacmacs-left . 458)
(pacmacs-up . 466)
(pacmacs-right . 472)
(pacmacs-down . 482)
(pacmacs-left . 483)
(pacmacs-down . 491)
(pacmacs-right . 493)
(pacmacs-down . 503)
(pacmacs-left . 506)
(pacmacs-up . 517)
(pacmacs-right . 519)
(pacmacs-up . 527)
(pacmacs-left . 528)
(pacmacs-right . 536)
(pacmacs-left . 546)
(pacmacs-up . 564)
(pacmacs-right . 571)
(pacmacs-down . 583)
(pacmacs-down . 620)
(pacmacs-left . 625)
(pacmacs-down . 630)
(pacmacs-right . 631)
(pacmacs-left . 664)
(pacmacs-up . 669)
(pacmacs-right . 675)
(pacmacs-down . 683)
(pacmacs-left . 686)
(pacmacs-down . 691)
(pacmacs-left . 696)
(pacmacs-down . 703)
(pacmacs-up . 708)
(pacmacs-right . 714)
(pacmacs-up . 718)
(pacmacs-right . 720)
(pacmacs-right . 724)
(pacmacs-right . 726)
(pacmacs-left . 729)
(pacmacs-down . 729)
(pacmacs-up . 731)
(pacmacs-left . 751)
(pacmacs-up . 758)
(pacmacs-right . 766)
(pacmacs-down . 772)
(pacmacs-left . 774)
(pacmacs-down . 782)
(pacmacs-right . 789)
(pacmacs-left . 807)
(pacmacs-up . 814)
(pacmacs-right . 819)
(pacmacs-left . 849)
(pacmacs-up . 855)
(pacmacs-left . 863)
(pacmacs-right . 870)
(pacmacs-down . 879)
(pacmacs-left . 886)
(pacmacs-right . 890)
(pacmacs-left . 893)
(pacmacs-right . 899)
(pacmacs-left . 902)
(pacmacs-right . 905)
(pacmacs-up . 911)
(pacmacs-right . 915)
(pacmacs-up . 917)
(pacmacs-right . 920)
(pacmacs-left . 926)
(pacmacs-right . 928)
(pacmacs-right . 937)
(pacmacs-left . 941))
63 changes: 63 additions & 0 deletions it-cases/it-case03.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
((pacmacs-down . 7)
(pacmacs-right . 10)
(pacmacs-down . 14)
(pacmacs-right . 17)
(pacmacs-right . 21)
(pacmacs-left . 25)
(pacmacs-right . 29)
(pacmacs-down . 31)
(pacmacs-right . 35)
(pacmacs-down . 36)
(pacmacs-right . 41)
(pacmacs-up . 45)
(pacmacs-left . 47)
(pacmacs-up . 49)
(pacmacs-left . 52)
(pacmacs-right . 54)
(pacmacs-down . 56)
(pacmacs-left . 61)
(pacmacs-up . 66)
(pacmacs-left . 68)
(pacmacs-down . 73)
(pacmacs-left . 75)
(pacmacs-right . 78)
(pacmacs-up . 81)
(pacmacs-left . 85)
(pacmacs-up . 89)
(pacmacs-left . 92)
(pacmacs-up . 108)
(pacmacs-up . 111)
(pacmacs-up . 114)
(pacmacs-up . 116)
(pacmacs-right . 121)
(pacmacs-down . 124)
(pacmacs-right . 130)
(pacmacs-up . 133)
(pacmacs-right . 139)
(pacmacs-down . 141)
(pacmacs-right . 147)
(pacmacs-up . 149)
(pacmacs-right . 154)
(pacmacs-down . 156)
(pacmacs-left . 159)
(pacmacs-right . 161)
(pacmacs-down . 163)
(pacmacs-left . 166)
(pacmacs-right . 169)
(pacmacs-down . 171)
(pacmacs-right . 175)
(pacmacs-down . 178)
(pacmacs-up . 192)
(pacmacs-up . 214)
(pacmacs-right . 217)
(pacmacs-down . 220)
(pacmacs-right . 225)
(pacmacs-up . 228)
(pacmacs-right . 233)
(pacmacs-down . 236)
(pacmacs-right . 241)
(pacmacs-up . 246)
(pacmacs-down . 281)
(pacmacs-left . 282)
(pacmacs-down . 289)
(pacmacs-right . 291))
14 changes: 14 additions & 0 deletions pacmacs-board.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@
(equal object-type type)))
cell)))

(defun pacmacs--object-at-p (board row column objects)
(plist-bind ((width :width)
(height :height))
board
(let ((wrapped-row (mod row height))
(wrapped-column (mod column width)))
(-find (-lambda (object)
(plist-bind ((object-row :row)
(object-column :column))
object
(and (= object-row wrapped-row)
(= object-column wrapped-column))))
objects))))

(defun pacmacs--step-point (board row column direction)
(plist-bind ((width :width)
(height :height))
Expand Down
3 changes: 2 additions & 1 deletion pacmacs-score.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

;;; Code:

(require 'f)
(require 'dash)
(require 'dash-functional)

Expand All @@ -53,7 +54,7 @@
(defun pacmacs--read-score-table ()
(when (file-exists-p pacmacs--score-file-name)
(-> pacmacs--score-file-name
(pacmacs--file-content)
(f-read-text)
(read-from-string)
(car))))

Expand Down
11 changes: 6 additions & 5 deletions pacmacs-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ side-effects."
(cdr (assoc direction-vector
direction-table))))

(defun pacmacs--file-content (filename)
(with-temp-buffer
(insert-file-contents filename)
(buffer-string)))

(defun pacmacs--levelname-from-filename (filename)
(when (string-match "\\(map[0-9]+\\)\\.txt" filename)
(match-string 1 filename)))

(defmacro pacmacs--measure-time (&rest body)
"Measure the time it takes to evaluate BODY."
`(let ((time (current-time)))
,@body
(float-time (time-since time))))

(provide 'pacmacs-utils)

;;; pacmacs.el ends here
3 changes: 2 additions & 1 deletion pacmacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

(require 'cl-lib)
(require 'dash)
(require 'f)

(require 'pacmacs-anim)
(require 'pacmacs-board)
Expand Down Expand Up @@ -508,7 +509,7 @@
(let* ((lines (split-string (->> map-name
(format "./maps/%s.txt")
(pacmacs--find-resource-file)
(pacmacs--file-content))
(f-read-text))
"\n" t))
(board-width (apply 'max (mapcar #'length lines)))
(board-height (length lines)))
Expand Down
5 changes: 0 additions & 5 deletions test/pacmacs-utils-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@
(should (equal "map06" (pacmacs--levelname-from-filename "map06.txt")))
(should (not (pacmacs--levelname-from-filename "."))))

(ert-deftest pacmacs--file-content-test ()
(with-mock
(should (equal "hello\n"
(pacmacs--file-content
"test-data/file-with-hello-string.txt")))))
39 changes: 35 additions & 4 deletions tools/it-recorder.el
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(require 'pacmacs)

(require 'dash)
(require 'f)
(require 'cl-lib)

(defvar pacmacs--tick-counter 0)
(defvar pacmacs--recorded-actions nil)
(defvar pacmacs--tick-times nil)

(defun pacmacs--record-action (action-name)
(add-to-list 'pacmacs--recorded-actions
Expand All @@ -22,31 +25,50 @@
(insert))
(write-file filename)))

(defun pacmacs--load-test-case (filename)
(-> (f-read-text filename)
(read-from-string)
(car)))

(defun pacmacs-record-up ()
(interactive)
(pacmacs--record-action 'up)
(pacmacs--record-action 'pacmacs-up)
(pacmacs-up))

(defun pacmacs-record-down ()
(interactive)
(pacmacs--record-action 'down)
(pacmacs--record-action 'pacmacs-down)
(pacmacs-down))

(defun pacmacs-record-left ()
(interactive)
(pacmacs--record-action 'left)
(pacmacs--record-action 'pacmacs-left)
(pacmacs-left))

(defun pacmacs-record-right ()
(interactive)
(pacmacs--record-action 'right)
(pacmacs--record-action 'pacmacs-right)
(pacmacs-right))

(defun pacmacs-record-tick ()
(interactive)
(cl-incf pacmacs--tick-counter)
(pacmacs-tick))

(defun pacmacs-replay-tick ()
(cl-incf pacmacs--tick-counter)

(add-to-list 'pacmacs--tick-times
(pacmacs--measure-time
(pacmacs-tick)))

(if (not pacmacs--recorded-actions)
(pacmacs-quit)
(-let ((((action . tick-number) . _) pacmacs--recorded-actions))
(when (= tick-number pacmacs--tick-counter)
(funcall action)
(setq pacmacs--recorded-actions (cdr pacmacs--recorded-actions))))))

(define-derived-mode pacmacs-it-recorder-mode pacmacs-mode "pacmacs-it-recorder-mode"
(define-key pacmacs-it-recorder-mode-map (kbd "<up>") 'pacmacs-record-up)
(define-key pacmacs-it-recorder-mode-map (kbd "<down>") 'pacmacs-record-down)
Expand All @@ -58,3 +80,12 @@
(pacmacs--initialize-game 'pacmacs-record-tick)
(pacmacs-it-recorder-mode)
(pacmacs--reset-recorder))

(defun pacmacs--start-it-replayer (filename)
(interactive "fLoad test case: ")
(pacmacs--initialize-game 'pacmacs-replay-tick)
(pacmacs-mode)

(setq pacmacs--recorded-actions (pacmacs--load-test-case filename))
(setq pacmacs--tick-counter 0)
(setq pacmacs--tick-times nil))

0 comments on commit b73dd82

Please sign in to comment.