go-hep.org/x/hep@v0.38.1/fwk/job/jsoncodec_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  
    10  	"reflect"
    11  	"testing"
    12  
    13  	"go-hep.org/x/hep/fwk"
    14  	"go-hep.org/x/hep/fwk/internal/fwktest"
    15  )
    16  
    17  func TestJSONEncode(t *testing.T) {
    18  	appcfg := C{
    19  		Name: "app",
    20  		Type: "go-hep.org/x/hep/fwk.appmgr",
    21  		Props: P{
    22  			"EvtMax": int64(10),
    23  			"NProcs": 42,
    24  		},
    25  	}
    26  
    27  	cfg0 := C{
    28  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.task1",
    29  		Name: "t0",
    30  		Props: P{
    31  			"Ints1": "t0-ints1",
    32  			"Ints2": "t0-ints2",
    33  		},
    34  	}
    35  
    36  	cfg1 := C{
    37  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.task1",
    38  		Name: "t1",
    39  		Props: P{
    40  			"Ints1": "t1-ints1",
    41  			"Ints2": "t1-ints2",
    42  		},
    43  	}
    44  
    45  	cfg2 := C{
    46  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.svc1",
    47  		Name: "svc1",
    48  		Props: P{
    49  			"Int":    fwktest.MyInt(12),
    50  			"Struct": fwktest.MyStruct{I: 12},
    51  		},
    52  	}
    53  
    54  	job := NewJob(
    55  		fwk.NewApp(),
    56  		appcfg.Props,
    57  	)
    58  
    59  	if job == nil {
    60  		t.Fatalf("got nil job.Job")
    61  	}
    62  
    63  	job.Create(cfg0)
    64  
    65  	comp1 := job.Create(cfg1)
    66  	prop11 := P{
    67  		"Ints1": "t1-ints1-modified",
    68  	}
    69  	job.SetProp(comp1, "Ints1", prop11["Ints1"])
    70  
    71  	job.Create(cfg2)
    72  
    73  	exp := []Stmt{
    74  		{
    75  			Type: StmtNewApp,
    76  			Data: appcfg,
    77  		},
    78  		{
    79  			Type: StmtCreate,
    80  			Data: cfg0,
    81  		},
    82  		{
    83  			Type: StmtCreate,
    84  			Data: cfg1,
    85  		},
    86  		{
    87  			Type: StmtSetProp,
    88  			Data: C{
    89  				Type:  comp1.Type(),
    90  				Name:  comp1.Name(),
    91  				Props: prop11,
    92  			},
    93  		},
    94  		{
    95  			Type: StmtCreate,
    96  			Data: cfg2,
    97  		},
    98  	}
    99  
   100  	stmts := job.Stmts()
   101  
   102  	if !reflect.DeepEqual(exp, stmts) {
   103  		t.Fatalf("unexpected statments:\nexp=%#v\ngot=%#v\n", exp, stmts)
   104  	}
   105  
   106  	buf := new(bytes.Buffer)
   107  	enc := NewJSONEncoder(buf)
   108  	err := enc.Encode(stmts)
   109  	if err != nil {
   110  		t.Fatalf("error json-encoding: %v\n", err)
   111  	}
   112  
   113  	dec := NewJSONDecoder(buf)
   114  	stmts = make([]Stmt, 0)
   115  	err = dec.Decode(&stmts)
   116  	if err != nil {
   117  		t.Fatalf("error json-decoding: %v\n", err)
   118  	}
   119  
   120  	// FIXME(sbinet)
   121  	//  issue is that JSON won't deserialize 'MyStruct' into fwktest.MyStruct...
   122  	//  same for 'MyInt' and fwktest.MyInt
   123  	//
   124  	// if !reflect.DeepEqual(exp, stmts) {
   125  	// 	t.Fatalf("unexpected statments:\nexp=%#v\ngot=%#v\n", exp, stmts)
   126  	// }
   127  }