Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame^] | 1 | package order |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | type SortTestStruct struct { |
| 8 | Id int |
| 9 | One string |
| 10 | Two string |
| 11 | Three uint |
| 12 | Four int |
| 13 | } |
| 14 | |
| 15 | var testSetOne = []SortTestStruct{ |
| 16 | { |
| 17 | Id: 0, |
| 18 | One: "a", |
| 19 | Two: "x", |
| 20 | Three: 10, |
| 21 | Four: 1, |
| 22 | }, |
| 23 | { |
| 24 | Id: 1, |
| 25 | One: "a", |
| 26 | Two: "c", |
| 27 | Three: 1, |
| 28 | Four: 10, |
| 29 | }, |
| 30 | { |
| 31 | Id: 2, |
| 32 | One: "a", |
| 33 | Two: "b", |
| 34 | Three: 2, |
| 35 | Four: 1000, |
| 36 | }, |
| 37 | { |
| 38 | Id: 3, |
| 39 | One: "a", |
| 40 | Two: "a", |
| 41 | Three: 3, |
| 42 | Four: 100, |
| 43 | }, |
| 44 | { |
| 45 | Id: 4, |
| 46 | One: "b", |
| 47 | Two: "a", |
| 48 | Three: 3, |
| 49 | Four: 0, |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | var testSetTwo = []SortTestStruct{ |
| 54 | { |
| 55 | Id: 0, |
| 56 | One: "a", |
| 57 | Two: "x", |
| 58 | Three: 10, |
| 59 | Four: 10, |
| 60 | }, |
| 61 | { |
| 62 | Id: 1, |
| 63 | One: "a", |
| 64 | Two: "y", |
| 65 | Three: 1, |
| 66 | Four: 1, |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | func Verify(v []SortTestStruct, order []int) bool { |
| 71 | for i, item := range v { |
| 72 | if item.Id != order[i] { |
| 73 | return false |
| 74 | } |
| 75 | } |
| 76 | return true |
| 77 | } |
| 78 | |
| 79 | func TestSort(t *testing.T) { |
| 80 | s, err := Parse("+One,-Two") |
| 81 | if err != nil { |
| 82 | t.Errorf("Unable to parse sort specification") |
| 83 | } |
| 84 | o, err := s.Process(testSetOne) |
| 85 | if err != nil { |
| 86 | t.Errorf("Sort failed: %s", err.Error()) |
| 87 | } |
| 88 | |
| 89 | if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) { |
| 90 | t.Errorf("incorrect sort") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestSortASC(t *testing.T) { |
| 95 | s, err := Parse("+One,Two") |
| 96 | if err != nil { |
| 97 | t.Errorf("Unable to parse sort specification") |
| 98 | } |
| 99 | o, err := s.Process(testSetTwo) |
| 100 | if err != nil { |
| 101 | t.Errorf("Sort failed: %s", err.Error()) |
| 102 | } |
| 103 | |
| 104 | if !Verify(o.([]SortTestStruct), []int{0, 1}) { |
| 105 | t.Errorf("incorrect sort") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestSortUintASC(t *testing.T) { |
| 110 | s, err := Parse("Three,One") |
| 111 | if err != nil { |
| 112 | t.Errorf("Unable to parse sort specification") |
| 113 | } |
| 114 | o, err := s.Process(testSetOne) |
| 115 | if err != nil { |
| 116 | t.Errorf("Sort failed: %s", err.Error()) |
| 117 | } |
| 118 | |
| 119 | if !Verify(o.([]SortTestStruct), []int{1, 2, 3, 4, 0}) { |
| 120 | t.Errorf("incorrect sort") |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestSortUintDSC(t *testing.T) { |
| 125 | s, err := Parse("-Three,One") |
| 126 | if err != nil { |
| 127 | t.Errorf("Unable to parse sort specification") |
| 128 | } |
| 129 | o, err := s.Process(testSetOne) |
| 130 | if err != nil { |
| 131 | t.Errorf("Sort failed: %s", err.Error()) |
| 132 | } |
| 133 | |
| 134 | if !Verify(o.([]SortTestStruct), []int{0, 3, 4, 2, 1}) { |
| 135 | t.Errorf("incorrect sort") |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestSortUintDSC2(t *testing.T) { |
| 140 | s, err := Parse("-Three,One") |
| 141 | if err != nil { |
| 142 | t.Errorf("Unable to parse sort specification") |
| 143 | } |
| 144 | o, err := s.Process(testSetTwo) |
| 145 | if err != nil { |
| 146 | t.Errorf("Sort failed: %s", err.Error()) |
| 147 | } |
| 148 | |
| 149 | if !Verify(o.([]SortTestStruct), []int{0, 1}) { |
| 150 | t.Errorf("incorrect sort") |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestSortIntASC(t *testing.T) { |
| 155 | s, err := Parse("Four,One") |
| 156 | if err != nil { |
| 157 | t.Errorf("Unable to parse sort specification") |
| 158 | } |
| 159 | o, err := s.Process(testSetOne) |
| 160 | if err != nil { |
| 161 | t.Errorf("Sort failed: %s", err.Error()) |
| 162 | } |
| 163 | if !Verify(o.([]SortTestStruct), []int{4, 0, 1, 3, 2}) { |
| 164 | t.Errorf("incorrect sort") |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestSortIntDSC(t *testing.T) { |
| 169 | s, err := Parse("-Four,One") |
| 170 | if err != nil { |
| 171 | t.Errorf("Unable to parse sort specification") |
| 172 | } |
| 173 | o, err := s.Process(testSetOne) |
| 174 | if err != nil { |
| 175 | t.Errorf("Sort failed: %s", err.Error()) |
| 176 | } |
| 177 | if !Verify(o.([]SortTestStruct), []int{2, 3, 1, 0, 4}) { |
| 178 | t.Errorf("incorrect sort") |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestSortIntDSC2(t *testing.T) { |
| 183 | s, err := Parse("-Four,One") |
| 184 | if err != nil { |
| 185 | t.Errorf("Unable to parse sort specification") |
| 186 | } |
| 187 | o, err := s.Process(testSetTwo) |
| 188 | if err != nil { |
| 189 | t.Errorf("Sort failed: %s", err.Error()) |
| 190 | } |
| 191 | if !Verify(o.([]SortTestStruct), []int{0, 1}) { |
| 192 | t.Errorf("incorrect sort") |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func TestOperString(t *testing.T) { |
| 197 | if ASC.String() != "ASC" { |
| 198 | t.Errorf("ASC to string failed") |
| 199 | } |
| 200 | if DSC.String() != "DSC" { |
| 201 | t.Errorf("DSC to string failed") |
| 202 | } |
| 203 | var o Operation = 5 // Invalid |
| 204 | if o.String() != "ASC" { |
| 205 | t.Errorf("to string default failed") |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func TestSortSingle(t *testing.T) { |
| 210 | s, err := Parse("-Four,One") |
| 211 | if err != nil { |
| 212 | t.Errorf("Unable to parse sort specification") |
| 213 | } |
| 214 | o, err := s.Process(testSetOne[0]) |
| 215 | if err != nil { |
| 216 | t.Errorf("Sort failed: %s", err.Error()) |
| 217 | } |
| 218 | |
| 219 | if o == nil { |
| 220 | t.Errorf("expected value, got nil") |
| 221 | } |
| 222 | |
| 223 | r, ok := o.(SortTestStruct) |
| 224 | if !ok { |
| 225 | t.Errorf("Unexpected result type") |
| 226 | } |
| 227 | |
| 228 | if r.Id != testSetOne[0].Id { |
| 229 | t.Errorf("results don't match input") |
| 230 | } |
| 231 | } |