Commits

Konstantine Rybnikov  committed 2448daa

some base full represent implementation

  • Participants
  • Parent commits bfa84fa

Comments (0)

Files changed (2)

File fat_po_editor/diffutil.py

         assert operation_type in self.operation_types
         if operation_type == 'modify':
             assert isiterable(entry)
+            entry = list(entry)
+            assert len(entry) == 2
         else:
             assert isinstance(entry, POEntry)
         storage = self.operation_to_storage_map[operation_type]
 
     def __unicode__(self):
         """Gives a nice text representation of what happened."""
-        return self.represent(compact=True)
-    
-    def represent(self, compact=False):
         def esc_chunk(s, maxlen):
             """Escapes chunk's ``\\n`` symbols etc. and returns a chunk
             of that.
             return chunk(escape(s), maxlen)
         
         rv = StringBuilder()
-
+        
         list_prefix = u"    * "
         if len(self.operation_to_storage_map['add']):
             rv += _(u"Added phrases:\n")
                                    esc_chunk(item[0].msgid,
                                              MAXLEN - len(list_prefix)))
         return unicode(rv)
+    
+    def full_represent(self):
+        """Full represent should guaranty that it represent all the information
+        needed to fully describe Diff. Used to make sure that two Diff's are
+        equal.
+        """
+        def prefix_lines(prefix, text):
+            return u"\n".join(u"".join([prefix, line])
+                              for line in text.splitlines())
+        
+        rv = StringBuilder()
+        for item in self.operation_to_storage_map['add']:
+            rv += prefix_lines('+ ', unicode(item))
+            rv += u'\n'
+        for item in self.operation_to_storage_map['rm']:
+            rv += prefix_lines('- ', unicode(item))
+            rv += u'\n'
+        for item in self.operation_to_storage_map['modify']:
+            rv += prefix_lines('+ ', unicode(item[0]))
+            rv += u'\n'
+            rv += prefix_lines('- ', unicode(item[1]))
+            rv += u'\n'
+        return unicode(rv)
 
     def __eq__(self, other):
         pass
-    
+
 def compare_two_pofiles(pofile1, pofile2):
     """Returns :class:`~Diff` object that represents difference between two po
     files.

File tests/test_diffutil.py

             (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.entry_append('add', entry)
+        entry = POEntry(msgid=u"Second msgid",
+                        msgstr=u"Second msgstr")
+        diff.entry_append('rm', entry)
+        entry = POEntry(msgid=u"Third msgid",
+                        msgstr=u"Third msgstr")
+        diff.entry_append('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",
         diff2.entry_append('add', entry)
         
         self.assertEquals(diff1 == diff2, True)
-        
+
     def test_eq_simple_noneq(self):
         diff1 = Diff()
         entry = POEntry(msgid=u"First msgid",