Add utils test
Showing
1 changed file
with
158 additions
and
0 deletions
| 1 | package beego | 1 | package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "html/template" | ||
| 4 | "net/url" | 5 | "net/url" |
| 5 | "testing" | 6 | "testing" |
| 7 | "time" | ||
| 6 | ) | 8 | ) |
| 7 | 9 | ||
| 10 | func TestWebTime(t *testing.T) { | ||
| 11 | ts := "Fri, 26 Jul 2013 12:27:42 CST" | ||
| 12 | tt, _ := time.Parse(time.RFC1123, ts) | ||
| 13 | if ts != webTime(tt) { | ||
| 14 | t.Error("should be equal") | ||
| 15 | } | ||
| 16 | if "Fri, 26 Jul 2013 04:27:42 GMT" != webTime(tt.UTC()) { | ||
| 17 | t.Error("should be equal") | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | func TestMarkDown(t *testing.T) { | ||
| 22 | raw := `## beego | ||
| 23 | |||
| 24 | [](https://drone.io/github.com/astaxie/beego/latest) | ||
| 25 | |||
| 26 | beego is a Go Framework which is inspired from tornado and sinatra. | ||
| 27 | |||
| 28 | It is a simply & powerful web framework. | ||
| 29 | |||
| 30 | |||
| 31 | ## Features | ||
| 32 | |||
| 33 | * RESTFul support | ||
| 34 | |||
| 35 | ## Documentation | ||
| 36 | |||
| 37 | [English](https://github.com/astaxie/beego/tree/master/docs/en) | ||
| 38 | |||
| 39 | ## LICENSE | ||
| 40 | |||
| 41 | beego is licensed under the Apache Licence, Version 2.0 | ||
| 42 | (http://www.apache.org/licenses/LICENSE-2.0.html). | ||
| 43 | |||
| 44 | |||
| 45 | ## Use case | ||
| 46 | |||
| 47 | - Displaying API documentation: [gowalker](https://github.com/Unknwon/gowalker) | ||
| 48 | ` | ||
| 49 | output := `<h2>beego</h2> | ||
| 50 | |||
| 51 | <p><a href="https://drone.io/github.com/astaxie/beego/latest"></a></p> | ||
| 52 | |||
| 53 | <p>beego is a Go Framework which is inspired from tornado and sinatra.</p> | ||
| 54 | |||
| 55 | <p>It is a simply & powerful web framework.</p> | ||
| 56 | |||
| 57 | <h2>Features</h2> | ||
| 58 | |||
| 59 | <ul> | ||
| 60 | <li>RESTFul support</li> | ||
| 61 | </ul> | ||
| 62 | |||
| 63 | <h2>Documentation</h2> | ||
| 64 | |||
| 65 | <p><a href="https://github.com/astaxie/beego/tree/master/docs/en">English</a></p> | ||
| 66 | |||
| 67 | <h2>LICENSE</h2> | ||
| 68 | |||
| 69 | <p>beego is licensed under the Apache Licence, Version 2.0 | ||
| 70 | (http://www.apache.org/licenses/LICENSE-2.0.html).</p> | ||
| 71 | |||
| 72 | <h2>Use case</h2> | ||
| 73 | |||
| 74 | <ul> | ||
| 75 | <li>Displaying API documentation: <a href="https://github.com/Unknwon/gowalker">gowalker</a></li> | ||
| 76 | </ul> | ||
| 77 | ` | ||
| 78 | if MarkDown(raw) != template.HTML(output) { | ||
| 79 | t.Error("should be equal") | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | func TestSubstr(t *testing.T) { | ||
| 84 | s := `012345` | ||
| 85 | if Substr(s, 0, 2) != "01" { | ||
| 86 | t.Error("should be equal") | ||
| 87 | } | ||
| 88 | if Substr(s, 0, 100) != "012345" { | ||
| 89 | t.Error("should be equal") | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | func TestHtml2str(t *testing.T) { | ||
| 94 | h := `<HTML><style></style><script>x<x</script></HTML><123> 123\n | ||
| 95 | |||
| 96 | |||
| 97 | \n` | ||
| 98 | if Html2str(h) != "123\\n\n\\n" { | ||
| 99 | t.Error("should be equal") | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | func TestDateFormat(t *testing.T) { | ||
| 104 | ts := "Mon, 01 Jul 2013 13:27:42 CST" | ||
| 105 | tt, _ := time.Parse(time.RFC1123, ts) | ||
| 106 | if DateFormat(tt, "2006-01-02 15:04:05") != "2013-07-01 13:27:42" { | ||
| 107 | t.Error("should be equal") | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | func TestDate(t *testing.T) { | ||
| 112 | ts := "Mon, 01 Jul 2013 13:27:42 CST" | ||
| 113 | tt, _ := time.Parse(time.RFC1123, ts) | ||
| 114 | if Date(tt, "Y-m-d H:i:s") != "2013-07-01 13:27:42" { | ||
| 115 | t.Error("should be equal") | ||
| 116 | } | ||
| 117 | if Date(tt, "y-n-j h:i:s A") != "13-7-1 01:27:42 PM" { | ||
| 118 | t.Error("should be equal") | ||
| 119 | } | ||
| 120 | if Date(tt, "D, d M Y g:i:s a") != "Mon, 01 Jul 2013 1:27:42 pm" { | ||
| 121 | t.Error("should be equal") | ||
| 122 | } | ||
| 123 | if Date(tt, "l, d F Y G:i:s") != "Monday, 01 July 2013 13:27:42" { | ||
| 124 | t.Error("should be equal") | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | func TestCompare(t *testing.T) { | ||
| 129 | if !Compare("abc", "abc") { | ||
| 130 | t.Error("should be equal") | ||
| 131 | } | ||
| 132 | if Compare("abc", "aBc") { | ||
| 133 | t.Error("should be not equal") | ||
| 134 | } | ||
| 135 | if !Compare("1", 1) { | ||
| 136 | t.Error("should be equal") | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | func TestHtmlquote(t *testing.T) { | ||
| 141 | h := `<' ”“&">` | ||
| 142 | s := `<' ”“&">` | ||
| 143 | if Htmlquote(s) != h { | ||
| 144 | t.Error("should be equal") | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | func TestHtmlunquote(t *testing.T) { | ||
| 149 | h := `<' ”“&">` | ||
| 150 | s := `<' ”“&">` | ||
| 151 | if Htmlunquote(h) != s { | ||
| 152 | t.Error("should be equal") | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | func TestInSlice(t *testing.T) { | ||
| 157 | sl := []string{"A", "b"} | ||
| 158 | if !inSlice("A", sl) { | ||
| 159 | t.Error("should be true") | ||
| 160 | } | ||
| 161 | if inSlice("B", sl) { | ||
| 162 | t.Error("should be false") | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 8 | func TestParseForm(t *testing.T) { | 166 | func TestParseForm(t *testing.T) { |
| 9 | type user struct { | 167 | type user struct { |
| 10 | Id int | 168 | Id int | ... | ... |
-
Please register or sign in to post a comment