github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/cmds/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/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{"-buildinfo", "-json"}, func(p Program) bool {
    18  		return p.(ShowBuildInfo).JSON
    19  	}},
    20  	{[]string{}, isShell},
    21  	{[]string{"-c", "echo"}, func(p Program) bool {
    22  		return p.(*shell.Shell).Cmd
    23  	}},
    24  	{[]string{"-compileonly"}, func(p Program) bool {
    25  		return p.(*shell.Shell).CompileOnly
    26  	}},
    27  
    28  	{[]string{"-bin", "/elvish"}, func(p Program) bool {
    29  		return p.(*shell.Shell).BinPath == "/elvish"
    30  	}},
    31  	{[]string{"-db", "/db"}, func(p Program) bool {
    32  		return p.(*shell.Shell).DbPath == "/db"
    33  	}},
    34  }
    35  
    36  func isShowHelp(p Program) bool         { _, ok := p.(ShowHelp); return ok }
    37  func isShowCorrectUsage(p Program) bool { _, ok := p.(ShowCorrectUsage); return ok }
    38  func isShowVersion(p Program) bool      { _, ok := p.(ShowVersion); return ok }
    39  func isShowBuildInfo(p Program) bool    { _, ok := p.(ShowBuildInfo); return ok }
    40  func isShell(p Program) bool            { _, ok := p.(*shell.Shell); return ok }
    41  
    42  func TestFindProgram(t *testing.T) {
    43  	for i, test := range findProgramTests {
    44  		f := parse(test.args)
    45  		p := FindProgram(f)
    46  		if !test.checker(p) {
    47  			t.Errorf("test #%d (args = %q) failed", i, test.args)
    48  		}
    49  	}
    50  }
    51  
    52  func parse(args []string) *flagSet {
    53  	f := newFlagSet()
    54  	err := f.Parse(args)
    55  	if err != nil {
    56  		panic(fmt.Sprintln("bad flags in test", args))
    57  	}
    58  	return f
    59  }