summaryrefslogtreecommitdiff
path: root/subversion/bindings/swig/python/tests/typemap.py
blob: 7f6e839176cb7e590872e78bca94dd5b13a948c8 (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
129
130
131
132
133
#
#
# 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
import os
import tempfile

import svn.core

class SubversionTypemapTestCase(unittest.TestCase):
  """Test cases for the SWIG typemaps argments and return values transration"""

  def test_char_ptr_in(self):
    """Check %typemap(in) IN_STRING works correctly"""
    self.assertEqual(svn.core.svn_path_canonicalize(b'foo'), b'foo')
    self.assertEqual(svn.core.svn_dirent_join(b'foo', 'bar'), b'foo/bar')
    with self.assertRaises(TypeError) as cm:
      svn.core.svn_dirent_join(None, b'bar')
    self.assertEqual(str(cm.exception),
                     "svn_dirent_join() argument base must be"
                     " bytes or str, not %s" % None.__class__.__name__)
    with self.assertRaises(TypeError) as cm:
      svn.core.svn_dirent_join(b'foo', self)
    self.assertEqual(str(cm.exception),
                     "svn_dirent_join() argument component must be"
                     " bytes or str, not %s" % self.__class__.__name__)
    with self.assertRaises(TypeError) as cm:
      svn.core.svn_dirent_join('foo', 10)
    self.assertEqual(str(cm.exception),
                     "svn_dirent_join() argument component must be"
                     " bytes or str, not int")

  def test_char_ptr_in_unicode_exception(self):
    """Check %typemap(in) IN_STRING handles unicode encode error correctly"""
    with self.assertRaises(UnicodeEncodeError):
      svn.core.svn_dirent_join(b'foo', u'\udc62\udc61\udc72')

  def test_char_ptr_may_be_null(self):
    """Check %typemap(in) char *MAY_BE_NULL works correctly"""
    cfg = svn.core.svn_config_create2(False, False)
    self.assertEqual(svn.core.svn_config_get(cfg, b'foo', b'bar', b'baz'),
                     b'baz')
    self.assertEqual(svn.core.svn_config_get(cfg, b'foo', b'bar', 'baz'),
                     b'baz')
    self.assertIsNone(svn.core.svn_config_get(cfg, b'foo', b'bar', None))
    with self.assertRaises(TypeError) as cm:
      svn.core.svn_config_get(cfg, b'foo', b'bar', self)
    self.assertEqual(str(cm.exception),
                     "svn_config_get() argument default_value"
                     " must be bytes or str or None, not %s"
                     % self.__class__.__name__)

  def test_char_ptr_may_be_null_unicode_exception(self):
    """Check %typemap(in) char *MAY_BE_NULL handles unicode encode error correctly"""
    cfg = svn.core.svn_config_create2(False, False)
    with self.assertRaises(UnicodeEncodeError):
      svn.core.svn_config_get(cfg, u'f\udc6fo', b'bar', None)

  def test_make_string_from_ob(self):
    """Check make_string_from_ob and make_svn_string_from_ob work correctly"""
    source_props = { b'a' : b'foo',
                     b'b' :  'foo',
                      'c' : b''     }
    target_props = { b'a' :  '',
                      'b' :  'bar',
                     b'c' : b'baz'  }
    expected     = { b'a' : b'',
                     b'b' : b'bar',
                     b'c' : b'baz'  }
    self.assertEqual(svn.core.svn_prop_diffs(target_props, source_props),
                     expected)

  def test_prophash_from_dict_null_value(self):
    """Check make_svn_string_from_ob_maybe_null work correctly"""
    source_props = {  'a' :  'foo',
                      'b' :  'foo',
                      'c' : None    }
    target_props = {  'a' : None,
                      'b' :  'bar',
                      'c' :  'baz'  }
    expected     = { b'a' : None,
                     b'b' : b'bar',
                     b'c' : b'baz'  }
    self.assertEqual(svn.core.svn_prop_diffs(target_props, source_props),
                     expected)

  def test_make_string_from_ob_unicode_exception(self):
    """Check make_string_from_ob  handles unicode encode error correctly"""
    source_props = { b'a'      : b'foo',
                     b'b'      : u'foo',
                     u'\udc63' : b''     }
    target_props = { b'a'      : u'',
                     u'b'      : u'bar',
                     b'c'      : b'baz'  }
    with self.assertRaises(UnicodeEncodeError):
      svn.core.svn_prop_diffs(target_props, source_props)

  def test_make_svn_string_from_ob_unicode_exception(self):
    """Check make_string_from_ob handles unicode encode error correctly"""
    source_props = { b'a' : b'foo',
                     b'b' :  'foo',
                     u'c' : b''     }
    target_props = { b'a' : u'',
                     u'b' : u'b\udc61r',
                     b'c' : b'baz'  }
    with self.assertRaises(UnicodeEncodeError):
      svn.core.svn_prop_diffs(target_props, source_props)


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

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