|
| 1 | +// Copyright © 2019 The Homeport Team |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package text_test |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + |
| 26 | + . "github.com/onsi/ginkgo" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | + |
| 29 | + . "github.com/gonvenience/text" |
| 30 | + . "github.com/gonvenience/bunt" |
| 31 | +) |
| 32 | + |
| 33 | +var _ = Describe("Generate random strings with fixed length", func() { |
| 34 | + Context("Random string with no prefix", func() { |
| 35 | + It("should generate a random string with fixed length", func() { |
| 36 | + Expect(len(RandomString(32))).To(BeEquivalentTo(32)) |
| 37 | + }) |
| 38 | + |
| 39 | + It("should fail when negative length is given", func() { |
| 40 | + defer func() { |
| 41 | + Expect(recover()).To(HaveOccurred()) |
| 42 | + }() |
| 43 | + |
| 44 | + RandomString(-1) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + Context("Random string with given prefix", func() { |
| 49 | + It("should generate a random string with fixed length", func() { |
| 50 | + Expect(len(RandomStringWithPrefix("foobar", 32))).To(BeEquivalentTo(32)) |
| 51 | + }) |
| 52 | + |
| 53 | + It("should fail when the prefix is already longer than the fixed length", func() { |
| 54 | + defer func() { |
| 55 | + Expect(recover()).To(HaveOccurred()) |
| 56 | + }() |
| 57 | + |
| 58 | + RandomStringWithPrefix("foobar", 4) |
| 59 | + }) |
| 60 | + |
| 61 | + It("should fail when negative length is given", func() { |
| 62 | + defer func() { |
| 63 | + Expect(recover()).To(HaveOccurred()) |
| 64 | + }() |
| 65 | + |
| 66 | + RandomStringWithPrefix("foobar", -1) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + Context("Text with given fixed length", func() { |
| 71 | + It("should create a string with the text and enough padding to fill it up to the required length", func() { |
| 72 | + Expect(FixedLength("Foobar", 10)).To(BeEquivalentTo("Foobar ")) |
| 73 | + }) |
| 74 | + |
| 75 | + It("should trim the text if the text alone exceeds the provided desired length", func() { |
| 76 | + Expect(FixedLength("This text is too long", 10)).To(BeEquivalentTo("This [...]")) |
| 77 | + }) |
| 78 | + |
| 79 | + It("should return the text as-is if it already has the perfect length", func() { |
| 80 | + Expect(FixedLength("Foobar", 6)).To(BeEquivalentTo("Foobar")) |
| 81 | + }) |
| 82 | + |
| 83 | + It("should work with text containing ANSI sequences", func() { |
| 84 | + // "This text is too long" 21 characters |
| 85 | + // "This text is [...]" 18 characters |
| 86 | + actual := FixedLength(Sprintf("*This* text is too long"), 18) |
| 87 | + expected := Sprintf("*This* text is [...]") |
| 88 | + |
| 89 | + Expect(fmt.Sprintf("%#v", actual)).To(BeEquivalentTo(fmt.Sprintf("%#v", expected))) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + Context("creating proper texts", func() { |
| 94 | + It("should return human readable plurals", func() { |
| 95 | + Expect(Plural(0, "foobar")).To(BeEquivalentTo("no foobars")) |
| 96 | + Expect(Plural(1, "foobar")).To(BeEquivalentTo("one foobar")) |
| 97 | + Expect(Plural(2, "foobar")).To(BeEquivalentTo("two foobars")) |
| 98 | + Expect(Plural(3, "foobar")).To(BeEquivalentTo("three foobars")) |
| 99 | + Expect(Plural(4, "foobar")).To(BeEquivalentTo("four foobars")) |
| 100 | + Expect(Plural(5, "foobar")).To(BeEquivalentTo("five foobars")) |
| 101 | + Expect(Plural(6, "foobar")).To(BeEquivalentTo("six foobars")) |
| 102 | + Expect(Plural(7, "foobar")).To(BeEquivalentTo("seven foobars")) |
| 103 | + Expect(Plural(8, "foobar")).To(BeEquivalentTo("eight foobars")) |
| 104 | + Expect(Plural(9, "foobar")).To(BeEquivalentTo("nine foobars")) |
| 105 | + Expect(Plural(10, "foobar")).To(BeEquivalentTo("ten foobars")) |
| 106 | + Expect(Plural(11, "foobar")).To(BeEquivalentTo("eleven foobars")) |
| 107 | + Expect(Plural(12, "foobar")).To(BeEquivalentTo("twelve foobars")) |
| 108 | + Expect(Plural(13, "foobar")).To(BeEquivalentTo("13 foobars")) |
| 109 | + Expect(Plural(147, "foobar")).To(BeEquivalentTo("147 foobars")) |
| 110 | + |
| 111 | + Expect(Plural(1, "basis", "bases")).To(BeEquivalentTo("one basis")) |
| 112 | + Expect(Plural(2, "basis", "bases")).To(BeEquivalentTo("two bases")) |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments