pyobjc / pyobjc-framework-Cocoa / PyObjCTest / test_cfstringtokenizer.py

from PyObjCTools.TestSupport import *
from CoreFoundation import *


try:
    long
except NameError:
    long = int


class TestStringTokenizer (TestCase):
    def testTypes(self):
        self.assertIsCFType(CFStringTokenizerRef)

    def testFunctions(self):
        s = b"Spring eens over een boom".decode('ascii')
        v = CFStringTokenizerCopyBestStringLanguage(s, (0, len(s)))
        self.assertEqual(v, 'nl')

        v = CFStringTokenizerGetTypeID()
        self.assertIsInstance(v, (int, long))
        self.assertResultIsCFRetained(CFStringTokenizerCreate)
        tok = CFStringTokenizerCreate(
                None, s, (0, len(s)), kCFStringTokenizerUnitWord,
                CFLocaleCopyCurrent())
        self.assertIsInstance(tok, CFStringTokenizerRef)
        v = CFStringTokenizerGoToTokenAtIndex(tok, 2)
        self.assertEqual(v, kCFStringTokenizerTokenNormal)

        v = CFStringTokenizerGetCurrentTokenRange(tok)
        self.assertEqual(v, CFRange(0, 6))

        v = CFStringTokenizerAdvanceToNextToken(tok)
        self.assertEqual(v, kCFStringTokenizerTokenNormal)

        v = CFStringTokenizerGetCurrentTokenRange(tok)
        self.assertEqual(v, CFRange(7, 4))

        self.assertResultIsCFRetained(CFStringTokenizerCopyCurrentTokenAttribute)
        v = CFStringTokenizerCopyCurrentTokenAttribute(tok, kCFStringTokenizerAttributeLanguage)
        self.assertIs(v, None)
        s = b"A dog jumped over a log. And then some more.".decode('ascii')
        CFStringTokenizerSetString(tok, s, (0, len(s)))

        subref = []
        idx, ranges = CFStringTokenizerGetCurrentSubTokens(tok, None, 20, subref)
        self.assertEqual(idx, 0)
        self.assertEqual(ranges, ())
        self.assertEqual(subref, [])

    def testConstants(self):
        self.assertEqual(kCFStringTokenizerUnitWord                           , 0 )
        self.assertEqual(kCFStringTokenizerUnitSentence                       , 1 )
        self.assertEqual(kCFStringTokenizerUnitParagraph                      , 2 )
        self.assertEqual(kCFStringTokenizerUnitLineBreak                      , 3 )
        self.assertEqual(kCFStringTokenizerUnitWordBoundary                   , 4 )
        self.assertEqual(kCFStringTokenizerAttributeLatinTranscription        , 1 << 16 )
        self.assertEqual(kCFStringTokenizerAttributeLanguage                  , 1 << 17 )
        self.assertEqual(kCFStringTokenizerTokenNone                          , 0 )
        self.assertEqual(kCFStringTokenizerTokenNormal                        , 1 )
        self.assertEqual(kCFStringTokenizerTokenHasSubTokensMask              , 1 << 1 )
        self.assertEqual(kCFStringTokenizerTokenHasDerivedSubTokensMask       , 1 << 2 )
        self.assertEqual(kCFStringTokenizerTokenHasHasNumbersMask             , 1 << 3 )
        self.assertEqual(kCFStringTokenizerTokenHasNonLettersMask             , 1 << 4 )
        self.assertEqual(kCFStringTokenizerTokenIsCJWordMask                  , 1 << 5 )

if __name__ == "__main__":
    main()
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.