(selective-load "mu.arc" section-level) (test-only-settings) (add-code:readfile "trace.mu") (ero "running tests in trace.arc.t (takes ~10 mins)") (freeze function*) (load-system-functions) (reset2) (new-trace "print-trace") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (x:string-address <- new "schedule: main run: main 0: (((1 integer)) <- ((copy)) ((1 literal))) run: main 0: 1 => ((1 integer)) mem: ((1 integer)): 1 <= 1 run: main 1: (((2 integer)) <- ((copy)) ((3 literal))) run: main 1: 3 => ((2 integer)) mem: ((2 integer)): 2 <= 3 run: main 2: (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) mem: ((1 integer)) => 1 mem: ((2 integer)) => 3 run: main 2: 4 => ((3 integer)) mem: ((3 integer)): 3 <= 4 schedule: done with routine") (s:stream-address <- init-stream x:string-address) (traces:instruction-trace-address-array-address <- parse-traces s:stream-address) (screen:terminal-address <- init-fake-terminal 70:literal 15:literal) (browser-state:space-address <- browser-state traces:instruction-trace-address-array-address 30:literal/screen-height) (print-traces-collapsed browser-state:space-address screen:terminal-address) (1:string-address/raw <- get screen:terminal-address/deref data:offset) ) (each routine completed-routines* (awhen rep.routine!error (prn "error - " it))) ;? (prn memory*.1) (when (~screen-contains memory*.1 70 (+ "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) ")) (prn "F - print-traces-collapsed works")) ;? (quit) ;? 1 (reset2) (new-trace "print-trace-from-middle-of-screen") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (x:string-address <- new "schedule: main run: main 0: (((1 integer)) <- ((copy)) ((1 literal))) run: main 0: 1 => ((1 integer)) mem: ((1 integer)): 1 <= 1 run: main 1: (((2 integer)) <- ((copy)) ((3 literal))) run: main 1: 3 => ((2 integer)) mem: ((2 integer)): 2 <= 3 run: main 2: (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) mem: ((1 integer)) => 1 mem: ((2 integer)) => 3 run: main 2: 4 => ((3 integer)) mem: ((3 integer)): 3 <= 4 schedule: done with routine") (s:stream-address <- init-stream x:string-address) (traces:instruction-trace-address-array-address <- parse-traces s:stream-address) (1:terminal-address/raw <- init-fake-terminal 70:literal 15:literal) ; position the cursor away from top of screen (cursor-down 1:terminal-address/raw) (cursor-down 1:terminal-address/raw) (browser-state:space-address <- browser-state traces:instruction-trace-address-array-address 30:literal/screen-height) (print-traces-collapsed browser-state:space-address 1:terminal-address/raw traces:instruction-trace-address-array-address) (2:string-address/raw <- get 1:terminal-address/raw/deref data:offset) ) (each routine completed-routines* (awhen rep.routine!error (prn "error - " it))) (when (~screen-contains memory*.2 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) ")) (prn "F - print-traces-collapsed works anywhere on the screen")) (run-code main2 (print-character 1:terminal-address/raw ((#\* literal)))) (when (~screen-contains memory*.2 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) " "* ")) (prn "F - print-traces-collapsed leaves cursor at next line")) (reset2) (new-trace "process-key-move-up-down") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (x:string-address <- new "schedule: main run: main 0: (((1 integer)) <- ((copy)) ((1 literal))) run: main 0: 1 => ((1 integer)) mem: ((1 integer)): 1 <= 1 run: main 1: (((2 integer)) <- ((copy)) ((3 literal))) run: main 1: 3 => ((2 integer)) mem: ((2 integer)): 2 <= 3 run: main 2: (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) mem: ((1 integer)) => 1 mem: ((2 integer)) => 3 run: main 2: 4 => ((3 integer)) mem: ((3 integer)): 3 <= 4 schedule: done with routine") (s:stream-address <- init-stream x:string-address) (1:instruction-trace-address-array-address/raw <- parse-traces s:stream-address) (2:terminal-address/raw <- init-fake-terminal 70:literal 15:literal) ; position the cursor away from top of screen (cursor-down 2:terminal-address/raw) (cursor-down 2:terminal-address/raw) (3:space-address/raw <- browser-state 1:instruction-trace-address-array-address/raw 30:literal/screen-height) ; draw trace (print-traces-collapsed 3:space-address/raw/browser-state 2:terminal-address/raw 1:instruction-trace-address-array-address/raw) ; move cursor up ; we have no way yet to test special keys like up-arrow (s:string-address <- new "k") (k:keyboard-address <- init-keyboard s:string-address) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) ; draw cursor (replace-character 2:terminal-address/raw ((#\* literal))) (4:string-address/raw <- get 2:terminal-address/raw/deref data:offset) ) (each routine completed-routines* (awhen rep.routine!error (prn "error - " it))) (when (~screen-contains memory*.4 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "* main/ 2 : 4 => ((3 integer)) ")) ;^cursor (prn "F - process-key can move up the cursor")) (run-code main2 (default-space:space-address <- new space:literal 30:literal/capacity) ; reset previous cursor (replace-character 2:terminal-address/raw ((#\+ literal))) ; move cursor up 3 more lines (s:string-address <- new "kkk") (k:keyboard-address <- init-keyboard s:string-address) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (replace-character 2:terminal-address/raw ((#\* literal))) ) ; cursor is now at line 3 (when (~screen-contains memory*.4 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "* main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " ;^cursor "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) ")) (prn "F - process-key can move up multiple times")) ; try to move cursor up thrice more (run-code main3 (default-space:space-address <- new space:literal 30:literal/capacity) (replace-character 2:terminal-address/raw ((#\+ literal))) (s:string-address <- new "kkk") (k:keyboard-address <- init-keyboard s:string-address) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (replace-character 2:terminal-address/raw ((#\* literal))) ) ; cursor doesn't go beyond the first line printed ; stuff on screen before browser-state was initialized is inviolate (when (~screen-contains memory*.4 70 (+ " " " " "* main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " ;^cursor "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) ")) (prn "F - process-key doesn't move above bounds")) ; now move cursor down 4 times (run-code main4 (default-space:space-address <- new space:literal 30:literal/capacity) (replace-character 2:terminal-address/raw ((#\+ literal))) (s:string-address <- new "jjjj") (k:keyboard-address <- init-keyboard s:string-address) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (replace-character 2:terminal-address/raw ((#\* literal))) ) (when (~screen-contains memory*.4 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "* main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " ;^cursor "+ main/ 2 : 4 => ((3 integer)) ")) (prn "F - process-key can move down multiple times")) ; try to move cursor down 4 more times (run-code main5 (default-space:space-address <- new space:literal 30:literal/capacity) (replace-character 2:terminal-address/raw ((#\+ literal))) (s:string-address <- new "jjjj") (k:keyboard-address <- init-keyboard s:string-address) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (process-key 3:space-address/raw/browser-state k:keyboard-address 2:terminal-address/raw) (replace-character 2:terminal-address/raw ((#\* literal))) ) (when (~screen-contains memory*.4 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) " "* ")) (prn "F - process-key doesn't move below bounds")) (reset2) (new-trace "process-key-expand") (run-code main (default-space:space-address <- new space:literal 30:literal/capacity) (x:string-address <- new "schedule: main run: main 0: (((1 integer)) <- ((copy)) ((1 literal))) run: main 0: 1 => ((1 integer)) mem: ((1 integer)): 1 <= 1 run: main 1: (((2 integer)) <- ((copy)) ((3 literal))) run: main 1: 3 => ((2 integer)) mem: ((2 integer)): 2 <= 3 run: main 2: (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) mem: ((1 integer)) => 1 mem: ((2 integer)) => 3 run: main 2: 4 => ((3 integer)) mem: ((3 integer)): 3 <= 4 schedule: done with routine") (s:stream-address <- init-stream x:string-address) (1:instruction-trace-address-array-address/raw <- parse-traces s:stream-address) (2:terminal-address/raw <- init-fake-terminal 70:literal 15:literal) ; position the cursor away from top of screen (cursor-down 2:terminal-address/raw) (cursor-down 2:terminal-address/raw) (3:space-address/raw <- browser-state 1:instruction-trace-address-array-address/raw 30:literal/screen-height) ; draw trace (print-traces-collapsed 3:space-address/raw/browser-state 2:terminal-address/raw 1:instruction-trace-address-array-address/raw) (4:string-address/raw <- get 2:terminal-address/raw/deref data:offset) ) (each routine completed-routines* (awhen rep.routine!error (prn "error - " it))) (when (~screen-contains memory*.4 70 (+ " " " " "+ main/ 0 : (((1 integer)) <- ((copy)) ((1 literal))) " "+ main/ 0 : 1 => ((1 integer)) " "+ main/ 1 : (((2 integer)) <- ((copy)) ((3 literal))) " "+ main/ 1 : 3 => ((2 integer)) " "+ main/ 2 : (((3 integer)) <- ((add)) ((1 integer)) ((2 integer))) " "+ main/ 2 : 4 => ((3 integer)) ")) (prn "F - process-key: before expand")) (run-code main2 (default-space:space-address <- new space:literal 30:literal/capacity) ; move cursor to final line and expand (s:st
# 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()
import unittest
from collections import deque
from ranger.ext.iter_tools import *
class TestCases(unittest.TestCase):
def test_flatten(self):
def f(x):
return list(flatten(x))
self.assertEqual(
[1,2,3,4,5],
f([1,2,3,4,5]))
self.assertEqual(
[1,2,3,4,5],
f([1,[2,3],4,5]))
self.assertEqual(
[1,2,3,4,5],
f([[1,[2,3]],4,5]))
self.assertEqual(
[],
f([[[[]]]]))
self.assertEqual(
['a', 'b', 'fskldfjl'],
f(['a', ('b', 'fskldfjl')]))
self.assertEqual(
['a', 'b', 'fskldfjl'],
f(['a', deque(['b', 'fskldfjl'])]))
self.assertEqual(
set([3.5, 4.3, 5.2, 6.0]),
set(f([6.0, set((3.5, 4.3)), (5.2, )])))
def test_unique(self):
def u(x):
return list(unique(x))
self.assertEqual(
[1,2,3],
u([1,2,3]))
self.assertEqual(
[1,2,3],
u([1,2,3,2,1]))
self.assertEqual(
[1,2,3],
u([1,2,3,1,2,3,2,2,3,1,2,3,1,2,3,2,3,2,1]))
self.assertEqual(
[1,[2,3]],
u([1,[2,3],1,[2,3],[2,3],1,[2,3],1,[2,3],[2,3],1]))
def test_unique_keeps_type(self):
def u(x):
return unique(x)
self.assertEqual(
[1,2,3],
u([1,2,3,1]))
self.assertEqual(
(1,2,3),
u((1,2,3,1)))
self.assertEqual(
set((1,2,3)),
u(set((1,2,3,1))))
self.assertEqual(
deque((1,2,3)),
u(deque((1,2,3,1))))
def test_mount_path(self):
# assuming ismount() is used
def my_ismount(path):
depth = path.count('/')
if path.startswith('/media'):
return depth == 0 or depth == 2
return depth <= 1
from ranger.ext import mount_path
original_ismount = mount_path.ismount
mount_path.ismount = my_ismount
try:
mp = mount_path.mount_path
self.assertEqual('/home', mp('/home/hut/porn/bondage'))
self.assertEqual('/', mp('/'))
self.assertEqual('/media/sdb1', mp('/media/sdb1/foo/bar'))
self.assertEqual('/media/sdc2', mp('/media/sdc2/a/b/c/d/e'))
finally:
mount_path.ismount = original_ismount
# TODO: links are not tested but I don't see how its possible
# without messing around with mounts.
# self.assertEqual('/media/foo',
# mount_path('/media/bar/some_link_to_a_foo_subdirectory'))
def test_openstruct(self):
from ranger.ext.openstruct import OpenStruct
from random import randint, choice
from string import ascii_letters
os = OpenStruct(a='a')
self.assertEqual(os.a, 'a')
self.assertRaises(AttributeError, getattr, os, 'b')
dictionary = {'foo': 'bar', 'zoo': 'zar'}
os = OpenStruct(dictionary)
self.assertEqual(os.foo, 'bar')
self.assertEqual(os.zoo, 'zar')
self.assertRaises(AttributeError, getattr, os, 'sdklfj')
for i in range(100):
attr_name = ''.join(choice(ascii_letters) \
for x in range(randint(3,9)))
value = randint(100,999)
if not attr_name in os:
self.assertRaises(AttributeError, getattr, os, attr_name)
setattr(os, attr_name, value)
value2 = randint(100,999)
setattr(os, attr_name, value2)
self.assertEqual(value2, getattr(os, attr_name))
def test_shell_escape(self):
from ranger.ext.shell_escape import shell_escape, shell_quote
self.assertEqual(r"'luigi'\''s pizza'", shell_quote("luigi's pizza"))
self.assertEqual(r"luigi\'s\ pizza", shell_escape("luigi's pizza"))
self.assertEqual(r"\$lol/foo\\xyz\|\>\<\]\[",
shell_escape(r"$lol/foo\xyz|><]["))
if __name__ == '__main__':
unittest.main()