go-hep.org/x/hep@v0.38.1/fwk/job/gocodec_test.go (about)

     1  // Copyright ©2017 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package job
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"go-hep.org/x/hep/fwk"
    13  	"go-hep.org/x/hep/fwk/internal/fwktest"
    14  )
    15  
    16  func TestGoEncode(t *testing.T) {
    17  	appcfg := C{
    18  		Name: "app",
    19  		Type: "go-hep.org/x/hep/fwk.appmgr",
    20  		Props: P{
    21  			"EvtMax": int64(10),
    22  			"NProcs": 42,
    23  		},
    24  	}
    25  
    26  	cfg0 := C{
    27  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.task1",
    28  		Name: "t0",
    29  		Props: P{
    30  			"Ints1": "t0-ints1",
    31  			"Ints2": "t0-ints2",
    32  		},
    33  	}
    34  
    35  	cfg1 := C{
    36  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.task1",
    37  		Name: "t1",
    38  		Props: P{
    39  			"Ints1": "t1-ints1",
    40  			"Ints2": "t1-ints2",
    41  		},
    42  	}
    43  
    44  	cfg2 := C{
    45  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.svc1",
    46  		Name: "svc1",
    47  		Props: P{
    48  			"Int":    fwktest.MyInt(12),
    49  			"Struct": fwktest.MyStruct{I: 12},
    50  		},
    51  	}
    52  
    53  	job := NewJob(
    54  		fwk.NewApp(),
    55  		appcfg.Props,
    56  	)
    57  
    58  	if job == nil {
    59  		t.Fatalf("got nil job.Job")
    60  	}
    61  
    62  	job.Create(cfg0)
    63  
    64  	comp1 := job.Create(cfg1)
    65  	prop11 := P{
    66  		"Ints1": "t1-ints1-modified",
    67  	}
    68  	job.SetProp(comp1, "Ints1", prop11["Ints1"])
    69  
    70  	job.Create(cfg2)
    71  
    72  	exp := []Stmt{
    73  		{
    74  			Type: StmtNewApp,
    75  			Data: appcfg,
    76  		},
    77  		{
    78  			Type: StmtCreate,
    79  			Data: cfg0,
    80  		},
    81  		{
    82  			Type: StmtCreate,
    83  			Data: cfg1,
    84  		},
    85  		{
    86  			Type: StmtSetProp,
    87  			Data: C{
    88  				Type:  comp1.Type(),
    89  				Name:  comp1.Name(),
    90  				Props: prop11,
    91  			},
    92  		},
    93  		{
    94  			Type: StmtCreate,
    95  			Data: cfg2,
    96  		},
    97  	}
    98  
    99  	stmts := job.Stmts()
   100  
   101  	if !reflect.DeepEqual(exp, stmts) {
   102  		t.Fatalf("unexpected statments:\nexp=%#v\ngot=%#v\n", exp, stmts)
   103  	}
   104  
   105  	buf := new(bytes.Buffer)
   106  	enc := NewGoEncoder(buf)
   107  	err := enc.Encode(stmts)
   108  	if err != nil {
   109  		t.Fatalf("error go-encoding: %v\n", err)
   110  	}
   111  }