summary refs log blame commit diff stats
path: root/test/tc_directory.py
blob: f1b204c3616c8e9a00d16a4764d823d7a85a2683 (plain) (tree)
3aef94d3<
<!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 - factorial.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #aaaaaa; background-color: #080808; }
body { font-size:12pt; font-family: monospace; color: #aaaaaa; background-color: #080808; }
.subxS2Comment a { color:inherit; }
.subxS1Comment a { color:inherit; }
.subxComment a { color:inherit; }
.subxH2Comment a { color:inherit; }
.subxH1Comment a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.muControl { color:#c0a020; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
.LineNr { color:#444444; }
.Delimiter { color:#800080; }
.Constant { color:#00a0a0; }
.Special { color:#c00000; }
.Comment { color: #8080ff; }
-->
</style>

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

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/factorial.mu'>https://github.com/akkartik/mu/blob/master/factorial.mu</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment"># example program: compute the factorial of 5</span>
<span id="L2" class="LineNr"> 2 </span>
<span id="L3" class="LineNr"> 3 </span><span class="muRecipe">def</span> <a href='factorial.mu.html#L3'>main</a> [
<span id="L4" class="LineNr"> 4 </span>  <span class="Constant">local-scope</span>
<span id="L5" class="LineNr"> 5 </span>  x:num <span class="Special">&lt;-</span> <a href='factorial.mu.html#L10'>factorial</a><span class="Constant"> 5</span>
<span id="L6" class="LineNr"> 6 </span>  $print <span class="Constant">[result: ]</span>, x, <span class="Constant">[ </span>
<span id="L7" class="LineNr"> 7 </span><span class="Constant">]</span>
<span id="L8" class="LineNr"> 8 </span>]
<span id="L9" class="LineNr"> 9 </span>
<span id="L10" class="LineNr">10 </span><span class="muRecipe">def</span> <a href='factorial.mu.html#L10'>factorial</a> n:num<span class="muRecipe"> -&gt; </span>result:num [
<span id="L11" class="LineNr">11 </span>  <span class="Constant">local-scope</span>
<span id="L12" class="LineNr">12 </span>  <span class="Constant">load-inputs</span>
<span id="L13" class="LineNr">13 </span>  <span class="Delimiter">{</span>
<span id="L14" class="LineNr">14 </span>    <span class="Comment"># if n=0 return 1</span>
<span id="L15" class="LineNr">15 </span>    zero?:bool <span class="Special">&lt;-</span> equal n,<span class="Constant"> 0</span>
<span id="L16" class="LineNr">16 </span>    <span class="muControl">break-unless</span> zero?
<span id="L17" class="LineNr">17 </span>   <span class="muControl"> return</span><span class="Constant"> 1</span>
<span id="L18" class="LineNr">18 </span>  <span class="Delimiter">}</span>
<span id="L19" class="LineNr">19 </span>  <span class="Comment"># return n * factorial(n-1)</span>
<span id="L20" class="LineNr">20 </span>  x:num <span class="Special">&lt;-</span> subtract n,<span class="Constant"> 1</span>
<span id="L21" class="LineNr">21 </span>  subresult:num <span class="Special">&lt;-</span> <a href='factorial.mu.html#L10'>factorial</a> x
<span id="L22" class="LineNr">22 </span>  result <span class="Special">&lt;-</span> multiply subresult, n
<span id="L23" class="LineNr">23 </span>]
<span id="L24" class="LineNr">24 </span>
<span id="L25" class="LineNr">25 </span><span class="Comment"># unit test</span>
<span id="L26" class="LineNr">26 </span><span class="muScenario">scenario</span> factorial-test [
<span id="L27" class="LineNr">27 </span>  run [
<span id="L28" class="LineNr">28 </span>    1:num <span class="Special">&lt;-</span> &
# 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()