github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/command/translatableerror/staging_timeout_error_test.go (about) 1 package translatableerror_test 2 3 import ( 4 "bytes" 5 "io" 6 "text/template" 7 "time" 8 9 . "code.cloudfoundry.org/cli/command/translatableerror" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("StagingTimeoutError", func() { 16 Describe("Translate()", func() { 17 var translateFunc func(string, ...interface{}) string 18 19 BeforeEach(func() { 20 translateFunc = func(templateStr string, subs ...interface{}) string { 21 t := template.Must(template.New("some-text-template").Parse(templateStr)) 22 buffer := bytes.NewBuffer([]byte{}) 23 err := t.Execute(buffer, subs[0]) 24 Expect(err).NotTo(HaveOccurred()) 25 translatedStr, err := buffer.ReadString('\n') 26 if err != io.EOF { 27 Expect(err).NotTo(HaveOccurred()) 28 } 29 return translatedStr 30 } 31 }) 32 33 When("called with a float", func() { 34 It("prints the error without trailing zeros", func() { 35 err := StagingTimeoutError{ 36 AppName: "sliders", 37 Timeout: 150 * time.Second, 38 } 39 40 Expect(err.Translate(translateFunc)).To(Equal("Error staging application sliders: timed out after 2.5 minute(s)")) 41 }) 42 }) 43 44 When("called with an integer", func() { 45 It("prints the error with integer precision", func() { 46 err := StagingTimeoutError{ 47 AppName: "sliders", 48 Timeout: 120 * time.Second, 49 } 50 51 Expect(err.Translate(translateFunc)).To(Equal("Error staging application sliders: timed out after 2 minute(s)")) 52 }) 53 }) 54 55 When("called with a timeout of less than one minute", func() { 56 It("prints the error with 'minutes' instead of 'minute'", func() { 57 err := StagingTimeoutError{ 58 AppName: "sliders", 59 Timeout: 30 * time.Second, 60 } 61 62 Expect(err.Translate(translateFunc)).To(Equal("Error staging application sliders: timed out after 0.5 minute(s)")) 63 }) 64 }) 65 66 When("called with a timeout of exactly one minute", func() { 67 It("prints the error with 'minute' instead of 'minutes'", func() { 68 err := StagingTimeoutError{ 69 AppName: "sliders", 70 Timeout: 60 * time.Second, 71 } 72 73 Expect(err.Translate(translateFunc)).To(Equal("Error staging application sliders: timed out after 1 minute(s)")) 74 }) 75 }) 76 77 When("called with a timeout of more than one minute", func() { 78 It("prints the error with 'minutes' instead of 'minute'", func() { 79 err := StagingTimeoutError{ 80 AppName: "sliders", 81 Timeout: 120 * time.Second, 82 } 83 84 Expect(err.Translate(translateFunc)).To(Equal("Error staging application sliders: timed out after 2 minute(s)")) 85 }) 86 }) 87 }) 88 })