github.com/lheiskan/zebrapack@v4.1.1-0.20181107023619-e955d028f9bf+incompatible/cmd/addzid/old_bambam_tests/slice_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	cv "github.com/glycerine/goconvey/convey"
     7  )
     8  
     9  func TestSliceToList(t *testing.T) {
    10  
    11  	cv.Convey("Given a parsable golang source file with struct containing a slice", t, func() {
    12  		cv.Convey("then the slice should be converted to a List() in the capnp output", func() {
    13  
    14  			ex0 := `
    15  type s1 struct {
    16    MyInts []int
    17  }`
    18  			cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct S1Capn { myInts  @0:   List(Int64); } `)
    19  
    20  		})
    21  	})
    22  }
    23  
    24  func TestSliceOfStructToList(t *testing.T) {
    25  
    26  	cv.Convey("Given a parsable golang source file with struct containing a slice of struct bbb", t, func() {
    27  		cv.Convey("then the slice should be converted to a List(Bbb) in the capnp output", func() {
    28  
    29  			ex0 := `
    30  type bbb struct {}
    31  type s1 struct {
    32    MyBees []bbb
    33  }`
    34  			out0 := ExtractString2String(ex0)
    35  
    36  			VPrintf("out0: '%s'\n", out0)
    37  
    38  			cv.So(out0, ShouldStartWithModuloWhiteSpace, `struct BbbCapn { } struct S1Capn { myBees  @0:   List(BbbCapn); } `)
    39  
    40  		})
    41  	})
    42  }
    43  
    44  func TestSliceOfPointerToList(t *testing.T) {
    45  
    46  	cv.Convey("Given a parsable golang source file with struct containing a slice of pointers to struct big", t, func() {
    47  		cv.Convey("then the slice should be converted to a List(Big) in the capnp output", func() {
    48  
    49  			ex0 := `
    50  type big struct {}
    51  type s1 struct {
    52    MyBigs []*big
    53  }`
    54  			cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct BigCapn { } struct S1Capn { myBigs  @0:   List(BigCapn); } `)
    55  
    56  		})
    57  	})
    58  }
    59  
    60  func TestSliceOfByteBecomesData(t *testing.T) {
    61  
    62  	cv.Convey("Given golang src with []byte", t, func() {
    63  		cv.Convey("then the slice should be converted to Data, not List(Byte), in the capnp output", func() {
    64  
    65  			ex0 := `
    66  type s1 struct {
    67    MyData []byte
    68  }`
    69  			cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct S1Capn { myData  @0:   List(UInt8); } `)
    70  
    71  		})
    72  	})
    73  }
    74  
    75  func TestStructWithSliceOfOtherStructs(t *testing.T) {
    76  
    77  	cv.Convey("Given a go struct containing MyBigs []Big, where Big is another struct", t, func() {
    78  		cv.Convey("then then the CapnToGo() translation code should call the CapnToGo translation function over each slice member during translation", func() {
    79  
    80  			in0 := `
    81  type Big struct {}
    82  type s1 struct {
    83    MyBigs []Big
    84  }`
    85  
    86  			expect0 := `
    87  struct BigCapn { }
    88  struct S1Capn { myBigs  @0:   List(BigCapn); } 
    89  
    90  
    91      func (s *Big) Save(w io.Writer) error {
    92      	seg := capn.NewBuffer(nil)
    93      	BigGoToCapn(seg, s)
    94      	_, err := seg.WriteTo(w)
    95          return err
    96      }
    97     
    98    
    99     
   100      func (s *Big) Load(r io.Reader) error {
   101      	capMsg, err := capn.ReadFromStream(r, nil)
   102      	if err != nil {
   103      		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
   104              return err
   105      	}
   106      	z := ReadRootBigCapn(capMsg)
   107          BigCapnToGo(z, s)
   108          return nil
   109      }
   110  
   111  func BigCapnToGo(src BigCapn, dest *Big) *Big { 
   112      if dest == nil { 
   113        dest = &Big{} 
   114      }
   115      return dest
   116  }
   117  func BigGoToCapn(seg *capn.Segment, src *Big) BigCapn { 
   118      dest := AutoNewBigCapn(seg)
   119      return dest
   120  }   
   121  
   122    
   123      func (s *s1) Save(w io.Writer) error {
   124      	seg := capn.NewBuffer(nil)
   125      	s1GoToCapn(seg, s)
   126      	_, err := seg.WriteTo(w)
   127          return err
   128      }
   129     
   130    
   131     
   132      func (s *s1) Load(r io.Reader) error {
   133      	capMsg, err := capn.ReadFromStream(r, nil)
   134      	if err != nil {
   135      		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
   136              return err
   137      	}
   138      	z := ReadRootS1Capn(capMsg)
   139          S1CapnToGo(z, s)
   140          return nil
   141      }
   142  
   143  func S1CapnToGo(src S1Capn, dest *s1) *s1 {
   144  	if dest == nil {
   145  		dest = &s1{}
   146  	}
   147      var n int
   148  
   149      // MyBigs
   150  	n = src.MyBigs().Len()
   151  	dest.MyBigs = make([]Big, n)
   152  	for i := 0; i < n; i++ {
   153          dest.MyBigs[i] = *BigCapnToGo(src.MyBigs().At(i), nil)
   154      }
   155  
   156  `
   157  			cv.So(ExtractString2String(in0), ShouldStartWithModuloWhiteSpace, expect0)
   158  
   159  		})
   160  	})
   161  }
   162  
   163  // ==========================================
   164  // ==========================================
   165  
   166  func Test008SliceOfSliceOfStruct(t *testing.T) {
   167  
   168  	cv.Convey("Given a go struct a slice of slice of int: type Cooper struct { Formation [][]Mini } ", t, func() {
   169  		cv.Convey("then then List(List(Mini)) should be generated in the capnp schema", func() {
   170  
   171  			in0 := `
   172  type Mini struct {
   173    A int64
   174  }
   175  type Cooper struct {
   176    Downey      []Mini
   177    Formation [][]Mini
   178  }`
   179  
   180  			expect0 := `
   181  struct CooperCapn { 
   182    downey     @0:   List(MiniCapn);
   183    formation  @1:   List(List(MiniCapn));
   184  } 
   185  
   186  struct MiniCapn { 
   187    a  @0:   Int64; 
   188  } 
   189    
   190    
   191    func (s *Cooper) Save(w io.Writer) error {
   192      	seg := capn.NewBuffer(nil)
   193      	CooperGoToCapn(seg, s)
   194      	_, err := seg.WriteTo(w)
   195          return err
   196    }
   197     
   198    
   199     
   200    func (s *Cooper) Load(r io.Reader) error {
   201      	capMsg, err := capn.ReadFromStream(r, nil)
   202      	if err != nil {
   203      		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
   204              return err
   205      	}
   206      	z := ReadRootCooperCapn(capMsg)
   207          CooperCapnToGo(z, s)
   208          return nil
   209    }
   210    
   211    
   212    
   213    func CooperCapnToGo(src CooperCapn, dest *Cooper) *Cooper { 
   214      if dest == nil { 
   215        dest = &Cooper{} 
   216      }
   217    
   218      var n int
   219    
   220        // Downey
   221    	n = src.Downey().Len()
   222    	dest.Downey = make([]Mini, n)
   223    	for i := 0; i < n; i++ {
   224            dest.Downey[i] = *MiniCapnToGo(src.Downey().At(i), nil)
   225        }
   226    
   227    
   228        // Formation
   229    	n = src.Formation().Len()
   230    	dest.Formation = make([][]Mini, n)
   231    	for i := 0; i < n; i++ {
   232            dest.Formation[i] = MiniCapnListToSliceMini(MiniCapn_List(src.Formation().At(i)))
   233        }
   234    
   235      return dest
   236    } 
   237    
   238    
   239    
   240    func CooperGoToCapn(seg *capn.Segment, src *Cooper) CooperCapn { 
   241      dest := AutoNewCooperCapn(seg)
   242    
   243      // Downey -> MiniCapn (go slice to capn list)
   244      if len(src.Downey) > 0 {
   245    		typedList := NewMiniCapnList(seg, len(src.Downey))
   246    		plist := capn.PointerList(typedList)
   247    		i := 0
   248    		for _, ele := range src.Downey {
   249    			plist.Set(i, capn.Object(MiniGoToCapn(seg, &ele)))
   250    			i++
   251    		}
   252    		dest.SetDowney(typedList)
   253    	}
   254    
   255      // Formation -> MiniCapn (go slice to capn list)
   256      if len(src.Formation) > 0 {
   257    		plist := seg.NewPointerList(len(src.Formation))
   258    		i := 0
   259    		for _, ele := range src.Formation {
   260    			plist.Set(i, capn.Object(SliceMiniToMiniCapnList(seg, ele)))
   261    			i++
   262    		}
   263    		dest.SetFormation(plist)
   264    	}
   265    
   266      return dest
   267    } 
   268    
   269    
   270    
   271    func (s *Mini) Save(w io.Writer) error {
   272      	seg := capn.NewBuffer(nil)
   273      	MiniGoToCapn(seg, s)
   274      	_, err := seg.WriteTo(w)
   275          return err
   276    }
   277     
   278    
   279     
   280    func (s *Mini) Load(r io.Reader) error {
   281      	capMsg, err := capn.ReadFromStream(r, nil)
   282      	if err != nil {
   283      		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
   284              return err
   285      	}
   286      	z := ReadRootMiniCapn(capMsg)
   287          MiniCapnToGo(z, s)
   288          return nil
   289    }
   290    
   291    
   292    
   293    func MiniCapnToGo(src MiniCapn, dest *Mini) *Mini { 
   294      if dest == nil { 
   295        dest = &Mini{} 
   296      }
   297      dest.A = src.A()
   298    
   299      return dest
   300    } 
   301    
   302    
   303    
   304    func MiniGoToCapn(seg *capn.Segment, src *Mini) MiniCapn { 
   305      dest := AutoNewMiniCapn(seg)
   306      dest.SetA(src.A)
   307    
   308      return dest
   309    } 
   310  
   311    
   312    func SliceMiniToMiniCapnList(seg *capn.Segment, m []Mini) MiniCapn_List {
   313    	lst := NewMiniCapnList(seg, len(m))
   314    	for i := range m {
   315    		lst.Set(i, MiniGoToCapn(seg, &m[i]))
   316    	}
   317    	return lst
   318    }
   319    
   320    
   321    
   322    func MiniCapnListToSliceMini(p MiniCapn_List) []Mini {
   323    	v := make([]Mini, p.Len())
   324    	for i := range v {
   325         MiniCapnToGo(p.At(i), &v[i])
   326    	}
   327    	return v
   328    } 
   329  
   330  `
   331  			cv.So(ExtractString2String(in0), ShouldMatchModuloWhiteSpace, expect0)
   332  
   333  		})
   334  	})
   335  }
   336  
   337  // ==========================================
   338  // ==========================================
   339  
   340  func Test009SliceOfSliceOfInt(t *testing.T) {
   341  
   342  	cv.Convey("Given a go struct a slice of slice: type Cooper struct { A [][]int } ", t, func() {
   343  		cv.Convey("then then List(List(Int64)) should be generated in the capnp schema", func() {
   344  
   345  			in0 := `
   346  type Cooper struct {
   347    A [][]int
   348  }`
   349  
   350  			expect0 := `
   351  struct CooperCapn { 
   352     a  @0:   List(List(Int64)); 
   353  } 
   354  
   355  func (s *Cooper) Save(w io.Writer) error {
   356  	seg := capn.NewBuffer(nil)
   357  	CooperGoToCapn(seg, s)
   358  	_, err := seg.WriteTo(w)
   359      return err
   360  }
   361  
   362  func (s *Cooper) Load(r io.Reader) error {
   363  	capMsg, err := capn.ReadFromStream(r, nil)
   364  	if err != nil {
   365  		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
   366          return err
   367  	}
   368  	z := ReadRootCooperCapn(capMsg)
   369  	CooperCapnToGo(z, s)
   370      return nil
   371  }
   372  
   373  func CooperCapnToGo(src CooperCapn, dest *Cooper) *Cooper {
   374  	if dest == nil {
   375  		dest = &Cooper{}
   376  	}
   377  
   378  	var n int
   379  
   380  	// A
   381  	n = src.A().Len()
   382  	dest.A = make([][]int, n)
   383  	for i := 0; i < n; i++ {
   384          dest.A[i] = Int64ListToSliceInt(capn.Int64List(src.A().At(i)))
   385  	}
   386  
   387  	return dest
   388  }
   389  
   390  func CooperGoToCapn(seg *capn.Segment, src *Cooper) CooperCapn {
   391  	dest := AutoNewCooperCapn(seg)
   392  
   393  	mylist1 := seg.NewPointerList(len(src.A))
   394  	for i := range src.A {
   395  		mylist1.Set(i, capn.Object(SliceIntToInt64List(seg, src.A[i])))
   396  	}
   397  	dest.SetA(mylist1)
   398  
   399  	return dest
   400  }
   401  
   402  func SliceIntToInt64List(seg *capn.Segment, m []int) capn.Int64List {
   403  	lst := seg.NewInt64List(len(m))
   404  	for i := range m {
   405  		lst.Set(i, int64(m[i]))
   406  	}
   407  	return lst
   408  }
   409  
   410  func Int64ListToSliceInt(p capn.Int64List) []int {
   411  	v := make([]int, p.Len())
   412  	for i := range v {
   413  		v[i] = int(p.At(i))
   414  	}
   415  	return v
   416  }
   417  `
   418  
   419  			cv.So(ExtractString2String(in0), ShouldStartWithModuloWhiteSpace, expect0)
   420  
   421  		})
   422  	})
   423  }