1 package table
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestTable_sortRows_WithName(t *testing.T) {
10 table := Table{}
11 table.AppendHeader(Row{"#", "First Name", "Last Name", "Salary"})
12 table.AppendRows([]Row{
13 {1, "Arya", "Stark", 3000},
14 {11, "Sansa", "Stark", 3000},
15 {20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
16 {300, "Tyrion", "Lannister", 5000},
17 })
18 table.SetStyle(StyleDefault)
19 table.initForRenderRows()
20
21
22 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
23
24
25 table.SortBy([]SortBy{{Name: "#", Mode: AscNumeric}})
26 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
27
28 table.SortBy([]SortBy{{Name: "#", Mode: DscNumeric}})
29 assert.Equal(t, []int{3, 2, 1, 0}, table.getSortedRowIndices())
30
31
32 table.SortBy([]SortBy{{Name: "First Name", Mode: Asc}, {Name: "Last Name", Mode: Asc}})
33 assert.Equal(t, []int{0, 2, 1, 3}, table.getSortedRowIndices())
34
35 table.SortBy([]SortBy{{Name: "First Name", Mode: Asc}, {Name: "Last Name", Mode: Dsc}})
36 assert.Equal(t, []int{0, 2, 1, 3}, table.getSortedRowIndices())
37
38 table.SortBy([]SortBy{{Name: "First Name", Mode: Dsc}, {Name: "Last Name", Mode: Asc}})
39 assert.Equal(t, []int{3, 1, 2, 0}, table.getSortedRowIndices())
40
41 table.SortBy([]SortBy{{Name: "First Name", Mode: Dsc}, {Name: "Last Name", Mode: Dsc}})
42 assert.Equal(t, []int{3, 1, 2, 0}, table.getSortedRowIndices())
43
44
45 table.SortBy([]SortBy{{Name: "Last Name", Mode: Asc}, {Name: "First Name", Mode: Asc}})
46 assert.Equal(t, []int{3, 2, 0, 1}, table.getSortedRowIndices())
47
48 table.SortBy([]SortBy{{Name: "Last Name", Mode: Asc}, {Name: "First Name", Mode: Dsc}})
49 assert.Equal(t, []int{3, 2, 1, 0}, table.getSortedRowIndices())
50
51 table.SortBy([]SortBy{{Name: "Last Name", Mode: Dsc}, {Name: "First Name", Mode: Asc}})
52 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
53
54 table.SortBy([]SortBy{{Name: "Last Name", Mode: Dsc}, {Name: "First Name", Mode: Dsc}})
55 assert.Equal(t, []int{1, 0, 2, 3}, table.getSortedRowIndices())
56
57
58 table.SortBy([]SortBy{{Name: "Last Name", Mode: Dsc}, {Name: "Foo Bar", Mode: Dsc}})
59 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
60
61
62 table.SortBy([]SortBy{{Name: "Salary", Mode: AscNumeric}})
63 assert.Equal(t, []int{2, 0, 1, 3}, table.getSortedRowIndices())
64
65 table.SortBy([]SortBy{{Name: "Salary", Mode: DscNumeric}})
66 assert.Equal(t, []int{3, 0, 1, 2}, table.getSortedRowIndices())
67
68 table.SortBy(nil)
69 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
70 }
71
72 func TestTable_sortRows_WithoutName(t *testing.T) {
73 table := Table{}
74 table.AppendRows([]Row{
75 {1, "Arya", "Stark", 3000},
76 {11, "Sansa", "Stark", 3000},
77 {20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
78 {300, "Tyrion", "Lannister", 5000},
79 })
80 table.SetStyle(StyleDefault)
81 table.initForRenderRows()
82
83
84 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
85
86
87 table.SortBy([]SortBy{{Number: 1, Mode: AscNumeric}})
88 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
89
90 table.SortBy([]SortBy{{Number: 1, Mode: DscNumeric}})
91 assert.Equal(t, []int{3, 2, 1, 0}, table.getSortedRowIndices())
92
93
94 table.SortBy([]SortBy{{Number: 2, Mode: Asc}, {Number: 3, Mode: Asc}})
95 assert.Equal(t, []int{0, 2, 1, 3}, table.getSortedRowIndices())
96
97 table.SortBy([]SortBy{{Number: 2, Mode: Asc}, {Number: 3, Mode: Dsc}})
98 assert.Equal(t, []int{0, 2, 1, 3}, table.getSortedRowIndices())
99
100 table.SortBy([]SortBy{{Number: 2, Mode: Dsc}, {Number: 3, Mode: Asc}})
101 assert.Equal(t, []int{3, 1, 2, 0}, table.getSortedRowIndices())
102
103 table.SortBy([]SortBy{{Number: 2, Mode: Dsc}, {Number: 3, Mode: Dsc}})
104 assert.Equal(t, []int{3, 1, 2, 0}, table.getSortedRowIndices())
105
106
107 table.SortBy([]SortBy{{Number: 3, Mode: Asc}, {Number: 2, Mode: Asc}})
108 assert.Equal(t, []int{3, 2, 0, 1}, table.getSortedRowIndices())
109
110 table.SortBy([]SortBy{{Number: 3, Mode: Asc}, {Number: 2, Mode: Dsc}})
111 assert.Equal(t, []int{3, 2, 1, 0}, table.getSortedRowIndices())
112
113 table.SortBy([]SortBy{{Number: 3, Mode: Dsc}, {Number: 2, Mode: Asc}})
114 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
115
116 table.SortBy([]SortBy{{Number: 3, Mode: Dsc}, {Number: 2, Mode: Dsc}})
117 assert.Equal(t, []int{1, 0, 2, 3}, table.getSortedRowIndices())
118
119
120 table.SortBy([]SortBy{{Number: 3, Mode: Dsc}, {Number: 99, Mode: Dsc}})
121 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
122
123
124 table.SortBy([]SortBy{{Number: 4, Mode: AscNumeric}})
125 assert.Equal(t, []int{2, 0, 1, 3}, table.getSortedRowIndices())
126
127 table.SortBy([]SortBy{{Number: 4, Mode: DscNumeric}})
128 assert.Equal(t, []int{3, 0, 1, 2}, table.getSortedRowIndices())
129
130 table.SortBy(nil)
131 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
132 }
133
134 func TestTable_sortRows_InvalidMode(t *testing.T) {
135 table := Table{}
136 table.AppendRows([]Row{
137 {1, "Arya", "Stark", 3000},
138 {11, "Sansa", "Stark", 3000},
139 {20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
140 {300, "Tyrion", "Lannister", 5000},
141 })
142 table.SetStyle(StyleDefault)
143 table.initForRenderRows()
144
145
146 table.SortBy([]SortBy{{Number: 2, Mode: AscNumeric}})
147 assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
148 }
149
View as plain text