1from PyObjCTools.TestSupport import * 2import os 3from Foundation import * 4import Foundation 5 6class TestNSLog (TestCase): 7 def _redirect(self): 8 fd = os.open('pyobjc-test-nslog.txt', os.O_RDWR|os.O_CREAT, 0o666) 9 self.real_stderr = os.dup(2) 10 os.dup2(fd, 2) 11 os.close(fd) 12 13 def _cleanup(self): 14 os.dup2(self.real_stderr, 2) 15 fp = open('pyobjc-test-nslog.txt', 'rb') 16 data = fp.read() 17 fp.close() 18 os.unlink('pyobjc-test-nslog.txt') 19 return data 20 21 def testBasic(self): 22 self._redirect() 23 try: 24 NSLog("Hello world") 25 finally: 26 data = self._cleanup().rstrip() 27 28 self.assert_(data.endswith(b'] Hello world')) 29 30 def testWithArguments(self): 31 self._redirect() 32 try: 33 NSLog("Hello %@: the count is %d", "ronald", 99) 34 finally: 35 data = self._cleanup().rstrip() 36 37 self.assert_(data.endswith(b'] Hello ronald: the count is 99')) 38 39 def testWithInvalidFormat(self): 40 self._redirect() 41 try: 42 self.assertRaises(ValueError, NSLog, "Hello %@: the count is %d", "ronald") 43 self.assertRaises(ValueError, NSLog, "Hello %@: the count is %d", "ronald", "foo") 44 self.assertRaises(ValueError, NSLog, "Hello %@: the count is %d", "ronald", 42, "foo") 45 46 finally: 47 data = self._cleanup().rstrip() 48 49class TestNSLogv (TestCase): 50 def testNotSuchThing(self): 51 self.assert_(not hasattr(Foundation, 'NSLogv')) 52 53if __name__ == "__main__": 54 main() 55