1 package pagination 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func newSections(n ...uint) [3]section { 9 var sections [3]section 10 l := len(n) 11 if l > 1 { 12 sections[0].First = n[0] 13 sections[0].Last = n[1] 14 } 15 if l > 3 { 16 sections[1].First = n[2] 17 sections[1].Last = n[3] 18 } 19 if l > 5 { 20 sections[2].First = n[4] 21 sections[2].Last = n[5] 22 } 23 return sections 24 } 25 26 func TestConfigGet(t *testing.T) { 27 28 noEnds := Config{0, 3} 29 noMiddle := Config{3, 0} 30 normal := Config{3, 3} 31 32 tests := []struct { 33 Config 34 currPage, lastPage uint 35 sections [3]section 36 }{ 37 {normal, 0, 9, newSections(0, 3, 7, 9)}, 38 {normal, 1, 9, newSections(0, 4, 7, 9)}, 39 {normal, 2, 9, newSections(0, 9)}, 40 {normal, 3, 9, newSections(0, 9)}, 41 {normal, 4, 9, newSections(0, 9)}, 42 {normal, 5, 9, newSections(0, 9)}, 43 {normal, 6, 9, newSections(0, 9)}, 44 {normal, 7, 9, newSections(0, 9)}, 45 {normal, 8, 9, newSections(0, 2, 5, 9)}, 46 {normal, 9, 9, newSections(0, 2, 6, 9)}, 47 {normal, 50, 100, newSections(0, 2, 47, 53, 98, 100)}, 48 {noMiddle, 0, 9, newSections(0, 2, 7, 9)}, 49 {noMiddle, 1, 9, newSections(0, 2, 7, 9)}, 50 {noMiddle, 2, 9, newSections(0, 2, 7, 9)}, 51 {noMiddle, 3, 9, newSections(0, 3, 7, 9)}, 52 {noMiddle, 4, 9, newSections(0, 4, 7, 9)}, 53 {noMiddle, 5, 9, newSections(0, 2, 5, 9)}, 54 {noMiddle, 6, 9, newSections(0, 2, 6, 9)}, 55 {noMiddle, 7, 9, newSections(0, 2, 7, 9)}, 56 {noMiddle, 8, 9, newSections(0, 2, 7, 9)}, 57 {noMiddle, 9, 9, newSections(0, 2, 7, 9)}, 58 {noEnds, 0, 9, newSections(0, 3)}, 59 {noEnds, 1, 9, newSections(0, 4)}, 60 {noEnds, 2, 9, newSections(0, 5)}, 61 {noEnds, 3, 9, newSections(0, 6)}, 62 {noEnds, 4, 9, newSections(1, 7)}, 63 {noEnds, 5, 9, newSections(2, 8)}, 64 {noEnds, 6, 9, newSections(3, 9)}, 65 {noEnds, 7, 9, newSections(4, 9)}, 66 {noEnds, 8, 9, newSections(5, 9)}, 67 {noEnds, 9, 9, newSections(6, 9)}, 68 } 69 70 for n, test := range tests { 71 if s := test.Get(test.currPage, test.lastPage); !reflect.DeepEqual(s.sections, test.sections) { 72 t.Errorf("test %d: expecting %v, got %v", n+1, test.sections, s.sections) 73 } 74 } 75 }