def test_with_offset(self): # Verify how Origin.with_offset() works as expected. origin1 = Origin(UnknownTextSource(), 1, 2) origin2 = origin1.with_offset(10) self.assertEqual(origin2.line_start, 11) self.assertEqual(origin2.line_end, 12) self.assertIs(origin2.source, origin1.source)
def test_relative_to(self): # Verify how Origin.relative_to() works in various situations. # # If the source does not have relative_to method, nothing is changed. origin = Origin(UnknownTextSource(), 1, 2) self.assertIs(origin.relative_to('/some/path'), origin) # otherwise the source is replaced and a new origin is returned self.assertEqual( Origin(FileTextSource('/some/path/file.txt'), 1, 2).relative_to('/some/path'), Origin(FileTextSource('file.txt'), 1, 2))
def test_relative_to(self): # Verify how Origin.relative_to() works in various situations. # # If the source does not have relative_to method, nothing is changed. origin = Origin(UnknownTextSource(), 1, 2) self.assertIs(origin.relative_to('/some/path'), origin) # otherwise the source is replaced and a new origin is returned self.assertEqual( Origin( FileTextSource('/some/path/file.txt'), 1, 2 ).relative_to('/some/path'), Origin(FileTextSource('file.txt'), 1, 2))
def test_gt(self): # Verify that Origin instances are ordered by their constituting # components. self.assertTrue( Origin(FileTextSource('file.txt'), 1, 1) < Origin( FileTextSource('file.txt'), 1, 2) < Origin( FileTextSource('file.txt'), 1, 3)) self.assertTrue( Origin(FileTextSource('file.txt'), 1, 10) < Origin( FileTextSource('file.txt'), 2, 10) < Origin( FileTextSource('file.txt'), 3, 10)) self.assertTrue( Origin(FileTextSource('file1.txt'), 1, 10) < Origin( FileTextSource('file2.txt'), 1, 10) < Origin( FileTextSource('file3.txt'), 1, 10))
def test_origin_from_stream_is_Unknown(self): # Verify that gen_rfc822_records() uses origin instances with source # equal to UnknownTextSource, when no explicit source is provided and # the stream has no name to infer a FileTextSource() from. expected_origin = Origin(UnknownTextSource(), 1, 1) with StringIO("key:value") as stream: records = type(self).loader(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0].data, {'key': 'value'}) self.assertEqual(records[0].origin, expected_origin)
def test_eq(self): # Verify instances of Origin are all equal to other instances with the # same instance attributes but not equal to instances with different # attributes. origin1 = Origin(self.origin.source, self.origin.line_start, self.origin.line_end) origin2 = Origin(self.origin.source, self.origin.line_start, self.origin.line_end) self.assertTrue(origin1 == origin2) origin_other1 = Origin(self.origin.source, self.origin.line_start + 1, self.origin.line_end) self.assertTrue(origin1 != origin_other1) self.assertFalse(origin1 == origin_other1) origin_other2 = Origin(self.origin.source, self.origin.line_start, self.origin.line_end + 1) self.assertTrue(origin1 != origin_other2) self.assertFalse(origin1 == origin_other2) origin_other3 = Origin(FileTextSource('unrelated'), self.origin.line_start, self.origin.line_end) self.assertTrue(origin1 != origin_other3) self.assertFalse(origin1 == origin_other3)
def test_origin_from_filename_is_filename(self): # If the test's origin has a filename, we need a valid origin # with proper data. # # We're faking the name by using a StringIO subclass with a # name property, which is how rfc822 gets that data. expected_origin = Origin(FileTextSource("file.txt"), 1, 1) with NamedStringIO("key:value", fake_filename="file.txt") as stream: records = type(self).loader(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0].data, {'key': 'value'}) self.assertEqual(records[0].origin, expected_origin)
def test_just_file(self): origin1 = Origin(UnknownTextSource(), 1, 2) origin2 = origin1.just_file() self.assertEqual(origin2.line_start, None) self.assertEqual(origin2.line_end, None) self.assertIs(origin2.source, origin1.source)
def test_with_offset_whole_file(self): origin1 = Origin(UnknownTextSource()) self.assertEqual(origin1.mode(), OriginMode.whole_file) self.assertEqual(origin1.with_offset(10), origin1)
def test_str__whole_file(self): # verify that Origin.__str__() behaves differently when the range # is empty. expected = 'file.txt' observed = str(Origin(FileTextSource('file.txt'))) self.assertEqual(expected, observed)
def test_str__single_line(self): # verify that Origin.__str__() behaves differently when the range # describes a single line. expected = 'file.txt:15' observed = str(Origin(FileTextSource('file.txt'), 15, 15)) self.assertEqual(expected, observed)
def setUp(self): self.origin = Origin(FileTextSource('file.txt'), 10, 12)
def setUp(self): self.raw_data = dict(key=' value') self.data = dict(key='value') self.origin = Origin(FileTextSource('file.txt'), 1, 1) self.record = RFC822Record(self.data, self.origin, self.raw_data)