forked from seattlerb/rubyinline
/
test_inline.rb
executable file
·1051 lines (792 loc) · 22.7 KB
/
test_inline.rb
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
$TESTING = true
$0 = __FILE__ if $0 =~ /-e|\(eval\)/
require 'inline'
require 'tempfile'
require 'tmpdir'
require 'fileutils' unless defined?(::FileUtils)
require 'minitest/autorun'
File.umask(0)
require 'pathname'
$expand_paths = Pathname.new(__FILE__).absolute?
$inline_path = './lib/inline.rb'
$test_inline_path = './test/test_inline.rb'
if $expand_paths then
$inline_path = File.expand_path $inline_path
$test_inline_path = File.expand_path $test_inline_path
end
class InlineTestCase < MiniTest::Unit::TestCase
def setup
super
@rootdir = File.join(Dir.tmpdir, "test_inline.#{$$}")
Dir.mkdir @rootdir, 0700 unless test ?d, @rootdir
ENV['INLINEDIR'] = @rootdir
end
def teardown
unless $DEBUG then
FileUtils.rm_rf @rootdir
ENV.delete 'INLINEDIR'
end
end
def test_stupid
#shuts test unit up
end
end
class TestDir < InlineTestCase
def setup
super
@count = 1
end
def util_assert_secure(perms, should_pass)
path = File.join(@rootdir, @count.to_s)
@count += 1
Dir.mkdir path, perms unless perms.nil?
if should_pass then
Dir.assert_secure path
else
assert_raises(SecurityError) do
Dir.assert_secure path
end
end
end
def test_assert_secure
# existing/good
util_assert_secure 0700, true
# existing/bad
util_assert_secure 0707, false
util_assert_secure 0770, false
util_assert_secure 0777, false
# missing
util_assert_secure nil, true
end
end unless Inline::WINDOZE
class TestInline < InlineTestCase
def test_rootdir
assert_equal(@rootdir, Inline.rootdir)
end
def test_directory
version = "#{Gem.ruby_engine}-#{RbConfig::CONFIG['ruby_version']}"
inlinedir = File.join(@rootdir, ".ruby_inline", version)
assert_equal(inlinedir, Inline.directory)
end
end
class TestInline
class TestC < InlineTestCase
def setup
super
@builder = Inline::C.new(self.class)
end
# quick hack to make tests more readable,
# does nothing I wouldn't otherwise do...
def inline(lang=:C)
self.class.inline(lang, true) do |builder|
yield(builder)
end
end
def test_initialize
x = Inline::C.new(self.class)
assert_equal TestInline::TestC, x.mod
assert_equal [], x.src
assert_equal({}, x.sig)
assert_equal [], x.flags
assert_equal [], x.libs
end
def test_accessor
builder = Inline::C.new self.class
builder.struct_name = 'MyStruct'
builder.accessor 'method_name', 'int'
source = util_strip_lines builder.src
expected = []
expected << <<-READER
# line N "#{$inline_path}"
static VALUE method_name(VALUE self) {
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
return (INT2FIX(pointer->method_name));
}
READER
expected << <<-WRITER
# line N "#{$inline_path}"
static VALUE method_name_equals(VALUE self, VALUE _value) {
VALUE value = (_value);
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
pointer->method_name = FIX2INT(value);
return (value);
}
WRITER
assert_equal expected, source
end
def test_accessor_member_name
builder = Inline::C.new self.class
builder.struct_name = 'MyStruct'
builder.accessor 'method_name', 'int', 'member_name'
source = util_strip_lines builder.src
expected = []
expected << <<-READER
# line N "#{$inline_path}"
static VALUE method_name(VALUE self) {
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
return (INT2FIX(pointer->member_name));
}
READER
expected << <<-WRITER
# line N "#{$inline_path}"
static VALUE method_name_equals(VALUE self, VALUE _value) {
VALUE value = (_value);
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
pointer->member_name = FIX2INT(value);
return (value);
}
WRITER
assert_equal expected, source
end
def test_accessor_no_struct_name
builder = Inline::C.new self.class
e = assert_raises RuntimeError do
builder.accessor 'method_name', 'int'
end
assert_equal "struct name not set for reader method_name int", e.message
end
def test_add_type_converter
builder = Inline::C.new self.class
builder.add_type_converter 'my_type', 'ruby_type2my_type',
'my_type2ruby_type'
assert_equal 'my_type2ruby_type', builder.c2ruby('my_type')
assert_equal 'ruby_type2my_type', builder.ruby2c('my_type')
end
def test_alias_type_converter
builder = Inline::C.new self.class
builder.alias_type_converter 'long long', 'int64_t'
assert_equal 'LL2NUM', builder.c2ruby('int64_t')
assert_equal 'NUM2LL', builder.ruby2c('int64_t')
end
def test_reader
builder = Inline::C.new self.class
builder.struct_name = 'MyStruct'
builder.reader 'method_name', 'int'
source = util_strip_lines builder.src
expected = []
expected << <<-READER
# line N "#{$inline_path}"
static VALUE method_name(VALUE self) {
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
return (INT2FIX(pointer->method_name));
}
READER
assert_equal expected, source
end
def test_reader_member_name
builder = Inline::C.new self.class
builder.struct_name = 'MyStruct'
builder.reader 'method_name', 'int', 'member_name'
source = util_strip_lines builder.src
expected = []
expected << <<-READER
# line N "#{$inline_path}"
static VALUE method_name(VALUE self) {
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
return (INT2FIX(pointer->member_name));
}
READER
assert_equal expected, source
end
def test_reader_no_struct_name
builder = Inline::C.new self.class
e = assert_raises RuntimeError do
builder.reader 'method_name', 'int'
end
assert_equal "struct name not set for reader method_name int", e.message
end
def test_remove_type_converter
builder = Inline::C.new self.class
builder.remove_type_converter 'long'
assert_raises ArgumentError do
builder.c2ruby 'long'
end
end
def test_writer
builder = Inline::C.new self.class
builder.struct_name = 'MyStruct'
builder.writer 'method_name', 'int'
source = util_strip_lines builder.src
expected = []
expected << <<-WRITER
# line N "#{$inline_path}"
static VALUE method_name_equals(VALUE self, VALUE _value) {
VALUE value = (_value);
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
pointer->method_name = FIX2INT(value);
return (value);
}
WRITER
assert_equal expected, source
end
def test_writer_member_name
builder = Inline::C.new self.class
builder.struct_name = 'MyStruct'
builder.writer 'method_name', 'int', 'member_name'
source = util_strip_lines builder.src
expected = []
expected << <<-WRITER
# line N "#{$inline_path}"
static VALUE method_name_equals(VALUE self, VALUE _value) {
VALUE value = (_value);
MyStruct *pointer;
Data_Get_Struct(self, MyStruct, pointer);
pointer->member_name = FIX2INT(value);
return (value);
}
WRITER
assert_equal expected, source
end
def test_writer_no_struct_name
builder = Inline::C.new self.class
e = assert_raises RuntimeError do
builder.writer 'method_name', 'int'
end
assert_equal "struct name not set for writer method_name int", e.message
end
def test_ruby2c
x = Inline::C.new(self.class)
assert_equal 'NUM2CHR', x.ruby2c("char")
assert_equal 'StringValuePtr', x.ruby2c("char *")
assert_equal "FI\X2INT", x.ruby2c("int")
assert_equal 'NUM2UINT', x.ruby2c("unsigned")
assert_equal 'NUM2UINT', x.ruby2c("unsigned int")
assert_equal 'NUM2LONG', x.ruby2c("long")
assert_equal 'NUM2ULONG', x.ruby2c("unsigned long")
assert_equal 'NUM2LL', x.ruby2c("long long")
assert_equal 'NUM2ULL', x.ruby2c("unsigned long long")
assert_equal 'NUM2DBL', x.ruby2c("double")
assert_equal 'NUM2OFFT', x.ruby2c("off_t")
assert_equal '', x.ruby2c("VALUE")
assert_raises ArgumentError do
x.ruby2c('blah')
end
end
def test_c2ruby
x = Inline::C.new(self.class)
assert_equal 'CHR2FIX', x.c2ruby("char")
assert_equal 'rb_str_new2', x.c2ruby("char *")
assert_equal 'INT2FIX', x.c2ruby("int")
assert_equal 'UINT2NUM', x.c2ruby("unsigned int")
assert_equal 'UINT2NUM', x.c2ruby("unsigned")
assert_equal 'LONG2NUM', x.c2ruby("long")
assert_equal 'ULONG2NUM', x.c2ruby("unsigned long")
assert_equal 'LL2NUM', x.c2ruby("long long")
assert_equal 'ULL2NUM', x.c2ruby("unsigned long long")
assert_equal 'rb_float_new', x.c2ruby("double")
assert_equal 'OFFT2NUM', x.c2ruby("off_t")
assert_equal '', x.c2ruby("VALUE")
assert_raises ArgumentError do
x.c2ruby('blah')
end
end
def util_module_name(*signatures)
md5 = Digest::MD5.new
signatures.each do |signature|
@builder.sig[signature] = [nil, 0]
md5 << signature.to_s
end
assert_equal("Inline_TestInline__TestC_#{md5}", @builder.module_name)
end
def test_module_name_0_methods
util_module_name
end
def test_module_name_1_method
util_module_name :something1
end
def test_module_name_2_methods
util_module_name :something2, :something3
end
def test_module_name_2_other_methods
util_module_name :something4, :something5
end
def util_parse_signature(src, expected, t=nil, a=nil, b=nil)
result = nil
@builder.add_type_converter t, a, b unless t.nil?
result = @builder.parse_signature(src)
assert_equal(expected, result)
end
def test_parse_signature
src = "// stupid cpp comment
#include \"header.h\"
/* stupid c comment */
int
add(int x, int y) {
int result = x+y;
return result;
}
"
expected = {
'name' => 'add',
'return' => 'int',
'arity' => 2,
'args' => [
['x', 'int'],
['y', 'int']
]
}
util_parse_signature(src, expected)
end
def test_parse_signature_custom
src = "// stupid cpp comment
#include \"header.h\"
/* stupid c comment */
int
add(fooby x, int y) {
int result = x+y;
return result;
}
"
expected = {
'name' => 'add',
'return' => 'int',
'arity' => 2,
'args' => [
[ 'x', 'fooby' ],
['y', 'int']
]
}
util_parse_signature(src, expected,
"fooby", "r2c_fooby", "c2r_fooby")
end
def test_parse_signature_register
src = "// stupid cpp comment
#include \"header.h\"
/* stupid c comment */
int
add(register int x, int y) {
int result = x+y;
return result;
}
"
expected = {
'name' => 'add',
'return' => 'int',
'arity' => 2,
'args' => [
[ 'x', 'register int' ],
['y', 'int']
]
}
util_parse_signature(src, expected,
"register int", "FI\X2INT", "INT2FI\X")
end
def util_generate(src, expected, expand_types=true)
result = @builder.generate src, expand_types
result = util_strip_lines result
result.gsub!(/\# line \d+/, '# line N')
expected = "# line N \"#{$0}\"\n" + expected
assert_equal(expected, result)
end
def util_generate_raw(src, expected)
util_generate(src, expected, false)
end
def util_strip_lines(src)
case src
when String then
src.gsub(/\# line \d+/, '# line N')
when Array then
src.map do |chunk|
util_strip_lines chunk
end
end
end
# Ruby Arity Rules, from the mouth of Matz:
# -2 = ruby array argv
# -1 = c array argv
# 0 = self
# 1 = self, value
# 2 = self, value, value
# ...
# 16 = self, value * 15
def test_generate_raw_arity_0
src = "VALUE y(VALUE self) {blah;}"
expected = "static VALUE y(VALUE self) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_0
src = "int y() { do_something; return 42; }"
expected = "static VALUE y(VALUE self) {\n do_something; return INT2FIX(42); }"
util_generate(src, expected)
end
def test_generate_arity_0_no_return
src = "void y() { do_something; }"
expected = "static VALUE y(VALUE self) {\n do_something;\nreturn Qnil;\n}"
util_generate(src, expected)
end
def test_generate_arity_0_void_return
src = "void y(void) {go_do_something_external;}"
expected = "static VALUE y(VALUE self) {
go_do_something_external;\nreturn Qnil;\n}"
util_generate(src, expected)
end
def test_generate_arity_0_int_return
src = "int x() {return 42}"
expected = "static VALUE x(VALUE self) {
return INT2FIX(42)}"
util_generate(src, expected)
end
def test_generate_raw_arity_1
src = "VALUE y(VALUE self, VALUE obj) {blah;}"
expected = "static VALUE y(VALUE self, VALUE obj) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_1
src = "int y(int x) {blah; return x+1;}"
expected = "static VALUE y(VALUE self, VALUE _x) {\n int x = FIX2INT(_x);\nblah; return INT2FIX(x+1);}"
util_generate(src, expected)
end
def test_generate_arity_1_no_return
src = "void y(int x) {blah;}"
expected = "static VALUE y(VALUE self, VALUE _x) {\n int x = FIX2INT(_x);\nblah;\nreturn Qnil;\n}"
util_generate(src, expected)
end
def test_generate_raw_arity_2
src = "VALUE func(VALUE self, VALUE obj1, VALUE obj2) {blah;}"
expected = "static VALUE func(VALUE self, VALUE obj1, VALUE obj2) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_2
src = "int func(int x, int y) {blah; return x+y;}"
expected = "static VALUE func(VALUE self, VALUE _x, VALUE _y) {\n int x = FIX2INT(_x);\n int y = FIX2INT(_y);\nblah; return INT2FIX(x+y);}"
util_generate(src, expected)
end
def test_generate_raw_arity_3
src = "VALUE func(VALUE self, VALUE obj1, VALUE obj2, VALUE obj3) {blah;}"
expected = "static VALUE func(VALUE self, VALUE obj1, VALUE obj2, VALUE obj3) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_too_many
src = "int too_many(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int aA, int aB, int aC, int aD, int aE, int aF, int ugh) {
int q = v + w;
return q+x+y+z;
}"
assert_raises ArgumentError do
@builder.generate src, true
end
end
def test_generate_comments
src = "// stupid cpp comment
/* stupid c comment */
int
add(int x, int y) { // add two numbers
return x+y;
}
"
expected = "static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y);
}
"
util_generate(src, expected)
end
def test_generate_local_header
src = "// stupid cpp comment
#include \"header\"
/* stupid c comment */
int
add(int x, int y) { // add two numbers
return x+y;
}
"
# FIX: should be 2 spaces before the return. Can't find problem.
expected = "#include \"header\"
static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y);
}
"
util_generate(src, expected)
end
def test_generate_map_name
src = "VALUE y_equals(VALUE self) {blah;}"
expected = "static VALUE y_equals(VALUE self) {blah;}"
util_generate_raw(src, expected)
assert_equal [-1, nil, 'y='], @builder.sig['y_equals']
end
def test_generate_system_header
src = "// stupid cpp comment
#include <header>
/* stupid c comment */
int
add(int x, int y) { // add two numbers
return x+y;
}
"
expected = "#include <header>
static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y);
}
"
util_generate(src, expected)
end
def test_generate_wonky_return
src = "unsigned\nlong z(void) {return 42}"
expected = "static VALUE z(VALUE self) {
return ULONG2NUM(42)}"
util_generate(src, expected)
end
def test_generate_compact
src = "int add(int x, int y) {return x+y}"
expected = "static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y)}"
util_generate(src, expected)
end
def test_generate_char_star_normalize
src = "char\n\*\n blah( char*s) {puts(s); return s}"
expected = "static VALUE blah(VALUE self, VALUE _s) {
char * s = StringValuePtr(_s);
puts(s); return rb_str_new2(s)}"
util_generate(src, expected)
end
def test_generate_ext
@builder.c_singleton "VALUE allocate() { return Qnil; }"
@builder.c "VALUE my_method() { return Qnil; }"
windoze = "\n __declspec(dllexport)" if Inline::WINDOZE
expected = <<-EXT
#include "ruby.h"
# line N "#{$test_inline_path}"
static VALUE allocate(VALUE self) {
return (Qnil); }
# line N "#{$test_inline_path}"
static VALUE my_method(VALUE self) {
return (Qnil); }
#ifdef __cplusplus
extern \"C\" {
#endif#{windoze}
void Init_Inline_TestInline__TestC_eba5e5454322e22fe2310198ef14e43f() {
VALUE c = rb_cObject;
c = rb_const_get(c, rb_intern("TestInline"));
c = rb_const_get(c, rb_intern("TestC"));
rb_define_alloc_func(c, (VALUE(*)(VALUE))allocate);
rb_define_method(c, "my_method", (VALUE(*)(ANYARGS))my_method, 0);
}
#ifdef __cplusplus
}
#endif
EXT
assert_equal expected, util_strip_lines(@builder.generate_ext)
end
def test_generate_ext_bad_allocate
@builder.c_singleton "VALUE allocate(VALUE bad) { return Qnil; }"
e = assert_raises RuntimeError do
@builder.generate_ext
end
assert_equal 'TestInline::TestC::allocate must have an arity of zero',
e.message
end
def test_c
result = @builder.c "int add(int a, int b) { return a + b; }"
expected = "# line N \"#{$0}\"\nstatic VALUE add(VALUE self, VALUE _a, VALUE _b) {\n int a = FIX2INT(_a);\n int b = FIX2INT(_b);\n return INT2FIX(a + b); }"
result.gsub!(/\# line \d+/, '# line N')
assert_equal expected, result
assert_equal [expected], @builder.src
end
def test_c_raw
src = "static VALUE answer_raw(int argc, VALUE *argv, VALUE self) { return INT2NUM(42); }"
result = @builder.c_raw src.dup
result.gsub!(/\# line \d+/, '# line N')
expected = "# line N \"#{$0}\"\n" + src
assert_equal expected, result
assert_equal [expected], @builder.src
end
def test_map_c_const
@builder.map_c_const :C_NAME => :int
expected = [
" rb_define_const(c, \"C_NAME\", INT2FIX(C_NAME));"
]
assert_equal expected, @builder.init_extra
end
def test_map_c_const_named
@builder.map_c_const :C_NAME => [:int, :RUBY_NAME]
expected = [
" rb_define_const(c, \"RUBY_NAME\", INT2FIX(C_NAME));"
]
assert_equal expected, @builder.init_extra
end
# TODO: fix ?
def util_simple_code(klassname, c_src)
result = "
require 'inline'
class #{klassname}
inline do |builder|
builder.c <<-EOC
#{c_src}
EOC
end
end"
result
end
def util_test_build(src)
tempfile = Tempfile.new("util_test_build")
tempfile.write src
tempfile.close
rb_file = tempfile.path + ".rb"
File.rename tempfile.path, rb_file
begin
Kernel.module_eval { require rb_file }
yield if block_given?
ensure
File.unlink rb_file
end
end
def test_build_good
code = util_simple_code(:DumbTest1, "long dumbpi() { return 314; }")
util_test_build(code) do
result = DumbTest1.new.dumbpi
assert_equal(314, result)
end
end
def test_build_bad
code = util_simple_code(:DumbTest2, "void should_puke() { 1+1 2+2 }")
assert_raises(CompilationError) do
util_test_build(code) do
flunk
end
end
end
def util_strip_comments(input)
expect = 'line 1
#if 0
line 2
#endif
line 3
'
assert_equal expect, @builder.strip_comments(input)
end
def test_strip_comments_cpp
input = 'line 1
#if 0
line 2
#endif
// 1 comment
// 2 comment
line 3 // trailing comment
'
util_strip_comments(input)
end
def test_strip_comments_c
input = 'line 1
#if 0
line 2
#endif
/*
* 1 comment
* 2 comment
*/
line 3 /* trailing comment */
'
util_strip_comments(input)
end
def test_load
# totally tested by test_build
end
end # class TestC
end # class TestInline
module Foo
class Bar
# inline stuff will go here...
end
end
$test_module_code = <<-EOR
module Foo
class Bar
inline do |builder|
builder.c <<-EOC
static int forty_two_instance() { return 42; }
EOC
builder.c_singleton <<-EOC
static int twenty_four_class() { return 24; }
EOC
end
end
end
EOR
$test_module_code2 = <<-EOR
require 'inline'
# Demonstrates native functions in nested classes and
# extending a class more than once from different ruby
# source files
module Foo
class Bar
inline do |builder|
builder.c <<-EOC
int twelve_instance() { return 12; }
EOC
builder.c_singleton <<-EOC
int twelve_class() { return 12; }
EOC
end
end
end
EOR
class TestModule < InlineTestCase
def test_nested
Object.class_eval $test_module_code
fb = Foo::Bar.new
assert_equal(42, fb.forty_two_instance)
assert_equal(24, Foo::Bar.twenty_four_class)
tempfile = Tempfile.new("test_inline_nested")
tempfile.write($test_module_code2)
tempfile.flush
tempfile.rewind
FileUtils.cp tempfile.path, "#{tempfile.path}.rb"
require "#{tempfile.path}.rb"
assert_equal(12, fb.twelve_instance)
assert_equal(12, Foo::Bar.twelve_class)