# Path: src/petmail/eventsource.py
# class EventSourceParser(basic.LineOnlyReceiver):
#     delimiter = "\n"
# 
#     def __init__(self, handler):
#         self.current_field = None
#         self.current_lines = []
#         self.handler = handler
#         self.done_deferred = defer.Deferred()
# 
#     def connectionLost(self, why):
#         if why.check(ResponseDone):
#             why = None
#         self.done_deferred.callback(why)
# 
#     def dataReceived(self, data):
#         # exceptions here aren't being logged properly, and tests will hang
#         # rather than halt. I suspect twisted.web._newclient's
#         # HTTP11ClientProtocol.dataReceived(), which catches everything and
#         # responds with self._giveUp() but doesn't log.err.
#         try:
#             basic.LineOnlyReceiver.dataReceived(self, data)
#         except:
#             log.err()
#             raise
# 
#     def lineReceived(self, line):
#         if not line:
#             # blank line ends the field
#             self.fieldReceived(self.current_field,
#                                "\n".join(self.current_lines))
#             self.current_field = None
#             self.current_lines[:] = []
#             return
#         if self.current_field is None:
#             self.current_field, data = line.split(": ", 1)
#             self.current_lines.append(data)
#         else:
#             self.current_lines.append(line)
# 
#     def fieldReceived(self, name, data):
#         self.handler(name, data)


from twisted.trial import unittest
from ..eventsource import EventSourceParser

class FakeTransport:
    disconnecting = False

def parse_events(s):
    fields = []
    p = EventSourceParser(lambda name, data: fields.append((name,data)))
    p.makeConnection(FakeTransport())
    p.dataReceived(s)
    return fields

class EventSource(unittest.TestCase):
    def test_parse(self):
        fields = []

# Path: src/petmail/eventsource.py
# class EventSourceParser(basic.LineOnlyReceiver):
#     delimiter = "\n"
# 
#     def __init__(self, handler):
#         self.current_field = None
#         self.current_lines = []
#         self.handler = handler
#         self.done_deferred = defer.Deferred()
# 
#     def connectionLost(self, why):
#         if why.check(ResponseDone):
#             why = None
#         self.done_deferred.callback(why)
# 
#     def dataReceived(self, data):
#         # exceptions here aren't being logged properly, and tests will hang
#         # rather than halt. I suspect twisted.web._newclient's
#         # HTTP11ClientProtocol.dataReceived(), which catches everything and
#         # responds with self._giveUp() but doesn't log.err.
#         try:
#             basic.LineOnlyReceiver.dataReceived(self, data)
#         except:
#             log.err()
#             raise
# 
#     def lineReceived(self, line):
#         if not line:
#             # blank line ends the field
#             self.fieldReceived(self.current_field,
#                                "\n".join(self.current_lines))
#             self.current_field = None
#             self.current_lines[:] = []
#             return
#         if self.current_field is None:
#             self.current_field, data = line.split(": ", 1)
#             self.current_lines.append(data)
#         else:
#             self.current_lines.append(line)
# 
#     def fieldReceived(self, name, data):
#         self.handler(name, data)


# Path: src/petmail/test/test_eventsource.py
from twisted.trial import unittest
from ..eventsource import EventSourceParser


class FakeTransport:
    disconnecting = False

def parse_events(s):
    fields = []
    p = EventSourceParser(lambda name, data: fields.append((name,data)))
    p.makeConnection(FakeTransport())
    p.dataReceived(s)
    return fields

class EventSource(unittest.TestCase):
    def test_parse(self):
        fields = []

        p = EventSourceParser(lambda name, data: fields.append((name,data)))