|
| 1 | +package table |
| 2 | + |
| 3 | +import ( |
| 4 | +"github.com/stretchr/testify/assert" |
| 5 | +"strings" |
| 6 | +"testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPager(t *testing.T) { |
| 10 | +expectedOutput := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 11 | +| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | |
| 12 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 13 | +| 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | A/5 21171 | 7.25 | | S | |
| 14 | +| 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female | 38 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | |
| 15 | +| 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | STON/O2. 3101282 | 7.925 | | S | |
| 16 | +| 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35 | 1 | 0 | 113803 | 53.1 | C123 | S | |
| 17 | +| 5 | 0 | 3 | Allen, Mr. William Henry | male | 35 | 0 | 0 | 373450 | 8.05 | | S | |
| 18 | +| 6 | 0 | 3 | Moran, Mr. James | male | | 0 | 0 | 330877 | 8.4583 | | Q | |
| 19 | +| 7 | 0 | 1 | McCarthy, Mr. Timothy J | male | 54 | 0 | 0 | 17463 | 51.8625 | E46 | S | |
| 20 | +| 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2 | 3 | 1 | 349909 | 21.075 | | S | |
| 21 | +| 9 | 1 | 3 | Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) | female | 27 | 0 | 2 | 347742 | 11.1333 | | S | |
| 22 | +| 10 | 1 | 2 | Nasser, Mrs. Nicholas (Adele Achem) | female | 14 | 1 | 0 | 237736 | 30.0708 | | C | |
| 23 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` |
| 24 | +expectedOutputP1 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 25 | +| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | |
| 26 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 27 | +| 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | A/5 21171 | 7.25 | | S | |
| 28 | +| 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female | 38 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | |
| 29 | +| 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | STON/O2. 3101282 | 7.925 | | S | |
| 30 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` |
| 31 | +expectedOutputP2 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 32 | +| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | |
| 33 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 34 | +| 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35 | 1 | 0 | 113803 | 53.1 | C123 | S | |
| 35 | +| 5 | 0 | 3 | Allen, Mr. William Henry | male | 35 | 0 | 0 | 373450 | 8.05 | | S | |
| 36 | +| 6 | 0 | 3 | Moran, Mr. James | male | | 0 | 0 | 330877 | 8.4583 | | Q | |
| 37 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` |
| 38 | +expectedOutputP3 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 39 | +| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | |
| 40 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 41 | +| 7 | 0 | 1 | McCarthy, Mr. Timothy J | male | 54 | 0 | 0 | 17463 | 51.8625 | E46 | S | |
| 42 | +| 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2 | 3 | 1 | 349909 | 21.075 | | S | |
| 43 | +| 9 | 1 | 3 | Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) | female | 27 | 0 | 2 | 347742 | 11.1333 | | S | |
| 44 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` |
| 45 | +expectedOutputP4 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 46 | +| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | |
| 47 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ |
| 48 | +| 10 | 1 | 2 | Nasser, Mrs. Nicholas (Adele Achem) | female | 14 | 1 | 0 | 237736 | 30.0708 | | C | |
| 49 | ++-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` |
| 50 | + |
| 51 | +tw := NewWriter() |
| 52 | +tw.AppendHeader(testTitanicHeader) |
| 53 | +tw.AppendRows(testTitanicRows) |
| 54 | +compareOutput(t, expectedOutput, tw.Render()) |
| 55 | + |
| 56 | +p := tw.Pager(PageSize(3)) |
| 57 | +assert.Equal(t, 1, p.Location()) |
| 58 | +compareOutput(t, expectedOutputP1, p.Render()) |
| 59 | +compareOutput(t, expectedOutputP2, p.Next()) |
| 60 | +compareOutput(t, expectedOutputP2, p.Render()) |
| 61 | +assert.Equal(t, 2, p.Location()) |
| 62 | +compareOutput(t, expectedOutputP3, p.Next()) |
| 63 | +compareOutput(t, expectedOutputP3, p.Render()) |
| 64 | +assert.Equal(t, 3, p.Location()) |
| 65 | +compareOutput(t, expectedOutputP4, p.Next()) |
| 66 | +compareOutput(t, expectedOutputP4, p.Render()) |
| 67 | +assert.Equal(t, 4, p.Location()) |
| 68 | +compareOutput(t, expectedOutputP4, p.Next()) |
| 69 | +compareOutput(t, expectedOutputP4, p.Render()) |
| 70 | +assert.Equal(t, 4, p.Location()) |
| 71 | +compareOutput(t, expectedOutputP3, p.Prev()) |
| 72 | +compareOutput(t, expectedOutputP3, p.Render()) |
| 73 | +assert.Equal(t, 3, p.Location()) |
| 74 | +compareOutput(t, expectedOutputP2, p.Prev()) |
| 75 | +compareOutput(t, expectedOutputP2, p.Render()) |
| 76 | +assert.Equal(t, 2, p.Location()) |
| 77 | +compareOutput(t, expectedOutputP1, p.Prev()) |
| 78 | +compareOutput(t, expectedOutputP1, p.Render()) |
| 79 | +assert.Equal(t, 1, p.Location()) |
| 80 | +compareOutput(t, expectedOutputP1, p.Prev()) |
| 81 | +compareOutput(t, expectedOutputP1, p.Render()) |
| 82 | +assert.Equal(t, 1, p.Location()) |
| 83 | + |
| 84 | +compareOutput(t, expectedOutputP1, p.GoTo(0)) |
| 85 | +compareOutput(t, expectedOutputP1, p.Render()) |
| 86 | +assert.Equal(t, 1, p.Location()) |
| 87 | +compareOutput(t, expectedOutputP1, p.GoTo(1)) |
| 88 | +compareOutput(t, expectedOutputP1, p.Render()) |
| 89 | +assert.Equal(t, 1, p.Location()) |
| 90 | +compareOutput(t, expectedOutputP2, p.GoTo(2)) |
| 91 | +compareOutput(t, expectedOutputP2, p.Render()) |
| 92 | +assert.Equal(t, 2, p.Location()) |
| 93 | +compareOutput(t, expectedOutputP3, p.GoTo(3)) |
| 94 | +compareOutput(t, expectedOutputP3, p.Render()) |
| 95 | +assert.Equal(t, 3, p.Location()) |
| 96 | +compareOutput(t, expectedOutputP4, p.GoTo(4)) |
| 97 | +compareOutput(t, expectedOutputP4, p.Render()) |
| 98 | +assert.Equal(t, 4, p.Location()) |
| 99 | +compareOutput(t, expectedOutputP4, p.GoTo(5)) |
| 100 | +compareOutput(t, expectedOutputP4, p.Render()) |
| 101 | +assert.Equal(t, 4, p.Location()) |
| 102 | + |
| 103 | +sb := strings.Builder{} |
| 104 | +p.SetOutputMirror(&sb) |
| 105 | +p.Render() |
| 106 | +compareOutput(t, expectedOutputP4, sb.String()) |
| 107 | +} |
0 commit comments