github.com/annchain/OG@v0.0.9/tests/msg_marshal/msg_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package msg_marshal
    15  
    16  import (
    17  	"encoding/hex"
    18  	"fmt"
    19  	"github.com/sirupsen/logrus"
    20  	"testing"
    21  )
    22  
    23  func TestPerson_MarshalMsg(t *testing.T) {
    24  
    25  	p := Person{
    26  		Name: "alice",
    27  		Age:  10,
    28  		Type: 1,
    29  	}
    30  	p2 := Person{
    31  		Name: "bob",
    32  		Age:  1522,
    33  		Type: 2,
    34  	}
    35  	s1 := Student{p, 15}
    36  	t1 := Teacher{p2, true}
    37  	var fooi FooI
    38  	fooi = &s1
    39  	data, _ := MashalFoo(fooi, nil)
    40  	fmt.Println(hex.EncodeToString(data))
    41  	_, s2, err := UnmarShalFoo(data)
    42  	fmt.Println(s2, err)
    43  	if s1 != *s2.(*Student) {
    44  		t.Fail()
    45  	}
    46  	_ = t1
    47  }
    48  
    49  type fooInt struct {
    50  	a    int
    51  	name string
    52  }
    53  
    54  func (f fooInt) String() string {
    55  	fmt.Println("f1", f.a, f.name)
    56  	return fmt.Sprintf("a %d name %s", f.a, f.name)
    57  
    58  }
    59  
    60  type fooHash struct {
    61  	b   int
    62  	c   float64
    63  	foo *fooInt
    64  }
    65  
    66  func (f fooHash) String() string {
    67  	fmt.Println("f2", f.b, f.foo)
    68  	return fmt.Sprintf("f1 %s, b %d, c %f", f.foo, f.b, f.c)
    69  
    70  }
    71  
    72  //TestLogrus
    73  func TestLogrus(t *testing.T) {
    74  	var f1 fooInt
    75  	f1.a = 100
    76  	f1.name = " alice"
    77  	f2 := fooHash{
    78  		foo: &fooInt{
    79  			a:    56,
    80  			name: "bob",
    81  		},
    82  		b: 95,
    83  		c: 4.56,
    84  	}
    85  	logrus.SetLevel(logrus.WarnLevel)
    86  	logrus.Info(f1)
    87  	logrus.Info(f2.String())
    88  }