github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/internal/hijackhelpers/container_sorter_test.go (about) 1 package hijackhelpers_test 2 3 import ( 4 "fmt" 5 "sort" 6 7 "github.com/pf-qiu/concourse/v6/atc" 8 . "github.com/pf-qiu/concourse/v6/fly/commands/internal/hijackhelpers" 9 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("ContainerSorter", func() { 15 var containers []atc.Container 16 var sorter ContainerSorter 17 18 BeforeEach(func() { 19 containers = []atc.Container{ 20 { 21 ID: "8", 22 BuildID: 2, 23 StepName: "first", 24 Type: "task", 25 }, 26 { 27 ID: "3", 28 BuildID: 1, 29 StepName: "first", 30 Type: "check", 31 }, 32 { 33 ID: "6", 34 BuildID: 1, 35 StepName: "second", 36 Type: "get", 37 }, 38 { 39 ID: "4", 40 BuildID: 1, 41 StepName: "first", 42 Type: "get", 43 }, 44 { 45 ID: "7", 46 BuildID: 2, 47 StepName: "first", 48 Type: "put", 49 }, 50 { 51 ID: "5", 52 BuildID: 1, 53 StepName: "second", 54 Type: "check", 55 }, 56 { 57 ID: "9", 58 BuildID: 3, 59 StepName: "first", 60 Type: "check", 61 }, 62 { 63 ID: "2", 64 ResourceName: "zed", 65 }, 66 { 67 ID: "1", 68 ResourceName: "clarity", 69 }, 70 } 71 sorter = ContainerSorter(containers) 72 }) 73 74 Context("implements Sort interface", func() { 75 Context("Len", func() { 76 It("is the number of elements in the collection", func() { 77 Expect(sorter.Len()).To(Equal(len(containers))) 78 }) 79 }) 80 81 Context("Swap", func() { 82 It("swaps the elements with indexes i and j", func() { 83 sorter.Swap(0, 1) 84 Expect(sorter[0].ID).To(Equal("3")) 85 Expect(sorter[1].ID).To(Equal("8")) 86 }) 87 }) 88 89 Context("Less", func() { 90 Context("reports whether the element with index i should sort before the element with index j", func() { 91 Context("when BuildID of i is less than j", func() { 92 It("returns true", func() { 93 Expect(sorter.Less(1, 6)).To(BeTrue()) 94 }) 95 }) 96 97 Context("when BuildID of i is greater than j", func() { 98 It("returns false", func() { 99 Expect(sorter.Less(0, 1)).To(BeFalse()) 100 }) 101 }) 102 103 Context("when BuildID of i is equal to j", func() { 104 Context("handling background check containers", func() { 105 Context("when ResourceName of i is less than j", func() { 106 It("returns true", func() { 107 Expect(sorter.Less(8, 7)).To(BeTrue()) 108 }) 109 }) 110 111 Context("when ResourceName of i is greater than j", func() { 112 It("returns false", func() { 113 Expect(sorter.Less(7, 8)).To(BeFalse()) 114 }) 115 }) 116 }) 117 118 Context("when StepName of i is less than j", func() { 119 It("returns true", func() { 120 Expect(sorter.Less(1, 2)).To(BeTrue()) 121 }) 122 }) 123 124 Context("when StepName of i is greater than j", func() { 125 It("returns false", func() { 126 Expect(sorter.Less(2, 1)).To(BeFalse()) 127 }) 128 }) 129 130 Context("when SetName of i is equal to j", func() { 131 Context("when Type of i is less than j", func() { 132 It("returns true", func() { 133 Expect(sorter.Less(1, 3)).To(BeTrue()) 134 }) 135 }) 136 137 Context("when Type of i is greater than j", func() { 138 It("returns false", func() { 139 Expect(sorter.Less(3, 1)).To(BeFalse()) 140 }) 141 }) 142 143 Context("when Type of i is equal to j", func() { 144 It("returns false", func() { 145 Expect(sorter.Less(1, 1)).To(BeFalse()) 146 }) 147 }) 148 }) 149 }) 150 }) 151 }) 152 }) 153 154 Context("Sort", func() { 155 It("returns a sorted list of containers", func() { 156 sort.Sort(sorter) 157 158 for i, container := range sorter { 159 Expect(container.ID).To(Equal(fmt.Sprint(i + 1))) 160 } 161 }) 162 }) 163 })