github.com/annwntech/go-micro/v2@v2.9.5/agent/command/command_test.go (about)

     1  package command
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestCommand(t *testing.T) {
     8  	c := &cmd{
     9  		name:        "test",
    10  		usage:       "test usage",
    11  		description: "test description",
    12  		exec: func(args ...string) ([]byte, error) {
    13  			return []byte("test"), nil
    14  		},
    15  	}
    16  
    17  	if c.String() != c.name {
    18  		t.Fatalf("expected name %s got %s", c.name, c.String())
    19  	}
    20  
    21  	if c.Usage() != c.usage {
    22  		t.Fatalf("expected usage %s got %s", c.usage, c.Usage())
    23  	}
    24  
    25  	if c.Description() != c.description {
    26  		t.Fatalf("expected description %s got %s", c.description, c.Description())
    27  	}
    28  
    29  	if r, err := c.Exec(); err != nil {
    30  		t.Fatal(err)
    31  	} else if string(r) != "test" {
    32  		t.Fatalf("expected exec result test got %s", string(r))
    33  	}
    34  }
    35  
    36  func TestNewCommand(t *testing.T) {
    37  	c := &cmd{
    38  		name:        "test",
    39  		usage:       "test usage",
    40  		description: "test description",
    41  		exec: func(args ...string) ([]byte, error) {
    42  			return []byte("test"), nil
    43  		},
    44  	}
    45  
    46  	nc := NewCommand(c.name, c.usage, c.description, c.exec)
    47  
    48  	if nc.String() != c.name {
    49  		t.Fatalf("expected name %s got %s", c.name, nc.String())
    50  	}
    51  
    52  	if nc.Usage() != c.usage {
    53  		t.Fatalf("expected usage %s got %s", c.usage, nc.Usage())
    54  	}
    55  
    56  	if nc.Description() != c.description {
    57  		t.Fatalf("expected description %s got %s", c.description, nc.Description())
    58  	}
    59  
    60  	if r, err := nc.Exec(); err != nil {
    61  		t.Fatal(err)
    62  	} else if string(r) != "test" {
    63  		t.Fatalf("expected exec result test got %s", string(r))
    64  	}
    65  }