File: test_body.py

package info (click to toggle)
python-nbxmpp 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,340 kB
  • sloc: python: 19,639; makefile: 4
file content (68 lines) | stat: -rw-r--r-- 2,083 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import annotations

from test.lib.util import StanzaHandlerTest

import nbxmpp
from nbxmpp.language import LanguageRange
from nbxmpp.structs import BodyData


class TestBody(StanzaHandlerTest):

    def test_body_language(self):
        text = "> Anna wrote:\n> Hi, how are you?\nGreat"
        text_de = "> Anna schrieb\n>Hallo, wie geht es dir?\nGut"

        xml = """
            <message to='anna@example.com' id='message-id2' type='groupchat'>
              <body>%s</body>
              <body xml:lang='en-us'>%s</body>
              <body xml:lang='de-DE'>%s</body>
            </message>
        """ % (
            text,
            text,
            text_de,
        )

        message = nbxmpp.Message(node=xml)

        body_data = BodyData(message, None, None)
        body = body_data.get([LanguageRange(tag="en")])
        self.assertEqual(body, text)

        body = body_data.get(None)
        self.assertEqual(body, text)

        body = body_data.get([LanguageRange(tag="de")])
        self.assertEqual(body, text_de)

        body = body_data.get([LanguageRange(tag="ar")])
        self.assertEqual(body, text)

        xml = """
            <message to='anna@example.com' id='message-id2' type='groupchat'>
              <body xml:lang='en-us'>en</body>
              <body xml:lang='de-DE'>de</body>
            </message>
        """

        message = nbxmpp.Message(node=xml)

        body_data = BodyData(message)
        body = body_data.get([LanguageRange(tag="en")])
        self.assertEqual(body, "en")

        # Language not in body, pick any language
        body_data = BodyData(message)
        body = body_data.get([LanguageRange(tag="ar")])
        self.assertEqual(body, "de")

        # Supply range with priority
        body_data = BodyData(message)
        body = body_data.get([LanguageRange(tag="de"), LanguageRange(tag="en")])
        self.assertEqual(body, "de")

        body_data = BodyData(message)
        body = body_data.get([LanguageRange(tag="ar"), LanguageRange(tag="en")])
        self.assertEqual(body, "en")