github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/testing/prompt_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing_test 5 6 import ( 7 "fmt" 8 "io" 9 "strconv" 10 "strings" 11 12 "github.com/juju/errors" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 cmdtesting "github.com/juju/juju/cmd/testing" 17 "github.com/juju/testing" 18 ) 19 20 type prompterSuite struct { 21 testing.IsolationSuite 22 } 23 24 var _ = gc.Suite(&prompterSuite{}) 25 26 func (*prompterSuite) TestPrompter(c *gc.C) { 27 noPrompt := func(p string) (string, error) { 28 c.Fatalf("unpexected prompt (text %q)", p) 29 panic("unreachable") 30 } 31 promptFn := noPrompt 32 p := cmdtesting.NewPrompter(func(p string) (string, error) { 33 return promptFn(p) 34 }) 35 36 promptText := "hello: " 37 promptReply := "reply\n" 38 39 fmt.Fprint(p, promptText) 40 promptFn = func(p string) (string, error) { 41 c.Assert(p, gc.Equals, promptText) 42 return promptReply, nil 43 } 44 c.Assert(readStr(c, p, 20), gc.Equals, promptReply) 45 46 promptText = "some text\ngoodbye: " 47 promptReply = "again\n" 48 fmt.Fprint(p, promptText[0:10]) 49 fmt.Fprint(p, promptText[10:]) 50 51 c.Assert(readStr(c, p, 3), gc.Equals, promptReply[0:3]) 52 c.Assert(readStr(c, p, 20), gc.Equals, promptReply[3:]) 53 54 fmt.Fprint(p, "final text\n") 55 56 c.Assert(p.Tail(), gc.Equals, "final text\n") 57 c.Assert(p.HasUnread(), gc.Equals, false) 58 } 59 60 func (*prompterSuite) TestUnreadInput(c *gc.C) { 61 p := cmdtesting.NewPrompter(func(s string) (string, error) { 62 return "hello world", nil 63 }) 64 c.Assert(readStr(c, p, 3), gc.Equals, "hel") 65 66 c.Assert(p.HasUnread(), gc.Equals, true) 67 } 68 69 func (*prompterSuite) TestError(c *gc.C) { 70 expectErr := errors.New("something") 71 p := cmdtesting.NewPrompter(func(s string) (string, error) { 72 return "", expectErr 73 }) 74 buf := make([]byte, 3) 75 n, err := p.Read(buf) 76 c.Assert(n, gc.Equals, 0) 77 c.Assert(err, gc.Equals, expectErr) 78 } 79 80 func (*prompterSuite) TestSeqPrompter(c *gc.C) { 81 p := cmdtesting.NewSeqPrompter(c, "»", ` 82 hello: »reply 83 some text 84 goodbye: »again 85 final 86 `[1:]) 87 fmt.Fprint(p, "hello: ") 88 c.Assert(readStr(c, p, 1), gc.Equals, "r") 89 c.Assert(readStr(c, p, 20), gc.Equals, "eply\n") 90 fmt.Fprint(p, "some text\n") 91 fmt.Fprint(p, "goodbye: ") 92 c.Assert(readStr(c, p, 20), gc.Equals, "again\n") 93 fmt.Fprint(p, "final\n") 94 p.AssertDone() 95 } 96 97 func (*prompterSuite) TestSeqPrompterEOF(c *gc.C) { 98 p := cmdtesting.NewSeqPrompter(c, "»", ` 99 hello: »» 100 final 101 `[1:]) 102 fmt.Fprint(p, "hello: ") 103 n, err := p.Read(make([]byte, 10)) 104 c.Assert(n, gc.Equals, 0) 105 c.Assert(err, gc.Equals, io.EOF) 106 fmt.Fprint(p, "final\n") 107 p.AssertDone() 108 } 109 110 func (*prompterSuite) TestNewIOChecker(c *gc.C) { 111 checker := cmdtesting.NewSeqPrompter(c, "»", `What is your name: »Bob 112 »more 113 And your age: »148 114 You're .* old, Bob 115 more! 116 `) 117 fmt.Fprintf(checker, "What is your name: ") 118 buf := make([]byte, 100) 119 n, _ := checker.Read(buf) 120 name := strings.TrimSpace(string(buf[0:n])) 121 fmt.Fprintf(checker, "And your age: ") 122 n, _ = checker.Read(buf) 123 age, err := strconv.Atoi(strings.TrimSpace(string(buf[0:n]))) 124 c.Assert(err, gc.IsNil) 125 if age > 90 { 126 fmt.Fprintf(checker, "You're very old, %s!\n", name) 127 } 128 checker.CheckDone() 129 } 130 131 func readStr(c *gc.C, r io.Reader, nb int) string { 132 buf := make([]byte, nb) 133 n, err := r.Read(buf) 134 c.Assert(err, jc.ErrorIsNil) 135 return string(buf[0:n]) 136 }