github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/example_test.go (about)

     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  
    15  package com_test
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/http"
    21  
    22  	"github.com/insionng/yougam/libraries/Unknwon/com"
    23  )
    24  
    25  // ------------------------------
    26  // cmd.go
    27  // ------------------------------
    28  
    29  func ExampleColorLogS() {
    30  	coloredLog := com.ColorLogS(fmt.Sprintf(
    31  		"[WARN] This is a tesing log that should be colored, path( %s ),"+
    32  			" highlight # %s #, error [ %s ].",
    33  		"path to somewhere", "highlighted content", "tesing error"))
    34  	fmt.Println(coloredLog)
    35  }
    36  
    37  func ExampleColorLog() {
    38  	com.ColorLog(fmt.Sprintf(
    39  		"[WARN] This is a tesing log that should be colored, path( %s ),"+
    40  			" highlight # %s #, error [ %s ].",
    41  		"path to somewhere", "highlighted content", "tesing error"))
    42  }
    43  
    44  func ExampleExecCmd() {
    45  	stdout, stderr, err := com.ExecCmd("go", "help", "get")
    46  	fmt.Println(stdout, stderr, err)
    47  }
    48  
    49  // ------------- END ------------
    50  
    51  // ------------------------------
    52  // html.go
    53  // ------------------------------
    54  
    55  func ExampleHtml2JS() {
    56  	htm := "<div id=\"button\" class=\"btn\">Click me</div>\n\r"
    57  	js := string(com.Html2JS([]byte(htm)))
    58  	fmt.Println(js)
    59  	// Output: <div id=\"button\" class=\"btn\">Click me</div>\n
    60  }
    61  
    62  // ------------- END ------------
    63  
    64  // ------------------------------
    65  // path.go
    66  // ------------------------------
    67  
    68  func ExampleGetGOPATHs() {
    69  	gps := com.GetGOPATHs()
    70  	fmt.Println(gps)
    71  }
    72  
    73  func ExampleGetSrcPath() {
    74  	srcPath, err := com.GetSrcPath("github.com/insionng/yougam/libraries/Unknwon/com")
    75  	if err != nil {
    76  		fmt.Println(err)
    77  		return
    78  	}
    79  	fmt.Println(srcPath)
    80  }
    81  
    82  func ExampleHomeDir() {
    83  	hd, err := com.HomeDir()
    84  	fmt.Println(hd, err)
    85  }
    86  
    87  // ------------- END ------------
    88  
    89  // ------------------------------
    90  // file.go
    91  // ------------------------------
    92  
    93  func ExampleIsFile() {
    94  	if com.IsFile("file.go") {
    95  		fmt.Println("file.go exists")
    96  		return
    97  	}
    98  	fmt.Println("file.go is not a file or does not exist")
    99  }
   100  
   101  func ExampleIsExist() {
   102  	if com.IsExist("file.go") {
   103  		fmt.Println("file.go exists")
   104  		return
   105  	}
   106  	fmt.Println("file.go does not exist")
   107  }
   108  
   109  // ------------- END ------------
   110  
   111  // ------------------------------
   112  // dir.go
   113  // ------------------------------
   114  
   115  func ExampleIsDir() {
   116  	if com.IsDir("files") {
   117  		fmt.Println("directory 'files' exists")
   118  		return
   119  	}
   120  	fmt.Println("'files' is not a directory or does not exist")
   121  }
   122  
   123  // ------------- END ------------
   124  
   125  // ------------------------------
   126  // string.go
   127  // ------------------------------
   128  
   129  func ExampleIsLetter() {
   130  	fmt.Println(com.IsLetter('1'))
   131  	fmt.Println(com.IsLetter('['))
   132  	fmt.Println(com.IsLetter('a'))
   133  	fmt.Println(com.IsLetter('Z'))
   134  	// Output:
   135  	// false
   136  	// false
   137  	// true
   138  	// true
   139  }
   140  
   141  func ExampleExpand() {
   142  	match := map[string]string{
   143  		"domain":    "gowalker.org",
   144  		"subdomain": "github.com",
   145  	}
   146  	s := "http://{domain}/{subdomain}/{0}/{1}"
   147  	fmt.Println(com.Expand(s, match, "Unknwon", "gowalker"))
   148  	// Output: http://gowalker.org/yougam/libraries/Unknwon/gowalker
   149  }
   150  
   151  // ------------- END ------------
   152  
   153  // ------------------------------
   154  // http.go
   155  // ------------------------------
   156  
   157  func ExampleHttpGet() ([]byte, error) {
   158  	rc, err := com.HttpGet(&http.Client{}, "http://gowalker.org", nil)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	p, err := ioutil.ReadAll(rc)
   163  	rc.Close()
   164  	return p, err
   165  }
   166  
   167  func ExampleHttpGetBytes() ([]byte, error) {
   168  	p, err := com.HttpGetBytes(&http.Client{}, "http://gowalker.org", nil)
   169  	return p, err
   170  }
   171  
   172  func ExampleHttpGetJSON() interface{} {
   173  	j := com.HttpGetJSON(&http.Client{}, "http://gowalker.org", nil)
   174  	return j
   175  }
   176  
   177  type rawFile struct {
   178  	name   string
   179  	rawURL string
   180  	data   []byte
   181  }
   182  
   183  func (rf *rawFile) Name() string {
   184  	return rf.name
   185  }
   186  
   187  func (rf *rawFile) RawUrl() string {
   188  	return rf.rawURL
   189  }
   190  
   191  func (rf *rawFile) Data() []byte {
   192  	return rf.data
   193  }
   194  
   195  func (rf *rawFile) SetData(p []byte) {
   196  	rf.data = p
   197  }
   198  
   199  func ExampleFetchFiles() {
   200  	// Code that should be outside of your function body.
   201  	// type rawFile struct {
   202  	// name   string
   203  	// 	rawURL string
   204  	// 	data   []byte
   205  	// }
   206  
   207  	// func (rf *rawFile) Name() string {
   208  	// 	return rf.name
   209  	// }
   210  
   211  	// func (rf *rawFile) RawUrl() string {
   212  	// 	return rf.rawURL
   213  	// }
   214  
   215  	// func (rf *rawFile) Data() []byte {
   216  	// 	return rf.data
   217  	// }
   218  
   219  	// func (rf *rawFile) SetData(p []byte) {
   220  	// 	rf.data = p
   221  	// }
   222  
   223  	files := []com.RawFile{
   224  		&rawFile{rawURL: "http://example.com"},
   225  		&rawFile{rawURL: "http://example.com/foo"},
   226  	}
   227  	err := com.FetchFiles(&http.Client{}, files, nil)
   228  	fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
   229  }
   230  
   231  func ExampleFetchFilesCurl() {
   232  	// Code that should be outside of your function body.
   233  	// type rawFile struct {
   234  	// name   string
   235  	// 	rawURL string
   236  	// 	data   []byte
   237  	// }
   238  
   239  	// func (rf *rawFile) Name() string {
   240  	// 	return rf.name
   241  	// }
   242  
   243  	// func (rf *rawFile) RawUrl() string {
   244  	// 	return rf.rawURL
   245  	// }
   246  
   247  	// func (rf *rawFile) Data() []byte {
   248  	// 	return rf.data
   249  	// }
   250  
   251  	// func (rf *rawFile) SetData(p []byte) {
   252  	// 	rf.data = p
   253  	// }
   254  
   255  	files := []com.RawFile{
   256  		&rawFile{rawURL: "http://example.com"},
   257  		&rawFile{rawURL: "http://example.com/foo"},
   258  	}
   259  	err := com.FetchFilesCurl(files)
   260  	fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
   261  }
   262  
   263  // ------------- END ------------
   264  
   265  // ------------------------------
   266  // regex.go
   267  // ------------------------------
   268  
   269  func ExampleIsEmail() {
   270  	fmt.Println(com.IsEmail("test@example.com"))
   271  	fmt.Println(com.IsEmail("@example.com"))
   272  	// Output:
   273  	// true
   274  	// false
   275  }
   276  
   277  func ExampleIsUrl() {
   278  	fmt.Println(com.IsUrl("http://example.com"))
   279  	fmt.Println(com.IsUrl("http//example.com"))
   280  	// Output:
   281  	// true
   282  	// false
   283  }
   284  
   285  // ------------- END ------------
   286  
   287  // ------------------------------
   288  // slice.go
   289  // ------------------------------
   290  
   291  func ExampleAppendStr() {
   292  	s := []string{"a"}
   293  	s = com.AppendStr(s, "a")
   294  	s = com.AppendStr(s, "b")
   295  	fmt.Println(s)
   296  	// Output: [a b]
   297  }
   298  
   299  // ------------- END ------------