from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks
from twisted.test import proto_helpers

from txgsm.txgsm import TxGSMProtocol


class TxGSMTextCase(TestCase):

    def setUp(self):
        self.protocol = TxGSMProtocol()
        self.protocol.verbose = True
        self.transport = proto_helpers.StringTransport()
        self.protocol.makeConnection(self.transport)
        self._command_index = 0

    def reply(self, data):
        self.protocol.dataReceived(data)

    def get_next_commands(self):
        commands = self.transport.value().split(self.protocol.delimiter)
        self.transport.clear()
        return filter(None, commands)

    def assertCommands(self, commands):
        self.assertEqual(commands, self.get_next_commands())

    @inlineCallbacks
    def test_send_sms(self):
        d = self.protocol.sendSMS('+27761234567', 'hello world')
        self.assertCommands(['AT+CMGS=23'])
        self.reply('> ')
        [pdu_payload] = self.get_next_commands()
        self.reply('OK')
        response = yield d
        self.assertEqual(response, ['OK'])

@@ -465,19 +465,35 @@
 lf, data
+, delimiter=None
 ):%0A
-
         
@@ -804,16 +804,573 @@
 nds())%0A%0A
+    def expectExchange(self, command, reply):%0A        self.assertCommands(%5Bcommand%5D)%0A        self.reply(reply)%0A%0A    @inlineCallbacks%0A    def test_configure_modem(self):%0A        d = self.protocol.configureModem()%0A        self.expectExchange('AT+CMGF=0', 'OK')%0A        self.expectExchange('ATE0', 'OK')%0A        self.expectExchange('AT+CMEE=1', 'OK')%0A        self.expectExchange('AT+WIND=0', 'OK')%0A        self.expectExchange('AT+CSMS=1', 'OK')%0A        self.expectExchange('AT+CSQ', 'OK')%0A        response = yield d%0A        self.assertEqual(response, %5B'OK'%5D)%0A%0A
     @inl
@@ -1520,16 +1520,16 @@
 S=23'%5D)%0A
-
         
@@ -1542,16 +1542,30 @@
 eply('%3E 
+', delimiter='
 ')%0A     

test for configureModem