Source

Silver / Tests / SVClassSystemTests.m

Full commit
  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
//
//  SVClassSystemTests.m
//  SV
//
//  Created by Michael Tindal on 9/24/10.
//  Copyright (c) 2010 Michael Tindal. All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as 
// published by the Free Software Foundation; either version 3 of 
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

#import "SVClassSystemTests.h"
#import "SVParserContext.h"
#import "SVVariable.h"
#import "SVVariableAssignment.h"
#import "SVBinaryOperator.h"
#import "SVUnaryOperator.h"
#import "SVBlock.h"
#import "SVFunction.h"
#import "SVControlOperations.h"
#import "SVFlowBlock.h"
#import "SVInstanceRef.h"
#import <OCLog/OCLog.h>

#define P(n) id n = [[context parse] evaluateNode]
#define PC(s) SVParserContext* context = [SVParserContext parserWithString:s]

@interface SVNativeFunctionHelper : NSObject

-(id)sv_getProtocol:(id)name;

@end

@interface NSNumber (SVShutUpItWorks)

-(id) sub:(id)num;

@end

@interface SVTestClass : NSObject {
    NSNumber* a;
    NSNumber* b;
}

-(id) init;

-(NSNumber*) add:(NSNumber*)c;

-(NSNumber*) mult;

+(SVTestClass*) testClassWithA:(NSNumber*)a b:(NSNumber*)b;

-(BOOL) isEqual:(id)object;
@end

@implementation SVTestClass

-(id) init {
    if((self = [super init])) {
        a = [NSNumber numberWithInt:5];
        b = [NSNumber numberWithInt:10];
    }
    return self;
}

-(NSNumber*) add:(NSNumber*)c {
    return [NSNumber numberWithInt:([a intValue] + [b intValue] + [c intValue])];
}

-(NSNumber*) mult {
    return [NSNumber numberWithInt:([a intValue] * [b intValue])];
}

+(SVTestClass*) testClassWithA:(NSNumber*)a b:(NSNumber*)b {
    SVTestClass* i = [[SVTestClass alloc] init];
    if(i) {
        i->a = a;
        i->b = b;
    }
    return i;
}

-(BOOL) isEqual:(id)object {
    if(object == self)
        return YES;
    
    if(![object isKindOfClass:[SVTestClass class]])
        return NO;
    
    SVTestClass* o = (SVTestClass*)object;
    return [self->a isEqual:o->a] && [self->b isEqual:o->b];
}
@end

@interface NSObject (SVClassSystemTests)

-(id) customSelector:(id)num0 withNum:(id)num1;

@end

@implementation SVClassSystemTests

- (void)setUp {
    [super setUp];
    
    [SVFunction nativeFunctionWithName:@"conformsToProtocol" target:[SVNativeFunctionHelper class] selector:@selector(sv_conformsToProtocol::)];
    SVParserContext* context = [SVParserContext parserWithString:@"require('bridge');"];
    [[context parse] evaluateNode];
}

- (void)tearDown {
    // Tear-down code here.
    
    [super tearDown];
}

-(void) testObjCreate {
    PC(@"SVTestClass.new;");
    
    P(var);
    assertThat([var class],is(equalTo([SVTestClass class])));
}

-(void) testObjInit {
    PC(@"SVTestClass.alloc.init;");
    
    P(var);
    assertThat(var,is(equalTo([[SVTestClass alloc] init])));
}

-(void) testObjMethodCall1 {
    PC(@"SVTestClass.alloc.init.add(10);");
    
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testObjIvarGet {
    PC(@"x = SVTestClass.alloc.init; x.a;");
    
    P(var);
    assertThat(var,is(equalTo(N(5))));
}

-(void) testObjIvarSet {
    PC(@"x = SVTestClass.alloc.init; x.a = 10; x.a;");
    
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testObjMethodCall2 {
    PC(@"x = SVTestClass.alloc.init; x.a = 10; x.mult;");
    
    P(var);
    assertThat(var,is(equalTo(N(100))));
}

-(void) testIvarIndependence {
    PC(@"x = SVTestClass.alloc.init; y = SVTestClass.alloc.init; x.a = 10; x.mult + y.mult;");
    
    P(var);
    assertThat(var,is(equalTo(N(150))));
}

-(void) testClassMethodCall {
    PC(@"x = SVTestClass.testClassWithA(10,b:20); x.mult;");
    
    P(var);
    assertThat(var,is(equalTo(N(200))));
}

-(void) testClassSimpleDefinition {
    PC(@"class sv_string { def print { return 'Hello world!'; }; }; sv_string.alloc.print;");
    
    P(var);
    assertThat(var,is(equalTo(@"Hello world!")));
}

-(void) testClassInheritSV {
    PC(@"class sv_superclass { def print { return 'Hello world!'; }; }; class sv_subclass < sv_superclass { ; }; sv_subclass.alloc.print;");
    
    P(var);
    assertThat(var,is(equalTo(@"Hello world!")));
}

-(void) testClassInheritObjc {
    PC(@"class sv_subclass_objc < SVTestClass { ; }; x = sv_subclass_objc.testClassWithA(15,b:10); x.mult;");
    
    P(var);
    assertThat(var,is(equalTo(N(150))));
}

-(void) testClassOverrideSimple {
    PC(@"class sv_superclass0 { def print { return 'Hello world!'; }; }; class sv_subclass0 < sv_superclass0 { def print { return 'Hello you!'; }; }; sv_subclass0.alloc.print;");
    
    P(var);
    assertThat(var,is(equalTo(@"Hello you!")));
}

-(void) testClassOverrideSuper {
    PC(@"class sv_superclass1 { def print { return 'Hello me!'; }; }; class sv_subclass1 < sv_superclass1 { def print { return super.print; }; }; sv_subclass1.alloc.print;");
    
    P(var);
    assertThat(var,is(equalTo(@"Hello me!")));
}

-(void) testClassOverrideSuper2 {
    PC(@"class sv_superclass2 { def print0 { return 'Hello'; }; def print { return 'Goodbye'; }; }; class sv_subclass2 < sv_superclass2 { def print0 { return ' world!'; }; def print { x = ''; x.appendString(super.print0); x.appendString(self.print0); return x; }; }; sv_subclass2.alloc.print;");
    P(var);
    assertThat(var,is(equalTo(@"Hello world!")));
}

-(void) testClassProtocol {
    PC(@"class sv_protocol > NSCopying { def copyWithZone(zone) { x = sv_protocol.alloc.init; x.i = self.i; return x; }; }; a = sv_protocol.new;"
       @"conformsToProtocol(a,'NSCopying');");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}

-(void) testClassProtocol2 {
    PC(@"class sv_protocol2 { ; }; a = sv_protocol2.new; conformsToProtocol(a,'NSCopying');");
    
    P(var);
    assertThat(var,is(equalTo(BL(NO))));
}

-(void) testClassCopy {
    PC(@"class sv_protocol3 > NSCopying { def copyWithZone(zone) { x = sv_protocol3.alloc.init; x.i = self.i; return x; }; }; a = sv_protocol3.alloc.init; a.i = 10; a.copyWithZone(NSZone.defaultZone).i;");
    
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testVarargCount {
    PC(@"def func(*a) { return a.count; };  func(1,2,3,4,5,8);");
    
    P(var);
    assertThat(var,is(equalTo(N(6))));
}

-(void) testClassDynamicIvar {
    PC(@"class sv_dynivar { ; }; x = sv_dynivar.alloc.init; x.a = 10; x.a;");
    
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testClassDynamicProperties {
    PC(@"class sv_dynvar2 { ; }; x = sv_dynvar2.alloc.init; x.a = 10; y = x.a; x.setA(20); z = x.a; y + z;");
    
    P(var);
    assertThat(var,is(equalTo(N(30))));
}

-(void) testClassSVCategorySV {
    PC(@"class sv_cat_base { ; };  class sv_cat_base { def add(num) { return num + 20; }; }; sv_cat_base.alloc.add(30);");
    
    P(var);
    assertThat(var,is(equalTo(N(50))));
}

-(void) testClassSVCategoryObjc {
    PC(@"class NSNumber { def add(num) { return self + num; }; }; x = 10; x.add(30);");
    
    P(var);
    assertThat(var,is(equalTo(N(40))));
}

-(void) testClassSVCategoryObjc2 {
    PC(@"class NSNumber { def sub(num) { return self - num; }; };");
    
    P(var);
    NSNumber* num = N(20);
    
    var = [num sub:N(10)];
    assertThat(var,is(equalTo(N(10))));
}

int bridgeFunctionTest(int x) {
    return x + 10;
}

-(void) testBridgeFunctions {
    PC(@"@func('bridgeFunctionTest','ii'); bridgeFunctionTest(10);");
    
    P(var);
    assertThat(var,is(equalTo(N(20))));
}

-(void) testBridge {
    PC(@"x = SVRange.rangeWithLocation(1,length:3); y = 'Hello world!'; y.substringWithRange(x);");
    
    P(var);
    assertThat(var,is(equalTo(@"ell")));
}

-(void) testOperatorOverload {
	PC(@"class sv_op_test { def init { if((self = super.init)) { self.x = 10; }; return self; }; def +(obj) { return self.x + obj.x; }; }; a = sv_op_test.alloc.init; b = sv_op_test.alloc.init; b.x = 20; a + b;");
	
	P(var);
	assertThat(var,is(equalTo(N(30))));
}

-(void) testBridgeSupport {
    PC(@"framework('Foundation'); NSGreaterThanComparison;");

    P(var);
    assertThat(var,is(equalTo(N(NSGreaterThanComparison))));
}

-(void) testBridgeSupport2 {
    PC(@"framework('Foundation'); x = NSMakeRange(1,3); y = 'Hello world!'; y.substringWithRange(x);");
    
    P(var);
    assertThat(var,is(equalTo(@"ell")));
}

-(void) testClassNestedArray {
    NSArray* sa1 = [NSArray arrayWithObjects:N(1),N(2),nil];
    NSArray* sa2 = [NSArray arrayWithObjects:N(3),N(4),nil];
    NSArray* sa3 = [NSArray arrayWithObjects:N(5),N(6),nil];
    NSArray* expect = [NSArray arrayWithObjects:sa1,sa2,sa3,nil];
    PC(@"class a { def func(b) { return b; }; }; x = a.new; x.func([[1,2],[3,4],[5,6]]);");
    
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testBridgeNestedArray {
    NSArray* sa1 = [NSArray arrayWithObjects:N(1),N(2),nil];
    NSArray* sa2 = [NSArray arrayWithObjects:N(3),N(4),nil];
    NSArray* sa3 = [NSArray arrayWithObjects:N(5),N(6),nil];
    NSArray* expect = [NSArray arrayWithObjects:sa1,sa2,sa3,nil];
    PC(@"class NSString { def func(b) { return b; }; }; x = NSString.stringWithString('test'); x.func([[1,2],[3,4],[5,6]]);");
    
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testCustomSelector {
    NSNumber* expect = N(20);
    PC(@"class NSObject { def customSelector(num0,withNum:num1) { return num0 + num1; }; };");
    
    P(var);
    id obj = [[NSObject alloc] init];
    var = [obj customSelector:N(10) withNum:N(10)];
    assertThat(var,is(equalTo(expect)));
}

-(void) testSymbolToI {
    SVSymbol* sym0 = [[SVSymbolTable sharedTable] symbolWithName:@"toitestsym2"];
    PC(@":toitestsym2.to_i;");
    P(var);
    assertThat(var,is(equalTo(sym0.to_i)));
}

-(void) testNumberLiteral {
    PC(@"2.stringValue;");
    P(var);
    assertThat(var,is(equalTo(@"2")));
}

-(void) testStringLiteral {
    PC(@"x = 'world'; 'hello #{x}';");
    P(var);
    assertThat(var,is(equalTo(@"hello world")));
}

-(void) testObjCProperty {
    PC(@"x = [1,2,3]; x.count;");
    P(var);
    assertThat(var,is(equalTo(N(3))));
}

-(void) testObjectFieldBinaryOp {
    PC(@"x = SVTestClass.alloc.init; x.a = 2; x.a *= 2; x.a;");
    P(var);
    assertThat(var,is(equalTo(N(4))));
}

-(void) testObjectAccessBinaryOp {
    PC(@"x = SVTestClass.alloc.init; x.a = [1,3]; x.a[1] *= 3;");
    P(var);
    assertThat(var,is(equalTo(N(9))));
}

-(void) testObjCondAssign1 {
    PC(@"x = SVTestClass.alloc.init; x.a = nil; x.a ||= 'test'; x.a;");
    P(var);
    assertThat(var,is(equalTo(@"test")));
}

-(void) testObjCondAssign2 {
    PC(@"x = SVTestClass.alloc.init; x.a = true; x.a ||= 'test'; x.a;");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}

-(void) testObjCondAssign3 {
    PC(@"x = SVTestClass.alloc.init; x.a = false; x.a &&= 'test'; x.a;");
    P(var);
    assertThat(var,is(equalTo(BL(NO))));
}

-(void) testObjCondAssign4 {
    PC(@"x = SVTestClass.alloc.init; x.a = true; x.a &&= 'test'; x.a;");
    P(var);
    assertThat(var,is(equalTo(@"test")));
}

-(void) testAnonymousType {
    SVParserContext* context = [SVParserContext parserWithString:@"x = ({ def init { if((self = super.init)) { self.x = 10; }; return self; }; def add(num) { return self.x + num; };}).alloc.init; x.add(20);"];
        
    P(var);
    assertThat(var,is(equalTo(N(30))));
}

-(void) testCustomIndex {
    PC(@"class SVCustomIndex { def init { if((self = super.init)) { self.s = {}; }; return self; }; def [](ix) { return self.s[ix]; };"
       @"def []=(key,obj) { self.s[key] = obj; }; }; x = SVCustomIndex.alloc.init; x[:test] = 'Hello world!'; x[:test];");
    P(var);
    assertThat(var,is(equalTo(@"Hello world!")));
}

-(void) testClassMacro {
    PC(@"require('ext'); def selMacro(n,r) { code = <<-CODE def sel#{n} { return '#{r}'; }; CODE; code; }; class MacroTest { self.class_eval(selMacro('Test','Hello world!')); }; MacroTest.alloc.init.selTest;");
    P(var);
    assertThat(var,is(equalTo(@"Hello world!")));
}


-(void) testAtIVarGet {
    PC(@"class AtIvar1 { def init { if((self = super.init)) { self.i = 10; }; return self; }; def getI { return @i; }; }; AtIvar1.alloc.init.getI;");
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testAtIVarSet {
    PC(@"class AtIvar2 { def init { if((self = super.init)) { @i = 10; }; return self; }; }; a = AtIvar2.alloc.init; a.i;");
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testAtDynamicIVar {
    PC(@"class AtDynIvar { def init { if((self = super.init)) { @i = 10; }; return self; }; }; a = AtDynIvar.alloc.init; a.i;");
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testCvar {
    NSArray* expect = [NSArray arrayWithObjects:N(1),N(2),nil];
    PC(@"class CVarTest { def test { @@x = 1; }; def test2 { @@x = 2; }; def test3 { return @@x; }; }; x = CVarTest.alloc.init; x.test; a = x.test3; y = CVarTest.alloc.init;"
       @"y.test2; b = x.test3; [a,b];");
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testCVarDistinct {
    NSArray* expect = [NSArray arrayWithObjects:N(2),N(1),nil];
    PC(@"class CVarTest1 { def init { @@y = 2; return self; }; def test { return @@y; }; }; class CVarTest2 { def init { @@y = 1; return self; };"
       @"def test { return @@y; }; }; y = CVarTest2.alloc.init; x = CVarTest1.alloc.init; [x.test,y.test];");
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testNestedClass {
    PC(@"class NestedClassTest { class NestedClassTestInner { def ret { return 10; }; }; def my_ret { x = NestedClassTestInner.alloc.init; return x.ret; }; }; y = NestedClassTest.alloc.init; y.my_ret;");
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testNestedClass2 {
    PC(@"class NestedClassTest2 { class NestedClassTestInner { def ret { return 10; }; }; }; x = NestedClassTestInner.alloc.init;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testNestedClassDifferent {
    NSArray *expect = [NSArray arrayWithObjects:N(10),N(25),nil];
    PC(@"class NestedClassTest3 { class NestedClassTestInner { def ret { return 10; }; }; def my_ret { x = NestedClassTestInner.alloc.init; return x.ret; }; }; class NestedClassTest4 { class NestedClassTestInner { def ret { return 25; }; }; def my_ret { x = NestedClassTestInner.alloc.init; return x.ret; }; }; na = NestedClassTest3.alloc.init; nb = NestedClassTest4.alloc.init; na,nb=na.my_ret,nb.my_ret;");
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testNestedClassSuper {
    PC(@"class NestedClassTestSuper { class NestedClassTestInner { def ret { return 10; }; }; }; class NestedClassTest5 < NestedClassTestSuper { def my_ret { x = NestedClassTestInner.alloc.init; return x.ret; }; }; y = NestedClassTest5.alloc.init; y.my_ret;");
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testCategoryNestedClass {
    PC(@"class NSNumber { class NestedClassTestInner { def ret { return 25; }; }; def my_ret { x = NestedClassTestInner.alloc.init; return x.ret if self > 10; }; }; 15.my_ret;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testClassMethod {
    PC(@"class ClassMethodTest { def ClassMethodTest.method { return 100; }; }; ClassMethodTest.method;");
    P(var);
    assertThat(var,is(equalTo(N(100))));
}

-(void) testSingletonOverride {
    NSArray *expect = [NSArray arrayWithObjects:N(10),N(25),nil];
    PC(@"class SingletonTest { def size { return 25; }; }; x = SingletonTest.alloc.init; def x.size { return 10; }; y = SingletonTest.alloc.init; w,z=x.size,y.size;");
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testSingletonNew {
    PC(@"class SingletonTest2 { ; }; x = SingletonTest2.alloc.init; def x.size { return 10; }; x.size;");
    P(var);
    assertThat(var,is(equalTo(N(10))));
}

-(void) testSingletonNotExist {
    PC(@"class SingletonTest3 { ; }; x = SingletonTest3.alloc.init; def x.size { return 10; }; y = SingletonTest3.alloc.init; y.size;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testClassAlias {
    PC(@"class AliasTest { def test_method { return 25; }; alias test_method ret; }; x = AliasTest.alloc.init; x.ret;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testSuperAlias {
    PC(@"class AliasSuperTest { def test_method { return 25; }; }; class AliasSubTest < AliasSuperTest { alias test_method ret; }; x = AliasSubTest.alloc.init; x.ret;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testAliasOld {
    PC(@"class AliasSuperTest2 { def test_method { return 25; }; }; class AliasSubTest2 < AliasSuperTest2 { alias test_method old_test_method; def test_method { 10 + self.old_test_method; }; }; x = AliasSubTest2.alloc.init; x.test_method;");
    P(var);
    assertThat(var,is(equalTo(N(35))));
}

-(void) testAliasCategory {
    PC(@"class AliasTest2 { def test_method { return 25; }; }; class AliasTest2 { alias test_method cat_alias; }; x = AliasTest2.alloc.init; x.cat_alias;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testAliasSelector {
    PC(@"class AliasTest3 { def test_method(n,with:x) { return n + x; }; alias 'test_method:with:' 'add:to:'; }; x = AliasTest3.alloc.init; x.add(10,to:25);");
    P(var);
    assertThat(var,is(equalTo(N(35))));
}

-(void) testClassOpening1 {
    PC(@"foobar = []; class << foobar; { def test { return 127; }; }; t = []; t.test;");
    P(var);
    assertThat(var,is(equalTo(N(127))));
}

-(void) testClassWeirdness {
    PC(@"class NSNumber { def test { class << self; { self; }; }; }; 10.test;");
    P(var);
    assertThat(var,is(equalTo([NSNumber class])));
}

-(void) testMoreClassWeirdness {
    PC(@"class NSNumber { def wtest { class << self; { def other_test { return self + 25; };  }; }; }; 10.wtest; 10.other_test;");
    P(var);
    assertThat(var,is(equalTo(N(35))));
}

-(void) testUndefMethod {
    PC(@"class UndefMethodTest { def test { return 25; }; undef_method :test; }; x = UndefMethodTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testUndefMethod2 {
    PC(@"class UndefMethodSuperTest { def test { return 25; }; }; class UndefMethodSubTest < UndefMethodSuperTest { def test { return 50; }; undef_method :test; }; x = UndefMethodSubTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testUndefMethod3 {
    PC(@"class UndefMethodTest2 { def test { return 25; }; }; class UndefMethodTest2 { undef_method :test; }; x = UndefMethodTest2.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testRemoveMethod {
    PC(@"class RemoveMethodTest { def test { return 25; }; remove_method :test; }; x = RemoveMethodTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testRemoveMethod2 {
    PC(@"class RemoveMethodSuperTest { def test { return 25; }; }; class RemoveMethodSubTest < RemoveMethodSuperTest { def test { return 50; }; remove_method :test; }; x = RemoveMethodSubTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testRemoveMethod3 {
    PC(@"class RemoveMethodTest2 { def test { return 25; }; }; class RemoveMethodTest2 { remove_method :test; }; x = RemoveMethodTest2.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(nil)));
}

-(void) testUndefMethodRedef {
    PC(@"class UndefMethodRedefTest { def test { return 10; }; undef_method :test; def test { return 25; }; }; x = UndefMethodRedefTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testRemoveMethodRedef {
    PC(@"class RemoveMethodRedefTest { def test { return 10; }; remove_method :test; def test { return 25; }; }; x = RemoveMethodRedefTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}


-(void) testRemoveMethodRedefUndef {
    PC(@"class RemoveMethodRedefSuperTest { def test { return 25; }; }; class RemoveMethodRedefSubTest < RemoveMethodRedefSuperTest { def test { return 50; }; remove_method :test; def test { return 10; }; undef_method :test; }; x = RemoveMethodRedefSubTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

#ifdef MAC_OS_X_VERSION_10_7
-(void) testProtocolDefinition {
    PC(@"protocol SimpleProtocol { req method(x); }; class SimpleProtocolTest > SimpleProtocol { def method(x) { return x; }; }; a = SimpleProtocolTest.new; conformsToProtocol(a,'SimpleProtocol');");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}

-(void) testProtocolOptionalDefinition {
    PC(@"protocol SimpleProtocolOpt { req method(x); opt other_method(y); }; class SimpleProtocolOptTest > SimpleProtocolOpt { def method(x) { return x; }; }; a = SimpleProtocolOptTest.new; conformsToProtocol(a,'SimpleProtocolOpt');");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}

-(void) testProtocolInherit {
    PC(@"protocol SimpleProtocolSup { req method(x); }; protocol SimpleProtocolSub < SimpleProtocolSup { req other_method(y); }; class SimpleProtocolInhTest > SimpleProtocolSub { def method(x) { return x; }; def other_method(y) { return y; }; }; a = SimpleProtocolInhTest.new; conformsToProtocol(a,'SimpleProtocolSup');");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}

-(void) testProtocolProtocol {
    PC(@"protocol SimpleProtocolA { req method(x); }; protocol SimpleProtocolB > SimpleProtocolA { req other_method(y); }; class SimpleProtocolPTest > SimpleProtocolB { def method(x) { return x; }; def other_method(y) { return y; }; }; a = SimpleProtocolPTest.new; conformsToProtocol(a,'SimpleProtocolA');");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}
#endif

-(void) testEachSelectNest {
    NSArray* expect = [NSArray arrayWithObjects:N(1),N(2),N(3),nil];
    PC(@"a = [1,2,3,4,5]; b = []; a.select { |n| n < 4; }.each { |n| b << n; };");
    P(var);
    assertThat(var,is(equalTo(expect)));
}

-(void) testToSNSObject {
    PC(@"a = [1,2,3]; a.to_s;");
    P(var);
    assertThat(var,is(equalTo(@"1, 2, 3")));
}

-(void) testResponds {
    PC(@"class ResTest { def blah(x) { return x; }; }; x = ResTest.alloc.init; x.respondsToSelector(@selector(blah:));");
    P(var);
    assertThat(var,is(equalTo(BL(YES))));
}

-(void) testNoResponds {
    PC(@"class ResTest2 { def foo(x) { return x; }; }; x = ResTest2.alloc.init; x.respondsToSelector(@selector(blah:));");
    P(var);
    assertThat(var,is(equalTo(BL(NO))));
}

-(void) testCodeEval {
    PC(@"require('ext'); def evalTest(s) { code = <<-CODE def test() { return 7; }; CODE; s.class_eval(code); }; class PropTest { evalTest(self.alloc.init); }; x = PropTest.alloc.init; x.test;");
    P(var);
    assertThat(var,is(equalTo(N(7))));
}

-(void) testClassEval {
    PC(@"require('ext'); class TestClassEval { def test() { return 7; }; }; x = TestClassEval.alloc.init; x.class_eval('def test2() { return 25; };'); x.test2;");
    P(var);
    assertThat(var,is(equalTo(N(25))));
}

-(void) testInstanceEval {
    PC(@"require('ext'); class TestClassEval2 { def test() { @x = 17; }; }; x = TestClassEval2.alloc.init; x.test; x.instance_eval('@x;');");
    P(var);
    assertThat(var,is(equalTo(N(17))));
}

-(void) testTask {
    PC(@"class Task { def initWithName(name,action:act) { @name = name; @action = act; self; }; def execute(*args) { args = flatten(args); for(dep in @deps) { args = Tasks[dep].execute(args); }; @action(args); }; };"
        "Tasks = {};"
        "def task(__x,&body) {"
        "  name='';deps=[];"
        "  if(__x.is?(:NSDictionary)) {"
        "    name = __x::[0];"
        "    deps = __x[name];"
        "    if(!deps.is?(:NSArray)) { deps = [deps]; };"
        "  } else {"
        "    name = __x;"
        "  };"
        "  t = Task.alloc.initWithName(name,action:body);"
        "  t.deps = deps if deps;"
        "  Tasks[name] = t;"
        "  t;"
        "};"
        "task :add5 do { |args|"
        "  args[0] += 5;"
        "  args;"
        "};"
        "task :multiply2 => :add5 do { |args|"
        "  args[0] *= 2;"
        "  args;"
        "};"
        "x = Tasks[:multiply2].execute(10)[0];"
        "x;"
       );
    P(var);
    assertThat(var,is(equalTo(N(30))));
}

-(void) testModule {
    PC(@"module TestModule { def testModuleFunc(x) { return x*2; }; }; class ModuleTestClass { include TestModule; }; x = ModuleTestClass.alloc.init; x.testModuleFunc(10);");
    P(var);
    
    assertThat(var,is(equalTo(N(20))));
}
@end