test_sentence.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for positioning sentences.
  5. """
  6. from __future__ import absolute_import, division
  7. import itertools
  8. from twisted.positioning import _sentence
  9. from twisted.trial.unittest import TestCase
  10. sentinelValueOne = "someStringValue"
  11. sentinelValueTwo = "someOtherStringValue"
  12. class DummyProtocol(object):
  13. """
  14. A simple, fake protocol.
  15. """
  16. @staticmethod
  17. def getSentenceAttributes():
  18. return ["type", sentinelValueOne, sentinelValueTwo]
  19. class DummySentence(_sentence._BaseSentence):
  20. """
  21. A sentence for L{DummyProtocol}.
  22. """
  23. ALLOWED_ATTRIBUTES = DummyProtocol.getSentenceAttributes()
  24. class MixinProtocol(_sentence._PositioningSentenceProducerMixin):
  25. """
  26. A simple, fake protocol that declaratively tells you the sentences
  27. it produces using L{base.PositioningSentenceProducerMixin}.
  28. """
  29. _SENTENCE_CONTENTS = {
  30. None: [
  31. sentinelValueOne,
  32. sentinelValueTwo,
  33. None # See MixinTests.test_noNoneInSentenceAttributes
  34. ],
  35. }
  36. class MixinSentence(_sentence._BaseSentence):
  37. """
  38. A sentence for L{MixinProtocol}.
  39. """
  40. ALLOWED_ATTRIBUTES = MixinProtocol.getSentenceAttributes()
  41. class SentenceTestsMixin(object):
  42. """
  43. Tests for positioning protocols and their respective sentences.
  44. """
  45. def test_attributeAccess(self):
  46. """
  47. A sentence attribute gets the correct value, and accessing an
  48. unset attribute (which is specified as being a valid sentence
  49. attribute) gets L{None}.
  50. """
  51. thisSentinel = object()
  52. sentence = self.sentenceClass({sentinelValueOne: thisSentinel})
  53. self.assertEqual(getattr(sentence, sentinelValueOne), thisSentinel)
  54. self.assertIsNone(getattr(sentence, sentinelValueTwo))
  55. def test_raiseOnMissingAttributeAccess(self):
  56. """
  57. Accessing a nonexistent attribute raises C{AttributeError}.
  58. """
  59. sentence = self.sentenceClass({})
  60. self.assertRaises(AttributeError, getattr, sentence, "BOGUS")
  61. def test_raiseOnBadAttributeAccess(self):
  62. """
  63. Accessing bogus attributes raises C{AttributeError}, *even*
  64. when that attribute actually is in the sentence data.
  65. """
  66. sentence = self.sentenceClass({"BOGUS": None})
  67. self.assertRaises(AttributeError, getattr, sentence, "BOGUS")
  68. sentenceType = "tummies"
  69. reprTemplate = "<%s (%s) {%s}>"
  70. def _expectedRepr(self, sentenceType="unknown type", dataRepr=""):
  71. """
  72. Builds the expected repr for a sentence.
  73. @param sentenceType: The name of the sentence type (e.g "GPGGA").
  74. @type sentenceType: C{str}
  75. @param dataRepr: The repr of the data in the sentence.
  76. @type dataRepr: C{str}
  77. @return: The expected repr of the sentence.
  78. @rtype: C{str}
  79. """
  80. clsName = self.sentenceClass.__name__
  81. return self.reprTemplate % (clsName, sentenceType, dataRepr)
  82. def test_unknownTypeRepr(self):
  83. """
  84. Test the repr of an empty sentence of unknown type.
  85. """
  86. sentence = self.sentenceClass({})
  87. expectedRepr = self._expectedRepr()
  88. self.assertEqual(repr(sentence), expectedRepr)
  89. def test_knownTypeRepr(self):
  90. """
  91. Test the repr of an empty sentence of known type.
  92. """
  93. sentence = self.sentenceClass({"type": self.sentenceType})
  94. expectedRepr = self._expectedRepr(self.sentenceType)
  95. self.assertEqual(repr(sentence), expectedRepr)
  96. class MixinTests(TestCase, SentenceTestsMixin):
  97. """
  98. Tests for protocols deriving from L{base.PositioningSentenceProducerMixin}
  99. and their sentences.
  100. """
  101. def setUp(self):
  102. self.protocol = MixinProtocol()
  103. self.sentenceClass = MixinSentence
  104. def test_noNoneInSentenceAttributes(self):
  105. """
  106. L{None} does not appear in the sentence attributes of the
  107. protocol, even though it's in the specification.
  108. This is because L{None} is a placeholder for parts of the sentence you
  109. don't really need or want, but there are some bits later on in the
  110. sentence that you do want. The alternative would be to have to specify
  111. things like "_UNUSED0", "_UNUSED1"... which would end up cluttering
  112. the sentence data and eventually adapter state.
  113. """
  114. sentenceAttributes = self.protocol.getSentenceAttributes()
  115. self.assertNotIn(None, sentenceAttributes)
  116. sentenceContents = self.protocol._SENTENCE_CONTENTS
  117. sentenceSpecAttributes = itertools.chain(*sentenceContents.values())
  118. self.assertIn(None, sentenceSpecAttributes)