github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/program/program_test.go (about)

     1  package program
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/u-root/u-root/cmds/core/elvish/program/shell"
     8  )
     9  
    10  var findProgramTests = []struct {
    11  	args    []string
    12  	checker func(Program) bool
    13  }{
    14  	{[]string{"-help"}, isShowHelp},
    15  	{[]string{"-version"}, isShowVersion},
    16  	{[]string{"-buildinfo"}, isShowBuildInfo},
    17  	{[]string{}, isShell},
    18  	{[]string{"-c", "echo"}, func(p Program) bool {
    19  		return p.(*shell.Shell).Cmd
    20  	}},
    21  	{[]string{"-compileonly"}, func(p Program) bool {
    22  		return p.(*shell.Shell).CompileOnly
    23  	}},
    24  
    25  	{[]string{"-bin", "/elvish"}, func(p Program) bool {
    26  		return p.(*shell.Shell).BinPath == "/elvish"
    27  	}},
    28  	{[]string{"-db", "/db"}, func(p Program) bool {
    29  		return p.(*shell.Shell).DbPath == "/db"
    30  	}},
    31  }
    32  
    33  func isShowHelp(p Program) bool         { _, ok := p.(ShowHelp); return ok }
    34  func isShowCorrectUsage(p Program) bool { _, ok := p.(ShowCorrectUsage); return ok }
    35  func isShowVersion(p Program) bool      { _, ok := p.(ShowVersion); return ok }
    36  func isShowBuildInfo(p Program) bool    { _, ok := p.(ShowBuildInfo); return ok }
    37  func isShell(p Program) bool            { _, ok := p.(*shell.Shell); return ok }
    38  
    39  func TestFindProgram(t *testing.T) {
    40  	for i, test := range findProgramTests {
    41  		f := parse(test.args)
    42  		p := FindProgram(f)
    43  		if !test.checker(p) {
    44  			t.Errorf("test #%d (args = %q) failed", i, test.args)
    45  		}
    46  	}
    47  }
    48  
    49  func parse(args []string) *flagSet {
    50  	f := newFlagSet()
    51  	err := f.Parse(args)
    52  	if err != nil {
    53  		panic(fmt.Sprintln("bad flags in test", args))
    54  	}
    55  	return f
    56  }