1from PyObjCTools.TestSupport import *
2from CoreFoundation import *
3from Foundation import NSCFAttributedString
4
5try:
6    long
7except NameError:
8    long = int
9
10
11
12class TestAttributedString (TestCase):
13    def testTypes(self):
14        try:
15            NSCFAttributedString = objc.lookUpClass('__NSCFAttributedString')
16        except objc.error:
17            NSCFAttributedString = objc.lookUpClass('NSCFAttributedString')
18
19        self.assertIs(CFAttributedStringRef, NSCFAttributedString )
20        self.assertIs(CFMutableAttributedStringRef, NSCFAttributedString )
21
22    def testTypeID(self):
23        v = CFAttributedStringGetTypeID()
24        self.assertIsInstance(v, (int, long))
25
26    def testCreate(self):
27        val = CFAttributedStringCreate(None, b"hello".decode('ascii'), {b'foo'.decode('ascii'): 42})
28        self.assertIsInstance(val, CFAttributedStringRef)
29        val = CFAttributedStringCreateWithSubstring(None, val, (1,2))
30        self.assertIsInstance(val, CFAttributedStringRef)
31        val2 = CFAttributedStringCreateCopy(None, val)
32        self.assertIs(val2, val)
33
34    def testGetting(self):
35        val = CFAttributedStringCreate(None, b"hello".decode('ascii'), {b'foo'.decode('ascii'): 42, b'bar'.decode('ascii'):b'baz'})
36        self.assertIsInstance(val, CFAttributedStringRef)
37        dta = CFAttributedStringGetString(val)
38        self.assertEqual(dta , b"hello".decode('ascii') )
39        l = CFAttributedStringGetLength(val)
40        self.assertEqual(l , 5 )
41        v, rng = CFAttributedStringGetAttributes(val, 1, None)
42        self.assertEqual(v , {b'foo'.decode('ascii'): 42, b'bar'.decode('ascii'): b'baz' } )
43        self.assertEqual(rng , (0, 5) )
44        v, rng = CFAttributedStringGetAttributes(val, 1, objc.NULL)
45        self.assertEqual(v , {b'foo'.decode('ascii'): 42, b'bar'.decode('ascii'): b'baz' } )
46        self.assertEqual(rng , objc.NULL )
47        v, rng = CFAttributedStringGetAttribute(val, 1, b"foo".decode('ascii'), None)
48        self.assertEqual(v , 42 )
49        self.assertEqual(rng , (0, 5) )
50        v, rng = CFAttributedStringGetAttribute(val, 1, b"foo".decode('ascii'), objc.NULL)
51        self.assertEqual(v , 42 )
52        self.assertEqual(rng , objc.NULL )
53        v, rng = CFAttributedStringGetAttributesAndLongestEffectiveRange(val, 1, (0,5), None)
54        self.assertEqual(v , {b"foo".decode('ascii'): 42, b"bar".decode('ascii'): b'baz' } )
55        self.assertEqual(rng , (0, 5) )
56        v, rng = CFAttributedStringGetAttributesAndLongestEffectiveRange(val, 1, (0,5), objc.NULL)
57        self.assertEqual(v , {b"foo".decode('ascii'): 42, b"bar".decode('ascii'): b'baz' } )
58        self.assertEqual(rng , objc.NULL )
59        v, rng = CFAttributedStringGetAttributeAndLongestEffectiveRange(val, 1, b"bar".decode('ascii'), (0,5), None)
60        self.assertEqual(v , b'baz' )
61        self.assertEqual(rng , (0, 5) )
62        v, rng = CFAttributedStringGetAttributeAndLongestEffectiveRange(val, 1, b"bar".decode('ascii'), (0,5), objc.NULL)
63        self.assertEqual(v , b'baz' )
64        self.assertEqual(rng , objc.NULL )
65
66    def testMutableCopy(self):
67        val = CFAttributedStringCreateMutable(None, 0)
68        self.assertIsInstance(val, CFAttributedStringRef)
69        orig = CFAttributedStringCreate(None, b"hello".decode("ascii"), {b'foo'.decode("ascii"): 42, b'bar'.decode("ascii"):'baz'})
70        self.assertIsInstance(orig, CFAttributedStringRef)
71        val = CFAttributedStringCreateMutableCopy(None, 0, orig)
72        self.assertIsInstance(orig, CFAttributedStringRef)
73        self.assertIsNot(val, orig)
74        CFAttributedStringReplaceString(val, (0,3), "Hal")
75        dta = CFAttributedStringGetString(val)
76        self.assertEqual(dta , b"Hallo".decode("ascii") )
77        v = CFAttributedStringGetMutableString(val)
78        self.assertIs(v, None )
79        CFAttributedStringSetAttributes(val, (0, 2), {b'ronald'.decode("ascii"):99}, False)
80        v, rng = CFAttributedStringGetAttributes(val, 1, None)
81        self.assertEqual(v , {b'ronald'.decode("ascii"):99, b'foo'.decode("ascii"): 42, b'bar'.decode("ascii"): 'baz' } )
82        self.assertEqual(rng , (0, 2) )
83        v, rng = CFAttributedStringGetAttributes(val, 3, None)
84        self.assertEqual(v , {b'foo'.decode("ascii"): 42, b'bar'.decode("ascii"): 'baz' } )
85        self.assertEqual(rng , (2, 3) )
86        self.assertIsInstance(rng, CFRange)
87        CFAttributedStringSetAttributes(val, (0, 2), {b'ronald'.decode("ascii"):99}, True)
88        v, rng = CFAttributedStringGetAttributes(val, 1, None)
89        self.assertEqual(v , {b'ronald'.decode("ascii"):99} )
90        self.assertEqual(rng , (0, 2) )
91        CFAttributedStringSetAttribute(val, (1, 3), b'color'.decode("ascii"), b'blue'.decode("ascii"))
92        v, rng = CFAttributedStringGetAttributes(val, 1, None)
93        self.assertEqual(v , {b'ronald'.decode("ascii"):99, b'color'.decode("ascii"):b'blue'.decode("ascii")} )
94        self.assertEqual(rng , (1, 1) )
95        CFAttributedStringRemoveAttribute(val, (1,3), b'color'.decode("ascii"))
96        v, rng = CFAttributedStringGetAttributes(val, 3, None)
97        self.assertEqual(v , {b'foo'.decode("ascii"): 42, b'bar'.decode("ascii"): 'baz' } )
98        self.assertEqual(rng , (2, 2) )
99        rep = CFAttributedStringCreate(None, "dummy", {b'attrib'.decode("ascii"): 99} )
100        CFAttributedStringReplaceAttributedString(val, (1,3), rep)
101        self.assertEqual(CFAttributedStringGetString(val) , b'Hdummyo'.decode("ascii"))
102
103    def testEditing(self):
104        val = CFAttributedStringCreateMutable(None, 0)
105        self.assertIsInstance(val, CFAttributedStringRef)
106        CFAttributedStringBeginEditing(val)
107        CFAttributedStringEndEditing(val)
108
109if __name__ == "__main__":
110    main()
111