From cb28ddcba9b9c87f14e19e0a0133e1c89063490c Mon Sep 17 00:00:00 2001 From: Ethan Karson Date: Fri, 8 Jun 2018 12:58:37 -0700 Subject: [PATCH 1/3] Allow appending plain text to twiml nodes --- tests/unit/twiml/__init__.py | 2 +- tests/unit/twiml/test_messaging_response.py | 21 ++++++++++++++++ twilio/twiml/__init__.py | 28 ++++++++++----------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/tests/unit/twiml/__init__.py b/tests/unit/twiml/__init__.py index 2681336661..b589cf460e 100644 --- a/tests/unit/twiml/__init__.py +++ b/tests/unit/twiml/__init__.py @@ -18,7 +18,7 @@ def strip(self, xml): @raises(TwiMLException) def test_append_fail(self): t = TwiML() - t.append('foobar') + t.append(12345) def test_format_language_none(self): language = None diff --git a/tests/unit/twiml/test_messaging_response.py b/tests/unit/twiml/test_messaging_response.py index bc9d9efb09..89233b88b0 100644 --- a/tests/unit/twiml/test_messaging_response.py +++ b/tests/unit/twiml/test_messaging_response.py @@ -88,3 +88,24 @@ def test_redirect(self): self.strip(r), 'example.com' ) + +class TestTest(TwilioTest): + def test_text(self): + r = MessagingResponse() + r.append('No tags!') + + assert_equal( + self.strip(r), + 'No tags!' + ) + + def text_mixed(self): + r = MessagingResponse() + r.append('before') + r.append(Body('Content')) + r.append('after') + + assert_equal( + self.strip(r), + 'beforeContentafter' + ) diff --git a/twilio/twiml/__init__.py b/twilio/twiml/__init__.py index 97892de90e..7cd4fb40ab 100644 --- a/twilio/twiml/__init__.py +++ b/twilio/twiml/__init__.py @@ -1,11 +1,3 @@ -# coding=utf-8 -""" -This code was generated by -\ / _ _ _| _ _ - | (_)\/(_)(_|\/| |(/_ v1.0.0 - / / -""" - import json import re import xml.etree.ElementTree as ET @@ -79,10 +71,7 @@ def append(self, verb): :returns: self """ - if not isinstance(verb, TwiML): - raise TwiMLException('Only appending of TwiML is allowed') - - self.verbs.append(verb) + self.nest(verb) return self def nest(self, verb): @@ -93,8 +82,8 @@ def nest(self, verb): :returns: the TwiML verb """ - if not isinstance(verb, TwiML): - raise TwiMLException('Only nesting of TwiML is allowed') + if not isinstance(verb, TwiML) and not isinstance(verb, str): + raise TwiMLException('Only nesting of TwiML and strings are allowed') self.verbs.append(verb) return verb @@ -118,7 +107,16 @@ def xml(self): el.text = self.value + last_child = None + for verb in self.verbs: - el.append(verb.xml()) + if isinstance(verb, str): + if last_child: + last_child.tail = verb + else: + el.text = verb + else: + last_child = verb.xml() + el.append(last_child) return el From 1577af2713563c543010caa14b8a0aa9fda11e75 Mon Sep 17 00:00:00 2001 From: Ethan Karson Date: Fri, 8 Jun 2018 13:01:10 -0700 Subject: [PATCH 2/3] add test for voice --- tests/unit/twiml/test_messaging_response.py | 2 +- tests/unit/twiml/test_voice_response.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/unit/twiml/test_messaging_response.py b/tests/unit/twiml/test_messaging_response.py index 89233b88b0..5e533b8395 100644 --- a/tests/unit/twiml/test_messaging_response.py +++ b/tests/unit/twiml/test_messaging_response.py @@ -89,7 +89,7 @@ def test_redirect(self): 'example.com' ) -class TestTest(TwilioTest): +class TestText(TwilioTest): def test_text(self): r = MessagingResponse() r.append('No tags!') diff --git a/tests/unit/twiml/test_voice_response.py b/tests/unit/twiml/test_voice_response.py index b5ed2213b3..4fb0105ad5 100644 --- a/tests/unit/twiml/test_voice_response.py +++ b/tests/unit/twiml/test_voice_response.py @@ -602,3 +602,24 @@ def test_nested_say_play_pause(self): self.strip(r), 'Heyhey.mp3' ) + +class TestText(TwilioTest): + def test_text(self): + r = VoiceResponse() + r.append('No tags!') + + assert_equal( + self.strip(r), + 'No tags!' + ) + + def text_mixed(self): + r = VoiceResponse() + r.append('before') + r.say('Content') + r.append('after') + + assert_equal( + self.strip(r), + 'beforeContentafter' + ) From 4df70eb2712ca85092666be1c9a7f1afdf97c6e8 Mon Sep 17 00:00:00 2001 From: Ethan Karson Date: Fri, 8 Jun 2018 13:27:38 -0700 Subject: [PATCH 3/3] missing newlines! --- tests/unit/twiml/test_messaging_response.py | 1 + tests/unit/twiml/test_voice_response.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/unit/twiml/test_messaging_response.py b/tests/unit/twiml/test_messaging_response.py index 5e533b8395..a1ff7d6141 100644 --- a/tests/unit/twiml/test_messaging_response.py +++ b/tests/unit/twiml/test_messaging_response.py @@ -89,6 +89,7 @@ def test_redirect(self): 'example.com' ) + class TestText(TwilioTest): def test_text(self): r = MessagingResponse() diff --git a/tests/unit/twiml/test_voice_response.py b/tests/unit/twiml/test_voice_response.py index 4fb0105ad5..1bea13424a 100644 --- a/tests/unit/twiml/test_voice_response.py +++ b/tests/unit/twiml/test_voice_response.py @@ -603,6 +603,7 @@ def test_nested_say_play_pause(self): 'Heyhey.mp3' ) + class TestText(TwilioTest): def test_text(self): r = VoiceResponse()