summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-02-11 20:27:49 +0100
committerhut <hut@lavabit.com>2010-03-09 14:40:20 +0100
commit0975223399cb7559659f3c04f4f46defe70865cc (patch)
tree58ac5b5d4b8f70b64e6b039b99cbf014c341b058 /test
parentcfe081ec178da88d6adb7edc651a05dd83810f70 (diff)
downloadranger-0975223399cb7559659f3c04f4f46defe70865cc.tar.gz
keyparser: added seperate Tree class
Diffstat (limited to 'test')
-rw-r--r--test/tc_newkeys.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index c8b560ee..5c40e260 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -231,6 +231,54 @@ def translate_keys(obj):
 			for c in bracket_content:
 				yield ord(c)
 
+Nothing = type('nothing', (object, ), {})
+
+class Tree(object):
+	def __init__(self):
+		self._tree = dict()
+
+	def plow(self, iterable, append=Nothing):
+		"""
+		Move along a path, creating nonexistant subtrees
+		The additional argument <append> allows you to define
+		one element which will be appended at the end
+		"""
+		tree = self._tree
+		last_tree = tree
+		char = Nothing
+		for char in iterable:
+			try:
+				newtree = tree[char]
+				if not isinstance(newtree, dict):
+					raise KeyError()
+			except KeyError:
+				newtree = dict()
+				tree[char] = newtree
+			last_tree = tree
+			tree = newtree
+		if append is not Nothing:
+			if char is not Nothing:
+				last_tree[char] = append
+			else:
+				self._tree = append
+		return tree
+
+	def traverse(self, iterable):
+		"""Move along a path, raising exceptions when failed"""
+		tree = self._tree
+		for char in iterable:
+			try:
+				tree = tree[char]
+			except TypeError:
+				raise KeyError("trying to enter leaf")
+			except KeyError:
+				raise KeyError(str(char) + " not in tree " + str(tree))
+		try:
+			return tree
+		except KeyError:
+			raise KeyError(str(char) + " not in tree " + str(tree))
+
+
 class KeyMap(object):
 	"""Contains a tree with all the keybindings"""
 	def __init__(self):
@@ -383,6 +431,11 @@ class Test(PressTestCase):
 		test('k<a<>nz>')
 		test('>nz>')
 
+	def test_tree(self):
+		t = Tree()
+		subtree = t.plow('abcd', "Yes")
+		self.assertEqual("Yes", t.traverse('abcd'))
+
 	def test_add(self):
 		# depends on internals
 		c = KeyMap()
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 */
#!/bin/sh
# Build minimal-size versions of all apps.
# Hacky; only intended for some stats at the moment.

set -e

[ ! -f tools/treeshake ] && {
  echo building tools/treeshake
  c++ -g -O3 tools/treeshake.cc -o tools/treeshake
}

export OS=${OS:-linux}

process() {
  app=$1
  tools/treeshake_translate init.$OS [012]*.subx apps/subx-params.subx apps/$app.subx
  echo "LoC $(cat apps/$app.subx |wc -l) => $(grep -vh '^\s*$\|^\s*#' apps/$app.subx |tools/treeshake |wc -l)"
  echo "LoC including common libraries: $(cat a.in |wc -l) => $(cat a.treeshake |wc -l)"
  echo "binary size: $(ls -lh apps/$app |column 5) => $(ls -lh a.elf |column 5)"
}

if [ $# -gt 0 ]
then
  process $1
  exit 0
fi

echo "== deleting dead code"
for app in factorial crenshaw2-1 crenshaw2-1b hex survey pack dquotes assort tests sigils calls braces
do
  echo "- $app"
  process $app
  mv a.in apps/$app.in
  mv a.treeshake apps/$app.treeshake
  mv a.elf apps/$app.treeshake.bin
done

echo "== testing treeshaken binaries"
for app in factorial crenshaw2-1 crenshaw2-1b
do
  echo $app
  tools/test_treeshake_translate init.$OS [01]*.subx apps/$app.subx
  diff apps/$app a.elf
done

for app in hex survey pack assort dquotes tests
do
  echo $app
  tools/test_treeshake_translate init.$OS [01]*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

for app in sigils calls braces
do
  echo $app
  tools/test_treeshake_translate init.$OS [012]*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

app=mu
echo $app
tools/test_treeshake_translate init.$OS [0-9]*.subx apps/$app.subx
diff apps/$app a.elf