1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3###############################################################################
4#
5# Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24#
25# https://www.opensource.org/licenses/mit-license.php
26#
27###############################################################################
28
29import sys
30import os
31import unittest
32# Ensure python finds the local simpletap module
33sys.path.append(os.path.dirname(os.path.abspath(__file__)))
34
35from basetest import Task, TestCase
36
37
38class TestUDACustomSort(TestCase):
39    @classmethod
40    def setUpClass(cls):
41        cls.t = Task()
42        cls.t.config('uda.foo.type', 'string')
43        cls.t.config('uda.foo.label', 'Foo')
44        cls.t.config('uda.foo.values', 'H,M,L,')
45        cls.t.config('report.list.columns', 'id,description,foo')
46        cls.t.config('report.list.labels', 'ID,Desc,Foo')
47        cls.t('add four foo:H')
48        cls.t('add three foo:M')
49        cls.t('add two foo:L')
50        cls.t('add one')
51
52    def test_ascending(self):
53        """Ascending custom sort order"""
54        self.t.config('uda.foo.values', 'H,M,L,')
55        code, out, err = self.t('rc.report.list.sort:foo+ list')
56
57        one   = out.find('one')
58        two   = out.find('two')
59        three = out.find('three')
60        four  = out.find('four')
61
62        self.assertTrue(one   < two)
63        self.assertTrue(two   < three)
64        self.assertTrue(three < four)
65
66    def test_descending(self):
67        """Descending custom sort order"""
68        self.t.config('uda.foo.values', 'H,M,L,')
69        code, out, err = self.t('rc.report.list.sort:foo- list')
70
71        one   = out.find('one')
72        two   = out.find('two')
73        three = out.find('three')
74        four  = out.find('four')
75
76        self.assertTrue(four  < three)
77        self.assertTrue(three < two)
78        self.assertTrue(two   < one)
79
80    def test_ridiculous(self):
81        """Ridiculous custom sort order"""
82        self.t.config('uda.foo.values', 'H,M,,L')
83        code, out, err = self.t('rc.report.list.sort:foo- list')
84
85        one   = out.find('one')
86        two   = out.find('two')
87        three = out.find('three')
88        four  = out.find('four')
89
90        self.assertTrue(four  < three)
91        self.assertTrue(three < one)
92        self.assertTrue(one   < two)
93
94
95class TestUDADefaultSort(TestCase):
96    @classmethod
97    def setUpClass(cls):
98        cls.t = Task()
99        cls.t.config('uda.foo.type', 'string')
100        cls.t.config('uda.foo.label', 'Foo')
101        cls.t.config('report.list.columns', 'id,description,foo')
102        cls.t.config('report.list.labels', 'ID,Desc,Foo')
103        cls.t('add one foo:A')
104        cls.t('add three')
105        cls.t('add two foo:B')
106
107    def test_ascending(self):
108        """Ascending default sort order"""
109        code, out, err = self.t('rc.report.list.sort:foo+ list')
110
111        one   = out.find('one')
112        two   = out.find('two')
113        three = out.find('three')
114
115        self.assertTrue(one < two)
116        self.assertTrue(two < three)
117
118    def test_descending(self):
119        """Descending default sort order"""
120        code, out, err = self.t('rc.report.list.sort:foo- list')
121
122        one   = out.find('one')
123        two   = out.find('two')
124        three = out.find('three')
125
126        self.assertTrue(one < three)
127        self.assertTrue(two < one)
128
129
130class TestBug1319(TestCase):
131    def setUp(self):
132        """Executed before each test in the class"""
133        self.t = Task()
134
135    def test_uda_sorting(self):
136        """1319: Verify that UDAs are sorted according to defined order"""
137        self.t.config("uda.when.type",      "string")
138        self.t.config("uda.when.values",    "night,evening,noon,morning")
139
140        self.t.config("report.foo.columns", "id,when,description")
141        self.t.config("report.foo.labels",  "ID,WHEN,DESCRIPTION")
142        self.t.config("report.foo.sort",    "when+")
143
144        self.t("add one when:night")
145        self.t("add two when:evening")
146        self.t("add three when:noon")
147        self.t("add four when:morning")
148
149        code, out, err = self.t("rc.verbose:nothing foo")
150        self.assertRegex(out, "4\s+morning\s+four\s+3\s+noon\s+three\s+2\s+evening\s+two\s+1\s+night\s+one")
151
152
153if __name__ == "__main__":
154    from simpletap import TAPTestRunner
155    unittest.main(testRunner=TAPTestRunner())
156
157# vim: ai sts=4 et sw=4 ft=python
158