github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/util/ui/ui_for_push_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/types"
     7  	"github.com/liamawhite/cli-with-i18n/util/configv3"
     8  	. "github.com/liamawhite/cli-with-i18n/util/ui"
     9  	"github.com/liamawhite/cli-with-i18n/util/ui/uifakes"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  )
    14  
    15  var _ = Describe("UI", func() {
    16  	var (
    17  		ui         *UI
    18  		fakeConfig *uifakes.FakeConfig
    19  		out        *Buffer
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeConfig = new(uifakes.FakeConfig)
    24  		fakeConfig.ColorEnabledReturns(configv3.ColorEnabled)
    25  	})
    26  
    27  	JustBeforeEach(func() {
    28  		var err error
    29  		ui, err = NewUI(fakeConfig)
    30  		Expect(err).NotTo(HaveOccurred())
    31  
    32  		out = NewBuffer()
    33  		ui.Out = out
    34  		ui.Err = NewBuffer()
    35  	})
    36  
    37  	Describe("DisplayChangeForPush", func() {
    38  		Context("in english", func() {
    39  			Context("when passed strings for values", func() {
    40  				Context("when the values are *not* hidden", func() {
    41  					Context("when the values are not equal", func() {
    42  						Context("when both values are not empty", func() {
    43  							It("should display the header with differences", func() {
    44  								err := ui.DisplayChangeForPush("val", 2, false, "old", "new")
    45  								Expect(err).ToNot(HaveOccurred())
    46  								Expect(out).To(Say("\x1b\\[31m\\-\\s+val  old\x1b\\[0m"))
    47  								Expect(out).To(Say("\x1b\\[32m\\+\\s+val  new\x1b\\[0m"))
    48  							})
    49  						})
    50  
    51  						Context("when the originalValue is empty", func() {
    52  							It("should display the header with the new value only", func() {
    53  								err := ui.DisplayChangeForPush("val", 2, false, "", "new")
    54  								Expect(err).ToNot(HaveOccurred())
    55  								Expect(out).To(Say("\x1b\\[32m\\+\\s+val  new\x1b\\[0m"))
    56  
    57  								err = ui.DisplayChangeForPush("val", 2, false, "", "new")
    58  								Expect(err).ToNot(HaveOccurred())
    59  								Expect(out).ToNot(Say("\x1b\\[31m\\-\\s+val  old\x1b\\[0m"))
    60  							})
    61  						})
    62  
    63  						Context("when the newValue is empty", func() {
    64  							It("should display the header with the new value only", func() {
    65  								err := ui.DisplayChangeForPush("val", 2, false, "old", "")
    66  								Expect(err).ToNot(HaveOccurred())
    67  								Expect(out).To(Say("\x1b\\[31m\\-\\s+val  old\x1b\\[0m"))
    68  								Expect(out).ToNot(Say("\x1b\\[32m\\+\\s+val  \x1b\\[0m"))
    69  							})
    70  						})
    71  					})
    72  
    73  					Context("when the values are the equal", func() {
    74  						It("should display the header without differences", func() {
    75  							err := ui.DisplayChangeForPush("val", 2, false, "old", "old")
    76  							Expect(err).ToNot(HaveOccurred())
    77  							Expect(out).To(Say("(?m)^\\s+val  old$"))
    78  						})
    79  					})
    80  
    81  					Context("when the values are a different type", func() {
    82  						It("should return an ErrValueMissmatch", func() {
    83  							err := ui.DisplayChangeForPush("asdf", 2, false, "asdf", 7)
    84  							Expect(err).To(MatchError(ErrValueMissmatch))
    85  						})
    86  					})
    87  				})
    88  
    89  				Context("when the values are hidden", func() {
    90  					Context("when the values are not equal", func() {
    91  						Context("when the originalValue is not empty", func() {
    92  							It("should display the header with differences", func() {
    93  								err := ui.DisplayChangeForPush("val", 2, true, "old", "new")
    94  								Expect(err).ToNot(HaveOccurred())
    95  								Expect(out).To(Say("\x1b\\[31m\\-\\s+val  %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue)))
    96  								Expect(out).To(Say("\x1b\\[32m\\+\\s+val  %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue)))
    97  							})
    98  						})
    99  
   100  						Context("when the originalValue is empty", func() {
   101  							It("should display the header with the new value only", func() {
   102  								err := ui.DisplayChangeForPush("val", 2, true, "", "new")
   103  								Expect(err).ToNot(HaveOccurred())
   104  								Expect(out).To(Say("\x1b\\[32m\\+\\s+val  %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue)))
   105  
   106  								err = ui.DisplayChangeForPush("val", 2, true, "", "new")
   107  								Expect(err).ToNot(HaveOccurred())
   108  								Expect(out).ToNot(Say("\x1b\\[31m\\-\\s+val  %s\x1b\\[0m", regexp.QuoteMeta(RedactedValue)))
   109  							})
   110  						})
   111  					})
   112  
   113  					Context("when the values are the equal", func() {
   114  						It("should display the header without differences", func() {
   115  							err := ui.DisplayChangeForPush("val", 2, true, "old", "old")
   116  							Expect(err).ToNot(HaveOccurred())
   117  							Expect(out).To(Say("(?m)^\\s+val  %s", regexp.QuoteMeta(RedactedValue)))
   118  						})
   119  					})
   120  
   121  					Context("when the values are a different type", func() {
   122  						It("should return an ErrValueMissmatch", func() {
   123  							err := ui.DisplayChangeForPush("asdf", 2, true, "asdf", 7)
   124  							Expect(err).To(MatchError(ErrValueMissmatch))
   125  						})
   126  					})
   127  				})
   128  			})
   129  
   130  			Context("when passed list of strings for values", func() {
   131  				It("should display the header with sorted differences", func() {
   132  					old := []string{"route2", "route1", "route4"}
   133  					new := []string{"route4", "route2", "route3"}
   134  					err := ui.DisplayChangeForPush("val", 2, false, old, new)
   135  					Expect(err).ToNot(HaveOccurred())
   136  					Expect(out).To(Say("\\s+val"))
   137  					Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m"))
   138  					Expect(out).To(Say("(?m)^\\s+route2$"))
   139  					Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m"))
   140  					Expect(out).To(Say("(?m)^\\s+route4$"))
   141  				})
   142  
   143  				Context("when the values are a different type", func() {
   144  					It("should return an ErrValueMissmatch", func() {
   145  						err := ui.DisplayChangeForPush("asdf", 2, false, []string{"route4", "route2", "route3"}, 7)
   146  						Expect(err).To(MatchError(ErrValueMissmatch))
   147  					})
   148  				})
   149  
   150  				Context("when both sets are empty", func() {
   151  					It("does not display anything", func() {
   152  						var old []string
   153  						new := []string{}
   154  						err := ui.DisplayChangeForPush("val", 2, false, old, new)
   155  						Expect(err).ToNot(HaveOccurred())
   156  						Expect(out).ToNot(Say("\\s+val"))
   157  					})
   158  				})
   159  			})
   160  
   161  			Context("when passed ints for values", func() {
   162  				Context("when the values are not equal", func() {
   163  					Context("when the originalValue is not empty", func() {
   164  						It("should display the header with differences", func() {
   165  							err := ui.DisplayChangeForPush("val", 2, false, 1, 2)
   166  							Expect(err).ToNot(HaveOccurred())
   167  							Expect(out).To(Say("\x1b\\[31m\\-\\s+val  1\x1b\\[0m"))
   168  							Expect(out).To(Say("\x1b\\[32m\\+\\s+val  2\x1b\\[0m"))
   169  						})
   170  					})
   171  
   172  					Context("when the originalValue is zero", func() {
   173  						It("should display the header with the new value only", func() {
   174  							err := ui.DisplayChangeForPush("val", 2, false, 0, 1)
   175  							Expect(err).ToNot(HaveOccurred())
   176  							Expect(out).To(Say("\x1b\\[32m\\+\\s+val  1\x1b\\[0m"))
   177  						})
   178  					})
   179  				})
   180  
   181  				Context("when the values are the equal", func() {
   182  					It("should display the header without differences", func() {
   183  						err := ui.DisplayChangeForPush("val", 2, false, 3, 3)
   184  						Expect(err).ToNot(HaveOccurred())
   185  						Expect(out).To(Say("(?m)^\\s+val  3$"))
   186  					})
   187  				})
   188  
   189  				Context("when the values are a different type", func() {
   190  					It("should return an ErrValueMissmatch", func() {
   191  						err := ui.DisplayChangeForPush("asdf", 2, false, 7, "asdf")
   192  						Expect(err).To(MatchError(ErrValueMissmatch))
   193  					})
   194  				})
   195  			})
   196  
   197  			Context("when passed NullInt for values", func() {
   198  				Context("when the values are not equal", func() {
   199  					Context("when the originalValue is not empty", func() {
   200  						It("should display the header with differences", func() {
   201  							err := ui.DisplayChangeForPush("val", 2, false, types.NullInt{
   202  								Value: 1,
   203  								IsSet: true,
   204  							}, types.NullInt{
   205  								Value: 2,
   206  								IsSet: true,
   207  							})
   208  							Expect(err).ToNot(HaveOccurred())
   209  							Expect(out).To(Say("\x1b\\[31m\\-\\s+val  1\x1b\\[0m"))
   210  							Expect(out).To(Say("\x1b\\[32m\\+\\s+val  2\x1b\\[0m"))
   211  						})
   212  					})
   213  
   214  					Context("when the originalValue is not set", func() {
   215  						It("should display the header with the new value only", func() {
   216  							err := ui.DisplayChangeForPush("val", 2, false, types.NullInt{
   217  								Value: 0,
   218  								IsSet: false,
   219  							}, types.NullInt{
   220  								Value: 1,
   221  								IsSet: true,
   222  							})
   223  							Expect(err).ToNot(HaveOccurred())
   224  							Expect(out).To(Say("\x1b\\[32m\\+\\s+val  1\x1b\\[0m"))
   225  						})
   226  					})
   227  				})
   228  
   229  				Context("when the values are the equal", func() {
   230  					It("should display the header without differences", func() {
   231  						err := ui.DisplayChangeForPush("val", 2, false, types.NullInt{
   232  							Value: 3,
   233  							IsSet: true,
   234  						}, types.NullInt{
   235  							Value: 3,
   236  							IsSet: true,
   237  						})
   238  						Expect(err).ToNot(HaveOccurred())
   239  						Expect(out).To(Say("(?m)^\\s+val  3$"))
   240  					})
   241  				})
   242  
   243  				Context("when the values are a different type", func() {
   244  					It("should return an ErrValueMissmatch", func() {
   245  						err := ui.DisplayChangeForPush("asdf", 2, false, types.NullInt{}, "asdf")
   246  						Expect(err).To(MatchError(ErrValueMissmatch))
   247  					})
   248  				})
   249  			})
   250  
   251  			Context("when passed map[string]string for values", func() {
   252  				It("should display the header with sorted differences", func() {
   253  					old := map[string]string{"key2": "2", "key3": "2", "key4": "4"}
   254  					new := map[string]string{"key1": "1", "key3": "3", "key4": "4"}
   255  					err := ui.DisplayChangeForPush("maps", 2, false, old, new)
   256  					Expect(err).ToNot(HaveOccurred())
   257  					Expect(out).To(Say("\\s+maps"))
   258  					Expect(out).To(Say("\x1b\\[32m\\+\\s+key1\x1b\\[0m"))
   259  					Expect(out).To(Say("\x1b\\[31m\\-\\s+key2\x1b\\[0m"))
   260  					Expect(out).To(Say("\x1b\\[31m\\-\\s+key3\x1b\\[0m"))
   261  					Expect(out).To(Say("\x1b\\[32m\\+\\s+key3\x1b\\[0m"))
   262  					Expect(out).To(Say("(?m)^\\s+key4"))
   263  				})
   264  
   265  				Context("when the values are a different type", func() {
   266  					It("should return an ErrValueMissmatch", func() {
   267  						err := ui.DisplayChangeForPush("asdf", 2, false, map[string]string{}, map[string]int{})
   268  						Expect(err).To(MatchError(ErrValueMissmatch))
   269  					})
   270  				})
   271  
   272  				Context("when both sets are empty", func() {
   273  					It("does not display anything", func() {
   274  						var old map[string]string
   275  						new := map[string]string{}
   276  						err := ui.DisplayChangeForPush("maps", 2, false, old, new)
   277  						Expect(err).ToNot(HaveOccurred())
   278  						Expect(out).ToNot(Say("\\s+maps"))
   279  					})
   280  				})
   281  			})
   282  		})
   283  
   284  		Context("in a non-english language", func() {
   285  			BeforeEach(func() {
   286  				fakeConfig.LocaleReturns("fr-FR")
   287  			})
   288  
   289  			Context("when passed strings for values", func() {
   290  				Context("when the values are not equal", func() {
   291  					It("should display the differences", func() {
   292  						err := ui.DisplayChangeForPush("Name", 2, false, "old", "new")
   293  						Expect(err).ToNot(HaveOccurred())
   294  						Expect(out).To(Say("\x1b\\[31m\\-\\s+Nom  old\x1b\\[0m"))
   295  						Expect(out).To(Say("\x1b\\[32m\\+\\s+Nom  new\x1b\\[0m"))
   296  					})
   297  				})
   298  
   299  				Context("when the values are the equal", func() {
   300  					It("should display the header without differences", func() {
   301  						err := ui.DisplayChangeForPush("Name", 2, false, "old", "old")
   302  						Expect(err).ToNot(HaveOccurred())
   303  						Expect(out).To(Say("(?m)^\\s+Nom  old$"))
   304  					})
   305  				})
   306  			})
   307  
   308  			Context("when passed list of strings for values", func() {
   309  				It("should display the header with sorted differences", func() {
   310  					old := []string{"route2", "route1", "route4"}
   311  					new := []string{"route4", "route2", "route3"}
   312  					err := ui.DisplayChangeForPush("Name", 2, false, old, new)
   313  					Expect(err).ToNot(HaveOccurred())
   314  					Expect(out).To(Say("\\s+Nom"))
   315  					Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m"))
   316  					Expect(out).To(Say("(?m)^\\s+route2$"))
   317  					Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m"))
   318  					Expect(out).To(Say("(?m)^\\s+route4$"))
   319  				})
   320  			})
   321  		})
   322  	})
   323  
   324  	Describe("DisplayChangesForPush", func() {
   325  		It("alings all the string types", func() {
   326  			changeSet := []Change{
   327  				{
   328  					Header:       "h1",
   329  					CurrentValue: "old",
   330  					NewValue:     "new",
   331  				},
   332  				{
   333  					Header:       "header2",
   334  					CurrentValue: "old",
   335  					NewValue:     "old",
   336  				},
   337  				{
   338  					Header:       "header3",
   339  					CurrentValue: []string{"route2", "route1", "route4"},
   340  					NewValue:     []string{"route4", "route2", "route3"},
   341  				},
   342  			}
   343  
   344  			err := ui.DisplayChangesForPush(changeSet)
   345  			Expect(err).ToNot(HaveOccurred())
   346  
   347  			Expect(out).To(Say("\x1b\\[31m\\-\\s+h1        old\x1b\\[0m"))
   348  			Expect(out).To(Say("\x1b\\[32m\\+\\s+h1        new\x1b\\[0m"))
   349  			Expect(out).To(Say("(?m)^\\s+header2   old"))
   350  			Expect(out).To(Say("(?m)^\\s+header3$"))
   351  			Expect(out).To(Say("\x1b\\[31m\\-\\s+route1\x1b\\[0m"))
   352  			Expect(out).To(Say("(?m)^\\s+route2$"))
   353  			Expect(out).To(Say("\x1b\\[32m\\+\\s+route3\x1b\\[0m"))
   354  			Expect(out).To(Say("(?m)^\\s+route4$"))
   355  		})
   356  	})
   357  })