summaryrefslogtreecommitdiff
path: root/subversion/bindings/swig/python/tests/delta.py
blob: 7f6ba6782f01d135ac54432b2ad9c89ff12c07e4 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#
import unittest, setup_path
import os
import tempfile
import svn.delta
import svn.core
from sys import version_info # For Python version check
if version_info[0] >= 3:
  # Python >=3.0
  from io import StringIO
else:
  # Python <3.0
  from cStringIO import StringIO

# Test case for svn.delta
class DeltaTestCase(unittest.TestCase):

  def testTxWindowHandler(self):
    """Test tx_invoke_window_handler"""
    src_stream = StringIO("hello world")
    target_stream = StringIO("bye world")

    # Invoke the window_handler using a helper function
    window_handler, baton = \
       svn.delta.tx_apply(src_stream, target_stream, None)
    svn.delta.tx_invoke_window_handler(window_handler, None, baton)

    # Invoke the window_handler directly (easier!)
    window_handler, baton = \
       svn.delta.tx_apply(src_stream, target_stream, None)
    window_handler(None, baton)

  def testTxWindowHandler_stream_IF(self):
    """Test tx_invoke_window_handler, with svn.core.svn_stream_t object"""
    pool = svn.core.Pool()
    in_str = "hello world"
    src_stream = svn.core.svn_stream_from_stringbuf(in_str)
    content_str = "bye world"
    content_stream = svn.core.svn_stream_from_stringbuf(content_str)
    fd, fname = tempfile.mkstemp()
    os.close(fd)
    try:
      target_stream = svn.core.svn_stream_from_aprfile2(fname, False)
      window_handler, baton = \
          svn.delta.tx_apply(src_stream, target_stream, None)
      svn.delta.tx_send_stream(content_stream, window_handler, baton, pool)
      fp = open(fname, 'rb')
      out_str = fp.read()
      fp.close()
      self.assertEqual(content_str, out_str)
    finally:
      del pool
      try:
        os.remove(fname)
      except OSError:
        pass

  def testTxWindowHandler_Stream_IF(self):
    """Test tx_invoke_window_handler, with svn.core.Stream object"""
    pool = svn.core.Pool()
    in_str = "hello world"
    src_stream = svn.core.Stream(
                    svn.core.svn_stream_from_stringbuf(in_str))
    content_str = "bye world"
    content_stream = svn.core.Stream(
                    svn.core.svn_stream_from_stringbuf(content_str))
    fd, fname = tempfile.mkstemp()
    os.close(fd)
    try:
      target_stream = svn.core.Stream(
                    svn.core.svn_stream_from_aprfile2(fname, False))
      window_handler, baton = \
          svn.delta.tx_apply(src_stream, target_stream, None)
      svn.delta.tx_send_stream(content_stream, window_handler, baton, None)
      fp = open(fname, 'rb')
      out_str = fp.read()
      fp.close()
      self.assertEqual(content_str, out_str)
    finally:
      del pool
      try:
        os.remove(fname)
      except OSError:
        pass

  def testTxdeltaWindowT(self):
    """Test the svn_txdelta_window_t wrapper."""
    a = StringIO("abc\ndef\n")
    b = StringIO("def\nghi\n")

    delta_stream = svn.delta.svn_txdelta(a, b)
    window = svn.delta.svn_txdelta_next_window(delta_stream)

    self.assert_(window.sview_offset + window.sview_len <= len(a.getvalue()))
    self.assert_(window.tview_len <= len(b.getvalue()))
    self.assert_(len(window.new_data) > 0)
    self.assertEqual(window.num_ops, len(window.ops))
    self.assertEqual(window.src_ops, len([op for op in window.ops
      if op.action_code == svn.delta.svn_txdelta_source]))

    # Check that the ops inherit the window's pool
    self.assertEqual(window.ops[0]._parent_pool, window._parent_pool)

def suite():
  return unittest.defaultTestLoader.loadTestsFromTestCase(DeltaTestCase)

if __name__ == '__main__':
  runner = unittest.TextTestRunner()
  runner.run(suite())