def test_splitter(self): """StrictFieldWrapper with splitter should use that splitter""" fields = ["label", "count"] splitter = DelimitedSplitter(":", -1) f = StrictFieldWrapper(fields, splitter) self.assertEqual(f("n:k:n:a:sd "), {"label": "n:k:n:a", "count": "sd"}) self.assertEqual(f("nknasd:"), {"label": "nknasd", "count": ""}) self.assertRaises(FieldError, f, "")
def test_default(self): """Default StrictFieldWrapper should wrap fields if count correct""" fields = list("abcde") f = StrictFieldWrapper(fields) self.assertEqual( f("1 2\t\t 3 \n4 5 "), {"a": "1", "b": "2", "c": "3", "d": "4", "e": "5"} ) self.assertRaises(FieldError, f, "") self.assertRaises(FieldError, f, "xy za ")
def test_constructor(self): """StrictFieldWrapper with constructor should use that constructor""" fields = list("ab") f = StrictFieldWrapper(fields, constructor=fake_dict) self.assertEqual(f("x y"), {"a": "x", "b": "y"}) assert isinstance(f("x y"), fake_dict)