summary refs log tree commit diff stats
path: root/test/tc_directory.py
blob: f1b204c3616c8e9a00d16a4764d823d7a85a2683 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if __name__ == '__main__': from __init__ import init; init()

from os.path import realpath, join, dirname

from ranger import fsobject
from ranger.fsobject.file import File
from ranger.fsobject.directory import Directory
from ranger.shared.settings import SettingsAware

SettingsAware._setup()

TESTDIR = realpath(join(dirname(__file__), 'testdir'))
TESTFILE = join(TESTDIR, 'testfile5234148')
NONEXISTANT_DIR = join(TESTDIR, 'nonexistant')

import unittest
class Test1(unittest.TestCase):
	def test_initial_condition(self):
		# Check for the expected initial condition
		dir = Directory(TESTDIR)

		self.assertEqual(dir.path, TESTDIR)
		self.assertFalse(dir.content_loaded)
		self.assertEqual(dir.filenames, None)
		self.assertEqual(dir.files, None)
		self.assertRaises(fsobject.NotLoadedYet, len, dir)

	def test_after_content_loaded(self):
		import os
		# Check whether the directory has the correct list of filenames.
		dir = Directory(TESTDIR)
		dir.load_content()

		self.assertTrue(dir.exists)
		self.assertEqual(type(dir.filenames), list)

		# Get the filenames you expect it to have and sort both before
		# comparing. I don't expect any order after only loading the filenames.
		assumed_filenames = os.listdir(TESTDIR)
		assumed_filenames = list(map(lambda str: os.path.join(TESTDIR, str),
			assumed_filenames))
		assumed_filenames.sort()
		dir.filenames.sort()

		self.assertTrue(len(dir) > 0)
		self.assertEqual(dir.filenames, assumed_filenames)

		# build a file object for each file in the list assumed_filenames
		# and find exactly one equivalent in dir.files
		for name in assumed_filenames:
			f = File(name)
			f.load()
			for dirfile in dir.files:
				if (f.path == dirfile.path and f.stat == dirfile.stat):
					break
			else:
				self.fail("couldn't find file {0}".format(name))

	def test_nonexistant_dir(self):
		dir = Directory(NONEXISTANT_DIR)
		dir.load_content()
		
		self.assertTrue(dir.content_loaded)
		self.assertFalse(dir.exists)
		self.assertFalse(dir.accessible)
		self.assertEqual(dir.filenames, None)
		self.assertRaises(fsobject.NotLoadedYet, len, dir)

	def test_load_if_outdated(self):
		import os
		import time
		# modify the directory. If the time between the last modification
		# was within the filesystems resolution of mtime, we should have a reload

		def modify_dir():
			open(TESTFILE, 'w').close()
			os.unlink(TESTFILE)

		def mtime():
			return os.stat(TESTDIR).st_mtime

		dir = Directory(TESTDIR)
		dir.load()

		# If the modification happens to be in the same second as the
		# last modification, it will result in mtime having the same
		# integer value. So we wait until the resolution is exceeded
		# and mtime differs.
		old_mtime = mtime()
		for i in range(50):
			modify_dir()
			if old_mtime != mtime(): break
			time.sleep(0.1)
		else:
			# fail after 5 seconds of trying
			self.fail(
					"Cannot perform test: mtime of TESTDIR is not being updated.")

		self.assertTrue(dir.load_if_outdated())

if __name__ == '__main__':
	unittest.main()
ic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - chessboard.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muRecipe { color: #ff8700; }
.Delimiter { color: #a04060; }
.muScenario { color: #00af00; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.CommentedCode { color: #6c6c6c; }
.muControl { color: #c0a020; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Chessboard program: you type in moves in algebraic notation, and it'll</span>
<span class="Comment"># display the position after each move.</span>

<span class="Comment"># recipes are mu's names for functions</span>
<span class="muRecipe">recipe</span> main [
  open-console  <span class="Comment"># take control of screen, keyboard and mouse</span>

  <span class="Comment"># The chessboard recipe takes keyboard and screen objects as 'ingredients'.</span>
<span class="Constant">  #</span>
  <span class="Comment"># In mu it is good form (though not required) to explicitly show the</span>
  <span class="Comment"># hardware you rely on.</span>
<span class="Constant">  #</span>
  <span class="Comment"># The chessboard also returns the same keyboard and screen objects. In mu it</span>
  <span class="Comment"># is good form to not modify ingredients of a recipe unless they are also</span>
  <span class="Comment"># results. Here we clearly modify both keyboard and screen, so we return</span>
  <span class="Comment"># both.</span>
<span class="Constant">  #</span>
  <span class="Comment"># Here the console and screen are both 0, which usually indicates real</span>
  <span class="Comment"># hardware rather than a fake for testing as you'll see below.</span>
  <span class="Constant">0:literal/screen</span>, <span class="Constant">0:literal/console</span><span class="Special"> &lt;- </span>chessboard <span class="Constant">0:literal/screen</span>, <span class="Constant">0:literal/console</span>

  close-console  <span class="Comment"># cleanup screen, keyboard and mouse</span>
]

<span class="SalientComment">## But enough about mu. Here's what it looks like to run the chessboard program.</span>

<span class="muScenario">scenario</span> print-board-and-read-move [
  $close-trace  <span class="Comment"># administrivia: most scenarios save and check traces, but this one gets too large/slow</span>
  <span class="Comment"># we'll make the screen really wide because the program currently prints out a long line</span>
  assume-screen <span class="Constant">120:literal/width</span>, <span class="Constant">20:literal/height</span>
  <span class="Comment"># initialize keyboard to type in a move</span>
  assume-console [
    type <span class="Constant">[a2-a4</span>
<span class="Constant">]</span>
  ]
  run [
    screen:address, console:address<span class="Special"> &lt;- </span>chessboard screen:address, console:address
<span class="CommentedCode">#?     $browse-trace #? 1</span>
<span class="CommentedCode">#?     $close-trace #? 1</span>
    <span class="Comment"># icon for the cursor</span>
    screen:address<span class="Special"> &lt;- </span>print-character screen:address, <span class="Constant">9251:literal</span>  <span class="Comment"># '␣'</span>
  ]
  screen-should-contain [
  <span class="Comment">#            1         2         3         4         5         6         7         8         9         10        11</span>
  <span class="Comment">#  012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789</span>
   <span class="Constant"> .Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves.         .</span>
   <span class="Constant"> .                                                                                                                        .</span>
   <span class="Constant"> .8 | r n b q k b n r                                                                                                     .</span>
   <span class="Constant"> .7 | p p p p p p p p                                                                                                     .</span>
   <span class="Constant"> .6 |                                                                                                                     .</span>
   <span class="Constant"> .5 |                                                                                                                     .</span>
   <span class="Constant"> .4 | P                                                                                                                   .</span>
   <span class="Constant"> .3 |                                                                                                                     .</span>
   <span class="Constant"> .2 |   P P P P P P P                                                                                                     .</span>
   <span class="Constant"> .1 | R N B Q K B N R                                                                                                     .</span>
   <span class="Constant"> .  +----------------                                                                                                     .</span>
   <span class="Constant"> .    a b c d e f g h                                                                                                     .</span>
   <span class="Constant"> .                                                                                                                        .</span>
   <span class="Constant"> .Type in your move as &lt;from square&gt;-&lt;to square&gt;. For example: 'a2-a4'. Then press &lt;enter&gt;.                               .</span>
   <span class="Constant"> .                                                                                                                        .</span>
   <span class="Constant"> .Hit 'q' to exit.                                                                                                        .</span>
   <span class="Constant"> .                                                                                                                        .</span>
   <span class="Constant"> .move: ␣                                                                                                                 .</span>
   <span class="Constant"> .                                                                                                                        .</span>
   <span class="Constant"> .                                                                                                                        .</span>
  ]
]

<span class="SalientComment">## Here's how 'chessboard' is implemented.</span>

<span class="muRecipe">recipe</span> chessboard [
<span class="CommentedCode">#?   $start-tracing [schedule] #? 2</span>
<span class="CommentedCode">#?   $start-tracing #? 1</span>
  <span class="Constant">local-scope</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  console:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
<span class="CommentedCode">#?   $print [screen: ], screen:address, [, console: ], console:address, 10:literal/newline</span>
  board:address:array:address:array:character<span class="Special"> &lt;- </span>initial-position
  <span class="Comment"># hook up stdin</span>
  stdin:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">10:literal/capacity</span>
  start-running send-keys-to-channel:<span class="muRecipe">recipe</span>, console:address, stdin:address:channel, screen:address
  <span class="Comment"># buffer lines in stdin</span>
  buffered-stdin:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">10:literal/capacity</span>
  start-running buffer-lines:<span class="muRecipe">recipe</span>, stdin:address:channel, buffered-stdin:address:channel
  <span class="Delimiter">{</span>
    msg:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves.</span>
<span class="Constant">]</span>
    print-string screen:address, msg:address:array:character
    cursor-to-next-line screen:address
    print-board screen:address, board:address:array:address:array:character
    cursor-to-next-line screen:address
    msg:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[Type in your move as &lt;from square&gt;-&lt;to square&gt;. For example: 'a2-a4'. Then press &lt;enter&gt;.</span>
<span class="Constant">]</span>
    print-string screen:address, msg:address:array:character
    cursor-to-next-line screen:address
    msg:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[Hit 'q' to exit.</span>
<span class="Constant">]</span>
    print-string screen:address, msg:address:array:character
    <span class="Delimiter">{</span>
      cursor-to-next-line screen:address
      msg:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[move: ]</span>
      print-string screen:address, msg:address:array:character
      m:address:move, quit:boolean, error:boolean<span class="Special"> &lt;- </span>read-move buffered-stdin:address:channel, screen:address
      <span class="muControl">break-if</span> quit:boolean, +quit:offset
      buffered-stdin:address:channel<span class="Special"> &lt;- </span>clear-channel buffered-stdin:address:channel  <span class="Comment"># cleanup after error. todo: test this?</span>
      <span class="muControl">loop-if</span> error:boolean
    <span class="Delimiter">}</span>
    board:address:array:address:array:character<span class="Special"> &lt;- </span>make-move board:address:array:address:array:character, m:address:move
    clear-screen screen:address
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
<span class="Constant">  +quit</span>
]

<span class="SalientComment">## a board is an array of files, a file is an array of characters (squares)</span>

<span class="muRecipe">recipe</span> new-board [
  <span class="Constant">local-scope</span>
  initial-position:address:array:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># assert(length(initial-position) == 64)</span>
  len:number<span class="Special"> &lt;- </span>length initial-position:address:array:number/deref
  correct-length?:boolean<span class="Special"> &lt;- </span>equal len:number, <span class="Constant">64:literal</span>
  assert correct-length?:boolean, <span class="Constant">[chessboard had incorrect size]</span>
  <span class="Comment"># board is an array of pointers to files; file is an array of characters</span>
  board:address:array:address:array:character<span class="Special"> &lt;- </span>new location:type, <span class="Constant">8:literal</span>
  col:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>equal col:number, <span class="Constant">8:literal</span>
    <span class="muControl">break-if</span> done?:boolean
    file:address:address:array:character<span class="Special"> &lt;- </span>index-address board:address:array:address:array:character/deref, col:number
    file:address:address:array:character/deref<span class="Special"> &lt;- </span>new-file initial-position:address:array:number, col:number
    col:number<span class="Special"> &lt;- </span>add col:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> board:address:array:address:array:character
]

<span class="muRecipe">recipe</span> new-file [
  <span class="Constant">local-scope</span>
  position:address:array:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  index:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  index:number<span class="Special"> &lt;- </span>multiply index:number, <span class="Constant">8:literal</span>
  result:address:array:character<span class="Special"> &lt;- </span>new character:type, <span class="Constant">8:literal</span>
  row:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>equal row:number, <span class="Constant">8:literal</span>
    <span class="muControl">break-if</span> done?:boolean
    dest:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, row:number
    dest:address:character/deref<span class="Special"> &lt;- </span>index position:address:array:number/deref, index:number
    row:number<span class="Special"> &lt;- </span>add row:number, <span class="Constant">1:literal</span>
    index:number<span class="Special"> &lt;- </span>add index:number, <span class="Constant">1:literal</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result:address:array:character
]

<span class="muRecipe">recipe</span> print-board [
  <span class="Constant">local-scope</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  board:address:array:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  row:number<span class="Special"> &lt;- </span>copy <span class="Constant">7:literal</span>  <span class="Comment"># start printing from the top of the board</span>
  <span class="Comment"># print each row</span>
<span class="CommentedCode">#?   $print [printing board to screen ], screen:address, 10:literal/newline</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>lesser-than row:number, <span class="Constant">0:literal</span>
    <span class="muControl">break-if</span> done?:boolean
<span class="CommentedCode">#?     $print [printing rank ], row:number, 10:literal/newline</span>
    <span class="Comment"># print rank number as a legend</span>
    rank:number<span class="Special"> &lt;- </span>add row:number, <span class="Constant">1:literal</span>
    print-integer screen:address, rank:number
    s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ | ]</span>
    print-string screen:address, s:address:array:character
    <span class="Comment"># print each square in the row</span>
    col:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
    <span class="Delimiter">{</span>
      done?:boolean<span class="Special"> &lt;- </span>equal col:number, <span class="Constant">8:literal</span>
      <span class="muControl">break-if</span> done?:boolean
      f:address:array:character<span class="Special"> &lt;- </span>index board:address:array:address:array:character/deref, col:number
      c:character<span class="Special"> &lt;- </span>index f:address:array:character/deref, row:number
      print-character screen:address, c:character
      print-character screen:address, <span class="Constant">32:literal</span>  <span class="Comment"># ' '</span>
      col:number<span class="Special"> &lt;- </span>add col:number, <span class="Constant">1:literal</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    row:number<span class="Special"> &lt;- </span>subtract row:number, <span class="Constant">1:literal</span>
    cursor-to-next-line screen:address
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># print file letters as legend</span>
<span class="CommentedCode">#?   $print [printing legend</span>
<span class="CommentedCode">#? ] #? 1</span>
  s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[  +----------------]</span>
  print-string screen:address, s:address:array:character
  screen:address<span class="Special"> &lt;- </span>cursor-to-next-line screen:address
<span class="CommentedCode">#?   screen:address &lt;- print-character screen:address, 97:literal #? 1</span>
  s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[    a b c d e f g h]</span>
  screen:address<span class="Special"> &lt;- </span>print-string screen:address, s:address:array:character
  screen:address<span class="Special"> &lt;- </span>cursor-to-next-line screen:address
<span class="CommentedCode">#?   $print [done printing board</span>
<span class="CommentedCode">#? ] #? 1</span>
]

<span class="Comment"># board:address:array:address:array:character &lt;- initial-position</span>
<span class="muRecipe">recipe</span> initial-position [
  <span class="Constant">local-scope</span>
  <span class="Comment"># layout in memory (in raster order):</span>
  <span class="Comment">#   R P _ _ _ _ p r</span>
  <span class="Comment">#   N P _ _ _ _ p n</span>
  <span class="Comment">#   B P _ _ _ _ p b</span>
  <span class="Comment">#   Q P _ _ _ _ p q</span>
  <span class="Comment">#   K P _ _ _ _ p k</span>
  <span class="Comment">#   B P _ _ _ _ p B</span>
  <span class="Comment">#   N P _ _ _ _ p n</span>
  <span class="Comment">#   R P _ _ _ _ p r</span>
  initial-position:address:array:number<span class="Special"> &lt;- </span>new-array <span class="Constant">82:literal/R</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">114:literal/r</span>, <span class="Constant">78:literal/N</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">110:literal/n</span>, <span class="Constant">66:literal/B</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">98:literal/b</span>, <span class="Constant">81:literal/Q</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">113:literal/q</span>, <span class="Constant">75:literal/K</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">107:literal/k</span>, <span class="Constant">66:literal/B</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">98:literal/b</span>, <span class="Constant">78:literal/N</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">110:literal/n</span>, <span class="Constant">82:literal/R</span>, <span class="Constant">80:literal/P</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">32:literal/blank</span>, <span class="Constant">112:literal/p</span>, <span class="Constant">114:literal/r</span>
<span class="CommentedCode">#?       82:literal/R, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 114:literal/r,</span>
<span class="CommentedCode">#?       78:literal/N, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 110:literal/n,</span>
<span class="CommentedCode">#?       66:literal/B, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 98:literal/b, </span>
<span class="CommentedCode">#?       81:literal/Q, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 113:literal/q,</span>
<span class="CommentedCode">#?       75:literal/K, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 107:literal/k,</span>
<span class="CommentedCode">#?       66:literal/B, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 98:literal/b,</span>
<span class="CommentedCode">#?       78:literal/N, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 110:literal/n,</span>
<span class="CommentedCode">#?       82:literal/R, 80:literal/P, 32:literal/blank, 32:literal/blank, 32:literal/blank, 32:literal/blank, 112:literal/p, 114:literal/r</span>
  board:address:array:address:array:character<span class="Special"> &lt;- </span>new-board initial-position:address:array:number
  <span class="muControl">reply</span> board:address:array:address:array:character
]

<span class="muScenario">scenario</span> printing-the-board [
  assume-screen <span class="Constant">30:literal/width</span>, <span class="Constant">12:literal/height</span>
  run [
    1:address:array:address:array:character/board<span class="Special"> &lt;- </span>initial-position
    screen:address<span class="Special"> &lt;- </span>print-board screen:address, 1:address:array:address:array:character/board
<span class="CommentedCode">#?     $dump-screen #? 1</span>
  ]
  screen-should-contain [
  <span class="Comment">#  012345678901234567890123456789</span>
   <span class="Constant"> .8 | r n b q k b n r           .</span>
   <span class="Constant"> .7 | p p p p p p p p           .</span>
   <span class="Constant"> .6 |                           .</span>
   <span class="Constant"> .5 |                           .</span>
   <span class="Constant"> .4 |                           .</span>
   <span class="Constant"> .3 |                           .</span>
   <span class="Constant"> .2 | P P P P P P P P           .</span>
   <span class="Constant"> .1 | R N B Q K B N R           .</span>
   <span class="Constant"> .  +----------------           .</span>
   <span class="Constant"> .    a b c d e f g h           .</span>
   <span class="Constant"> .                              .</span>
   <span class="Constant"> .                              .</span>
  ]
]

<span class="SalientComment">## data structure: move</span>

container move [
  <span class="Comment"># valid range: 0-7</span>
  from-file:number
  from-rank:number
  to-file:number
  to-rank:number
]

<span class="Comment"># result:address:move, quit?:boolean, error?:boolean &lt;- read-move stdin:address:channel, screen:address</span>
<span class="Comment"># prints only error messages to screen</span>
<span class="muRecipe">recipe</span> read-move [
  <span class="Constant">local-scope</span>
  stdin:address:channel<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
<span class="CommentedCode">#?   $print screen:address #? 1</span>
  from-file:number, quit?:boolean, error?:boolean<span class="Special"> &lt;- </span>read-file stdin:address:channel, screen:address
  <span class="muControl">reply-if</span> quit?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
  <span class="muControl">reply-if</span> error?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
<span class="CommentedCode">#?   close-console #? 1</span>
  <span class="Comment"># construct the move object</span>
  result:address:move<span class="Special"> &lt;- </span>new move:type
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:move/deref, from-file:offset
  x:address:number/deref<span class="Special"> &lt;- </span>copy from-file:number
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:move/deref, from-rank:offset
  x:address:number/deref, quit?:boolean, error?:boolean<span class="Special"> &lt;- </span>read-rank stdin:address:channel, screen:address
  <span class="muControl">reply-if</span> quit?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
  <span class="muControl">reply-if</span> error?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
  error?:boolean<span class="Special"> &lt;- </span>expect-from-channel stdin:address:channel, <span class="Constant">45:literal/dash</span>, screen:address
  <span class="muControl">reply-if</span> error?:boolean, <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, error?:boolean
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:move/deref, to-file:offset
  x:address:number/deref, quit?:boolean, error?:boolean<span class="Special"> &lt;- </span>read-file stdin:address:channel, screen:address
  <span class="muControl">reply-if</span> quit?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
  <span class="muControl">reply-if</span> error?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
  x:address:number<span class="Special"> &lt;- </span>get-address result:address:move/deref, to-rank:offset
  x:address:number/deref, quit?:boolean, error?:boolean<span class="Special"> &lt;- </span>read-rank stdin:address:channel, screen:address
  <span class="muControl">reply-if</span> quit?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
  <span class="muControl">reply-if</span> error?:boolean, <span class="Constant">0:literal/dummy</span>, quit?:boolean, error?:boolean
<span class="CommentedCode">#?   $exit #? 1</span>
  error?:boolean<span class="Special"> &lt;- </span>expect-from-channel stdin:address:channel, <span class="Constant">10:literal/newline</span>, screen:address
  <span class="muControl">reply-if</span> error?:boolean, <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, error?:boolean
  <span class="muControl">reply</span> result:address:move, quit?:boolean, error?:boolean
]

<span class="Comment"># file:number, quit:boolean, error:boolean &lt;- read-file stdin:address:channel, screen:address</span>
<span class="Comment"># valid values for file: 0-7</span>
<span class="muRecipe">recipe</span> read-file [
  <span class="Constant">local-scope</span>
  stdin:address:channel<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  c:character, stdin:address:channel<span class="Special"> &lt;- </span>read stdin:address:channel
  <span class="Delimiter">{</span>
    q-pressed?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">81:literal</span>  <span class="Comment"># 'Q'</span>
    <span class="muControl">break-unless</span> q-pressed?:boolean
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">1:literal/quit</span>, <span class="Constant">0:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    q-pressed?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">113:literal</span>  <span class="Comment"># 'q'</span>
    <span class="muControl">break-unless</span> q-pressed?:boolean
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">1:literal/quit</span>, <span class="Constant">0:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    empty-fake-keyboard?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">0:literal/eof</span>
    <span class="muControl">break-unless</span> empty-fake-keyboard?:boolean
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">1:literal/quit</span>, <span class="Constant">0:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    newline?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">10:literal/newline</span>
    <span class="muControl">break-unless</span> newline?:boolean
    error-message:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[that's not enough]</span>
    print-string screen:address, error-message:address:array:character
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, <span class="Constant">1:literal/error</span>
  <span class="Delimiter">}</span>
  file:number<span class="Special"> &lt;- </span>subtract c:character, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
<span class="CommentedCode">#?   $print file:number, 10:literal/newline</span>
  <span class="Comment"># 'a' &lt;= file &lt;= 'h'</span>
  <span class="Delimiter">{</span>
    above-min:boolean<span class="Special"> &lt;- </span>greater-or-equal file:number, <span class="Constant">0:literal</span>
    <span class="muControl">break-if</span> above-min:boolean
    error-message:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[file too low: ]</span>
    print-string screen:address, error-message:address:array:character
    print-character screen:address, c:character
    cursor-to-next-line screen:address
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, <span class="Constant">1:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    below-max:boolean<span class="Special"> &lt;- </span>lesser-than file:number, <span class="Constant">8:literal</span>
    <span class="muControl">break-if</span> below-max:boolean
    error-message:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[file too high: ]</span>
    print-string screen:address, error-message:address:array:character
    print-character screen:address, c:character
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, <span class="Constant">1:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> file:number, <span class="Constant">0:literal/quit</span>, <span class="Constant">0:literal/error</span>
]

<span class="Comment"># rank:number &lt;- read-rank stdin:address:channel, screen:address</span>
<span class="Comment"># valid values: 0-7, -1 (quit), -2 (error)</span>
<span class="muRecipe">recipe</span> read-rank [
  <span class="Constant">local-scope</span>
  stdin:address:channel<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  c:character, stdin:address:channel<span class="Special"> &lt;- </span>read stdin:address:channel
  <span class="Delimiter">{</span>
    q-pressed?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">81:literal</span>  <span class="Comment"># 'Q'</span>
    <span class="muControl">break-unless</span> q-pressed?:boolean
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">1:literal/quit</span>, <span class="Constant">0:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    q-pressed?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">113:literal</span>  <span class="Comment"># 'q'</span>
    <span class="muControl">break-unless</span> q-pressed?:boolean
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">1:literal/quit</span>, <span class="Constant">0:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    newline?:boolean<span class="Special"> &lt;- </span>equal c:character, <span class="Constant">10:literal</span>  <span class="Comment"># newline</span>
    <span class="muControl">break-unless</span> newline?:boolean
    error-message:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[that's not enough]</span>
    print-string screen:address, error-message:address:array:character
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, <span class="Constant">1:literal/error</span>
  <span class="Delimiter">}</span>
  rank:number<span class="Special"> &lt;- </span>subtract c:character, <span class="Constant">49:literal</span>  <span class="Comment"># '1'</span>
<span class="CommentedCode">#?   $print rank:number, 10:literal/newline</span>
  <span class="Comment"># assert'1' &lt;= rank &lt;= '8'</span>
  <span class="Delimiter">{</span>
    above-min:boolean<span class="Special"> &lt;- </span>greater-or-equal rank:number, <span class="Constant">0:literal</span>
    <span class="muControl">break-if</span> above-min:boolean
    error-message:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[rank too low: ]</span>
    print-string screen:address, error-message:address:array:character
    print-character screen:address, c:character
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, <span class="Constant">1:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    below-max:boolean<span class="Special"> &lt;- </span>lesser-or-equal rank:number, <span class="Constant">7:literal</span>
    <span class="muControl">break-if</span> below-max:boolean
    error-message:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[rank too high: ]</span>
    print-string screen:address, error-message:address:array:character
    print-character screen:address, c:character
    <span class="muControl">reply</span> <span class="Constant">0:literal/dummy</span>, <span class="Constant">0:literal/quit</span>, <span class="Constant">1:literal/error</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> rank:number, <span class="Constant">0:literal/quit</span>, <span class="Constant">0:literal/error</span>
]

<span class="Comment"># read a character from the given channel and check that it's what we expect</span>
<span class="Comment"># return true on error</span>
<span class="muRecipe">recipe</span> expect-from-channel [
  <span class="Constant">local-scope</span>
  stdin:address:channel<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  expected:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  screen:address<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  c:character, stdin:address:channel<span class="Special"> &lt;- </span>read stdin:address:channel
  match?:boolean<span class="Special"> &lt;- </span>equal c:character, expected:character
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> match?:boolean
    s:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[expected character not found]</span>
    print-string screen:address, s:address:array:character
  <span class="Delimiter">}</span>
  result:boolean<span class="Special"> &lt;- </span>not match?:boolean
  <span class="muControl">reply</span> result:boolean
]

<span class="muScenario">scenario</span> read-move-blocking [
  assume-screen <span class="Constant">20:literal/width</span>, <span class="Constant">2:literal/height</span>
  run [
<span class="CommentedCode">#?     $start-tracing #? 1</span>
    1:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">2:literal</span>
<span class="CommentedCode">#?     $print [aaa channel address: ], 1:address:channel, 10:literal/newline</span>
    2:number/routine<span class="Special"> &lt;- </span>start-running read-move:<span class="muRecipe">recipe</span>, 1:address:channel, screen:address
    <span class="Comment"># 'read-move' is waiting for input</span>
    wait-for-routine 2:number
<span class="CommentedCode">#?     $print [bbb channel address: ], 1:address:channel, 10:literal/newline</span>
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
<span class="CommentedCode">#?     $print [I: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause <span class="muRecipe">after</span> coming up (<span class="muRecipe">before</span> any keys were pressed)]
    <span class="Comment"># press 'a'</span>
<span class="CommentedCode">#?     $print [ccc channel address: ], 1:address:channel, 10:literal/newline</span>
<span class="CommentedCode">#?     $exit #? 1</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' still waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
<span class="CommentedCode">#?     $print [II: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause <span class="muRecipe">after</span> rank 'a']
    <span class="Comment"># press '2'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">50:literal</span>  <span class="Comment"># '2'</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' still waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
<span class="CommentedCode">#?     $print [III: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause <span class="muRecipe">after</span> file 'a2']
    <span class="Comment"># press '-'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">45:literal</span>  <span class="Comment"># '-'</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' still waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number
<span class="CommentedCode">#?     $print [IV: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?/routine-state, [
F read-move-blocking: routine failed to pause <span class="muRecipe">after</span> hyphen 'a2-']
    <span class="Comment"># press 'a'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' still waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number
<span class="CommentedCode">#?     $print [V: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?/routine-state, [
F read-move-blocking: routine failed to pause <span class="muRecipe">after</span> rank 'a2-a']
    <span class="Comment"># press '4'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">52:literal</span>  <span class="Comment"># '4'</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' still waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number
<span class="CommentedCode">#?     $print [VI: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-blocking: routine failed to pause <span class="muRecipe">after</span> file 'a2-a4']
    <span class="Comment"># press 'newline'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">10:literal</span>  <span class="Comment"># newline</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' now completes</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number
<span class="CommentedCode">#?     $print [VII: routine ], 2:number, [ state ], 3:number 10:literal/newline</span>
    4:boolean/completed?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">1:literal/completed</span>
    assert 4:boolean/completed?, [
F read-move-blocking: routine failed to terminate on newline]
    trace <span class="Constant">[test]</span>, <span class="Constant">[reached end]</span>
  ]
  trace-should-contain [
    test: reached end
  ]
]

<span class="muScenario">scenario</span> read-move-quit [
  assume-screen <span class="Constant">20:literal/width</span>, <span class="Constant">2:literal/height</span>
  run [
    1:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">2:literal</span>
    2:number/routine<span class="Special"> &lt;- </span>start-running read-move:<span class="muRecipe">recipe</span>, 1:address:channel, screen:address
    <span class="Comment"># 'read-move' is waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-quit: routine failed to pause <span class="muRecipe">after</span> coming up (<span class="muRecipe">before</span> any keys were pressed)]
    <span class="Comment"># press 'q'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">113:literal</span>  <span class="Comment"># 'q'</span>
    restart 2:number/routine
    <span class="Comment"># 'read-move' completes</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
    4:boolean/completed?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">1:literal/completed</span>
    assert 4:boolean/completed?, [
F read-move-quit: routine failed to terminate on 'q']
    trace <span class="Constant">[test]</span>, <span class="Constant">[reached end]</span>
  ]
  trace-should-contain [
    test: reached end
  ]
]

<span class="muScenario">scenario</span> read-move-illegal-file [
  assume-screen <span class="Constant">20:literal/width</span>, <span class="Constant">2:literal/height</span>
  run [
    1:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">2:literal</span>
    2:number/routine<span class="Special"> &lt;- </span>start-running read-move:<span class="muRecipe">recipe</span>, 1:address:channel, screen:address
    <span class="Comment"># 'read-move' is waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-file: routine failed to pause <span class="muRecipe">after</span> coming up (<span class="muRecipe">before</span> any keys were pressed)]
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">50:literal</span>  <span class="Comment"># '2'</span>
    restart 2:number/routine
    wait-for-routine 2:number
  ]
  screen-should-contain [
   <span class="Constant"> .file too low: 2     .</span>
   <span class="Constant"> .                    .</span>
  ]
]

<span class="muScenario">scenario</span> read-move-illegal-rank [
  assume-screen <span class="Constant">20:literal/width</span>, <span class="Constant">2:literal/height</span>
  run [
    1:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">2:literal</span>
    2:number/routine<span class="Special"> &lt;- </span>start-running read-move:<span class="muRecipe">recipe</span>, 1:address:channel, screen:address
    <span class="Comment"># 'read-move' is waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-file: routine failed to pause <span class="muRecipe">after</span> coming up (<span class="muRecipe">before</span> any keys were pressed)]
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
    restart 2:number/routine
    wait-for-routine 2:number
  ]
  screen-should-contain [
   <span class="Constant"> .rank too high: a    .</span>
   <span class="Constant"> .                    .</span>
  ]
]

<span class="muScenario">scenario</span> read-move-empty [
  assume-screen <span class="Constant">20:literal/width</span>, <span class="Constant">2:literal/height</span>
  run [
    1:address:channel<span class="Special"> &lt;- </span>new-channel <span class="Constant">2:literal</span>
    2:number/routine<span class="Special"> &lt;- </span>start-running read-move:<span class="muRecipe">recipe</span>, 1:address:channel, screen:address
    <span class="Comment"># 'read-move' is waiting for input</span>
    wait-for-routine 2:number
    3:number<span class="Special"> &lt;- </span>routine-state 2:number/id
    4:boolean/waiting?<span class="Special"> &lt;- </span>equal 3:number/routine-state, <span class="Constant">2:literal/waiting</span>
    assert 4:boolean/waiting?, [
F read-move-file: routine failed to pause <span class="muRecipe">after</span> coming up (<span class="muRecipe">before</span> any keys were pressed)]
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">10:literal/newline</span>
    1:address:channel<span class="Special"> &lt;- </span>write 1:address:channel, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
    restart 2:number/routine
    wait-for-routine 2:number
  ]
  screen-should-contain [
   <span class="Constant"> .that's not enough   .</span>
   <span class="Constant"> .                    .</span>
  ]
]

<span class="muRecipe">recipe</span> make-move [
  <span class="Constant">local-scope</span>
  b:address:array:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  m:address:move<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  from-file:number<span class="Special"> &lt;- </span>get m:address:move/deref, from-file:offset
<span class="CommentedCode">#?   $print from-file:number, 10:literal/newline</span>
  from-rank:number<span class="Special"> &lt;- </span>get m:address:move/deref, from-rank:offset
<span class="CommentedCode">#?   $print from-rank:number, 10:literal/newline</span>
  to-file:number<span class="Special"> &lt;- </span>get m:address:move/deref, to-file:offset
<span class="CommentedCode">#?   $print to-file:number, 10:literal/newline</span>
  to-rank:number<span class="Special"> &lt;- </span>get m:address:move/deref, to-rank:offset
<span class="CommentedCode">#?   $print to-rank:number, 10:literal/newline</span>
  f:address:array:character<span class="Special"> &lt;- </span>index b:address:array:address:array:character/deref, from-file:number
  src:address:character/square<span class="Special"> &lt;- </span>index-address f:address:array:character/deref, from-rank:number
  f:address:array:character<span class="Special"> &lt;- </span>index b:address:array:address:array:character/deref, to-file:number
  dest:address:character/square<span class="Special"> &lt;- </span>index-address f:address:array:character/deref, to-rank:number
<span class="CommentedCode">#?   $print src:address:character/deref, 10:literal/newline</span>
  dest:address:character/deref/square<span class="Special"> &lt;- </span>copy src:address:character/deref/square
  src:address:character/deref/square<span class="Special"> &lt;- </span>copy <span class="Constant">32:literal</span>  <span class="Comment"># ' '</span>
  <span class="muControl">reply</span> b:address:array:address:array:character/same-as-ingredient:0
]

<span class="muScenario">scenario</span> making-a-move [
  assume-screen <span class="Constant">30:literal/width</span>, <span class="Constant">12:literal/height</span>
  run [
    2:address:array:address:array:character/board<span class="Special"> &lt;- </span>initial-position
    3:address:move<span class="Special"> &lt;- </span>new move:type
    4:address:number<span class="Special"> &lt;- </span>get-address 3:address:move/deref, from-file:offset
    4:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">6:literal/g</span>
    5:address:number<span class="Special"> &lt;- </span>get-address 3:address:move/deref, from-rank:offset
    5:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">1:literal/2</span>
    6:address:number<span class="Special"> &lt;- </span>get-address 3:address:move/deref, to-file:offset
    6:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">6:literal/g</span>
    7:address:number<span class="Special"> &lt;- </span>get-address 3:address:move/deref, to-rank:offset
    7:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">3:literal/4</span>
    2:address:array:address:array:character/board<span class="Special"> &lt;- </span>make-move 2:address:array:address:array:character/board, 3:address:move
    screen:address<span class="Special"> &lt;- </span>print-board screen:address, 2:address:array:address:array:character/board
  ]
  screen-should-contain [
  <span class="Comment">#  012345678901234567890123456789</span>
   <span class="Constant"> .8 | r n b q k b n r           .</span>
   <span class="Constant"> .7 | p p p p p p p p           .</span>
   <span class="Constant"> .6 |                           .</span>
   <span class="Constant"> .5 |                           .</span>
   <span class="Constant"> .4 |             P             .</span>
   <span class="Constant"> .3 |                           .</span>
   <span class="Constant"> .2 | P P P P P P   P           .</span>
   <span class="Constant"> .1 | R N B Q K B N R           .</span>
   <span class="Constant"> .  +----------------           .</span>
   <span class="Constant"> .    a b c d e f g h           .</span>
   <span class="Constant"> .                              .</span>
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->