github.com/taloric/strfmt@v0.1.6/strfmt_test.go (about) 1 package strfmt 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 "time" 8 ) 9 10 const ( 11 format_student = "[{Id}]-Student Info:name-[{Name}],gender-[{Gender}],age-[{Age}],birthday-[{BirthDay}],is graduate-[{IsGraduate}]" 12 format_people = "[{Id}]-People Info:name-[{Name}],gender-[{Gender}],age-[{Age}],birthday-[{BirthDay:2006/01/02}]" 13 14 format_today = "Today is a {0} day" 15 format_today_rightpad = "Today is a {0,-20} day" 16 format_today_leftpad = "Today is a {0,20} day" 17 format_today_info = "Today is {NotMapTest1} {DayofWeek} {NotMap_Test} {NotMap}" 18 format_date = "Today is {year}-{month}-{day},current time is {hour}-{minute}-{seconds}" 19 20 format_error_only_left_brace = "Today is { Day" 21 format_error_without_condition = "Today is {} Day" 22 format_error_only_right_brace = "Today is } Day" 23 format_error_without_complete_format = "Today is {0, Day" 24 format_error_index_out_of_range = "Today is {1} Day" 25 26 format_time_normal = "Current Time is {0:2006-01-02 15:04:05 Mon}" 27 format_time_short = "Current Time is {0:3:04PM}" 28 format_time_map = "Current Time is {day:2006-01-02 15:04:05 Mon}" 29 30 //maybe should support yyyy-mm format?(convert it to golang time format) 31 format_time_error_format = "Current Time is {0:yyyy-mm}" 32 format_time_error_format_lost = "Current Time is {0:}" 33 format_time_error_format_wrong = "Current Time is {0:2003-02-02}" 34 ) 35 36 type People struct { 37 Id string 38 Name string 39 Gender string 40 Age int 41 BirthDay time.Time 42 } 43 44 type Student struct { 45 Info *People 46 IsGraduate bool 47 } 48 49 var g_student *Student 50 var g_people *People 51 52 func TestMain(m *testing.M) { 53 g_people = &People{ 54 Id: strconv.FormatInt(time.Now().Unix(), 10), 55 Name: "Mary", 56 Gender: "Women", 57 Age: 43, 58 BirthDay: time.Date(1978, 1, 1, 1, 1, 1, 1, time.Local), 59 } 60 61 g_student = &Student{ 62 Info: &People{ 63 Id: strconv.FormatInt(time.Now().Unix(), 10), 64 Name: "Donald", 65 Gender: "Man", 66 Age: 51, 67 BirthDay: time.Date(1970, 1, 1, 1, 1, 1, 1, time.Local), 68 }, 69 IsGraduate: true, 70 } 71 fmt.Println("----Test Main Begin----") 72 m.Run() 73 fmt.Println("----Test Main End----") 74 } 75 76 func Test_Format(t *testing.T) { 77 res, err := Format(format_today, "wonderful") 78 if err != nil { 79 t.Error("test_format throw error " + err.Error()) 80 } 81 fmt.Println(res) 82 83 res, err = Format(format_today, "bad") 84 if err != nil { 85 t.Error("Test_format throw error " + err.Error()) 86 } 87 fmt.Println(res) 88 89 res, err = Format(format_today_rightpad, "wonderful") 90 if err != nil { 91 t.Error("Test_format throw error " + err.Error()) 92 } 93 fmt.Println(res) 94 95 res, err = Format(format_today_leftpad, "wonderful") 96 if err != nil { 97 t.Error("Test_format throw error " + err.Error()) 98 } 99 fmt.Println(res) 100 } 101 102 func Test_FormatMap(t *testing.T) { 103 args := make(map[string]string) 104 args["DayofWeek"] = time.Now().Weekday().String() 105 res, err := FormatMap(format_today_info, &args) 106 if err != nil { 107 t.Error("Test_FormatMap throw error " + err.Error()) 108 } 109 fmt.Println(res) 110 } 111 112 func Test_FormatMap_2(t *testing.T) { 113 args := make(map[string]string) 114 time_now := time.Now() 115 args["year"] = strconv.FormatInt(int64(time_now.Year()), 10) 116 args["month"] = strconv.FormatInt(int64(time_now.Month()), 10) 117 args["day"] = strconv.FormatInt(int64(time_now.Day()), 10) 118 args["hour"] = strconv.FormatInt(int64(time_now.Hour()), 10) 119 args["minute"] = strconv.FormatInt(int64(time_now.Minute()), 10) 120 args["seconds"] = strconv.FormatInt(int64(time_now.Second()), 10) 121 122 res, err := FormatMap(format_date, &args) 123 if err != nil { 124 t.Error("Test_FormatMap_2 throw error " + err.Error()) 125 } 126 fmt.Println(res) 127 } 128 129 func Test_FormatMap_3(t *testing.T) { 130 args := make(map[string]string) 131 str := `【{UserName}】修改工单评论,评论如下: 132 ------------------------------------------------------------- 133 {} 134 ------------------------------------------------------------- 135 工单链接:{OrderDetailUrl}` 136 args["UserName"] = "123" 137 args["OrderDetailUrl"] = "456" 138 res, err := FormatMap(str, &args) 139 fmt.Println(res) 140 fmt.Println(err) 141 } 142 143 func Test_FormatData(t *testing.T) { 144 res, err := FormatData(format_people, g_people) 145 if err != nil { 146 t.Error("Test_FormatData throw error " + err.Error()) 147 } 148 fmt.Println(res) 149 } 150 151 func Test_format_sub_data(t *testing.T) { 152 res, err := FormatData(format_student, g_student) 153 if err != nil { 154 t.Error("Test_format_sub_data throw error " + err.Error()) 155 } 156 fmt.Println(res) 157 } 158 159 func Test_format_error(t *testing.T) { 160 161 fmt_data := "wonderful" 162 _, err := Format(format_error_only_left_brace, fmt_data) 163 if err != nil { 164 t.Error("Test_format_error [format_error_only_left_brace] should not throw error ") 165 t.FailNow() 166 } 167 fmt.Println("Test_format_error [format_error_only_left_brace] not throwing error") 168 169 _, err = Format(format_error_without_condition, fmt_data) 170 if err != nil { 171 t.Error("Test_format_error [format_error_without_condition] should not throw error ") 172 t.FailNow() 173 } 174 fmt.Println("Test_format_error [format_error_without_condition] not throwing error") 175 176 _, err = Format(format_error_only_right_brace, fmt_data) 177 if err != nil { 178 t.Error("Test_format_error [format_error_only_right_brace] should not throw error ") 179 t.FailNow() 180 } 181 fmt.Println("Test_format_error [format_error_only_right_brace] not throwing error") 182 183 _, err = Format(format_error_without_complete_format, fmt_data) 184 if err == nil { 185 t.Error("Test_format_error [format_error_without_complete_format] should throw error ") 186 t.FailNow() 187 } 188 fmt.Println("Test_format_error [format_error_without_complete_format] throw error", err.Error()) 189 190 _, err = Format(format_error_index_out_of_range, fmt_data) 191 if err == nil { 192 t.Error("Test_format_error [format_error_index_out_of_range] should throw error ") 193 t.FailNow() 194 } 195 fmt.Println("Test_format_error [format_error_index_out_of_range] throw error", err.Error()) 196 } 197 198 func Test_FormatTime(t *testing.T) { 199 current_time := time.Now() 200 res, err := Format(format_time_normal, current_time.Format(time.RFC1123Z)) 201 if err != nil { 202 t.Error("Test_FormatTime throw error " + err.Error()) 203 } 204 fmt.Println(res) 205 206 res, err = Format(format_time_short, current_time.Format(time.RFC1123Z)) 207 if err != nil { 208 t.Error("Test_FormatTime throw error " + err.Error()) 209 } 210 fmt.Println(res) 211 212 time_map := make(map[string]string) 213 time_map["day"] = current_time.Format(time.RFC1123Z) 214 res, err = FormatMap(format_time_map, &time_map) 215 if err != nil { 216 t.Error("Test_FormatTime throw error " + err.Error()) 217 } 218 fmt.Println(res) 219 } 220 221 func Test_FormatTimeError(t *testing.T) { 222 //todo: should be able to recognize any format of time 223 current_time := time.Now() 224 res, err := Format(format_time_error_format, current_time.Format(time.RFC1123Z)) 225 if err != nil { 226 t.Error("Test_FormatTimeError throw error " + err.Error()) 227 } 228 fmt.Println(res) 229 230 res, err = Format(format_time_error_format_lost, current_time.Format(time.RFC1123Z)) 231 if err != nil { 232 t.Error("Test_FormatTimeError throw error " + err.Error()) 233 } 234 fmt.Println(res) 235 236 res, err = Format(format_time_error_format_wrong, current_time.Format(time.RFC1123Z)) 237 if err != nil { 238 t.Error("Test_FormatTimeError throw error " + err.Error()) 239 } 240 fmt.Println(res) 241 } 242 243 //------------------------------------------// 244 // Benchmark Test Below // 245 //------------------------------------------// 246 247 func Benchmark_Format(b *testing.B) { 248 _, err := Format(format_today, "wonderful") 249 if err != nil { 250 b.Error("test_format throw error " + err.Error()) 251 return 252 } 253 //fmt.Println(res) 254 } 255 256 func Benchmark_FormatMap(b *testing.B) { 257 args := make(map[string]string) 258 time_now := time.Now() 259 args["year"] = strconv.FormatInt(int64(time_now.Year()), 10) 260 args["month"] = strconv.FormatInt(int64(time_now.Month()), 10) 261 args["day"] = strconv.FormatInt(int64(time_now.Day()), 10) 262 args["hour"] = strconv.FormatInt(int64(time_now.Hour()), 10) 263 args["minute"] = strconv.FormatInt(int64(time_now.Minute()), 10) 264 args["seconds"] = strconv.FormatInt(int64(time_now.Second()), 10) 265 266 _, err := FormatMap(format_date, &args) 267 if err != nil { 268 b.Error("Test_FormatMap_2 throw error " + err.Error()) 269 } 270 } 271 272 func Benchmark_FormatData(b *testing.B) { 273 _, err := FormatData(format_people, g_people) 274 if err != nil { 275 b.Error("Test_FormatData throw error " + err.Error()) 276 } 277 }