example_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2013 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com_test
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "notabug.org/makenotabuggreatagain/com"
  20. )
  21. // ------------------------------
  22. // cmd.go
  23. // ------------------------------
  24. func ExampleColorLogS() {
  25. coloredLog := com.ColorLogS(fmt.Sprintf(
  26. "[WARN] This is a tesing log that should be colored, path( %s ),"+
  27. " highlight # %s #, error [ %s ].",
  28. "path to somewhere", "highlighted content", "tesing error"))
  29. fmt.Println(coloredLog)
  30. }
  31. func ExampleColorLog() {
  32. com.ColorLog(fmt.Sprintf(
  33. "[WARN] This is a tesing log that should be colored, path( %s ),"+
  34. " highlight # %s #, error [ %s ].",
  35. "path to somewhere", "highlighted content", "tesing error"))
  36. }
  37. func ExampleExecCmd() {
  38. stdout, stderr, err := com.ExecCmd("go", "help", "get")
  39. fmt.Println(stdout, stderr, err)
  40. }
  41. // ------------- END ------------
  42. // ------------------------------
  43. // html.go
  44. // ------------------------------
  45. func ExampleHtml2JS() {
  46. htm := "<div id=\"button\" class=\"btn\">Click me</div>\n\r"
  47. js := string(com.Html2JS([]byte(htm)))
  48. fmt.Println(js)
  49. // Output: <div id=\"button\" class=\"btn\">Click me</div>\n
  50. }
  51. // ------------- END ------------
  52. // ------------------------------
  53. // path.go
  54. // ------------------------------
  55. func ExampleGetGOPATHs() {
  56. gps := com.GetGOPATHs()
  57. fmt.Println(gps)
  58. }
  59. func ExampleGetSrcPath() {
  60. srcPath, err := com.GetSrcPath("notabug.org/makenotabuggreatagain/com")
  61. if err != nil {
  62. fmt.Println(err)
  63. return
  64. }
  65. fmt.Println(srcPath)
  66. }
  67. func ExampleHomeDir() {
  68. hd, err := com.HomeDir()
  69. fmt.Println(hd, err)
  70. }
  71. // ------------- END ------------
  72. // ------------------------------
  73. // file.go
  74. // ------------------------------
  75. func ExampleIsFile() {
  76. if com.IsFile("file.go") {
  77. fmt.Println("file.go exists")
  78. return
  79. }
  80. fmt.Println("file.go is not a file or does not exist")
  81. }
  82. func ExampleIsExist() {
  83. if com.IsExist("file.go") {
  84. fmt.Println("file.go exists")
  85. return
  86. }
  87. fmt.Println("file.go does not exist")
  88. }
  89. // ------------- END ------------
  90. // ------------------------------
  91. // dir.go
  92. // ------------------------------
  93. func ExampleIsDir() {
  94. if com.IsDir("files") {
  95. fmt.Println("directory 'files' exists")
  96. return
  97. }
  98. fmt.Println("'files' is not a directory or does not exist")
  99. }
  100. // ------------- END ------------
  101. // ------------------------------
  102. // string.go
  103. // ------------------------------
  104. func ExampleIsLetter() {
  105. fmt.Println(com.IsLetter('1'))
  106. fmt.Println(com.IsLetter('['))
  107. fmt.Println(com.IsLetter('a'))
  108. fmt.Println(com.IsLetter('Z'))
  109. // Output:
  110. // false
  111. // false
  112. // true
  113. // true
  114. }
  115. //func ExampleExpand() {
  116. // match := map[string]string{
  117. // "domain": "gowalker.org",
  118. // "subdomain": "github.com",
  119. // }
  120. // s := "http://{domain}/{subdomain}/{0}/{1}"
  121. // fmt.Println(com.Expand(s, match, "Unknwon", "gowalker"))
  122. // Output: http://gowalker.org/notabug.org/makenotabuggreatagain/gowalker
  123. //}
  124. // ------------- END ------------
  125. // ------------------------------
  126. // http.go
  127. // ------------------------------
  128. func ExampleHttpGet() ([]byte, error) {
  129. rc, err := com.HttpGet(&http.Client{}, "http://gowalker.org", nil)
  130. if err != nil {
  131. return nil, err
  132. }
  133. p, err := ioutil.ReadAll(rc)
  134. rc.Close()
  135. return p, err
  136. }
  137. func ExampleHttpGetBytes() ([]byte, error) {
  138. p, err := com.HttpGetBytes(&http.Client{}, "http://gowalker.org", nil)
  139. return p, err
  140. }
  141. func ExampleHttpGetJSON() interface{} {
  142. j := com.HttpGetJSON(&http.Client{}, "http://gowalker.org", nil)
  143. return j
  144. }
  145. type rawFile struct {
  146. name string
  147. rawURL string
  148. data []byte
  149. }
  150. func (rf *rawFile) Name() string {
  151. return rf.name
  152. }
  153. func (rf *rawFile) RawUrl() string {
  154. return rf.rawURL
  155. }
  156. func (rf *rawFile) Data() []byte {
  157. return rf.data
  158. }
  159. func (rf *rawFile) SetData(p []byte) {
  160. rf.data = p
  161. }
  162. func ExampleFetchFiles() {
  163. // Code that should be outside of your function body.
  164. // type rawFile struct {
  165. // name string
  166. // rawURL string
  167. // data []byte
  168. // }
  169. // func (rf *rawFile) Name() string {
  170. // return rf.name
  171. // }
  172. // func (rf *rawFile) RawUrl() string {
  173. // return rf.rawURL
  174. // }
  175. // func (rf *rawFile) Data() []byte {
  176. // return rf.data
  177. // }
  178. // func (rf *rawFile) SetData(p []byte) {
  179. // rf.data = p
  180. // }
  181. files := []com.RawFile{
  182. &rawFile{rawURL: "http://example.com"},
  183. &rawFile{rawURL: "http://example.com/foo"},
  184. }
  185. err := com.FetchFiles(&http.Client{}, files, nil)
  186. fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
  187. }
  188. func ExampleFetchFilesCurl() {
  189. // Code that should be outside of your function body.
  190. // type rawFile struct {
  191. // name string
  192. // rawURL string
  193. // data []byte
  194. // }
  195. // func (rf *rawFile) Name() string {
  196. // return rf.name
  197. // }
  198. // func (rf *rawFile) RawUrl() string {
  199. // return rf.rawURL
  200. // }
  201. // func (rf *rawFile) Data() []byte {
  202. // return rf.data
  203. // }
  204. // func (rf *rawFile) SetData(p []byte) {
  205. // rf.data = p
  206. // }
  207. files := []com.RawFile{
  208. &rawFile{rawURL: "http://example.com"},
  209. &rawFile{rawURL: "http://example.com/foo"},
  210. }
  211. err := com.FetchFilesCurl(files)
  212. fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
  213. }
  214. // ------------- END ------------
  215. // ------------------------------
  216. // regex.go
  217. // ------------------------------
  218. func ExampleIsEmail() {
  219. fmt.Println(com.IsEmail("test@example.com"))
  220. fmt.Println(com.IsEmail("@example.com"))
  221. // Output:
  222. // true
  223. // false
  224. }
  225. func ExampleIsUrl() {
  226. fmt.Println(com.IsUrl("http://example.com"))
  227. fmt.Println(com.IsUrl("http//example.com"))
  228. // Output:
  229. // true
  230. // false
  231. }
  232. // ------------- END ------------
  233. // ------------------------------
  234. // slice.go
  235. // ------------------------------
  236. func ExampleAppendStr() {
  237. s := []string{"a"}
  238. s = com.AppendStr(s, "a")
  239. s = com.AppendStr(s, "b")
  240. fmt.Println(s)
  241. // Output: [a b]
  242. }
  243. // ------------- END ------------