pyobjc / pyobjc / Scripts / gen_test_methods.py

   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
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
#!/usr/bin/env python
"""
Script to generate Lib/objc/test/testbnd2.m and Lib/objc/test/test_methods2.py.

These files test method passing from Objective-C to Python subclasses of 
Objective-C classes, and method passing from Python to Objective-C. 

Note that we need two variations for calls from Objective-C to Python: direct
calls and calls using an NSInvocation. The latter use a different calling
mechanism.

NOTES:
* These tests are generated because doing this by hand is *very* labour 
  intensive and boring. The chance of bugs in the testsuite is IMHO larger when
  you manually build these.
* Two argument test-cases are needed to check argument aligment. Three argument
  test-cases would also be nice, but would take way to many space. An inital
  version with 3 argument test-cases generated a C file with over 150K lines
  of code.
* Note that the testcases are very explicit (no loops with self.assert* calls)
  to make it easier to debug problems.

TODO:
- Check that we include all tests from the manually generated version.
  (We don't, see below)
- The 'values' items in the TYPES array need more work: We don't really test
  boundary conditions.
- We don't test invalid arguments, bad number of arguments
- We don't test calling class-methods defined in Python. I wouldn't be 
  surprised if that doesn't work at all.
"""
import sys
import objc

OBJC_HEADER="""\
/*
 *          THIS IS A GENERATED FILE DO NOT EDIT
 *
 * This file implements a number classes that are used to test calling methods,
 * both from Python to Objective-C and from Objective-C to Python. See 
 * test_methods.py for the actual tests.
 *
 */

#include <Python.h>
#include <pyobjc-api.h>
#include <limits.h>

#import <Foundation/Foundation.h>

#if defined(MACOSX) && ((__GNUC__* 100) + (__GNUC_MINOR__)) >= 303

#define HAVE_BOOL

#endif

#if !defined(LLONG_MAX) && defined(LONG_LONG_MAX)

#  define LLONG_MAX LONG_LONG_MAX
#  define LLONG_MIN LONG_LONG_MIN
#  define ULLONG_MAX ULONG_LONG_MAX

#endif


struct TestStruct1 {
    int i;
    int d; 
    short  s[5];
};

struct TestStruct2 {
    int i;
    double d; 
    short  s[5];
};

struct TestStruct3 {
    char ch;
    int  i;
};
"""

OBJC_FOOTER="""\

/* 
 * Some glue to make this a valid Python extension module
 */

static PyMethodDef no_methods[] = {
        { 0, 0, 0, 0 }
};

void inittestbndl2(void); // Remove GCC warning
void inittestbndl2(void)
{
        PyObject* m;

        m = Py_InitModule4("testbndl2", no_methods,
                    NULL, NULL, PYTHON_API_VERSION);
        if (!m) return;

        if (PyObjC_ImportAPI(m) < 0) return;

        PyModule_AddObject(m, "PyObjC_TestClass1",
            PyObjCClass_New([PyObjC_TestClass1 class]));
        PyModule_AddObject(m, "PyObjC_TestClass2",
            PyObjCClass_New([PyObjC_TestClass2 class]));
#ifdef HAVE_BOOL
        PyModule_AddIntConstant(m, "HAVE_BOOL", 1);
#else
        PyModule_AddIntConstant(m, "HAVE_BOOL", 0);
#endif

"""

PY_HEADER="""\
'''
          THIS IS A GENERATED FILE DO NOT EDIT

This file tests method calling to and from Objective-C. See testbndl.m for
the Objective-C helper classes used in theses tests.

'''
import unittest
import objc
from objc import YES, NO, nil
from Foundation import NSPriorDayDesignations
import sys

from objc.test import ctests

#
# NSInvocation in MacOS X 10.2 doesn't like some struct definition, it 
# garbles the contents of those structs. These unittests disable NSInvocation
# tests that fail because of this problem.
#
# ctests.CheckNSInvoke checks if NSInvocation has this problem, it does so
# in pure Objective-C code and will therefore not mask problems with the
# bridge.
#
try:
    ctests.CheckNSInvoke()
    nsinvoke_ok = 1
except AssertionError:
    nsinvoke_ok = 0

NSArray = objc.runtime.NSArray

# First make sure that the pass-by-reference methods have the correct signature
setSignature = objc.setSignatureForSelector

# FIXME
import sys
HAVE_BOOL=sys.platform == 'darwin'
"""

PY_MIDTEXT="""
from objc.test.testbndl2 import *

if hasattr(unittest.TestCase, 'assertAlmostEquals'):
    TestCase = unittest.TestCase
else:
    # We use assertAlmostEquals for comparing floats, python 2.2 doesn't
    # have this method in unittest.TestCase.

    class TestCase (unittest.TestCase):
        def assertAlmostEquals(self, val1, val2, message=None):
            self.assert_ (abs (val1 - val2) < 0.00001, message)

"""

PY_FOOTER="""\

if __name__ == "__main__":
    unittest.main()
"""

#if hasattr(objc, '_C_BOOL'):
TYPES = [('bool', objc._C_BOOL, [ 'YES', 'NO' ]),]
#else:
#    TYPES = []

TYPES.extend([
    # ( typename, objc_signature, testvalues )
    ('BOOL', objc._C_NSBOOL, [ 'YES', 'NO' ]),
    ('char', objc._C_CHR, ('-128', '0', '127') ),
    ('signed short', objc._C_SHT, ('-(1<<14)', '-42', '0', '42', '1 << 14') ),
    ('signed int', objc._C_INT, ('-(1<<30)', '-42', '0', '42', '1 << 30') ),
    ('signed long', objc._C_LNG, ('-(1<<30)', '-42', '0', '42', '1 << 30') ),
    ('signed long long', objc._C_LNGLNG, ('-(1LL << 60)', '-42', '0', '42', '1LL << 60') ),
    ('unsigned char', objc._C_UCHR, ( '0', '128', '255') ),
    ('unsigned short', objc._C_USHT, ('0', '42', '1<<14') ),
    ('unsigned int', objc._C_UINT, ('0', '42', '1 << 30') ),
    ('unsigned long', objc._C_ULNG, ('0', '42', '1L << 30') ),
    ('unsigned long long', objc._C_ULNGLNG, ( '0', '42', '1LL << 62') ),
    ('float', objc._C_FLT, ( '0.128', '1.0', '42.0', '1e10')),
    ('double', objc._C_DBL, ( '0.128', '1.0', '42.0', '1e10')),
    ('id', objc._C_ID, ( 'NSPriorDayDesignations', )), # Fix me
    ('char*', objc._C_CHARPTR, ('"hello"', '"world"', '"foobar"')),
    ('NSPoint', "{_NSPoint=ff}", ((1, 2), (3,4),)),
    ('NSRect', "{_NSRect={_NSPoint=ff}{_NSSize=ff}}", (((1,2), (3,4)), ((7,8),(9,10)),)),
    ('struct TestStruct1', '{_TestStruct1=ii[5s]}', ((1,2,(1,2,3,4,5)), (9,8,(-1,-2,-3,-4,-5)))),
    ('struct TestStruct2', '{_TestStruct2=id[5s]}', ((1,2,(1,2,3,4,5)), (9,8,(-1,-2,-3,-4,-5)))),
    ('struct TestStruct3', '{_TestStruct3=ci}', ((1,2), (2,4))),
])



def IS_PROBLEM(tp):
    # See CheckNSInvoke in PYTHON_HEADER 
    return tp in ['struct TestStruct1', 'struct TestStruct2', 'struct TestStruct3']

def tp2ident(tp):
    return tp.replace(' ', '').replace('*', 'Ptr')

def emit_objc_interfaces(fp):
    fp.write('@interface PyObjC_TestClass1 : NSObject\n{\n}\n\n')

    fp.write('/* Reset the test counter */\n')
    fp.write('+(void)clsReset;\n')
    fp.write('-(void)reset;\n')
    fp.write('\n')

    fp.write('/* Test return values */\n')
    for tp, sign, values in TYPES:
        if not values: continue;
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(%s)%sClsMethod;\n'%(tp, tp2ident(tp)))
        fp.write('-(%s)%sMethod;\n'%(tp, tp2ident(tp)))
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* Single argument passing */\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('-(id)%sArg:(%s)arg;\n'%(tp2ident(tp), tp))
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* Multiple arguments */\n')
    for tp1, sign1, values2 in TYPES:
        for tp2, sign2, values2 in TYPES:
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#ifdef HAVE_BOOL\n')
            fp.write('-(id)%sArg:(%s)arg1 and%sArg:(%s)arg2;\n'%(tp2ident(tp1), tp1, tp2ident(tp2), tp2))
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* in, out and in-out arguments */\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('-(id)%sInArg:(%s*)arg;\n'%(tp2ident(tp), tp))
        fp.write('-(void)%sOutArg:(%s*)arg;\n'%(tp2ident(tp), tp))
        fp.write('-(id)%sInOutArg:(%s*)arg;\n'%(tp2ident(tp), tp))
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('\n@end // interface PyObjC_TestClass1\n')

    fp.write('\n\n\n\n')
    fp.write('@interface PyObjC_TestClass2 : NSObject\n{\n}\n\n')

    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(%s)call%sMethodOf:(PyObjC_TestClass1*)obj;\n'%(tp, tp2ident(tp)))
        fp.write('+(%s)invoke%sMethodOf:(PyObjC_TestClass1*)obj;\n'%(tp, tp2ident(tp)))
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* Single argument passing */\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(id)call%sArg:(%s)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        fp.write('+(id)invoke%sArg:(%s)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* Multiple arguments */\n')
    for tp1, sign1, values2 in TYPES:
        for tp2, sign2, values2 in TYPES:
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#ifdef  HAVE_BOOL\n')
            fp.write('+(id)call%sArg:(%s)arg1 and%sArg:(%s)arg2 of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp1), tp1, tp2ident(tp2), tp2))
            fp.write('+(id)invoke%sArg:(%s)arg1 and%sArg:(%s)arg2 of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp1), tp1, tp2ident(tp2), tp2))
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* in, out and in-out arguments */\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(id)invoke%sInArg:(%s*)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        fp.write('+(id)call%sInArg:(%s*)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        fp.write('+(void)invoke%sOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        fp.write('+(void)call%sOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        fp.write('+(id)invoke%sInOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        fp.write('+(id)call%sInOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj;\n'%(tp2ident(tp), tp))
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('\n@end // interface PyObjC_TestClass2\n\n')


def write_objc_item(fp, item):
    if isinstance(item, (tuple, list)):
        fp.write('{')
        for i in item[:-1]:
            write_objc_item(fp, i)
            fp.write(', ')
        write_objc_item(fp, item[-1])
        fp.write('}')
    else:
        fp.write(str(item))

def write_py_item(fp, item):
    if isinstance(item, (tuple, list)):
        if len(item) > 1:
            fp.write('(')
            for i in item[:-1]:
                write_py_item(fp, i)
                fp.write(', ')
            write_py_item(fp, item[-1])
            fp.write(')')
        else:
            fp.write('(')
            write_py_item(fp, item[0])
            fp.write(',)')
    else:
        fp.write(str(item).replace('LL', 'L'))

def emit_objc_implementations(fp):

    fp.write('/* Some global variables */\n')
    fp.write('static int g_idx = 0;\n')

    for tp, sign, values in TYPES:
        if not values: continue

        if tp == 'id':
            fp.write('static %s g_%s_values[%d];\n'%(tp, tp2ident(tp), len(values)))
            continue

        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')

        fp.write('static %s g_%s_values[] = {\n'%(tp, tp2ident(tp)))
        for item in values[:-1]:
            fp.write('\t')
            write_objc_item(fp, item)
            fp.write(",\n")
        fp.write('\t')
        write_objc_item(fp, values[-1])
        fp.write('\n};\n\n')

        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')


    fp.write('static id arg2id(const char* argtype, void* argptr)\n')
    fp.write('{\n')
    fp.write('\tid res;\n')
    fp.write('\tPyObject* tmp;\n')
    fp.write('\ttmp = PyObjC_ObjCToPython(argtype, argptr);\n')
    fp.write('\tif (tmp == NULL) { PyObjCErr_ToObjC(); return nil; }\n')
    fp.write('\tres = PyObjC_PythonToId(tmp);\n')
    fp.write('\tPy_DECREF(tmp);\n')
    fp.write('\tif (PyErr_Occurred()) { PyObjCErr_ToObjC(); return nil; }\n')
    fp.write('\treturn res;')
    fp.write('}\n\n\n')

    fp.write('@implementation PyObjC_TestClass1 : NSObject\n\n')

    fp.write('+(void)clsReset\n')
    fp.write('{\n')
    fp.write('\tg_idx = 0;\n')
    fp.write('}\n')
    fp.write('-(void)reset;\n')
    fp.write('{\n')
    fp.write('\tg_idx = 0;\n')
    fp.write('}\n')
    fp.write('\n')

    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(%s)%sClsMethod\n'%(tp, tp2ident(tp)))
        fp.write('{\n')
        fp.write('\tif (g_idx > %d) g_idx = 0;\n'%(len(values),))
        fp.write('\treturn g_%s_values[g_idx++];\n'%(tp2ident(tp),))
        fp.write('}\n\n')

        fp.write('-(%s)%sMethod;\n'%(tp, tp2ident(tp)))
        fp.write('{\n')
        fp.write('\tif (g_idx > %d) g_idx = 0;\n'%(len(values),))
        fp.write('\treturn g_%s_values[g_idx++];\n'%(tp2ident(tp),))
        fp.write('}\n\n')
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* Single argument passing */\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('-(id)%sArg:(%s)arg\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\treturn arg2id(@encode(%s), &arg);\n'%(tp))
        fp.write('}\n\n')
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    for tp1, sign1, values1 in TYPES:
        for tp2, sign2, values2 in TYPES:
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#ifdef HAVE_BOOL\n')
            fp.write('-(id)%sArg:(%s)arg1 and%sArg:(%s)arg2\n'%(tp2ident(tp1), tp1, tp2ident(tp2), tp2))
            fp.write('{\n')
            fp.write('\tNSMutableArray* res;\n')
            fp.write('\tres = [NSMutableArray array];\n')
            fp.write('\t[res addObject:arg2id(@encode(%s), &arg1)];\n'%(tp1,))
            fp.write('\t[res addObject:arg2id(@encode(%s), &arg2)];\n'%(tp2,))
            fp.write('\treturn res;\n')
            fp.write('}\n\n')
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('/* in, out and in-out arguments */\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('-(id)%sInArg:(%s*)arg\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\treturn arg2id(@encode(%s), arg);\n'%(tp,))
        fp.write('}\n\n')

        fp.write('-(void)%sOutArg:(%s*)arg;\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\tif (g_idx > %d) g_idx = 0;\n'%(len(values),))
        fp.write('\t*arg = g_%s_values[g_idx++];\n'%(tp2ident(tp),))
        fp.write('}\n\n')

        fp.write('-(id)%sInOutArg:(%s*)arg;\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\tid res = arg2id(@encode(%s), arg);\n'%(tp,))
        fp.write('\tif (g_idx > %d) g_idx = 0;\n'%(len(values),))
        fp.write('\t*arg = g_%s_values[g_idx++];\n'%(tp2ident(tp),))
        fp.write('\treturn res;\n')
        fp.write('}\n\n')
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('\n@end // implementation PyObjC_TestClass1\n')

    fp.write('\n\n\n\n')

    fp.write('@implementation PyObjC_TestClass2 : NSObject\n\n')

    fp.write('#define SETUP_INVOCATION(inv, target, selector)\\\n')
    fp.write('\t{\\\n')
    fp.write('\t\tid sign = [target methodSignatureForSelector:selector];\\\n')
    fp.write('\t\tif (sign == NULL) {\\\n')
    fp.write('\t\t\tPyErr_SetString(PyExc_AttributeError, PyObjCRT_SELName(selector));\\\n')
    fp.write('\t\t\tPyObjCErr_ToObjC();\\\n')
    fp.write('\t\t}\\\n')
    fp.write('\t\tinv = [NSInvocation invocationWithMethodSignature:\\\n')
    fp.write('\t\t\t[target methodSignatureForSelector:selector]];\\\n')
    fp.write('\t\t[inv setTarget:target];\\\n')
    fp.write('\t\t[inv setSelector:selector];\\\n')
    fp.write('\t}\n')
    fp.write('\n\n')

    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(%s)call%sMethodOf:(PyObjC_TestClass1*)obj\n'%(tp, tp2ident(tp)))
        fp.write('{\n')
        fp.write('\treturn [obj %sMethod];\n'%(tp2ident(tp)))
        fp.write('}\n\n')
        fp.write('+(%s)invoke%sMethodOf:(PyObjC_TestClass1*)obj\n'%(tp, tp2ident(tp)))
        fp.write('{\n')
        fp.write('\t%s res;\n'%(tp,))
        fp.write('\tNSInvocation* inv;\n\n')
        fp.write('\tSETUP_INVOCATION(inv, obj, @selector(%sMethod))\n'%(tp2ident(tp),))
        fp.write('\t[obj forwardInvocation:inv];\n')
        fp.write('\t[inv getReturnValue:&res];\n')
        fp.write('\treturn res;\n')
        fp.write('}\n\n')
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')


    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(id)call%sArg:(%s)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\treturn [obj %sArg:arg];\n'%(tp2ident(tp),))
        fp.write('}\n\n')
            
        fp.write('+(id)invoke%sArg:(%s)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\tid res;\n')
        fp.write('\tNSInvocation* inv;\n\n')
        fp.write('\tSETUP_INVOCATION(inv, obj, @selector(%sArg:))\n'%(tp2ident(tp),))
        fp.write('\t[inv setArgument:&arg atIndex:2];\n')
        fp.write('\t[obj forwardInvocation:inv];\n')
        fp.write('\t[inv getReturnValue:&res];\n')
        fp.write('\treturn res;\n')
        fp.write('}\n\n')
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    for tp1, sign1, values1 in TYPES:
        for tp2, sign2, values2 in TYPES:
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#ifdef HAVE_BOOL\n')
            fp.write('+(id)call%sArg:(%s)arg1 and%sArg:(%s)arg2 of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp1), tp1, tp2ident(tp2), tp2))
            fp.write('{\n')
            fp.write('\treturn [obj %sArg:arg1 and%sArg:arg2];\n'%(tp2ident(tp1), tp2ident(tp2)))
            fp.write('}\n\n')

            fp.write('+(id)invoke%sArg:(%s)arg1 and%sArg:(%s)arg2 of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp1), tp1, tp2ident(tp2), tp2))
            fp.write('{\n')
            fp.write('\tid res;\n')
            fp.write('\tNSInvocation* inv;\n\n')
            fp.write('\tSETUP_INVOCATION(inv, obj, @selector(%sArg:and%sArg:))\n'%(tp2ident(tp1), tp2ident(tp2)))
            fp.write('\t[inv setArgument:&arg1 atIndex:2];\n')
            fp.write('\t[inv setArgument:&arg2 atIndex:3];\n')
            fp.write('\t[obj forwardInvocation:inv];\n')
            fp.write('\t[inv getReturnValue:&res];\n')
            fp.write('\treturn res;\n')
            fp.write('}\n\n')
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('#endif /* HAVE_BOOL */\n')

    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('#ifdef HAVE_BOOL\n')
        fp.write('+(id)call%sInArg:(%s*)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\treturn [obj %sInArg:arg];\n'%(tp2ident(tp),))
        fp.write('}\n\n')
        fp.write('+(void)call%sOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\t[obj %sOutArg:arg];\n'%(tp2ident(tp),))
        fp.write('}\n\n')
        fp.write('+(id)call%sInOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\treturn [obj %sInOutArg:arg];\n'%(tp2ident(tp),))
        fp.write('}\n\n')
        fp.write('+(id)invoke%sInArg:(%s*)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\tid res;\n')
        fp.write('\tNSInvocation* inv;\n\n')
        fp.write('\tSETUP_INVOCATION(inv, obj, @selector(%sInArg:))\n'%(tp2ident(tp),))
        fp.write('\t[inv setArgument:&arg atIndex:2];\n')
        fp.write('\t[obj forwardInvocation:inv];\n')
        fp.write('\t[inv getReturnValue:&res];\n')
        fp.write('\treturn res;\n')
        fp.write('}\n\n')
        fp.write('+(void)invoke%sOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\tNSInvocation* inv;\n\n')
        fp.write('\tSETUP_INVOCATION(inv, obj, @selector(%sOutArg:))\n'%(tp2ident(tp),))
        fp.write('\t[inv setArgument:&arg atIndex:2];\n')
        fp.write('\t[obj forwardInvocation:inv];\n')
        fp.write('}\n\n')
        fp.write('+(id)invoke%sInOutArg:(%s*)arg of:(PyObjC_TestClass1*)obj\n'%(tp2ident(tp), tp))
        fp.write('{\n')
        fp.write('\tid res;\n')
        fp.write('\tNSInvocation* inv;\n\n')
        fp.write('\tSETUP_INVOCATION(inv, obj, @selector(%sInOutArg:))\n'%(tp2ident(tp),))
        fp.write('\t[inv setArgument:&arg atIndex:2];\n')
        fp.write('\t[obj forwardInvocation:inv];\n')
        fp.write('\t[inv getReturnValue:&res];\n')
        fp.write('\treturn res;\n')
        fp.write('}\n\n')
        if tp == 'bool':
            fp.write('#endif /* HAVE_BOOL */\n')

    fp.write('\n@end // implementation PyObjC_TestClass2\n\n')

def emit_py_method_signatures(fp):
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            pfx = 'if HAVE_BOOL: '
        else:
            pfx = ''
        fp.write('%ssetSignature("PyObjC_TestClass1", "%sInOutArg:", "@@:N^%s")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass1", "%sOutArg:", "v@:o^%s")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass1", "%sInArg:", "@@:n^%s")\n'%(pfx, nm, sign))

        fp.write('%ssetSignature("PyObjC_TestClass2", "call%sInOutArg:of:", "@@:N^%s@")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass2", "call%sOutArg:of:", "v@:o^%s@")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass2", "call%sInArg:of:", "@@:n^%s@")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass2", "invoke%sInOutArg:of:", "@@:N^%s@")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass2", "invoke%sOutArg:of:", "v@:o^%s@")\n'%(pfx, nm, sign))
        fp.write('%ssetSignature("PyObjC_TestClass2", "invoke%sInArg:of:", "@@:n^%s@")\n'%(pfx, nm, sign))

def emit_py_to_objc(fp):
    fp.write('class PyToObjC (TestCase):\n')
    fp.write('\t# Test calling Objective-C from Python\n')

    # Simple returns (Class methods)
    fp.write('\t# Simple returns from class methods\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('\tdef testCls%sResult(self):\n'%(nm,))
        if tp == 'bool':
            fp.write('\t\tif not HAVE_BOOL: return\n')
        fp.write('\t\tPyObjC_TestClass1.clsReset()\n')
        for v in values:
            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals')
            else:
                fp.write('\t\tself.assertAlmostEquals')

            fp.write('(PyObjC_TestClass1.%sClsMethod(), '%(nm,))
            write_py_item(fp, v)
            fp.write(')\n')
        fp.write('\n\n')

    # Simple returns (Instance methods)
    fp.write('\t# Simple returns from instance methods\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('\tdef test%sResult(self):\n'%(nm,))
        if tp == 'bool':
            fp.write('\t\tif not HAVE_BOOL: return\n')
        fp.write('\t\to = PyObjC_TestClass1.alloc().init()\n')
        fp.write('\t\tself.assert_(o is not None)\n')
        fp.write('\t\to.reset()\n')
        for v in values:
            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals')
            else:
                fp.write('\t\tself.assertAlmostEquals')

            fp.write('(o.%sMethod(), '%(nm, ))
            write_py_item(fp, v)
            fp.write(')\n')
        fp.write('\n\n')

    # One argument
    fp.write('\t# One argument\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('\tdef test%sArg(self):\n'%(nm,))
        if tp == 'bool':
            fp.write('\t\tif not HAVE_BOOL: return\n')
        fp.write('\t\to = PyObjC_TestClass1.alloc().init()\n')
        fp.write('\t\tself.assert_(o is not None)\n')
        for v in values:
            fp.write('\t\tr = o.%sArg_('%(nm,))
            write_py_item(fp, v)
            fp.write(')\n')

            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals(r, ')
            else:
                fp.write('\t\tself.assertAlmostEquals(r, ')
            write_py_item(fp, v)
            fp.write(')\n')
        fp.write('\n\n')

    # Two arguments
    fp.write('\t# Two arguments\n\n')
    for tp1, sign1, values1 in TYPES:
        nm1 = tp2ident(tp1)
        for tp2, sign2, values2 in TYPES:
            nm2 = tp2ident(tp2)
            fp.write('\tdef test%sAnd%sArg(self):\n'%(nm1,nm2))
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('\t\tif not HAVE_BOOL: return\n')
            fp.write('\t\to = PyObjC_TestClass1.alloc().init()\n')
            fp.write('\t\tself.assert_(o is not None)\n')
            for v1 in values1:
                for v2 in values2:
                    fp.write('\t\tr = o.%sArg_and%sArg_('%(nm1,nm2))
                    write_py_item(fp, v1)
                    fp.write(', ')
                    write_py_item(fp, v2)
                    fp.write(')\n')

                    if tp1 not in ('float', 'double'):
                        fp.write('\t\tself.assertEquals(r[0], ')
                    else:
                        fp.write('\t\tself.assertAlmostEquals(r[0], ')
                    write_py_item(fp, v1)
                    fp.write(')\n')

                    if tp2 not in ('float', 'double'):
                        fp.write('\t\tself.assertEquals(r[1], ')
                    else:
                        fp.write('\t\tself.assertAlmostEquals(r[1], ')
                    write_py_item(fp, v2)
                    fp.write(')\n')
            fp.write('\n\n')

    # Pass by reference arguments (in)
    fp.write('\t# Pass by reference arguments (in)\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('\tdef test%sIn(self):\n'%(nm,))
        if tp == 'bool':
            fp.write('\t\tif not HAVE_BOOL: return\n')
        fp.write('\t\to = PyObjC_TestClass1.alloc().init()\n')
        fp.write('\t\tself.assert_(o is not None)\n')
        for v in values:
            fp.write('\t\tr = o.%sInArg_('%(nm,))
            write_py_item(fp, v)
            fp.write(')\n')

            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals(r, ')
            else:
                fp.write('\t\tself.assertAlmostEquals(r, ')
            write_py_item(fp, v)
            fp.write(')\n')
        fp.write('\n\n')

    # Pass by reference arguments (out)
    fp.write('\t# Pass by reference arguments (out)\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('\tdef test%sOut(self):\n'%(nm,))
        if tp == 'bool':
            fp.write('\t\tif not HAVE_BOOL: return\n')
        fp.write('\t\to = PyObjC_TestClass1.alloc().init()\n')
        fp.write('\t\tself.assert_(o is not None)\n')
        fp.write('\t\to.reset()\n')
        for v in values:
            fp.write('\t\tr = o.%sOutArg_()\n'%(nm,))

            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals(r, ')
            else:
                fp.write('\t\tself.assertAlmostEquals(r, ')
            write_py_item(fp, v)
            fp.write(')\n')
        fp.write('\n\n')
        
    # Pass by reference arguments (inout)
    fp.write('\t# Pass by reference arguments (inout)\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('\tdef test%sInOut(self):\n'%(nm,))
        if tp == 'bool':
            fp.write('\t\tif not HAVE_BOOL: return\n')
        fp.write('\t\to = PyObjC_TestClass1.alloc().init()\n')
        fp.write('\t\tself.assert_(o is not None)\n')
        fp.write('\t\to.reset()\n')
        valuesrev = list(values[:])
        valuesrev.reverse()
        valuesrev = tuple(valuesrev)
        for vin, vout in zip(valuesrev, values):
            fp.write('\t\tr = o.%sInOutArg_('%(nm,))
            write_py_item(fp, vin) 
            fp.write(')\n')

            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals(r[0], ')
            else:
                fp.write('\t\tself.assertAlmostEquals(r[0], ')
            write_py_item(fp, vin)
            fp.write(')\n')

            if tp not in ('float', 'double'):
                fp.write('\t\tself.assertEquals(r[1], ')
            else:
                fp.write('\t\tself.assertAlmostEquals(r[1], ')
            write_py_item(fp, vout)
            fp.write(')\n')
        fp.write('\n\n')

def emit_python_subclass(fp):
    fp.write('# Helper arrays\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        fp.write('g_%s_values = '%(nm,))
        write_py_item(fp, values)
        fp.write('\n')

    fp.write('\n\n')

    fp.write('class Python_TestClass (objc.runtime.NSObject):\n')
    fp.write('\tdef init(self):\n')
    fp.write('\t\tself = super(Python_TestClass, self).init()\n')
    fp.write('\t\tself.reset()\n')
    fp.write('\t\treturn self\n')
    fp.write('\n\n')
    fp.write('\tdef reset(self):\n')
    fp.write('\t\tself.counter = 0\n')
    fp.write('\n\n')

    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx = '\t'
        else:
            pfx = ''
        fp.write('%s\tdef %sMethod(self):\n'%(pfx, nm,))
        fp.write('%s\t\tif self.counter > %d: self.reset()\n'%(pfx, len(values),))
        fp.write('%s\t\tidx = self.counter\n'%(pfx,))
        fp.write('%s\t\tself.counter += 1\n'%(pfx,))
        fp.write('%s\t\treturn g_%s_values[idx]\n'%(pfx, nm,))
        fp.write('%s\t%sMethod = objc.selector(%sMethod, signature="%s@:")\n'%(
            pfx, nm, nm, sign))
        fp.write('\n\n')

    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx = '\t'
        else:
            pfx = ''
        fp.write('%s\tdef %sArg_(self, arg):\n'%(pfx, nm,))
        fp.write('%s\t\treturn arg # return the same\n'%(pfx,))
        fp.write('%s\t%sArg_ = objc.selector(%sArg_, signature="@@:%s")\n'%(
            pfx, nm, nm, sign))
        fp.write('\n\n')

    for tp1, sign1, values1 in TYPES:
        for tp2, sign2, values2 in TYPES:
            nm1 = tp2ident(tp1)
            nm2 = tp2ident(tp2)
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('\tif HAVE_BOOL:\n')
                pfx = '\t'
            else:
                pfx = ''
            fp.write('%s\tdef %sArg_and%sArg_(self, arg1, arg2):\n'%(pfx, nm1, nm2))
            fp.write('%s\t\treturn [ arg1, arg2 ]\n'%(pfx,))
            fp.write('%s\t%sArg_and%sArg_ = objc.selector(%sArg_and%sArg_, signature="@@:%s%s")\n'%(
                pfx, nm1, nm2, nm1, nm2, sign1, sign2))
            fp.write('\n\n')

    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx = '\t'
        else:
            pfx = ''
        fp.write('%s\tdef %sInArg_(self, arg):\n'%(pfx, nm,))
        fp.write('%s\t\treturn arg\n'%(pfx,))
        fp.write('%s\t%sInArg_ = objc.selector(%sInArg_, signature="@@:%s^%s")\n'%(
            pfx, nm, nm, objc._C_IN, sign))
        fp.write('\n\n')

        fp.write('%s\tdef %sOutArg_(self):\n'%(pfx, tp2ident(tp),))
        fp.write('%s\t\tif (self.counter > %d): self.reset()\n'%(pfx, len(values),))
        fp.write('%s\t\tres = g_%s_values[self.counter]\n'%(pfx, nm,))
        fp.write('%s\t\tself.counter += 1\n'%(pfx,))
        fp.write('%s\t\treturn res\n'%(pfx,))
        fp.write('%s\t%sOutArg_ = objc.selector(%sOutArg_, signature="v@:%s^%s")\n'%(
            pfx, nm, nm, objc._C_OUT, sign))
        fp.write('\n\n')

        fp.write('%s\tdef %sInOutArg_(self, arg):\n'%(pfx, tp2ident(tp),))
        fp.write('%s\t\tif (self.counter > %d): self.reset()\n'%(pfx, len(values),))
        fp.write('%s\t\tres = g_%s_values[self.counter];\n'%(pfx, nm,))
        fp.write('%s\t\tself.counter += 1\n'%(pfx,))
        fp.write('%s\t\treturn (arg, res)\n'%(pfx,))
        fp.write('%s\t%sInOutArg_ = objc.selector(%sInOutArg_, signature="@@:%s^%s")\n'%(
            pfx, nm, nm, objc._C_INOUT, sign))
        fp.write('\n\n')

def emit_py_from_objc(fp):
    fp.write('class ObjCToPy (TestCase):\n')
    fp.write('\t# Test calling Python from Objective-C\n')

    # Simple returns (Instance methods)
    fp.write('\t# Simple returns from instance methods\n\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx = '\t'
        else:
            pfx = ''
        for kind in   ('call', 'invoke'):
            nm = tp2ident(tp)
            fp.write('%s\tdef test%s%sResult(self):\n'%(pfx, kind, nm,))
            fp.write('%s\t\to = Python_TestClass.alloc().init()\n'%(pfx,))
            fp.write('%s\t\tself.assert_(o is not None)\n'%(pfx,))
            fp.write('%s\t\to.reset()\n'%(pfx,))
            for v in values:
                if tp not in ('float', 'double'):
                    fp.write('%s\t\tself.assertEquals'%(pfx,))
                else:
                    fp.write('%s\t\tself.assertAlmostEquals'%(pfx,))

                fp.write('(PyObjC_TestClass2.%s%sMethodOf_(o), '%(kind, nm, ))
                write_py_item(fp, v)
                fp.write(')\n')
            fp.write('\n\n')

    # One argument
    fp.write('\t# One argument\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx = '\t'
        else:
            pfx = ''
        for kind in  ('call', 'invoke'):
            fp.write('%s\tdef test%s%sArg(self):\n'%(pfx, kind, nm,))
            for kind in ('call', 'invoke'):
                if kind == 'invoke' and IS_PROBLEM(tp):
                    fp.write('%s\t\tif not nsinvoke_ok:\n'%(pfx,))
                    fp.write('%s\t\t\treturn\n'%(pfx,))
            fp.write('%s\t\to = Python_TestClass.alloc().init()\n'%(pfx,))
            fp.write('%s\t\tself.assert_(o is not None)\n'%(pfx,))
            for v in values:
                fp.write('%s\t\tr = PyObjC_TestClass2.%s%sArg_of_('%(pfx, kind, nm,))
                write_py_item(fp, v)
                fp.write(', o)\n')

                if tp not in ('float', 'double'):
                    fp.write('%s\t\tself.assertEquals(r, '%(pfx,))
                else:
                    fp.write('%s\t\tself.assertAlmostEquals(r, '%(pfx,))
                write_py_item(fp, v)
                fp.write(')\n')
            fp.write('\n\n')

    # Two arguments
    fp.write('\t# Two arguments\n\n')
    for tp1, sign1, values1 in TYPES:
        nm1 = tp2ident(tp1)
        for tp2, sign2, values2 in TYPES:
            if tp1 == 'bool' or tp2 == 'bool':
                fp.write('\tif HAVE_BOOL:\n')
                pfx = '\t'
            else:
                pfx = ''
            nm2 = tp2ident(tp2)
            for kind in ('call', 'invoke'):
                fp.write('%s\tdef test%s%sAnd%sArg(self):\n'%(pfx, kind, nm1,nm2))
                if kind == 'invoke' and (IS_PROBLEM(tp1)  or IS_PROBLEM(tp2)):
                    fp.write('%s\t\tif not nsinvoke_ok:\n'%(pfx,))
                    fp.write('%s\t\t\treturn\n'%(pfx,))

                fp.write('%s\t\to = Python_TestClass.alloc().init()\n'%(pfx,))
                fp.write('%s\t\tself.assert_(o is not None)\n'%(pfx,))
                for v1 in values1:
                    for v2 in values2:
                        fp.write('%s\t\tr = PyObjC_TestClass2.%s%sArg_and%sArg_of_('%(pfx, kind, nm1,nm2))
                        write_py_item(fp, v1)
                        fp.write(', ')
                        write_py_item(fp, v2)
                        fp.write(', o)\n')

                        if tp1 not in ('float', 'double'):
                            fp.write('%s\t\tself.assertEquals(r[0], '%(pfx,))
                        else:
                            fp.write('%s\t\tself.assertAlmostEquals(r[0], '%(pfx,))
                        write_py_item(fp, v1)
                        fp.write(')\n')

                        if tp2 not in ('float', 'double'):
                            fp.write('%s\t\tself.assertEquals(r[1], '%(pfx,))
                        else:
                            fp.write('%s\t\tself.assertAlmostEquals(r[1], '%(pfx,))
                        write_py_item(fp, v2)
                        fp.write(')\n')
                fp.write('\n\n')

    # Pass by reference arguments (in)
    fp.write('\t# Pass by reference arguments (in)\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx='\t'
        else:
            pfx=''

        for kind in ('call', 'invoke'):
            fp.write('%s\tdef test%s%sIn(self):\n'%(pfx, kind, nm,))
            fp.write('%s\t\to = Python_TestClass.alloc().init()\n'%(pfx,))
            fp.write('%s\t\tself.assert_(o is not None)\n'%(pfx,))
            for v in values:
                fp.write('%s\t\tr = PyObjC_TestClass2.%s%sInArg_of_('%(pfx, kind, nm,))
                write_py_item(fp, v)
                fp.write(', o)\n')

                if tp not in ('float', 'double'):
                    fp.write('%s\t\tself.assertEquals(r, '%(pfx,))
                else:
                    fp.write('%s\t\tself.assertAlmostEquals(r, '%(pfx,))
                write_py_item(fp, v)
                fp.write(')\n')
            fp.write('\n\n')

    # Pass by reference arguments (out)
    fp.write('\t# Pass by reference arguments (out)\n\n')
    for tp, sign, values in TYPES:
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx='\t'
        else:
            pfx=''
        nm = tp2ident(tp)
        for kind in ('call', 'invoke'):
            fp.write('%s\tdef test%s%sOut(self):\n'%(pfx, kind, nm,))
            fp.write('%s\t\to = Python_TestClass.alloc().init()\n'%(pfx,))
            fp.write('%s\t\tself.assert_(o is not None)\n'%(pfx,))
            fp.write('%s\t\to.reset()\n'%(pfx,))
            for v in values:
                fp.write('%s\t\tr = PyObjC_TestClass2.%s%sOutArg_of_(o)\n'%(
                    pfx, kind, nm,))

                if tp not in ('float', 'double'):
                    fp.write('%s\t\tself.assertEquals(r, '%(pfx,))
                else:
                    fp.write('%s\t\tself.assertAlmostEquals(r, '%(pfx,))
                write_py_item(fp, v)
                fp.write(')\n')
            fp.write('\n\n')
            
    # Pass by reference arguments (inout)
    fp.write('\t# Pass by reference arguments (out)\n\n')
    for tp, sign, values in TYPES:
        nm = tp2ident(tp)
        if tp == 'bool':
            fp.write('\tif HAVE_BOOL:\n')
            pfx='\t'
        else:
            pfx=''
        for kind in ('call', 'invoke'):
            fp.write('%s\tdef test%s%sInOut(self):\n'%(pfx, kind, nm,))
            fp.write('%s\t\to = Python_TestClass.alloc().init()\n'%(pfx,))
            fp.write('%s\t\tself.assert_(o is not None)\n'%(pfx,))
            fp.write('%s\t\to.reset()\n'%(pfx,))
            valuesrev = list(values[:])
            valuesrev.reverse()
            valuesrev = tuple(valuesrev)
            for vin, vout in zip(valuesrev, values):
                fp.write('%s\t\tr = PyObjC_TestClass2.%s%sInOutArg_of_('%(
                    pfx, kind, nm,))
                write_py_item(fp, vin) 
                fp.write(', o)\n')

                if tp not in ('float', 'double'):
                    fp.write('%s\t\tself.assertEquals(r[0], '%(pfx,))
                else:
                    fp.write('%s\t\tself.assertAlmostEquals(r[0], '%(pfx,))
                write_py_item(fp, vin)
                fp.write(')\n')

                if tp not in ('float', 'double'):
                    fp.write('%s\t\tself.assertEquals(r[1], '%(pfx,))
                else:
                    fp.write('%s\t\tself.assertAlmostEquals(r[1], '%(pfx,))
                write_py_item(fp, vout)
                fp.write(')\n')
            fp.write('\n\n')


print "------------- testbndl.m -----------"
fp = open('Lib/objc/test/testbndl2.m', 'w')
fp.write(OBJC_HEADER)
emit_objc_interfaces(fp)
emit_objc_implementations(fp)
fp.write(OBJC_FOOTER)
for tp, sign, values in TYPES:
    if tp != 'id': continue
    
    fp.write('\t/* Initialize g_id_values */\n')
    def counted(lst):
        """ 'enumurate' for dummies """
        i = 0
        result = []
        for item in lst:
                result.append((i, item))
                i = i+1
        return result

    for idx, v in counted(values):
        fp.write('\tg_id_values[%d] = %s;\n'%(idx, v))

fp.write('}\n')
fp.close()

print "------------- test_methods.py -----------"
fp = open('Lib/objc/test/test_methods2.py', 'w')
fp.write(PY_HEADER)
emit_py_method_signatures(fp)
fp.write(PY_MIDTEXT)
emit_py_to_objc(fp)
emit_python_subclass(fp)
emit_py_from_objc(fp)

fp.write(PY_FOOTER)

fp.close()
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.