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

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	cv "github.com/glycerine/goconvey/convey"
     7  )
     8  
     9  func Test005SliceOfStringToListOfText(t *testing.T) {
    10  
    11  	cv.Convey("Given a struct that contains a slice of string", t, func() {
    12  		cv.Convey("then the capnp schema should contain a list of string and list translating code should be generated", func() {
    13  
    14  			ex0 := `
    15  type s1 struct {
    16     Names []string
    17  }`
    18  
    19  			expected0 := `
    20    struct S1Capn { names  @0:   List(Text); } 
    21  
    22      func (s *s1) Save(w io.Writer) error {
    23      	seg := capn.NewBuffer(nil)
    24      	s1GoToCapn(seg, s)
    25      	_, err := seg.WriteTo(w)
    26          return err
    27      }
    28        
    29      func (s *s1) Load(r io.Reader) error {
    30      	capMsg, err := capn.ReadFromStream(r, nil)
    31      	if err != nil {
    32      		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
    33              return err
    34      	}
    35      	z := ReadRootS1Capn(capMsg)
    36          S1CapnToGo(z, s)
    37          return nil
    38      }
    39    
    40    func S1CapnToGo(src S1Capn, dest *s1) *s1 { 
    41      if dest == nil { 
    42        dest = &s1{} 
    43      }
    44  
    45      dest.Names = src.Names().ToArray()
    46    
    47      return dest
    48    } 
    49    
    50    func s1GoToCapn(seg *capn.Segment, src *s1) S1Capn { 
    51      dest := AutoNewS1Capn(seg)
    52    
    53      mylist1 := seg.NewTextList(len(src.Names))
    54      for i := range src.Names {
    55         mylist1.Set(i, string(src.Names[i]))
    56      }
    57      dest.SetNames(mylist1)
    58    
    59      return dest
    60    } 
    61  
    62    func SliceStringToTextList(seg *capn.Segment, m []string) capn.TextList {
    63    	lst := seg.NewTextList(len(m))
    64    	for i := range m {
    65    		lst.Set(i, string(m[i]))
    66    	}
    67    	return lst
    68    }
    69    
    70    
    71    
    72    func TextListToSliceString(p capn.TextList) []string {
    73    	v := make([]string, p.Len())
    74    	for i := range v {
    75    		v[i] = string(p.At(i))
    76    	}
    77    	return v
    78    } 
    79  
    80  `
    81  
    82  			cv.So(ExtractString2String(ex0), ShouldMatchModuloWhiteSpace, expected0)
    83  			//cv.So(expected0, ShouldStartWithModuloWhiteSpace, ExtractString2String(ex0))
    84  
    85  		})
    86  	})
    87  }