github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/commands/ui/ui_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/commands/ui"
     5  	"code.cloudfoundry.org/cli/commands/ui/uifakes"
     6  	"code.cloudfoundry.org/cli/utils/config"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  )
    12  
    13  var _ = Describe("UI", func() {
    14  	var (
    15  		ui         UI
    16  		fakeConfig *uifakes.FakeConfig
    17  	)
    18  
    19  	// type TranslateFunc func(translationID string, args ...interface{}) string
    20  	BeforeEach(func() {
    21  		fakeConfig = new(uifakes.FakeConfig)
    22  		fakeConfig.ColorEnabledReturns(config.ColorEnabled)
    23  
    24  		var err error
    25  		ui, err = NewUI(fakeConfig)
    26  		Expect(err).NotTo(HaveOccurred())
    27  
    28  		ui.Out = NewBuffer()
    29  	})
    30  
    31  	Describe("DisplayText", func() {
    32  		Context("when only a string is passed in", func() {
    33  			It("displays the string to Out with a newline", func() {
    34  				ui.DisplayText("some-string")
    35  
    36  				Expect(ui.Out).To(Say("some-string\n"))
    37  			})
    38  		})
    39  
    40  		Context("when a map is passed in", func() {
    41  			It("merges the map content with the string", func() {
    42  				ui.DisplayText("some-string {{.SomeMapValue}}", map[string]interface{}{
    43  					"SomeMapValue": "my-map-value",
    44  				})
    45  
    46  				Expect(ui.Out).To(Say("some-string my-map-value\n"))
    47  			})
    48  		})
    49  
    50  		Context("when multiple maps are passed in", func() {
    51  			It("merges all map value with the string", func() {
    52  				ui.DisplayText("some-string {{.SomeMapValue}} {{.SomeOtherMapValue}}",
    53  					map[string]interface{}{
    54  						"SomeMapValue": "my-map-value",
    55  					},
    56  					map[string]interface{}{
    57  						"SomeOtherMapValue": "my-other-map-value",
    58  					},
    59  				)
    60  
    61  				Expect(ui.Out).To(Say("some-string my-map-value my-other-map-value\n"))
    62  			})
    63  
    64  			Context("when maps share the same key", func() {
    65  				It("keeps the rightmost map value", func() {
    66  					ui.DisplayText("some-string {{.SomeMapValue}}",
    67  						map[string]interface{}{
    68  							"SomeMapValue": "my-map-value",
    69  						},
    70  						map[string]interface{}{
    71  							"SomeMapValue": "my-other-map-value",
    72  						},
    73  					)
    74  
    75  					Expect(ui.Out).To(Say("some-string my-other-map-value\n"))
    76  				})
    77  			})
    78  
    79  			Context("when the local is not set to 'en-us'", func() {
    80  				BeforeEach(func() {
    81  					fakeConfig = new(uifakes.FakeConfig)
    82  					fakeConfig.ColorEnabledReturns(config.ColorEnabled)
    83  					fakeConfig.LocaleReturns("fr-FR")
    84  
    85  					var err error
    86  					ui, err = NewUI(fakeConfig)
    87  					Expect(err).NotTo(HaveOccurred())
    88  
    89  					ui.Out = NewBuffer()
    90  				})
    91  
    92  				It("translates the main string passed to DisplayText", func() {
    93  					ui.DisplayText("\nTIP: Use '{{.Command}}' to target new org",
    94  						map[string]interface{}{
    95  							"Command": "foo",
    96  						},
    97  					)
    98  
    99  					Expect(ui.Out).To(Say("\nASTUCE : utilisez 'foo' pour cibler une nouvelle organisation"))
   100  				})
   101  
   102  				It("translates the main string and keys passed to DisplayTextWithKeyTranslations", func() {
   103  					ui.DisplayTextWithKeyTranslations("   {{.CommandName}} - {{.CommandDescription}}",
   104  						[]string{"CommandDescription"},
   105  						map[string]interface{}{
   106  							"CommandName":        "ADVANCED", // In translation file, should not be translated
   107  							"CommandDescription": "A command line tool to interact with Cloud Foundry",
   108  						})
   109  
   110  					Expect(ui.Out).To(Say("   ADVANCED - Outil de ligne de commande permettant d'interagir avec Cloud Foundry"))
   111  				})
   112  			})
   113  		})
   114  	})
   115  
   116  	Describe("DisplayTextWithKeyTranslations", func() {
   117  		Context("when the local is not set to 'en-us'", func() {
   118  			BeforeEach(func() {
   119  				fakeConfig = new(uifakes.FakeConfig)
   120  				fakeConfig.ColorEnabledReturns(config.ColorEnabled)
   121  				fakeConfig.LocaleReturns("fr-FR")
   122  
   123  				var err error
   124  				ui, err = NewUI(fakeConfig)
   125  				Expect(err).NotTo(HaveOccurred())
   126  
   127  				ui.Out = NewBuffer()
   128  			})
   129  
   130  			It("translates the main string and keys passed to DisplayTextWithKeyTranslations", func() {
   131  				ui.DisplayTextWithKeyTranslations("   {{.CommandName}} - {{.CommandDescription}}",
   132  					[]string{"CommandDescription"},
   133  					map[string]interface{}{
   134  						"CommandName":        "ADVANCED", // In translation file, should not be translated
   135  						"CommandDescription": "A command line tool to interact with Cloud Foundry",
   136  					})
   137  
   138  				Expect(ui.Out).To(Say("   ADVANCED - Outil de ligne de commande permettant d'interagir avec Cloud Foundry"))
   139  			})
   140  		})
   141  	})
   142  
   143  	Describe("DisplayNewline", func() {
   144  		It("displays a new line", func() {
   145  			ui.DisplayNewline()
   146  
   147  			Expect(ui.Out).To(Say("\n"))
   148  		})
   149  	})
   150  
   151  	Describe("DisplayHelpHeader", func() {
   152  		It("bolds and colorizes the input string", func() {
   153  			ui.DisplayHelpHeader("some-text")
   154  			Expect(ui.Out).To(Say("\x1b\\[38;1msome-text\x1b\\[0m"))
   155  		})
   156  
   157  		Context("when the local is not set to 'en-us'", func() {
   158  			BeforeEach(func() {
   159  				fakeConfig = new(uifakes.FakeConfig)
   160  				fakeConfig.ColorEnabledReturns(config.ColorEnabled)
   161  				fakeConfig.LocaleReturns("fr-FR")
   162  
   163  				var err error
   164  				ui, err = NewUI(fakeConfig)
   165  				Expect(err).NotTo(HaveOccurred())
   166  
   167  				ui.Out = NewBuffer()
   168  			})
   169  
   170  			It("bolds and colorizes the input string", func() {
   171  				ui.DisplayHelpHeader("FEATURE FLAGS")
   172  				Expect(ui.Out).To(Say("\x1b\\[38;1mINDICATEURS DE FONCTION\x1b\\[0m"))
   173  			})
   174  		})
   175  	})
   176  })