Ejemplo n.º 1
0
    def testCreationFailsIfYearsNotOrdered(self):
        """Verify that creation of a range with the first year greater than the last fails."""
        first = 2014
        last = 2013
        regex = r"%d.*is greater than.*%d" % (first, last)

        with self.assertRaisesRegex(ValueError, regex):
            Range(first, last)
Ejemplo n.º 2
0
  def testParseRanges(self):
    """Test the parsing functionality for sequences of ranges."""
    def doTest(rangesString, expected):
      """Parse a string of ranges and compare the results against the expected one."""
      ranges = parseRanges(rangesString)
      self.assertEqual(ranges, expected)

    doTest("2015", [Range(2015, 2015)])
    doTest("2014 -2015", [Range(2014, 2015)])
    doTest("2013- 2015", [Range(2013, 2015)])
    doTest("2012-2015", [Range(2012, 2015)])
    doTest("2014,2015", [Range(2014, 2014), Range(2015, 2015)])
    doTest("2013 , \t2015", [Range(2013, 2013), Range(2015, 2015)])
    doTest("2015, 2013,2014 ", [Range(2015, 2015),
                                Range(2013, 2013),
                                Range(2014, 2014)])
    doTest("2013-2015,2011-2013", [Range(2013, 2015), Range(2011, 2013)])
    # A trailing separator should just be ignored.
    doTest("2011-2012,", [Range(2011, 2012)])
Ejemplo n.º 3
0
  def testNormalizeRanges(self):
    """Test normalizing of lists of ranges."""
    def doTest(ranges, expected):
      """Verify that a normalization produces the expected ranges."""
      normalizeRanges(ranges)
      self.assertEqual(ranges, expected)

    # Merging of a single range should be trivial.
    doTest([Range(2014, 2014)], [Range(2014, 2014)])

    # Merging of two ranges into a single.
    doTest([Range(2014, 2015), Range(2014, 2014)], [Range(2014, 2015)])
    doTest([Range(2014, 2015), Range(2013, 2013)], [Range(2013, 2015)])
    doTest([Range(2013, 2014), Range(2015, 2015)], [Range(2013, 2015)])

    # Merging of three ranges into a single.
    doTest([Range(2014, 2014),
            Range(2014, 2014),
            Range(2014, 2014)], [Range(2014, 2014)])
    doTest([Range(2013, 2015),
            Range(2010, 2010),
            Range(2011, 2012)], [Range(2010, 2015)])
    doTest([Range(2013, 2013),
            Range(2012, 2012),
            Range(1995, 2014)], [Range(1995, 2014)])

    # All sorts of merges (hopefully) with more ranges involved or as a
    # result.
    doTest([Range(2010, 2011),
            Range(2012, 2013),
            Range(2015, 2015)], [Range(2010, 2013), Range(2015, 2015)])

    ranges = [Range(2015, 2015),
              Range(2010, 2011),
              Range(2013, 2013)]
    expected = [Range(2010, 2011),
                Range(2013, 2013),
                Range(2015, 2015)]
    doTest(ranges, expected)

    doTest([Range(2013, 2013),
            Range(2012, 2012),
            Range(1995, 2014),
            Range(2015, 2015)], [Range(1995, 2015)])
Ejemplo n.º 4
0
    def testRangeParsingFailsCreation(self):
        """Verify that parsing of a range fails if the years are not ordered properly."""
        regex = r"is greater than"

        with self.assertRaisesRegex(ValueError, regex):
            Range.parse("2015-2012")
Ejemplo n.º 5
0
 def fail(rangeString):
     """Verify that parsing of a range string fails as expected."""
     with self.assertRaisesRegex(ValueError, regex):
         Range.parse(rangeString)
Ejemplo n.º 6
0
 def testRangeParsing(self):
     """Verify that we can correctly parse ranges."""
     self.assertEqual(Range.parse("2014"), Range(2014, 2014))
     self.assertEqual(Range.parse("2014-2015"), Range(2014, 2015))
     self.assertEqual(Range.parse("2013-2015"), Range(2013, 2015))
Ejemplo n.º 7
0
 def testNoExtension(self):
     """Negative test for the extension check functionality."""
     self.assertFalse(Range(2013, 2014).extendedBy(2016))
     self.assertFalse(Range(2013, 2014).extendedBy(2012))
Ejemplo n.º 8
0
 def testExtensionCheck(self):
     """Verify that checking if a year extends a range works properly."""
     self.assertTrue(Range(2013, 2014).extendedBy(2015))
Ejemplo n.º 9
0
 def testExclusionOfYears(self):
     """Negative test for the "contains" functionality of a range."""
     self.assertFalse(2015 in Range(2014, 2014))
     self.assertFalse(2013 in Range(2014, 2015))
     self.assertFalse(2016 in Range(2013, 2015))
Ejemplo n.º 10
0
 def testContainmentOfYears(self):
     """Test for the "contains" functionality of a range."""
     self.assertTrue(2014 in Range(2014, 2014))
     self.assertTrue(2014 in Range(2014, 2015))
     self.assertTrue(2014 in Range(2013, 2015))
Ejemplo n.º 11
0
 def testConversionIntoString(self):
     """Test for the conversion of a range into a string."""
     self.assertEqual(str(Range(2015, 2015)), "2015")
     self.assertEqual(str(Range(2014, 2015)), "2014-2015")
     self.assertEqual(str(Range(2013, 2015)), "2013-2015")
Ejemplo n.º 12
0
 def testCreationSucceedsForValidRange(self):
     """Verify that creation of a Range object succeeds for valid ranges."""
     Range(2015, 2015)
     Range(2014, 2015)
     Range(2013, 2015)
Ejemplo n.º 13
0
    def testRangeComparison(self):
        """Verify that comparison of two ranges works properly."""
        self.assertLess(Range(2014, 2014), Range(2015, 2015))
        self.assertLess(Range(2013, 2014), Range(2014, 2015))

        self.assertEqual(Range(2013, 2013), Range(2013, 2013))
        self.assertEqual(Range(2014, 2015), Range(2014, 2015))

        self.assertNotEqual(Range(2013, 2014), Range(2013, 2015))
        self.assertNotEqual(Range(2012, 2014), Range(2013, 2014))