github.com/gogf/gf/v2@v2.7.4/util/gpage/gpage_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" 8 9 package gpage_test 10 11 import ( 12 "testing" 13 14 "github.com/gogf/gf/v2/test/gtest" 15 "github.com/gogf/gf/v2/util/gpage" 16 ) 17 18 func Test_New(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 page := gpage.New(9, 2, 1, `/user/list?page={.page}`) 21 t.Assert(page.TotalSize, 9) 22 t.Assert(page.TotalPage, 5) 23 t.Assert(page.CurrentPage, 1) 24 }) 25 gtest.C(t, func(t *gtest.T) { 26 page := gpage.New(9, 2, 0, `/user/list?page={.page}`) 27 t.Assert(page.TotalSize, 9) 28 t.Assert(page.TotalPage, 5) 29 t.Assert(page.CurrentPage, 1) 30 }) 31 } 32 33 func Test_Basic(t *testing.T) { 34 gtest.C(t, func(t *gtest.T) { 35 page := gpage.New(9, 2, 1, `/user/list?page={.page}`) 36 t.Assert(page.NextPage(), `<a class="GPageLink" href="/user/list?page=2" title="">></a>`) 37 t.Assert(page.PrevPage(), `<span class="GPageSpan"><</span>`) 38 t.Assert(page.FirstPage(), `<span class="GPageSpan">|<</span>`) 39 t.Assert(page.LastPage(), `<a class="GPageLink" href="/user/list?page=5" title="">>|</a>`) 40 t.Assert(page.PageBar(), `<span class="GPageSpan">1</span><a class="GPageLink" href="/user/list?page=2" title="2">2</a><a class="GPageLink" href="/user/list?page=3" title="3">3</a><a class="GPageLink" href="/user/list?page=4" title="4">4</a><a class="GPageLink" href="/user/list?page=5" title="5">5</a>`) 41 }) 42 43 gtest.C(t, func(t *gtest.T) { 44 page := gpage.New(9, 2, 3, `/user/list?page={.page}`) 45 t.Assert(page.NextPage(), `<a class="GPageLink" href="/user/list?page=4" title="">></a>`) 46 t.Assert(page.PrevPage(), `<a class="GPageLink" href="/user/list?page=2" title=""><</a>`) 47 t.Assert(page.FirstPage(), `<a class="GPageLink" href="/user/list?page=1" title="">|<</a>`) 48 t.Assert(page.LastPage(), `<a class="GPageLink" href="/user/list?page=5" title="">>|</a>`) 49 t.Assert(page.PageBar(), `<a class="GPageLink" href="/user/list?page=1" title="1">1</a><a class="GPageLink" href="/user/list?page=2" title="2">2</a><span class="GPageSpan">3</span><a class="GPageLink" href="/user/list?page=4" title="4">4</a><a class="GPageLink" href="/user/list?page=5" title="5">5</a>`) 50 }) 51 52 gtest.C(t, func(t *gtest.T) { 53 page := gpage.New(9, 2, 5, `/user/list?page={.page}`) 54 t.Assert(page.NextPage(), `<span class="GPageSpan">></span>`) 55 t.Assert(page.PrevPage(), `<a class="GPageLink" href="/user/list?page=4" title=""><</a>`) 56 t.Assert(page.FirstPage(), `<a class="GPageLink" href="/user/list?page=1" title="">|<</a>`) 57 t.Assert(page.LastPage(), `<span class="GPageSpan">>|</span>`) 58 t.Assert(page.PageBar(), `<a class="GPageLink" href="/user/list?page=1" title="1">1</a><a class="GPageLink" href="/user/list?page=2" title="2">2</a><a class="GPageLink" href="/user/list?page=3" title="3">3</a><a class="GPageLink" href="/user/list?page=4" title="4">4</a><span class="GPageSpan">5</span>`) 59 }) 60 } 61 62 func Test_CustomTag(t *testing.T) { 63 gtest.C(t, func(t *gtest.T) { 64 page := gpage.New(5, 1, 2, `/user/list/{.page}`) 65 page.PrevPageTag = "《" 66 page.NextPageTag = "》" 67 page.FirstPageTag = "|《" 68 page.LastPageTag = "》|" 69 page.PrevBarTag = "《《" 70 page.NextBarTag = "》》" 71 t.Assert(page.NextPage(), `<a class="GPageLink" href="/user/list/3" title="">》</a>`) 72 t.Assert(page.PrevPage(), `<a class="GPageLink" href="/user/list/1" title="">《</a>`) 73 t.Assert(page.FirstPage(), `<a class="GPageLink" href="/user/list/1" title="">|《</a>`) 74 t.Assert(page.LastPage(), `<a class="GPageLink" href="/user/list/5" title="">》|</a>`) 75 t.Assert(page.PageBar(), `<a class="GPageLink" href="/user/list/1" title="1">1</a><span class="GPageSpan">2</span><a class="GPageLink" href="/user/list/3" title="3">3</a><a class="GPageLink" href="/user/list/4" title="4">4</a><a class="GPageLink" href="/user/list/5" title="5">5</a>`) 76 }) 77 } 78 79 func Test_CustomStyle(t *testing.T) { 80 gtest.C(t, func(t *gtest.T) { 81 page := gpage.New(5, 1, 2, `/user/list/{.page}`) 82 page.LinkStyle = "MyPageLink" 83 page.SpanStyle = "MyPageSpan" 84 page.SelectStyle = "MyPageSelect" 85 t.Assert(page.NextPage(), `<a class="MyPageLink" href="/user/list/3" title="">></a>`) 86 t.Assert(page.PrevPage(), `<a class="MyPageLink" href="/user/list/1" title=""><</a>`) 87 t.Assert(page.FirstPage(), `<a class="MyPageLink" href="/user/list/1" title="">|<</a>`) 88 t.Assert(page.LastPage(), `<a class="MyPageLink" href="/user/list/5" title="">>|</a>`) 89 t.Assert(page.PageBar(), `<a class="MyPageLink" href="/user/list/1" title="1">1</a><span class="MyPageSpan">2</span><a class="MyPageLink" href="/user/list/3" title="3">3</a><a class="MyPageLink" href="/user/list/4" title="4">4</a><a class="MyPageLink" href="/user/list/5" title="5">5</a>`) 90 t.Assert(page.SelectBar(), `<select name="MyPageSelect" onchange="window.location.href=this.value"><option value="/user/list/1">1</option><option value="/user/list/2" selected>2</option><option value="/user/list/3">3</option><option value="/user/list/4">4</option><option value="/user/list/5">5</option></select>`) 91 }) 92 } 93 94 func Test_Ajax(t *testing.T) { 95 gtest.C(t, func(t *gtest.T) { 96 page := gpage.New(5, 1, 2, `/user/list/{.page}`) 97 page.AjaxActionName = "LoadPage" 98 t.Assert(page.NextPage(), `<a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="">></a>`) 99 t.Assert(page.PrevPage(), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title=""><</a>`) 100 t.Assert(page.FirstPage(), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">|<</a>`) 101 t.Assert(page.LastPage(), `<a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="">>|</a>`) 102 t.Assert(page.PageBar(), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="1">1</a><span class="GPageSpan">2</span><a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="3">3</a><a class="GPageLink" href="javascript:LoadPage('/user/list/4')" title="4">4</a><a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="5">5</a>`) 103 }) 104 } 105 106 func Test_PredefinedContent(t *testing.T) { 107 gtest.C(t, func(t *gtest.T) { 108 page := gpage.New(5, 1, 2, `/user/list/{.page}`) 109 page.AjaxActionName = "LoadPage" 110 t.Assert(page.GetContent(1), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">上一页</a> <span class="current">2</span> <a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="">下一页</a>`) 111 t.Assert(page.GetContent(2), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">首页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title=""><<上一页</a><span class="current">[第2页]</span><a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="">下一页>></a><a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="">尾页</a>第<select name="GPageSelect" onchange="window.location.href=this.value"><option value="/user/list/1">1</option><option value="/user/list/2" selected>2</option><option value="/user/list/3">3</option><option value="/user/list/4">4</option><option value="/user/list/5">5</option></select>页`) 112 t.Assert(page.GetContent(3), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">首页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">上一页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="1">1</a><span class="GPageSpan">2</span><a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="3">3</a><a class="GPageLink" href="javascript:LoadPage('/user/list/4')" title="4">4</a><a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="5">5</a><a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="">下一页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="">尾页</a><span>当前页2/5</span> <span>共5条</span>`) 113 t.Assert(page.GetContent(4), `<a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">首页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="">上一页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/1')" title="1">1</a><span class="GPageSpan">2</span><a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="3">3</a><a class="GPageLink" href="javascript:LoadPage('/user/list/4')" title="4">4</a><a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="5">5</a><a class="GPageLink" href="javascript:LoadPage('/user/list/3')" title="">下一页</a><a class="GPageLink" href="javascript:LoadPage('/user/list/5')" title="">尾页</a>`) 114 t.Assert(page.GetContent(5), ``) 115 }) 116 }