From 10e8466568b2b204b9500d5db65f339f8b92ef2c Mon Sep 17 00:00:00 2001 From: gloomyzerg Date: Thu, 27 Feb 2020 10:10:23 +0800 Subject: [PATCH] test(publish_time): add test --- publish.go => publish_time.go | 25 +++++++++--- publish_time_test.go | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 6 deletions(-) rename publish.go => publish_time.go (79%) create mode 100644 publish_time_test.go diff --git a/publish.go b/publish_time.go similarity index 79% rename from publish.go rename to publish_time.go index 66ab6ab..5fb423f 100644 --- a/publish.go +++ b/publish_time.go @@ -25,6 +25,11 @@ var timePattern []string = []string{ `(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9])`, `(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9])`, `(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`, + `(\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`, + `(\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`, + `(\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9])`, + `(\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9])`, + `(\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`, `(\d{4}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`, `(\d{4}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`, `(\d{4}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9])`, @@ -42,6 +47,7 @@ var timePattern []string = []string{ `(\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`, `(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2})`, `(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2})`, + `(\d{1,2}[-|/|.]\d{1,2})`, `(\d{4}年\d{1,2}月\d{1,2}日)`, `(\d{2}年\d{1,2}月\d{1,2}日)`, `(\d{1,2}月\d{1,2}日)`, @@ -60,13 +66,20 @@ func timeExtract(body *goquery.Selection) string { } } for _, t := range text { - for _, v := range timePattern { - ok, err := regexp.MatchString(v, t) - if err == nil && ok { - re, _ := regexp.Compile(v) - return re.FindString(t) - } + if timeVal, ok := matchTime(t); ok { + return timeVal } } return "" } + +func matchTime(text string) (string, bool) { + for _, v := range timePattern { + ok, err := regexp.MatchString(v, text) + if err == nil && ok { + re, _ := regexp.Compile(v) + return re.FindString(text), true + } + } + return "", false +} diff --git a/publish_time_test.go b/publish_time_test.go new file mode 100644 index 0000000..8d191b8 --- /dev/null +++ b/publish_time_test.go @@ -0,0 +1,71 @@ +package textractor + +import "testing" + +func Test_matchTime(t *testing.T) { + tests := []struct { + text string + want string + want1 bool + }{ + { + text: "2020年02月27日 09:00", + want: "2020年02月27日 09:00", + want1: true, + }, + { + text: "2020年02月27日", + want: "2020年02月27日", + want1: true, + }, + { + text: "2020年02月27日 09:00:00", + want: "2020年02月27日 09:00:00", + want1: true, + }, + { + text: "2020-02-27 09:00:00", + want: "2020-02-27 09:00:00", + want1: true, + }, + { + text: "2020-02-27 09:00", + want: "2020-02-27 09:00", + want1: true, + }, + { + text: "2020-02-27", + want: "2020-02-27", + want1: true, + }, + { + text: "test 2020-02-27 test", + want: "2020-02-27", + want1: true, + }, + { + text: "2020-2-2 09:00", + want: "2020-2-2 09:00", + want1: true, + }, + { + text: "2-27 09:00", + want: "2-27 09:00", + want1: true, + }, + { + text: "2-27", + want: "2-27", + want1: true, + }, + } + for _, tt := range tests { + got, got1 := matchTime(tt.text) + if got != tt.want { + t.Errorf("matchTime() got = %v, want %v, input %v", got, tt.want, tt.text) + } + if got1 != tt.want1 { + t.Errorf("matchTime() got1 = %v, want %v, input %v", got1, tt.want1, tt.text) + } + } +}