Tests: Convert sorting.t to python

This commit is contained in:
Renato Alves 2015-06-05 22:26:15 +01:00
parent f001499ac0
commit 78525f509d

View file

@ -1,184 +1,230 @@
#! /usr/bin/env perl #!/usr/bin/env python2.7
################################################################################ # -*- coding: utf-8 -*-
## ###############################################################################
## Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez. #
## # Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
## Permission is hereby granted, free of charge, to any person obtaining a copy #
## of this software and associated documentation files (the "Software"), to deal # Permission is hereby granted, free of charge, to any person obtaining a copy
## in the Software without restriction, including without limitation the rights # of this software and associated documentation files (the "Software"), to deal
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # in the Software without restriction, including without limitation the rights
## copies of the Software, and to permit persons to whom the Software is # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## furnished to do so, subject to the following conditions: # copies of the Software, and to permit persons to whom the Software is
## # furnished to do so, subject to the following conditions:
## The above copyright notice and this permission notice shall be included #
## in all copies or substantial portions of the Software. # The above copyright notice and this permission notice shall be included
## # in all copies or substantial portions of the Software.
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS #
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## SOFTWARE. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## # SOFTWARE.
## http://www.opensource.org/licenses/mit-license.php #
## # http://www.opensource.org/licenses/mit-license.php
################################################################################ #
###############################################################################
use strict; import sys
use warnings; import os
use Test::More tests => 98; import re
import unittest
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Ensure environment has no influence. from basetest import Task, TestCase
delete $ENV{'TASKDATA'};
delete $ENV{'TASKRC'};
# Create the rc file.
if (open my $fh, '>', 'sorting.rc')
{
print $fh "data.location=.\n";
close $fh;
}
# Test assorted sort orders. class MetaTests(type):
qx{../src/task rc:sorting.rc add zero 2>&1}; """Helper metaclass to simplify test logic below (TestSorting)
qx{../src/task rc:sorting.rc add priority:H project:A due:yesterday one 2>&1};
qx{../src/task rc:sorting.rc add priority:M project:B due:today two 2>&1};
qx{../src/task rc:sorting.rc add priority:L project:C due:tomorrow three 2>&1};
qx{../src/task rc:sorting.rc add priority:H project:C due:today four 2>&1};
qx{../src/task rc:sorting.rc 2 start 2>&1};
#diag (qx{../src/task rc:sorting.rc list});
my %tests = Creates test_methods in the TestCase class dynamically named after the
( filter used.
# Single sort column. """
'priority-' => ['(?:one.+four|four.+one).+two.+three.+zero'], @staticmethod
'priority+' => ['zero.+three.+two.+(?:one.+four|four.+one)'], def _make_function(filter, expectations):
'project-' => ['(?:three.+four|four.+three).+two.+one.+zero'], def test(self):
'project+' => ['zero.+one.+two.+(?:three.+four|four.+three)'], # ### Body of the usual test_testcase ### #
'start-' => ['one.+zero', 'one.+two', 'one.+three', 'one.+four'], code, out, err = self.t(
'start+' => ['zero.+one', 'two.+one', 'three.+one', 'four.+one'], ("rc.report.list.sort:{0}".format(filter), "list")
'due-' => ['three.+(?:two.+four|four.+two).+one.+zero'], )
'due+' => ['one.+(?:two.+four|four.+two).+three.+zero'],
'description-' => ['zero.+two.+three.+one.+four'],
'description+' => ['four.+one.+three.+two.+zero'],
# Two sort columns. for expected in expectations:
'priority-,project-' => ['four.+one.+two.+three.+zero'], regex = re.compile(expected, re.DOTALL)
'priority-,project+' => ['one.+four.+two.+three.+zero'], self.assertRegexpMatches(out, regex)
'priority+,project-' => ['zero.+three.+two.+four.+one'],
'priority+,project+' => ['zero.+three.+two.+one.+four'],
'priority-,start-' => ['one.+four.+two.+three.+zero'], # Title of test in report
'priority-,start+' => ['four.+one.+two.+three.+zero'], test.__doc__ = "sort:{0}".format(filter)
'priority+,start-' => ['zero.+three.+two.+one.+four'],
'priority+,start+' => ['zero.+three.+two.+four.+one'],
'priority-,due-' => ['four.+one.+two.+three.+zero'], # Rename the function according to its parameters
'priority-,due+' => ['one.+four.+two.+three.+zero'], # Name of function must start with test_ to be ran by unittest
'priority+,due-' => ['zero.+three.+two.+four.+one'], test.__name__ = "test_{0}".format(
'priority+,due+' => ['zero.+three.+two.+one.+four'], filter.replace(",", "_")
.replace("+", "_asc")
.replace("-", "_desc")
)
'priority-,description-' => ['one.+four.+two.+three.+zero'], return test
'priority-,description+' => ['four.+one.+two.+three.+zero'],
'priority+,description-' => ['zero.+three.+two.+one.+four'],
'priority+,description+' => ['zero.+three.+two.+four.+one'],
'project-,priority-' => ['four.+three.+two.+one.+zero'], def __new__(meta, name, bases, dct):
'project-,priority+' => ['three.+four.+two.+one.+zero'], tests = dct.get("TESTS")
'project+,priority-' => ['zero.+one.+two.+four.+three'],
'project+,priority+' => ['zero.+one.+two.+three.+four'],
'project-,start-' => ['three.+four.+two.+one.+zero'], for filter, expectations in tests.iteritems():
'project-,start+' => ['(?:four.+three|three.+four).+two.+one.+zero'], func = meta._make_function(filter, expectations)
'project+,start-' => ['zero.+one.+two.+three.+four'],
'project+,start+' => ['zero.+one.+two.+(?:four.+three|three.+four)'],
'project-,due-' => ['three.+four.+two.+one.+zero'], # Ensure function exists only once
'project-,due+' => ['four.+three.+two.+one.+zero'], try:
'project+,due-' => ['zero.+one.+two.+three.+four'], dct[func.__name__]
'project+,due+' => ['zero.+one.+two.+four.+three'], except KeyError:
dct[func.__name__] = func
else:
raise ValueError(
"Test {0}->{1} exists twice, consider merging"
.format(filter, func.__name__)
)
'project-,description-' => ['three.+four.+two.+one.+zero'], return super(MetaTests, meta).__new__(meta, name, bases, dct)
'project-,description+' => ['four.+three.+two.+one.+zero'],
'project+,description-' => ['zero.+one.+two.+three.+four'],
'project+,description+' => ['zero.+one.+two.+four.+three'],
'start-,priority-' => ['one.+four.+two.+three.+zero'],
'start-,priority+' => ['one.+zero.+three.+two.+four'],
'start+,priority-' => ['four.+two.+three.+zero.+one'],
'start+,priority+' => ['zero.+three.+two.+four.+one'],
'start-,project-' => ['one.+(?:three.+four|four.+three).+two.+zero'], class TestSorting(TestCase):
'start-,project+' => ['one.+zero.+two.+(?:three.+four|four.+three)'], __metaclass__ = MetaTests
'start+,project-' => ['(?:three.+four|four.+three).+two.+zero.+one'],
'start+,project+' => ['zero.+two.+(?:three.+four|four.+three).+one'],
'start-,due-' => ['one.+three.+(?:four.+two|two.+four).+zero'], @classmethod
'start-,due+' => ['one.+(?:four.+two|two.+four).+three.+zero'], def setUpClass(cls):
'start+,due-' => ['three.+(?:four.+two|two.+four).+zero.+one'], cls.t = Task()
'start+,due+' => ['(?:four.+two|two.+four).+three.+zero.+one'],
'start-,description-' => ['one.+zero.+two.+three.+four'], cls.t(("add", "zero"))
'start-,description+' => ['one.+four.+three.+two.+zero'], cls.t(("add", "priority:H", "project:A", "due:yesterday", "one"))
'start+,description-' => ['zero.+two.+three.+four.+one'], cls.t(("add", "priority:M", "project:B", "due:today", "two"))
'start+,description+' => ['four.+three.+two.+zero.+one'], cls.t(("add", "priority:L", "project:C", "due:tomorrow", "three"))
cls.t(("add", "priority:H", "project:C", "due:today", "four"))
cls.t(("2", "start"))
'due-,priority-' => ['three.+four.+two.+one.+zero'], TESTS = {
'due-,priority+' => ['three.+two.+four.+one.+zero'], # Filter # Expected matches/outputs
'due+,priority-' => ['one.+four.+two.+three.+zero'],
'due+,priority+' => ['one.+two.+four.+three.+zero'],
'due-,project-' => ['three.+four.+two.+one.+zero'], # Single sort column.
'due-,project+' => ['three.+two.+four.+one.+zero'], 'priority-': ['(?:one.+four|four.+one).+two.+three.+zero'],
'due+,project-' => ['one.+four.+two.+three.+zero'], 'priority+': ['zero.+three.+two.+(?:one.+four|four.+one)'],
'due+,project+' => ['one.+two.+four.+three.+zero'], 'project-': ['(?:three.+four|four.+three).+two.+one.+zero'],
'project+': ['zero.+one.+two.+(?:three.+four|four.+three)'],
'start-': ['one.+zero', 'one.+two', 'one.+three', 'one.+four'],
'start+': ['zero.+one', 'two.+one', 'three.+one', 'four.+one'],
'due-': ['three.+(?:two.+four|four.+two).+one.+zero'],
'due+': ['one.+(?:two.+four|four.+two).+three.+zero'],
'description-': ['zero.+two.+three.+one.+four'],
'description+': ['four.+one.+three.+two.+zero'],
'due-,start-' => ['three.+(?:four.+two|two.+four).+one.+zero'], # Two sort columns.
'due-,start+' => ['three.+(?:four.+two|two.+four).+one.+zero'], 'priority-,project-': ['four.+one.+two.+three.+zero'],
'due+,start-' => ['one.+(?:four.+two|two.+four).+three.+zero'], 'priority-,project+': ['one.+four.+two.+three.+zero'],
'due+,start+' => ['one.+(?:four.+two|two.+four).+three.+zero'], 'priority+,project-': ['zero.+three.+two.+four.+one'],
'priority+,project+': ['zero.+three.+two.+one.+four'],
'due-,description-' => ['three.+two.+four.+one.+zero'], 'priority-,start-': ['one.+four.+two.+three.+zero'],
'due-,description+' => ['three.+four.+two.+one.+zero'], 'priority-,start+': ['four.+one.+two.+three.+zero'],
'due+,description-' => ['one.+two.+four.+three.+zero'], 'priority+,start-': ['zero.+three.+two.+one.+four'],
'due+,description+' => ['one.+four.+two.+three.+zero'], 'priority+,start+': ['zero.+three.+two.+four.+one'],
'description-,priority-' => ['zero.+two.+three.+one.+four'], 'priority-,due-': ['four.+one.+two.+three.+zero'],
'description-,priority+' => ['zero.+two.+three.+one.+four'], 'priority-,due+': ['one.+four.+two.+three.+zero'],
'description+,priority-' => ['four.+one.+three.+two.+zero'], 'priority+,due-': ['zero.+three.+two.+four.+one'],
'description+,priority+' => ['four.+one.+three.+two.+zero'], 'priority+,due+': ['zero.+three.+two.+one.+four'],
'description-,project-' => ['zero.+two.+three.+one.+four'], 'priority-,description-': ['one.+four.+two.+three.+zero'],
'description-,project+' => ['zero.+two.+three.+one.+four'], 'priority-,description+': ['four.+one.+two.+three.+zero'],
'description+,project-' => ['four.+one.+three.+two.+zero'], 'priority+,description-': ['zero.+three.+two.+one.+four'],
'description+,project+' => ['four.+one.+three.+two.+zero'], 'priority+,description+': ['zero.+three.+two.+four.+one'],
'description-,start-' => ['zero.+two.+three.+one.+four'], 'project-,priority-': ['four.+three.+two.+one.+zero'],
'description-,start+' => ['zero.+two.+three.+one.+four'], 'project-,priority+': ['three.+four.+two.+one.+zero'],
'description+,start-' => ['four.+one.+three.+two.+zero'], 'project+,priority-': ['zero.+one.+two.+four.+three'],
'description+,start+' => ['four.+one.+three.+two.+zero'], 'project+,priority+': ['zero.+one.+two.+three.+four'],
'description-,due-' => ['zero.+two.+three.+one.+four'], 'project-,start-': ['three.+four.+two.+one.+zero'],
'description-,due+' => ['zero.+two.+three.+one.+four'], 'project-,start+': ['(?:four.+three|three.+four).+two.+one.+zero'],
'description+,due-' => ['four.+one.+three.+two.+zero'], 'project+,start-': ['zero.+one.+two.+three.+four'],
'description+,due+' => ['four.+one.+three.+two.+zero'], 'project+,start+': ['zero.+one.+two.+(?:four.+three|three.+four)'],
# Four sort columns. 'project-,due-': ['three.+four.+two.+one.+zero'],
'start+,project+,due+,priority+' => ['zero.+two.+four.+three.+one'], 'project-,due+': ['four.+three.+two.+one.+zero'],
'project+,due+,priority+,start+' => ['zero.+one.+two.+four.+three'], 'project+,due-': ['zero.+one.+two.+three.+four'],
); 'project+,due+': ['zero.+one.+two.+four.+three'],
for my $sort (sort keys %tests) 'project-,description-': ['three.+four.+two.+one.+zero'],
{ 'project-,description+': ['four.+three.+two.+one.+zero'],
my $output = qx{../src/task rc:sorting.rc rc.report.list.sort:${sort} list 2>&1}; 'project+,description-': ['zero.+one.+two.+three.+four'],
for my $expectation (@{$tests{$sort}}) 'project+,description+': ['zero.+one.+two.+four.+three'],
{
like ($output, qr/$expectation/ms, "sort:${sort}");
}
}
# Cleanup. 'start-,priority-': ['one.+four.+two.+three.+zero'],
unlink qw(pending.data completed.data undo.data backlog.data sorting.rc); 'start-,priority+': ['one.+zero.+three.+two.+four'],
exit 0; 'start+,priority-': ['four.+two.+three.+zero.+one'],
'start+,priority+': ['zero.+three.+two.+four.+one'],
'start-,project-': ['one.+(?:three.+four|four.+three).+two.+zero'],
'start-,project+': ['one.+zero.+two.+(?:three.+four|four.+three)'],
'start+,project-': ['(?:three.+four|four.+three).+two.+zero.+one'],
'start+,project+': ['zero.+two.+(?:three.+four|four.+three).+one'],
'start-,due-': ['one.+three.+(?:four.+two|two.+four).+zero'],
'start-,due+': ['one.+(?:four.+two|two.+four).+three.+zero'],
'start+,due-': ['three.+(?:four.+two|two.+four).+zero.+one'],
'start+,due+': ['(?:four.+two|two.+four).+three.+zero.+one'],
'start-,description-': ['one.+zero.+two.+three.+four'],
'start-,description+': ['one.+four.+three.+two.+zero'],
'start+,description-': ['zero.+two.+three.+four.+one'],
'start+,description+': ['four.+three.+two.+zero.+one'],
'due-,priority-': ['three.+four.+two.+one.+zero'],
'due-,priority+': ['three.+two.+four.+one.+zero'],
'due+,priority-': ['one.+four.+two.+three.+zero'],
'due+,priority+': ['one.+two.+four.+three.+zero'],
'due-,project-': ['three.+four.+two.+one.+zero'],
'due-,project+': ['three.+two.+four.+one.+zero'],
'due+,project-': ['one.+four.+two.+three.+zero'],
'due+,project+': ['one.+two.+four.+three.+zero'],
'due-,start-': ['three.+(?:four.+two|two.+four).+one.+zero'],
'due-,start+': ['three.+(?:four.+two|two.+four).+one.+zero'],
'due+,start-': ['one.+(?:four.+two|two.+four).+three.+zero'],
'due+,start+': ['one.+(?:four.+two|two.+four).+three.+zero'],
'due-,description-': ['three.+two.+four.+one.+zero'],
'due-,description+': ['three.+four.+two.+one.+zero'],
'due+,description-': ['one.+two.+four.+three.+zero'],
'due+,description+': ['one.+four.+two.+three.+zero'],
'description-,priority-': ['zero.+two.+three.+one.+four'],
'description-,priority+': ['zero.+two.+three.+one.+four'],
'description+,priority-': ['four.+one.+three.+two.+zero'],
'description+,priority+': ['four.+one.+three.+two.+zero'],
'description-,project-': ['zero.+two.+three.+one.+four'],
'description-,project+': ['zero.+two.+three.+one.+four'],
'description+,project-': ['four.+one.+three.+two.+zero'],
'description+,project+': ['four.+one.+three.+two.+zero'],
'description-,start-': ['zero.+two.+three.+one.+four'],
'description-,start+': ['zero.+two.+three.+one.+four'],
'description+,start-': ['four.+one.+three.+two.+zero'],
'description+,start+': ['four.+one.+three.+two.+zero'],
'description-,due-': ['zero.+two.+three.+one.+four'],
'description-,due+': ['zero.+two.+three.+one.+four'],
'description+,due-': ['four.+one.+three.+two.+zero'],
'description+,due+': ['four.+one.+three.+two.+zero'],
# Four sort columns.
'start+,project+,due+,priority+': ['zero.+two.+four.+three.+one'],
'project+,due+,priority+,start+': ['zero.+one.+two.+four.+three'],
}
if __name__ == "__main__":
from simpletap import TAPTestRunner
unittest.main(testRunner=TAPTestRunner())
# vim: ai sts=4 et sw=4