github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/blogweb_gin/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"github.com/PuerkitoBio/goquery"
     8  	"github.com/bmizerany/assert"
     9  	"github.com/russross/blackfriday"
    10  	"github.com/sourcegraph/syntaxhighlight"
    11  	"regexp"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestSwitchTimeStampToData(t *testing.T) {
    17  	now := time.Now()
    18  	t.Log(SwitchTimeStampToData(now.Unix()))
    19  	tim, _ := time.ParseInLocation("2006-01-02 15:04:05", "2016-06-13 15:30:00", time.Local)
    20  
    21  	t.Log(tim.Round(1 * time.Hour)) //接近,四舍五入的方式
    22  	t.Log(tim.Truncate(1 * time.Hour))
    23  	t.Log(int(now.Weekday()))
    24  	t.Log(int(now.Month()))
    25  	t.Log(now.Day()) // dayofmouth
    26  	t.Log(now.YearDay())
    27  	t.Log(now.ISOWeek())
    28  }
    29  
    30  func TestMdToHtml(t *testing.T) {
    31  	mdStr := `
    32  	## 一、russross/blackfriday包`
    33  	markdown := blackfriday.MarkdownCommon([]byte(mdStr))
    34  	doc, _ := goquery.NewDocumentFromReader(bufio.NewReader(bytes.NewReader(markdown)))
    35  	htmlString, _ := doc.Html()
    36  	t.Log(htmlString)
    37  }
    38  
    39  func TestGoQuery(t *testing.T) {
    40  
    41  	doc, err := goquery.NewDocument("http://studygolang.com/topics") // goquery 用于解析html
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	doc.Find(".topic").Each(func(i int, contentSelection *goquery.Selection) {
    47  		title := contentSelection.Find(".title a").Text()
    48  		//Find(".title a")与Find(".title").Find("a")一样
    49  		fmt.Println("第", i+1, "个帖子的标题:", title)
    50  		//ret,_ := contentSelection.Html()
    51  		//fmt.Printf("\n\n\n%v", ret)
    52  		//fmt.Println(contentSelection.Text())
    53  	})
    54  	//最终输出为 html 文档:
    55  	//new, err := doc.Html()
    56  }
    57  
    58  // 语法高亮
    59  func TestSyntaxhighlight(t *testing.T) {
    60  	src := []byte(`
    61  /* hello, world! */
    62  var a = 3;
    63  
    64  // b is a cool function
    65  function b() {
    66    return 7;
    67  }`)
    68  
    69  	highlighted, err := syntaxhighlight.AsHTML(src)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	t.Log(string(highlighted))
    74  }
    75  
    76  func TestRe(t *testing.T) {
    77  	re := regexp.MustCompile(`\?`)
    78  	s := re.ReplaceAllString("inset into album(filepath,filename,status,createtime)values(?,?,?,?)", "%v")
    79  	assert.Equal(t, `inset into album(filepath,filename,status,createtime)values(%v,%v,%v,%v)`, s)
    80  }