github.com/diamondburned/arikawa/v2@v2.1.0/bot/subcommand_test.go (about)

     1  package bot
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestUnderline(t *testing.T) {
     9  	HelpUnderline = false
    10  	if underline("astolfo") != "astolfo" {
    11  		t.Fatal("Unexpected underlining with HelpUnderline = false")
    12  	}
    13  
    14  	HelpUnderline = true
    15  	if underline("arikawa hime") != "__arikawa hime__" {
    16  		t.Fatal("Unexpected normal style with HelpUnderline = true")
    17  	}
    18  }
    19  
    20  func TestNewSubcommand(t *testing.T) {
    21  	_, err := NewSubcommand(&testc{})
    22  	if err != nil {
    23  		t.Fatal("Failed to create new subcommand:", err)
    24  	}
    25  }
    26  
    27  func TestSubcommand(t *testing.T) {
    28  	var given = &testc{}
    29  	var sub = &Subcommand{
    30  		command: given,
    31  	}
    32  
    33  	t.Run("reflect commands", func(t *testing.T) {
    34  		if err := sub.reflectCommands(); err != nil {
    35  			t.Fatal("Failed to reflect commands:", err)
    36  		}
    37  	})
    38  
    39  	t.Run("parse commands", func(t *testing.T) {
    40  		if err := sub.parseCommands(); err != nil {
    41  			t.Fatal("Failed to parse commands:", err)
    42  		}
    43  
    44  		// !!! CHANGE ME
    45  		if len(sub.Commands) < 8 {
    46  			t.Fatal("too low sub.Methods len", len(sub.Commands))
    47  		}
    48  		if len(sub.Events) < 1 {
    49  			t.Fatal("No events found.")
    50  		}
    51  
    52  		var (
    53  			foundSend   bool
    54  			foundCustom bool
    55  			foundNoArgs bool
    56  		)
    57  
    58  		for _, this := range sub.Commands {
    59  			switch this.Command {
    60  			case "send":
    61  				foundSend = true
    62  				if len(this.Arguments) != 1 {
    63  					t.Fatal("invalid arguments len", len(this.Arguments))
    64  				}
    65  
    66  			case "custom":
    67  				foundCustom = true
    68  				if len(this.Arguments) != 1 {
    69  					t.Fatal("arguments should be 1 for custom")
    70  				}
    71  
    72  			case "noArgs":
    73  				foundNoArgs = true
    74  				if len(this.Arguments) != 0 {
    75  					t.Fatal("expected 0 arguments, got non-zero")
    76  				}
    77  			}
    78  		}
    79  
    80  		if !foundSend {
    81  			t.Fatal("missing send")
    82  		}
    83  
    84  		if !foundCustom {
    85  			t.Fatal("missing custom")
    86  		}
    87  
    88  		if !foundNoArgs {
    89  			t.Fatal("missing noargs")
    90  		}
    91  	})
    92  
    93  	t.Run("init commands", func(t *testing.T) {
    94  		ctx := &Context{}
    95  		if err := sub.InitCommands(ctx); err != nil {
    96  			t.Fatal("Failed to init commands:", err)
    97  		}
    98  	})
    99  
   100  	t.Run("help commands", func(t *testing.T) {
   101  		h := sub.Help()
   102  		if h == "" {
   103  			t.Fatal("Empty subcommand help?")
   104  		}
   105  
   106  		if strings.Contains(h, "hidden") {
   107  			t.Fatal("Hidden command shown in help:\n", h)
   108  		}
   109  	})
   110  
   111  	t.Run("change command", func(t *testing.T) {
   112  		sub.ChangeCommandInfo("Noop", "crossdressing", "best")
   113  		if h := sub.Help(); !strings.Contains(h, "crossdressing: best") {
   114  			t.Fatal("Changed command is not in help.")
   115  		}
   116  	})
   117  }
   118  
   119  func BenchmarkSubcommandConstructor(b *testing.B) {
   120  	for i := 0; i < b.N; i++ {
   121  		NewSubcommand(&testc{})
   122  	}
   123  }