summary refs log blame commit diff stats
path: root/test/tc_ext.py
blob: c57d4aa8a6af32397b6fd0dfa7a95ef2e372d495 (plain) (tree)


































































                                                                           









                                                               
                                                     
                                               

                                                  
 





                                                                                    





                                                                               

























                                                                                         
 

                          
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))


if __name__ == '__main__':
	unittest.main()
an class="ow">in-focus?, env show-screen screen loop +next-event:label } ] # some preconditions for attempting to delete a sandbox def should-attempt-delete? click-row:number, click-column:number, env:address:programming-environment-data -> result:boolean [ local-scope load-ingredients # are we below the sandbox editor? click-sandbox-area?:boolean <- click-on-sandbox-area? click-row, click-column, env reply-unless click-sandbox-area?, 0/false # narrower, is the click in the columns spanning the 'copy' button? first-sandbox:address:editor-data <- get *env, current-sandbox:offset assert first-sandbox, [!!] sandbox-left-margin:number <- get *first-sandbox, left:offset sandbox-right-margin:number <- get *first-sandbox, right:offset _, _, _, _, delete-button-left:number <- sandbox-menu-columns sandbox-left-margin, sandbox-right-margin result <- within-range? click-column, delete-button-left, sandbox-right-margin ] def try-delete-sandbox click-row:number, env:address:programming-environment-data -> clicked-on-delete-button?:boolean, env:address:programming-environment-data [ local-scope load-ingredients # identify the sandbox to delete, if the click was actually on the 'delete' button sandbox:address:sandbox-data <- find-sandbox env, click-row return-unless sandbox, 0/false clicked-on-delete-button? <- copy 1/true env <- delete-sandbox env, sandbox ] def delete-sandbox env:address:programming-environment-data, sandbox:address:sandbox-data -> env:address:programming-environment-data [ local-scope load-ingredients curr-sandbox:address:sandbox-data <- get *env, sandbox:offset first-sandbox?:boolean <- equal curr-sandbox, sandbox { # first sandbox? pop break-unless first-sandbox? next-sandbox:address:sandbox-data <- get *curr-sandbox, next-sandbox:offset *env <- put *env, sandbox:offset, next-sandbox } { # not first sandbox? break-if first-sandbox? prev-sandbox:address:sandbox-data <- copy curr-sandbox curr-sandbox <- get *curr-sandbox, next-sandbox:offset { assert curr-sandbox, [sandbox not found! something is wrong.] found?:boolean <- equal curr-sandbox, sandbox break-if found? prev-sandbox <- copy curr-sandbox curr-sandbox <- get *curr-sandbox, next-sandbox:offset loop } # snip sandbox out of its list next-sandbox:address:sandbox-data <- get *curr-sandbox, next-sandbox:offset *prev-sandbox <- put *prev-sandbox, next-sandbox:offset, next-sandbox } # update sandbox count sandbox-count:number <- get *env, number-of-sandboxes:offset sandbox-count <- subtract sandbox-count, 1 *env <- put *env, number-of-sandboxes:offset, sandbox-count # reset scroll if deleted sandbox was last { break-if next-sandbox render-from:number <- get *env, render-from:offset reset-scroll?:boolean <- equal render-from, sandbox-count break-unless reset-scroll? *env <- put *env, render-from:offset, -1 } ] scenario deleting-sandbox-after-scroll [ trace-until 100/app # trace too long assume-screen 100/width, 10/height # initialize environment 1:text <- new [] 2:text <- new [] 3:address:programming-environment-data <- new-programming-environment screen:address:screen, 1:text, 2:text render-all screen, 3:address:programming-environment-data, render # create 2 sandboxes and scroll to second assume-console [ press ctrl-n type [add 2, 2] press F4 type [add 1, 1] press F4 press page-down ] event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data screen-should-contain [ . run (F4) . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0 edit copy delete . . add 1, 1 . . 2 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . 1 edit copy delete . ] # delete the second sandbox assume-console [ left-click 6, 99 ] run [ event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data ] # second sandbox shows in editor; scroll resets to display first sandbox screen-should-contain [ . run (F4) . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0 edit copy delete . . add 1, 1 . . 2 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . . ] ] scenario deleting-top-sandbox-after-scroll [ trace-until 100/app # trace too long assume-screen 100/width, 10/height # initialize environment 1:text <- new [] 2:text <- new [] 3:address:programming-environment-data <- new-programming-environment screen:address:screen, 1:text, 2:text render-all screen, 3:address:programming-environment-data, render # create 2 sandboxes and scroll to second assume-console [ press ctrl-n type [add 2, 2] press F4 type [add 1, 1] press F4 press page-down ] event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data screen-should-contain [ . run (F4) . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0 edit copy delete . . add 1, 1 . . 2 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . 1 edit copy delete . ] # delete the second sandbox assume-console [ left-click 2, 99 ] run [ event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data ] # second sandbox shows in editor; scroll resets to display first sandbox screen-should-contain [ . run (F4) . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0 edit copy delete . . add 2, 2 . . 4 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . . ] ] scenario deleting-final-sandbox-after-scroll [ trace-until 100/app # trace too long assume-screen 100/width, 10/height # initialize environment 1:text <- new [] 2:text <- new [] 3:address:programming-environment-data <- new-programming-environment screen:address:screen, 1:text, 2:text render-all screen, 3:address:programming-environment-data, render # create 2 sandboxes and scroll to second assume-console [ press ctrl-n type [add 2, 2] press F4 type [add 1, 1] press F4 press page-down press page-down ] event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data screen-should-contain [ . run (F4) . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊1 edit copy delete . . add 2, 2 . . 4 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . . ] # delete the second sandbox assume-console [ left-click 2, 99 ] run [ event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data ] # implicitly scroll up to first sandbox screen-should-contain [ . run (F4) . . . .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . 0 edit copy delete . . add 1, 1 . . 2 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . . ] ] scenario deleting-updates-sandbox-count [ trace-until 100/app # trace too long assume-screen 100/width, 10/height # initialize environment 1:text <- new [] 2:text <- new [] 3:address:programming-environment-data <- new-programming-environment screen:address:screen, 1:text, 2:text render-all screen, 3:address:programming-environment-data, render # create 2 sandboxes assume-console [ press ctrl-n type [add 2, 2] press F4 type [add 1, 1] press F4 ] event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data screen-should-contain [ . run (F4) . . . .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . 0 edit copy delete . . add 1, 1 . . 2 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . 1 edit copy delete . . add 2, 2 . . 4 . ] # delete the second sandbox, then try to scroll down twice assume-console [ left-click 3, 99 press page-down press page-down ] run [ event-loop screen:address:screen, console:address:console, 3:address:programming-environment-data ] # shouldn't go past last sandbox screen-should-contain [ . run (F4) . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0 edit copy delete . . add 2, 2 . . 4 . . ┊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━. . . ] ]