def testParameterEncodingDecoding(self): prop = Property(name="X-FOO", value="Test") prop.addParameter(Parameter("X-BAR", '"Check"')) self.assertEqual(str(prop), "X-FOO;X-BAR=^'Check^':Test\r\n") prop.addParameter(Parameter("X-BAR2", "Check\nThis\tOut\n")) self.assertEqual(str(prop), "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test\r\n") data = "X-FOO;X-BAR=^'Check^':Test" prop = Property.parseText(data) self.assertEqual(prop.getParameterValue("X-BAR"), '"Check"') data = "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test" prop = Property.parseText(data) self.assertEqual(prop.getParameterValue("X-BAR"), '"Check"') self.assertEqual(prop.getParameterValue("X-BAR2"), "Check\nThis\tOut\n")
def testHash(self): hashes = [] for item in TestProperty.test_data: prop = Property.parseText(item) hashes.append(hash(prop)) hashes.sort() for i in range(1, len(hashes)): self.assertNotEqual(hashes[i - 1], hashes[i])
def _doNonEquality(caldata): card1 = Card() card1.parse(StringIO.StringIO(caldata)) card2 = Card() card2.parse(StringIO.StringIO(caldata)) card2.addProperty(Property("X-FOO", "BAR")) self.assertNotEqual(card1, card2)
def testParseGenerate(self): for data in TestProperty.test_data: prop = Property.parseText(data) propstr = str(prop) self.assertEqual( propstr[:-2], data, "Failed parse/generate: %s to %s" % ( data, propstr, ))
def testParseProperty(self): items = ( ("ORG:", "ORG:"), ("ORG:Example", "ORG:Example"), ("ORG:Example\, Inc.", "ORG:Example\, Inc."), ("ORG:Example\; Inc;Dept. of Silly Walks", "ORG:Example\; Inc;Dept. of Silly Walks"), ("ORG;VALUE=TEXT:Example", "ORG:Example"), ) for item, result in items: prop = Property.parseText(item) test = prop.getText() self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
def testDefaultValueCreate(self): test_data = ( ("SOURCE", "http://example.com/source", "SOURCE:http://example.com/source\r\n"), ("souRCE", "http://example.com/source", "souRCE:http://example.com/source\r\n"), ("PHOTO", "YWJj", "PHOTO:\r\n YWJj\r\n"), ("photo", "YWJj", "photo:\r\n YWJj\r\n"), ("URL", "http://example.com/tz1", "URL:http://example.com/tz1\r\n"), ) for propname, propvalue, result in test_data: prop = Property(name=propname, value=propvalue) self.assertEqual(str(prop), result)
def testParseProperty(self): items = ( ("ORG:", "ORG:"), ("ORG:Example", "ORG:Example"), ("ORG:Example\, Inc.", "ORG:Example\, Inc."), ("ORG:Example\; Inc;Dept. of Silly Walks", "ORG:Example\; Inc;Dept. of Silly Walks"), ("ORG;VALUE=TEXT:Example", "ORG:Example"), ) for item, result in items: prop = Property.parseText(item) test = prop.getText() self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item, ))
def testParseProperty(self): items = ( ("ADR:", "ADR:;;;;;;"), ("ADR:;", "ADR:;;;;;;"), ("ADR:;;;;;;", "ADR:;;;;;;"), ("ADR:;;123 Main Street;Any Town;CA;91921-1234", "ADR:;;123 Main Street;Any Town;CA;91921-1234;"), ("ADR:;;;;;;USA", "ADR:;;;;;;USA"), ("ADR:POB1", "ADR:POB1;;;;;;"), ("ADR:;EXT", "ADR:;EXT;;;;;"), ("ADR;VALUE=TEXT:;;123 Main Street;Any Town;CA;91921-1234", "ADR:;;123 Main Street;Any Town;CA;91921-1234;"), ) for item, result in items: prop = Property.parseText(item) test = prop.getText() self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
def testParseProperty(self): items = ( ("N:", "N:;;;;"), ("N:;", "N:;;;;"), ("N:;;;;", "N:;;;;"), ("N:Cyrus;Daboo;;Dr.", "N:Cyrus;Daboo;;Dr.;"), ("N:;;;;PhD.", "N:;;;;PhD."), ("N:Cyrus", "N:Cyrus;;;;"), ("N:;Daboo", "N:;Daboo;;;"), ("N;VALUE=TEXT:Cyrus;Daboo;;Dr.", "N:Cyrus;Daboo;;Dr.;"), ) for item, result in items: prop = Property.parseText(item) test = prop.getText() self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
def testEquality(self): for data in TestProperty.test_data: prop1 = Property.parseText(data) prop2 = Property.parseText(data) self.assertEqual(prop1, prop2, "Failed equality: %s" % (data,))
def testParseGenerate(self): for data in TestProperty.test_data: prop = Property.parseText(data) propstr = str(prop) self.assertEqual(propstr[:-2], data, "Failed parse/generate: %s to %s" % (data, propstr))
def addDefaultProperties(self): self.addProperty(Property(definitions.Property_PRODID, Card.sProdID)) self.addProperty(Property(definitions.Property_VERSION, "3.0"))
def testParameterEncodingDecoding(self): prop = Property(name="X-FOO", value="Test") prop.addParameter(Parameter("X-BAR", "\"Check\"")) self.assertEqual(str(prop), "X-FOO;X-BAR=^'Check^':Test\r\n") prop.addParameter(Parameter("X-BAR2", "Check\nThis\tOut\n")) self.assertEqual( str(prop), "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test\r\n") data = "X-FOO;X-BAR=^'Check^':Test" prop = Property.parseText(data) self.assertEqual(prop.getParameterValue("X-BAR"), "\"Check\"") data = "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test" prop = Property.parseText(data) self.assertEqual(prop.getParameterValue("X-BAR"), "\"Check\"") self.assertEqual(prop.getParameterValue("X-BAR2"), "Check\nThis\tOut\n")
def testEquality(self): for data in TestProperty.test_data: prop1 = Property.parseText(data) prop2 = Property.parseText(data) self.assertEqual(prop1, prop2, "Failed equality: %s" % (data, ))
def parseMultipleTextData(cls, ins): if isinstance(ins, str): ins = StringIO(ins) results = [] card = cls(add_defaults=False) LOOK_FOR_VCARD = 0 GET_PROPERTY = 1 state = LOOK_FOR_VCARD # Get lines looking for start of calendar lines = [None, None] while readFoldedLine(ins, lines): line = lines[0] if state == LOOK_FOR_VCARD: # Look for start if line == card.getBeginDelimiter(): # Next state state = GET_PROPERTY # Handle blank line elif len(line) == 0: # Raise if requested, otherwise just ignore if ParserContext.BLANK_LINES_IN_DATA == ParserContext.PARSER_RAISE: raise InvalidData("vCard data has blank lines") # Unrecognized data else: raise InvalidData("vCard data not recognized", line) elif state == GET_PROPERTY: # Look for end of object if line == card.getEndDelimiter(): # Finalise the current calendar card.finalise() # Validate some things if not card.hasProperty("VERSION"): raise InvalidData("vCard missing VERSION", "") results.append(card) # Change state card = Card(add_defaults=False) state = LOOK_FOR_VCARD # Blank line elif len(line) == 0: # Raise if requested, otherwise just ignore if ParserContext.BLANK_LINES_IN_DATA == ParserContext.PARSER_RAISE: raise InvalidData("vCard data has blank lines") # Must be a property else: # Parse parameter/value for top-level calendar item prop = Property.parseText(line) # Check for valid property if not card.validProperty(prop): raise InvalidData("Invalid property", str(prop)) else: card.addProperty(prop) # Check for truncated data if state != LOOK_FOR_VCARD: raise InvalidData("vCard data not complete") return results