Source

fat_po_editor / tests / test_diffutil.py

# -*- coding: utf-8 -*-

import unittest

from polib import POEntry, POFile

from fat_po_editor.diffutil import Diff, compare_two_pofiles

class TestDiffUtilDiff(unittest.TestCase):
    def test_append_simple(self):
        diff = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff.append_entry('add', entry)

        self.assertEquals(
            diff.get_entries('add'),
            [entry])

    def test_representation_simple(self):
        diff = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff.append_entry('add', entry)

        self.assertEquals(
            unicode(diff),
            (u"Added phrases:\n"
             u"    * First msgid\n"))

    def test_representation_more_complex(self):
        diff = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff.append_entry('add', entry)
        entry = POEntry(msgid=u"Second msgid",
                        msgstr=u"Second msgstr")
        diff.append_entry('rm', entry)
        entry = [POEntry(msgid=u"Third msgid",
                         msgstr=u"Third msgstr"),
                 POEntry(msgid=u"Third msgid",
                         msgstr=u"Third msgstr")]
        diff.append_entry('modify', entry)
        
        self.assertEquals(
            unicode(diff),
            (u"Added phrases:\n"
             u"    * First msgid\n"
             u"Removed phrases:\n"
             u"    * Second msgid\n"
             u"Modified translation of phrases:\n"
             u"    * Third msgid\n"))

    def test_representation_too_long(self):
        diff = Diff()
        entry = POEntry(msgid=u"F" * 81,
                        msgstr=u"First msgstr")
        diff.append_entry('add', entry)

        self.assertEquals(
            unicode(diff),
            (u"Added phrases:\n"
             u"    * %s...\n") % (u"F" * 71))

    def test_representation_with_newline(self):
        diff = Diff()
        entry = POEntry(msgid=u"First \nmsgid",
                        msgstr=u"First msgstr")
        diff.append_entry('add', entry)

        self.assertEquals(
            unicode(diff),
            (u"Added phrases:\n"
             u"    * First \\nmsgid\n"))

    def test_full_represent(self):
        diff = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff.append_entry('add', entry)
        entry = POEntry(msgid=u"Second msgid",
                        msgstr=u"Second msgstr")
        diff.append_entry('rm', entry)
        entry = POEntry(msgid=u"Third msgid",
                        msgstr=u"Third msgstr")
        diff.append_entry('modify', [entry, entry])

        self.assertEquals(
            diff.full_represent(),
            (u'''+ msgid "First msgid"\n'''
             u'''+ msgstr "First msgstr"\n'''
             u'''- msgid "Second msgid"\n'''
             u'''- msgstr "Second msgstr"\n'''
             u'''+ msgid "Third msgid"\n'''
             u'''+ msgstr "Third msgstr"\n'''
             u'''- msgid "Third msgid"\n'''
             u'''- msgstr "Third msgstr"\n'''))

    def test_eq_simple(self):
        diff1 = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff1.append_entry('add', entry)
        diff2 = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff2.append_entry('add', entry)
        
        self.assertEquals(diff1 == diff2, True)

    def test_eq_simple_noneq(self):
        diff1 = Diff()
        entry = POEntry(msgid=u"First msgid",
                        msgstr=u"First msgstr")
        diff1.append_entry('add', entry)
        diff2 = Diff()
        entry = POEntry(msgid=u"Second msgid",
                        msgstr=u"Second msgstr")
        diff2.append_entry('add', entry)
        
        self.assertEquals(diff1 == diff2, False)

    def test_eq_empty(self):
        diff1 = Diff()
        diff2 = Diff()
        
        self.assertEquals(diff1 == diff2, True)

class TestCompareTwoPofiles(unittest.TestCase):
    def test_empty(self):
        f1 = POFile()
        f2 = POFile()

        self.assertEquals(compare_two_pofiles(f1, f2),
                          Diff())

    def test_simple(self):
        f1 = POFile()
        f2 = POFile()
        expected = Diff()
        
        e = POEntry(msgid=u"First msgid",
                    msgstr=u"First msgstr")
        f1.append(e)
        f2.append(e)
        e = POEntry(msgid=u"Second msgid",
                    msgstr=u"Second msgstr")
        f1.append(e)
        f2.append(e)
        e = POEntry(msgid=u"Third msgid",
                    msgstr=u"Third msgstr")
        f2.append(e)
        expected.append_entry('add', e)
        
        self.assertEquals(compare_two_pofiles(f1, f2),
                          expected)
        
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.