當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.Command方法代碼示例

本文整理匯總了Golang中github.com/codegangsta/cli.Context.Command方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Command方法的具體用法?Golang Context.Command怎麽用?Golang Context.Command使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/codegangsta/cli.Context的用法示例。


在下文中一共展示了Context.Command方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: testTileAction

func testTileAction(actionName string) {
	var (
		controlErrorHandler = new(ErrorHandler)
	)

	Context(fmt.Sprintf("when called with the %s command name", actionName), func() {
		controlName := actionName
		controlUsage := "something about the usage here"

		It("Then it should return a cli.Command with required values", func() {
			cmd := CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
			Ω(cmd.Name).Should(Equal(controlName))
			Ω(cmd.Usage).Should(Equal(controlUsage))
			Ω(cmd.Action).ShouldNot(BeNil())
		})

		Describe(fmt.Sprintf("given a %s command with an Action value", actionName), func() {
			var (
				controlExit       = 0
				controlCliContext *cli.Context
				controlCmd        cli.Command
			)
			BeforeEach(func() {
				controlErrorHandler = new(ErrorHandler)
				controlErrorHandler.ExitCode = controlExit
			})

			Context("when the action is called with all proper flags on a registered tile", func() {
				var (
					controlTileName      = "fake-tile"
					controlTileGenerator *fake.TileGenerator
					controlTile          *fake.Tile
				)
				BeforeEach(func() {
					controlTile = new(fake.Tile)
					controlTileGenerator = new(fake.TileGenerator)
					controlTileGenerator.TileSpy = controlTile
					tileregistry.Register(controlTileName, controlTileGenerator)
					set := flag.NewFlagSet("", 0)
					set.String("tile", controlTileName, "")
					set.String("opsmanagerhost", "*****", "")
					set.String("adminuser", "*****", "")
					set.String("adminpass", "*****", "")
					set.String("opsmanageruser", "*****", "")
					set.String("opsmanagerpass", "*****", "")
					set.String("destination", "*****", "")
					controlCliContext = cli.NewContext(cli.NewApp(), set, nil)
					controlCmd = CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
					controlCliContext.Command = controlCmd
				})
				It("then it should execute a call on the tiles Action func()", func() {
					controlCmd.Action(controlCliContext)
					Ω(controlErrorHandler.ExitCode).Should(Equal(controlExit))
					Ω(controlErrorHandler.Error).ShouldNot(HaveOccurred())
					switch controlName {
					case "backup":
						Ω(controlTile.BackupCallCount).ShouldNot(Equal(0))
					case "restore":
						Ω(controlTile.RestoreCallCount).ShouldNot(Equal(0))
					default:
						panic("this should never happen")
					}
				})
			})

			Context("when the action is called w/o a matching registered tile", func() {
				BeforeEach(func() {
					set := flag.NewFlagSet(controlName, 0)
					set.Bool("myflag", false, "doc")
					set.String("otherflag", "hello world", "doc")
					controlCliContext = cli.NewContext(cli.NewApp(), set, nil)
					controlCmd = CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
					controlCliContext.Command = controlCmd
				})

				It("then it should set an error and failure exit code", func() {
					controlCmd.Action(controlCliContext)
					Ω(controlErrorHandler.ExitCode).ShouldNot(Equal(controlExit))
					Ω(controlErrorHandler.Error).Should(HaveOccurred())
					Ω(controlErrorHandler.Error).Should(Equal(ErrInvalidTileSelection))
				})
			})

			Context("when the action is called w/o proper flags but with a registered tile", func() {
				var controlTileName = "fake-tile"
				BeforeEach(func() {
					tileregistry.Register(controlTileName, new(fake.TileGenerator))
					set := flag.NewFlagSet("", 0)
					set.String("tile", controlTileName, "doc")
					set.Bool("myflag", false, "doc")
					set.String("otherflag", "hello world", "doc")
					controlCliContext = cli.NewContext(cli.NewApp(), set, nil)
					controlCmd = CreateBURACliCommand(controlName, controlUsage, controlErrorHandler)
					controlCliContext.Command = controlCmd
				})
				It("then it should set an error and failure exit code", func() {
					controlCmd.Action(controlCliContext)
					Ω(controlErrorHandler.ExitCode).ShouldNot(Equal(controlExit))
					Ω(controlErrorHandler.Error).Should(HaveOccurred())
					Ω(controlErrorHandler.Error).Should(Equal(ErrInvalidFlagArgs))
//.........這裏部分代碼省略.........
開發者ID:gitter-badger,項目名稱:cfops,代碼行數:101,代碼來源:createCliCommand_test.go


注:本文中的github.com/codegangsta/cli.Context.Command方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。