Пример #1
0
func (*prompterSuite) TestPrompter(c *gc.C) {
	noPrompt := func(p string) (string, error) {
		c.Fatalf("unpexected prompt (text %q)", p)
		panic("unreachable")
	}
	promptFn := noPrompt
	p := cmdtesting.NewPrompter(func(p string) (string, error) {
		return promptFn(p)
	})

	promptText := "hello: "
	promptReply := "reply\n"

	fmt.Fprint(p, promptText)
	promptFn = func(p string) (string, error) {
		c.Assert(p, gc.Equals, promptText)
		return promptReply, nil
	}
	c.Assert(readStr(c, p, 20), gc.Equals, promptReply)

	promptText = "some text\ngoodbye: "
	promptReply = "again\n"
	fmt.Fprint(p, promptText[0:10])
	fmt.Fprint(p, promptText[10:])

	c.Assert(readStr(c, p, 3), gc.Equals, promptReply[0:3])
	c.Assert(readStr(c, p, 20), gc.Equals, promptReply[3:])

	fmt.Fprint(p, "final text\n")

	c.Assert(p.Tail(), gc.Equals, "final text\n")
	c.Assert(p.HasUnread(), gc.Equals, false)
}
Пример #2
0
func (*prompterSuite) TestUnreadInput(c *gc.C) {
	p := cmdtesting.NewPrompter(func(s string) (string, error) {
		return "hello world", nil
	})
	c.Assert(readStr(c, p, 3), gc.Equals, "hel")

	c.Assert(p.HasUnread(), gc.Equals, true)
}
Пример #3
0
func (*prompterSuite) TestError(c *gc.C) {
	expectErr := errors.New("something")
	p := cmdtesting.NewPrompter(func(s string) (string, error) {
		return "", expectErr
	})
	buf := make([]byte, 3)
	n, err := p.Read(buf)
	c.Assert(n, gc.Equals, 0)
	c.Assert(err, gc.Equals, expectErr)
}