|
7 | 7 |
|
8 | 8 | from menu import Menu, MenuItem
|
9 | 9 |
|
| 10 | +# XXX TODO: test MENU_HIDE_EMPTY |
| 11 | +# XXX TODO: test check_children |
| 12 | + |
10 | 13 | class MenuTests(TestCase):
|
| 14 | + """ |
| 15 | + Tests for Menu |
| 16 | + """ |
| 17 | + |
11 | 18 | def setUp(self):
|
12 |
| - # allow the title of kids3-2 to be changed |
| 19 | + """ |
| 20 | + Build some menus for our tests |
| 21 | + """ |
13 | 22 | self.kids3_2_desired_title = None
|
14 | 23 | def kids3_2_title(request):
|
| 24 | + "Allow the title of kids3-2 to be changed" |
15 | 25 | if self.kids3_2_desired_title is not None:
|
16 | 26 | return "-".join([request.path, self.kids3_2_desired_title])
|
17 | 27 | return 'kids3-2'
|
18 | 28 |
|
19 |
| - # hide kids2-2 whenever the request path ends with /hidden |
20 | 29 | def kids2_2_check(request):
|
| 30 | + "Hide kids2-2 whenever the request path ends with /hidden" |
21 | 31 | if request.path.endswith('/hidden'):
|
22 | 32 | return False
|
23 | 33 | return True
|
24 | 34 |
|
25 |
| - kids2 = [ |
26 |
| - MenuItem("kids2-1", "/parent2/kids2-1", weight=999), |
27 |
| - MenuItem("kids2-2", "/kids2-2", check=kids2_2_check) |
28 |
| - ] |
29 |
| - kids3 = [ |
30 |
| - MenuItem("kids3-1", "/parent3/kids3-1", children=[ |
31 |
| - MenuItem("kids3-1-1", "/parent3/kids3-1/kid1", exact_url=True) |
32 |
| - ]), |
| 35 | + # Ensure we can pass children as tuples (or other iterables, like generators) |
| 36 | + # Following the implementation of sorted children there was a bug reported due to children |
| 37 | + # being passed as a tuple, which has no .sort method |
| 38 | + # See: https://github.com/borgstrom/django-simple-menu/issues/38 |
| 39 | + def kids2(): |
| 40 | + "Generator for kids2" |
| 41 | + class RepeatIterator(object): |
| 42 | + "We need this to be reusable -- http://stackoverflow.com/a/1985733" |
| 43 | + def __iter__(self): |
| 44 | + yield MenuItem("kids2-1", "/parent2/kids2-1", weight=999) |
| 45 | + yield MenuItem("kids2-2", "/kids2-2", check=kids2_2_check) |
| 46 | + return RepeatIterator() |
| 47 | + |
| 48 | + def kids3_1(request): |
| 49 | + "Callable for kids3-1" |
| 50 | + return [ |
| 51 | + MenuItem("kids3-1-1", "/parent3/kids3-1/kid1", exact_url=True), |
| 52 | + ] |
| 53 | + |
| 54 | + kids3 = ( |
| 55 | + MenuItem("kids3-1", "/parent3/kids3-1", children=kids3_1), |
33 | 56 | MenuItem(kids3_2_title, "/parent3/kids3-2")
|
34 |
| - ] |
| 57 | + ) |
35 | 58 |
|
36 | 59 | Menu.items = {}
|
37 | 60 | Menu.sorted = {}
|
38 | 61 | Menu.loaded = False
|
| 62 | + |
| 63 | + # add our items. because we set weight to 999 for parent 1 it will become the last child |
| 64 | + # even though it's added first |
39 | 65 | Menu.add_item("test", MenuItem("Parent 1", "/parent1", weight=999))
|
40 |
| - Menu.add_item("test", MenuItem("Parent 2", "/parent2", children=kids2)) |
| 66 | + Menu.add_item("test", MenuItem("Parent 2", "/parent2", children=kids2())) |
41 | 67 | Menu.add_item("test", MenuItem("Parent 3", "/parent3", children=kids3))
|
42 | 68 |
|
43 | 69 | self.factory = RequestFactory()
|
@@ -152,6 +178,10 @@ def test_template_tag_missing_attribute(self):
|
152 | 178 | self.assertEqual(out, "Parent 2,Parent 3,Parent 1,")
|
153 | 179 |
|
154 | 180 | class MenuItemTests(TestCase):
|
| 181 | + """ |
| 182 | + Tests for MenuItem |
| 183 | + """ |
| 184 | + |
155 | 185 | def test_kwargs(self):
|
156 | 186 | """
|
157 | 187 | MenuItems should accept arbitrary keyword args
|
|
0 commit comments