emmental/curds/test_sort.py
Anna Schumaker 8a2c817de0 curds: Pass key_func() to sort.bisect()
Rather than relying on giving objects their own sort_key() func. This is
needed to switch over to the new Track class.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2020-11-01 08:59:37 -05:00

43 lines
1.7 KiB
Python

# Copyright 2019 (c) Anna Schumaker.
from . import sort
import string
import unittest
class CharSort:
def __init__(self, char):
self.c = char
class TestSort(unittest.TestCase):
def key_func(self, obj):
return [ obj.c ]
def test_bisect(self):
chars = [ CharSort(c) for c in string.ascii_lowercase ]
self.assertEqual(sort.bisect([ ], "a", self.key_func), (0, None))
self.assertEqual(sort.bisect(chars, ["A"], self.key_func), (0, None))
self.assertEqual(sort.bisect(chars, ["|"], self.key_func), (26, None))
for i, c in enumerate(string.ascii_lowercase):
self.assertEqual(sort.bisect(chars, [c], self.key_func), (i, chars[i]))
def test_key(self):
self.assertEqual(sort.key("Test Key"), [ "test", "key" ])
self.assertEqual(sort.key("Test! Key!"), [ "test", "key" ])
self.assertEqual(sort.key("Test - Key"), [ "test", "key" ])
self.assertEqual(sort.key("The Test Key"), [ "test", "key" ])
self.assertEqual(sort.key("Test The Key"), [ "test", "the", "key" ])
self.assertEqual(sort.key("A Test Key"), [ "test", "key" ])
self.assertEqual(sort.key("Test A Key"), [ "test", "a", "key" ])
self.assertEqual(sort.key("Toäst Keäy"), [ "toast", "keay" ])
def test_key_path(self):
self.assertEqual(sort.key("/a/b/c/d/"), [ "abcd" ])
self.assertEqual(sort.key("trier/Test Album"), [ "trier", "Test Album" ])
self.assertEqual(sort.key("trier/Test Album/"), [ "trier", "Test Album" ])
def test_normalize(self):
self.assertEqual(sort.normalize("Test Text"), "Test Text")
self.assertEqual(sort.normalize("Toäst Keäy"), "Toast Keay")