github.com/tada-team/tdproto@v1.51.57/jid_test.go (about)

     1  package tdproto
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  func TestNewJID(t *testing.T) {
     9  	t.Run("marshall", func(t *testing.T) {
    10  		res := new(struct {
    11  			Val JID `json:"val,omitempty"`
    12  		})
    13  
    14  		b,  err := json.Marshal(res)
    15  		if err != nil {
    16  			t.Fatal(err)
    17  		}
    18  
    19  		if string(b) != "{}" {
    20  			t.Error("must be empty, got:", string(b))
    21  		}
    22  	})
    23  
    24  	t.Run("unmarshall", func(t *testing.T) {
    25  		res := new(struct {
    26  			Val JID `json:"val"`
    27  		})
    28  
    29  		data := `{"val": "t-63150419-c5c7-40fb-8aa2-ca6b613ca5a0"}`
    30  		if err := json.Unmarshal([]byte(data), res); err != nil {
    31  			t.Fatal(err)
    32  		}
    33  
    34  		if !res.Val.IsTask() {
    35  			t.Error("must be task")
    36  		}
    37  
    38  		if res.Val.Uid() != "63150419-c5c7-40fb-8aa2-ca6b613ca5a0" {
    39  			t.Error("invalid uid:", res.Val.Uid())
    40  		}
    41  	})
    42  }