forked from go-python/gopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
997 lines (927 loc) · 25.2 KB
/
main_test.go
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
// Copyright 2015 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
"github.com/go-python/gopy/bind"
)
var (
testBackends = map[string]string{}
features = map[string][]string{
"_examples/hi": []string{"py3"}, // output is different for 2 vs. 3 -- only checking 3 output
"_examples/funcs": []string{"py2", "py3"},
"_examples/sliceptr": []string{"py2", "py3"},
"_examples/simple": []string{"py2", "py3"},
"_examples/empty": []string{"py2", "py3"},
"_examples/named": []string{"py2", "py3"},
"_examples/structs": []string{"py2", "py3"},
"_examples/consts": []string{"py2", "py3"}, // 2 doesn't report .666 decimals
"_examples/vars": []string{"py2", "py3"},
"_examples/seqs": []string{"py2", "py3"},
"_examples/cgo": []string{"py2", "py3"},
"_examples/pyerrors": []string{"py2", "py3"},
"_examples/iface": []string{"py3"}, // output order diff for 2, fails but actually works
"_examples/pointers": []string{"py2", "py3"},
"_examples/arrays": []string{"py2", "py3"},
"_examples/slices": []string{"py2", "py3"},
"_examples/maps": []string{"py2", "py3"},
"_examples/gostrings": []string{"py2", "py3"},
"_examples/rename": []string{"py2", "py3"},
"_examples/lot": []string{"py2", "py3"},
"_examples/unicode": []string{"py3"}, // doesn't work for 2
"_examples/osfile": []string{"py2", "py3"},
"_examples/gopygc": []string{"py2", "py3"},
"_examples/cstrings": []string{"py2", "py3"},
}
testEnvironment = os.Environ()
)
func TestGovet(t *testing.T) {
cmd := exec.Command("go", "vet", "./...")
buf := new(bytes.Buffer)
cmd.Stdout = buf
cmd.Stderr = buf
err := cmd.Run()
if err != nil {
t.Fatalf("error running %s:\n%s\n%v", "go vet", string(buf.Bytes()), err)
}
}
func TestGofmt(t *testing.T) {
exe, err := exec.LookPath("goimports")
if err != nil {
switch e := err.(type) {
case *exec.Error:
if e.Err == exec.ErrNotFound {
exe, err = exec.LookPath("gofmt")
}
}
}
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(exe, "-d", ".")
buf := new(bytes.Buffer)
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Run()
if err != nil {
t.Fatalf("error running %s:\n%s\n%v", exe, string(buf.Bytes()), err)
}
if len(buf.Bytes()) != 0 {
t.Errorf("some files were not gofmt'ed:\n%s\n", string(buf.Bytes()))
}
}
func TestGoPyErrors(t *testing.T) {
pyvm := testBackends["py3"]
workdir, err := ioutil.TempDir("", "gopy-")
if err != nil {
t.Fatalf("could not create workdir: %v\n", err)
}
t.Logf("pyvm: %s making work dir: %s\n", pyvm, workdir)
defer os.RemoveAll(workdir)
curPkgPath := reflect.TypeOf(pkg{}).PkgPath()
fpath := filepath.Join(curPkgPath, "_examples/gopyerrors")
cmd := exec.Command("go", "run", ".", "gen", "-vm="+pyvm, "-output="+workdir, fpath)
t.Logf("running: %v\n", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("could not run %v: %+v\n", strings.Join(cmd.Args, " "), err)
}
contains := `--- Processing package: github.com/go-python/gopy/_examples/gopyerrors ---
ignoring python incompatible function: gopyerrors.func github.com/go-python/gopy/_examples/gopyerrors.NotErrorMany() (int, int): func() (int, int): gopy: second result value must be of type error: func() (int, int)
ignoring python incompatible method: gopyerrors.func (*github.com/go-python/gopy/_examples/gopyerrors.Struct).NotErrorMany() (int, string): func() (int, string): gopy: second result value must be of type error: func() (int, string)
ignoring python incompatible method: gopyerrors.func (*github.com/go-python/gopy/_examples/gopyerrors.Struct).TooMany() (int, int, string): func() (int, int, string): gopy: too many results to return: func() (int, int, string)
ignoring python incompatible function: gopyerrors.func github.com/go-python/gopy/_examples/gopyerrors.TooMany() (int, int, string): func() (int, int, string): gopy: too many results to return: func() (int, int, string)
`
if got, want := string(out), contains; !strings.Contains(got, want) {
t.Fatalf("%v does not contain %v\n", got, want)
}
}
func TestHi(t *testing.T) {
// t.Parallel()
path := "_examples/hi"
// NOTE: output differs for python2 -- only valid checking for 3
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`hi from go
hello you from go
working...
worked for 2 hours
working...
working...
worked for 4 hours
--- doc(hi)...
package hi exposes a few Go functions to be wrapped and used from Python.
--- hi.Universe: 42
--- hi.Version: 0.1
--- hi.Debug(): False
--- hi.Set_Debug(true)
--- hi.Debug(): True
--- hi.Set_Debug(false)
--- hi.Debug(): False
--- hi.Anon(): hi.Person{Name="<nobody>", Age=1}
--- new anon: hi.Person{Name="you", Age=24}
--- hi.Set_Anon(hi.NewPerson('you', 24))...
--- hi.Anon(): hi.Person{Name="you", Age=24}
--- doc(hi.Hi)...
Hi()
Hi prints hi from Go
--- hi.Hi()...
--- doc(hi.Hello)...
Hello(str s)
Hello prints a greeting from Go
--- hi.Hello('you')...
--- doc(hi.Add)...
Add(int i, int j) int
Add returns the sum of its arguments.
--- hi.Add(1, 41)...
42
--- hi.Concat('4', '2')...
42
--- hi.LookupQuestion(42)...
Life, the Universe and Everything
--- hi.LookupQuestion(12)...
caught: <built-in function hi_LookupQuestion> returned a result with an error set
--- doc(hi.Person):
Person is a simple struct
--- p = hi.Person()...
--- p: hi.Person{Name="", Age=0}
--- p.Name:
--- p.Age: 0
--- doc(hi.Greet):
Greet() str
Greet sends greetings
--- p.Greet()...
Hello, I am
--- p.String()...
hi.Person{Name="", Age=0}
--- doc(p):
Person is a simple struct
--- p.Name = "foo"...
--- p.Age = 42...
--- p.String()...
hi.Person{Name="foo", Age=42}
--- p.Age: 42
--- p.Name: foo
--- p.Work(2)...
--- p.Work(24)...
caught: <built-in function hi_Person_Work> returned a result with an error set
--- p.Salary(2): 20
--- p.Salary(24): caught: <built-in function hi_Person_Salary> returned a result with an error set
--- Person.__init__
caught: argument 2 must be str, not int | err-type: <class 'TypeError'>
caught: an integer is required (got type str) | err-type: <class 'TypeError'>
*ERROR* no exception raised!
hi.Person{Name="name", Age=0}
hi.Person{Name="name", Age=42}
hi.Person{Name="name", Age=42}
hi.Person{Name="name", Age=42}
--- hi.NewPerson('me', 666): hi.Person{Name="me", Age=666}
--- hi.NewPersonWithAge(666): hi.Person{Name="stranger", Age=666}
--- hi.NewActivePerson(4):
hi.Person{Name="", Age=0}
--- c = hi.Couple()...
hi.Couple{P1=hi.Person{Name="", Age=0}, P2=hi.Person{Name="", Age=0}}
--- c.P1: hi.Person{Name="", Age=0}
--- c: hi.Couple{P1=hi.Person{Name="tom", Age=5}, P2=hi.Person{Name="bob", Age=2}}
--- c = hi.NewCouple(tom, bob)...
hi.Couple{P1=hi.Person{Name="tom", Age=50}, P2=hi.Person{Name="bob", Age=41}}
hi.Couple{P1=hi.Person{Name="mom", Age=50}, P2=hi.Person{Name="bob", Age=51}}
--- Couple.__init__
hi.Couple{P1=hi.Person{Name="p1", Age=42}, P2=hi.Person{Name="p2", Age=52}}
hi.Couple{P1=hi.Person{Name="p1", Age=42}, P2=hi.Person{Name="p2", Age=52}}
hi.Couple{P1=hi.Person{Name="p2", Age=52}, P2=hi.Person{Name="p1", Age=42}}
caught: supplied argument type <class 'int'> is not a go.GoClass | err-type: <class 'TypeError'>
caught: supplied argument type <class 'int'> is not a go.GoClass | err-type: <class 'TypeError'>
caught: supplied argument type <class 'int'> is not a go.GoClass | err-type: <class 'TypeError'>
--- testing GC...
--- len(objs): 100000
--- len(vs): 100000
--- testing GC... [ok]
--- testing array...
arr: hi.Array_2_int len: 2 handle: 300036 [1, 2]
len(arr): 2
arr[0]: 1
arr[1]: 2
arr[2]: caught: slice index out of range
arr: hi.Array_2_int len: 2 handle: 300036 [1, 2]
len(arr): 2
mem(arr): caught: memoryview: a bytes-like object is required, not 'Array_2_int'
--- testing slice...
slice: go.Slice_int len: 2 handle: 300037 [1, 2]
len(slice): 2
slice[0]: 1
slice[1]: 2
slice[2]: caught: slice index out of range
slice: go.Slice_int len: 2 handle: 300037 [1, 42]
slice repr: go.Slice_int([1, 42])
len(slice): 2
mem(slice): caught: memoryview: a bytes-like object is required, not 'Slice_int'
OK
`),
})
}
func TestBindFuncs(t *testing.T) {
// t.Parallel()
path := "_examples/funcs"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`got return value: true
got nil
ofs FieldI: 42 FieldS: str field
fs.CallBack(22, cbfun)...
in python cbfun: FieldI: 42 FieldS: str field ival: 22 sval: str field
fs.CallBackIf(22, cbfunif)...
in python cbfunif: FieldI: 42 FieldS: str field ival: 22 ifval: str field
fs.CallBackRval(22, cbfunrval)...
in python cbfunrval: FieldI: 42 FieldS: str field ival: 22 ifval: str field
fs.CallBack(32, cls.ClassFun)...
in python class fun: FieldI: 42 FieldS: str field ival: 32 sval: str field
cls.CallSelf...
in python class fun: FieldI: 42 FieldS: str field ival: 77 sval: str field
fs.ObjArg with nil
fs.ObjArg with fs
OK
`),
})
}
func TestBindSimple(t *testing.T) {
// t.Parallel()
path := "_examples/simple"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`doc(pkg):
'\nsimple is a simple package.\n\n'
pkg.Func()...
fct = pkg.Func...
fct()...
pkg.Add(1,2)= 3
pkg.Bool(True)= True
pkg.Bool(False)= False
pkg.Comp64Add((3+4j), (2+5j)) = (5+9j)
pkg.Comp128Add((3+4j), (2+5j)) = (5+9j)
OK
`),
})
}
func TestBindEmpty(t *testing.T) {
// t.Parallel()
path := "_examples/empty"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`doc(pkg):
'\nPackage empty does not expose anything.\nWe may want to wrap and import it just for its side-effects.\n\n'
OK
`),
})
}
func TestBindPointers(t *testing.T) {
// t.Parallel()
path := "_examples/pointers"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`s = pointers.S(2)
s = pointers.S{Value=2, handle=1}
s.Value = 2
OK
`),
})
}
func TestBindNamed(t *testing.T) {
// t.Parallel()
path := "_examples/named"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`doc(named): '\npackage named tests various aspects of named types.\n\n'
s = named.Slice()
s = named.named_Slice len: 0 handle: 1 []
s = named.Slice([1,2])
s = named.named_Slice len: 2 handle: 2 [1.0, 2.0]
s = named.Slice(range(10))
s = named.named_Slice len: 10 handle: 3 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
s = named.Slice(xrange(10))
s = named.named_Slice len: 10 handle: 4 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
OK
`),
})
}
func TestBindStructs(t *testing.T) {
// t.Parallel()
path := "_examples/structs"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`s = structs.S()
s = structs.S{handle=1}
s.Init()
s.Upper('boo')= 'BOO'
s1 = structs.S1()
s1 = structs.S1{handle=2}
caught error: 'S1' object has no attribute 'private'
s2 = structs.S2()
s2 = structs.S2{Public=1, handle=5}
s2 = structs.S2{Public=42, handle=7}
s2.Public = 42
caught error: 'S2' object has no attribute 'private'
s2child = S2Child{S2: structs.S2{Public=42, handle=8, local=123}, local: 123}
s2child.Public = 42
s2child.local = 123
caught error: 'S2Child' object has no attribute 'private'
OK
`),
})
}
func TestBindConsts(t *testing.T) {
// t.Parallel()
path := "_examples/consts"
// NOTE: python2 does not properly output .666 decimals for unknown reasons.
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`c1 = c1
c2 = 42
c3 = 666.666
c4 = c4
c5 = 42
c6 = 42
c7 = 666.666
k1 = 1
k2 = 2
OK
`),
})
}
func TestBindVars(t *testing.T) {
// t.Parallel()
path := "_examples/vars"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`doc(vars):
None
doc(vars.V1()):
'\n\tV1 Gets Go Variable: vars.V1\n\t\n\t'
doc(vars.Set_V1()):
'\n\tSet_V1 Sets Go Variable: vars.V1\n\t\n\t'
Initial values
v1 = v1
v2 = 42
v3 = 666.666
v4 = c4
v5 = 42
v6 = 42
v7 = 666.666
k1 = 1
k2 = 2
New values
v1 = test1
v2 = 90
v3 = 1111.1111
v4 = test2
v5 = 50
v6 = 50
v7 = 1111.1111
k1 = 123
k2 = 456
vars.Doc() = 'A variable with some documentation'
doc of vars.Doc = '\n\tDoc Gets Go Variable: vars.Doc\n\tDoc is a top-level string with some documentation attached.\n\t\n\t'
doc of vars.Set_Doc = '\n\tSet_Doc Sets Go Variable: vars.Doc\n\tDoc is a top-level string with some documentation attached.\n\t\n\t'
OK
`),
})
}
func TestBindSeqs(t *testing.T) {
// t.Parallel()
path := "_examples/seqs"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`doc(seqs): '\npackage seqs tests various aspects of sequence types.\n\n'
s = seqs.Slice()
s = seqs.seqs_Slice len: 0 handle: 1 []
s = seqs.Slice([1,2])
s = seqs.seqs_Slice len: 2 handle: 2 [1.0, 2.0]
s = seqs.Slice(range(10))
s = seqs.seqs_Slice len: 10 handle: 3 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
s = seqs.Slice(xrange(10))
s = seqs.seqs_Slice len: 10 handle: 4 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
s = seqs.Slice()
s = seqs.seqs_Slice len: 0 handle: 5 []
s += [1,2]
s = seqs.seqs_Slice len: 2 handle: 5 [1.0, 2.0]
s += [10,20]
s = seqs.seqs_Slice len: 4 handle: 5 [1.0, 2.0, 10.0, 20.0]
OK
`),
})
}
func TestBindInterfaces(t *testing.T) {
// t.Parallel()
path := "_examples/iface"
// NOTE: python2 outputs this in a different order, so test fails, but results are the same
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`t.F [CALLED]
iface.CallIface...
t.F [CALLED]
iface.CallIface... [DONE]
iface as string: test string
iface as string: 42
iface as handle: &{0 }
iface as handle: <nil>
doc(iface): '\npackage iface tests various aspects of interfaces.\n\n'
t = iface.T()
t.F()
iface.CallIface(t)
iface.IfaceString("test string"
iface.IfaceString(str(42))
iface.IfaceHandle(t)
iface.IfaceHandle(go.nil)
OK
`),
})
}
func TestBindCgoPackage(t *testing.T) {
// t.Parallel()
path := "_examples/cgo"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`cgo.doc: '\nPackage cgo tests bindings of CGo-based packages.\n\n'
cgo.Hi()= 'hi from go\n'
cgo.Hello(you)= 'hello you from go\n'
OK
`),
})
}
func TestPyErrors(t *testing.T) {
// t.Parallel()
path := "_examples/pyerrors"
testPkg(t, pkg{
path: path,
lang: features[path], // TODO: should print out the error message!
cmd: "build",
extras: nil,
want: []byte(`<built-in function pyerrors_Div> returned a result with an error set
pyerrors.Div(5, 2) = 2
OK
`),
})
}
func TestBuiltinArrays(t *testing.T) {
// t.Parallel()
path := "_examples/arrays"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`Python list: [1, 2, 3, 4]
Go array: arrays.Array_4_int len: 4 handle: 1 [1, 2, 3, 4]
arrays.IntSum from Go array: 10
OK
`),
})
}
func TestBuiltinSlices(t *testing.T) {
// t.Parallel()
path := "_examples/slices"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`0: S0
1: S1
2: S2
S0
S1
Python list: [1, 2, 3, 4]
Go slice: go.Slice_int len: 4 handle: 1 [1, 2, 3, 4]
slices.IntSum from Python list: 10
slices.IntSum from Go slice: 10
unsigned slice elements: 1 2 3 4
signed slice elements: -1 -2 -3 -4
struct slice: slices.Slice_Ptr_slices_S len: 3 handle: 11 [12, 13, 14]
struct slice[0]: slices.S{Name=S0, handle=15}
struct slice[1]: slices.S{Name=S1, handle=16}
struct slice[2].Name: S2
OK
`),
})
}
func TestBuiltinMaps(t *testing.T) {
// t.Parallel()
path := "_examples/maps"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`map a[1]: 3.0
map a[2]: 5.0
2 in map: True
3 in map: False
maps.Sum from Go map: 8.0
map b: {1: 3.0, 2: 5.0}
maps.Sum from Python dictionary: 8.0
maps.Keys from Go map: go.Slice_int len: 2 handle: 3 [1, 2]
maps.Values from Go map: go.Slice_float64 len: 2 handle: 4 [3.0, 5.0]
maps.Keys from Python dictionary: go.Slice_int len: 2 handle: 6 [1, 2]
maps.Values from Python dictionary: go.Slice_float64 len: 2 handle: 8 [3.0, 5.0]
deleted 1 from a: maps.Map_int_float64 len: 1 handle: 1 {2=5.0, }
OK
`),
})
}
func TestBindStrings(t *testing.T) {
// t.Parallel()
path := "_examples/gostrings"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`S1 = S1
GetString() = MyString
OK
`),
})
}
func TestBindRename(t *testing.T) {
// t.Parallel()
path := "_examples/rename"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`hi
something
OK
`),
})
}
func TestLot(t *testing.T) {
// t.Parallel()
path := "_examples/lot"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`l.SomeString : some string
l.SomeInt : 1337
l.SomeFloat : 1337.1337
l.SomeBool : True
l.SomeListOfStrings: go.Slice_string len: 4 handle: 2 [some, list, of, strings]
l.SomeListOfInts: go.Slice_int64 len: 4 handle: 3 [6, 2, 9, 1]
l.SomeListOfFloats: go.Slice_float64 len: 4 handle: 4 [6.6, 2.2, 9.9, 1.1]
l.SomeListOfBools: go.Slice_bool len: 4 handle: 5 [True, False, True, False]
OK
`),
})
}
func TestSlicePtr(t *testing.T) {
// t.Parallel()
path := "_examples/sliceptr"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`sliceptr.sliceptr_IntVector len: 3 handle: 1 [1, 2, 3]
sliceptr.sliceptr_IntVector len: 4 handle: 1 [1, 2, 3, 4]
sliceptr.sliceptr_StrVector len: 4 handle: 2 [1, 2, 3, 4]
OK
`),
})
}
func TestUnicode(t *testing.T) {
// t.Parallel()
path := "_examples/unicode"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`encoding.HandleString(unicodestr) -> Python Unicode string 🐱
encoding.GetString() -> Go Unicode string 🐱
OK
`),
})
}
func TestPYGC(t *testing.T) {
// t.Parallel()
path := "_examples/gopygc"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`0
3
0
3
5
6
7
8
5
3
2
1
0
1
0
1
1
1
0
OK
`),
})
}
func TestCStrings(t *testing.T) {
// t.Parallel()
path := "_examples/cstrings"
testPkg(t, pkg{
path: path,
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`gofnString leaked: False
gofnStruct leaked: False
gofnNestedStruct leaked: False
gofnSlice leaked: False
gofnMap leaked: False
OK
`),
})
}
// see notes in _examples/osfile/test.py for why this doesn't work..
// leaving here for now in case someone wants to follow-up and make it work..
//
// func TestOsFile(t *testing.T) {
// // t.Parallel()
// path := "_examples/osfile"
// testPkg(t, pkg{
// path: path,
// lang: features[path],
// cmd: "pkg",
// extras: []string{"os"},
// want: []byte(`yeah..
// OK
// `),
// })
// }
// Generate / verify SUPPORT_MATRIX.md from features map.
func TestCheckSupportMatrix(t *testing.T) {
var buf bytes.Buffer
buf.WriteString("# Support matrix\n")
buf.WriteString(`
NOTE: File auto-generated by TestCheckSupportMatrix in main_test.go. Please
don't modify manually.
`)
// Generate
// - sorted list of features
// - sorted list of backends
// - a map of feature to available backends
var featuresSorted []string
var allBackendsSorted []string
featureToBackendMap := make(map[string]map[string]bool)
allBackends := make(map[string]bool)
for feature, backends := range features {
featuresSorted = append(featuresSorted, feature)
featureToBackendMap[feature] = make(map[string]bool)
for _, backend := range backends {
featureToBackendMap[feature][backend] = true
allBackends[backend] = true
}
}
for backend, _ := range allBackends {
allBackendsSorted = append(allBackendsSorted, backend)
}
sort.Strings(featuresSorted)
sort.Strings(allBackendsSorted)
// Write the table header and the line separating header and rows.
fmt.Fprintf(&buf, "Feature |%s\n", strings.Join(allBackendsSorted, " | "))
var tableDelimiters []string
for i := 0; i <= len(allBackendsSorted); i++ {
tableDelimiters = append(tableDelimiters, "---")
}
buf.WriteString(strings.Join(tableDelimiters, " | "))
buf.WriteString("\n")
// Write the actual rows of the support matrix.
for _, feature := range featuresSorted {
var cells []string
cells = append(cells, feature)
for _, backend := range allBackendsSorted {
if featureToBackendMap[feature][backend] {
cells = append(cells, "yes")
} else {
cells = append(cells, "no")
}
}
buf.WriteString(strings.Join(cells, " | "))
buf.WriteString("\n")
}
if os.Getenv("GOPY_GENERATE_SUPPORT_MATRIX") == "1" {
err := ioutil.WriteFile("SUPPORT_MATRIX.md", buf.Bytes(), 0644)
if err != nil {
log.Fatalf("Unable to write SUPPORT_MATRIX.md")
}
return
}
src, err := ioutil.ReadFile("SUPPORT_MATRIX.md")
if err != nil {
log.Fatalf("Unable to read SUPPORT_MATRIX.md")
}
msg := `
This is a test case to verify the support matrix. This test is likely failing
because the map features has been updated and the
auto-generated file SUPPORT_MATRIX.md hasn't been updated. Please run 'go test'
with environment variable GOPY_GENERATE_SUPPORT_MATRIX=1 to regenerate
SUPPORT_MATRIX.md and commit the changes to SUPPORT_MATRIX.md onto git.
`
if bytes.Compare(buf.Bytes(), src) != 0 {
t.Fatalf(msg)
}
}
type pkg struct {
path string
lang []string
cmd string
extras []string
want []byte
}
func testPkg(t *testing.T, table pkg) {
// backends := []string{"py2", "py3"}
backends := []string{"py3"}
// backends := table.lang // todo: enabling py2 testing requires separate "want" output
for _, be := range backends {
fmt.Printf("looping over backends: %s in %s\n", be, backends)
vm, ok := testBackends[be]
if !ok || vm == "" {
// backend not available.
t.Logf("Skipped testing backend %s for %s\n", be, table.path)
continue
}
switch be {
case "py2":
t.Run(be, func(t *testing.T) {
// t.Parallel()
testPkgBackend(t, vm, table)
})
case "py3":
t.Run(be, func(t *testing.T) {
// t.Parallel()
testPkgBackend(t, vm, table)
})
default:
t.Errorf("invalid backend name %q", be)
}
}
}
func writeGoMod(t *testing.T, pkgDir, tstDir string) {
template := `
module dummy
require github.com/go-python/gopy v0.0.0
replace github.com/go-python/gopy => "%s"
`
contents := fmt.Sprintf(template, pkgDir)
if err := ioutil.WriteFile(filepath.Join(tstDir, "go.mod"), []byte(contents), 0666); err != nil {
t.Fatalf("failed to write go.mod file: %v", err)
}
}
func testPkgBackend(t *testing.T, pyvm string, table pkg) {
curPkgPath := reflect.TypeOf(table).PkgPath()
_, pkgNm := filepath.Split(table.path)
cwd, _ := os.Getwd()
workdir, err := ioutil.TempDir("", "gopy-")
if err != nil {
t.Fatalf("[%s:%s]: could not create workdir: %v\n", pyvm, table.path, err)
}
fmt.Printf("pyvm: %s making work dir: %s\n", pyvm, workdir)
err = os.MkdirAll(workdir, 0644)
if err != nil {
t.Fatalf("[%s:%s]: could not create workdir: %v\n", pyvm, table.path, err)
}
defer os.RemoveAll(workdir)
defer bind.ResetPackages()
writeGoMod(t, cwd, workdir)
// fmt.Printf("building in work dir: %s\n", workdir)
fpath := "./" + table.path
if table.cmd != "build" { // non-build cases end up inside the working dir -- need a global import path
fpath = filepath.Join(curPkgPath, table.path)
}
args := []string{table.cmd, "-vm=" + pyvm, "-output=" + workdir, fpath}
if table.extras != nil {
args = append(args, table.extras...)
}
fmt.Printf("run cmd: %s\n", args)
err = run(args)
if err != nil {
t.Fatalf("[%s:%s]: error running gopy-build: %v\n", pyvm, table.path, err)
}
// fmt.Printf("copying test.py\n")
tstDir := workdir
if table.cmd != "build" {
tstDir = filepath.Join(workdir, pkgNm)
}
tstSrc := filepath.Join(filepath.Join(cwd, table.path), "test.py")
tstDst := filepath.Join(tstDir, "test.py")
err = copyCmd(tstSrc, tstDst)
if err != nil {
t.Fatalf("[%s:%s]: error copying 'test.py' from: %s to: %s: %v\n", pyvm, table.path, tstSrc, tstDst, err)
}
fmt.Printf("running %s test.py\n", pyvm)
cmd := exec.Command(pyvm, "./test.py")
cmd.Env = testEnvironment
cmd.Dir = tstDir
cmd.Stdin = os.Stdin
buf, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf(
"[%s:%s]: error running python module: err=%v\n%v\n",
pyvm, table.path,
err,
string(buf),
)
}
var (
got = strings.Replace(string(buf), "\r\n", "\n", -1)
want = strings.Replace(string(table.want), "\r\n", "\n", -1)
)
if !reflect.DeepEqual(got, want) {
diffTxt := ""
diffBin, diffErr := exec.LookPath("diff")
if diffErr == nil {
wantFile, wantErr := os.Create(filepath.Join(workdir, "want.txt"))
if wantErr == nil {
wantFile.Write([]byte(want))
wantFile.Close()
}
gotFile, gotErr := os.Create(filepath.Join(workdir, "got.txt"))
if gotErr == nil {
gotFile.Write([]byte(got))
gotFile.Close()
}
if gotErr == nil && wantErr == nil {
cmd = exec.Command(diffBin, "-urN",
wantFile.Name(),
gotFile.Name(),
)
diff, _ := cmd.CombinedOutput()
diffTxt = string(diff) + "\n"
}
}
t.Fatalf("[%s:%s]: error running python module:\ngot:\n%s\n\nwant:\n%s\n[%s:%s] diff:\n%s",
pyvm, table.path,
got, want,
pyvm, table.path,
diffTxt,
)
}
}