...

Source file src/cuelang.org/go/pkg/path/example_nix_test.go

Documentation: cuelang.org/go/pkg/path

     1  // Copyright 2020 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain 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,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Copyright 2013 The Go Authors. All rights reserved.
    16  // Use of this source code is governed by a BSD-style
    17  // license that can be found in the LICENSE file.
    18  
    19  package path_test
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"cuelang.org/go/pkg/path"
    25  )
    26  
    27  func ExampleSplitList() {
    28  	fmt.Println("On Unix:", path.SplitList("/a/b/c:/usr/bin", path.Unix))
    29  	// Output:
    30  	// On Unix: [/a/b/c /usr/bin]
    31  }
    32  
    33  func ExampleRel() {
    34  	paths := []string{
    35  		"/a/b/c",
    36  		"/b/c",
    37  		"./b/c",
    38  	}
    39  	base := "/a"
    40  
    41  	fmt.Println("On Unix:")
    42  	for _, p := range paths {
    43  		rel, err := path.Rel(base, p, path.Unix)
    44  		fmt.Printf("%q: %q %v\n", p, rel, err)
    45  	}
    46  
    47  	// Output:
    48  	// On Unix:
    49  	// "/a/b/c": "b/c" <nil>
    50  	// "/b/c": "../b/c" <nil>
    51  	// "./b/c": "" Rel: can't make ./b/c relative to /a
    52  }
    53  
    54  func ExampleSplit() {
    55  	paths := []string{
    56  		"/home/arnie/amelia.jpg",
    57  		"/mnt/photos/",
    58  		"rabbit.jpg",
    59  		"/usr/local//go",
    60  	}
    61  	fmt.Println("On Unix:")
    62  	for _, p := range paths {
    63  		pair := path.Split(p, path.Unix)
    64  		fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, pair[0], pair[1])
    65  	}
    66  	// Output:
    67  	// On Unix:
    68  	// input: "/home/arnie/amelia.jpg"
    69  	// 	dir: "/home/arnie/"
    70  	// 	file: "amelia.jpg"
    71  	// input: "/mnt/photos/"
    72  	// 	dir: "/mnt/photos/"
    73  	// 	file: ""
    74  	// input: "rabbit.jpg"
    75  	// 	dir: ""
    76  	// 	file: "rabbit.jpg"
    77  	// input: "/usr/local//go"
    78  	// 	dir: "/usr/local//"
    79  	// 	file: "go"
    80  }
    81  
    82  func ExampleJoin() {
    83  	fmt.Println("On Unix:")
    84  	fmt.Println(path.Join([]string{"a", "b", "c"}, path.Unix))
    85  	fmt.Println(path.Join([]string{"a", "b/c"}, path.Unix))
    86  	fmt.Println(path.Join([]string{"a/b", "c"}, path.Unix))
    87  	fmt.Println(path.Join([]string{"a/b", "/c"}, path.Unix))
    88  
    89  	fmt.Println(path.Join([]string{"a/b", "../../../xyz"}, path.Unix))
    90  
    91  	// Output:
    92  	// On Unix:
    93  	// a/b/c
    94  	// a/b/c
    95  	// a/b/c
    96  	// a/b/c
    97  	// ../xyz
    98  }
    99  
   100  func ExampleMatch() {
   101  	fmt.Println("On Unix:")
   102  	fmt.Println(path.Match("/home/catch/*", "/home/catch/foo", path.Unix))
   103  	fmt.Println(path.Match("/home/catch/*", "/home/catch/foo/bar", path.Unix))
   104  	fmt.Println(path.Match("/home/?opher", "/home/gopher", path.Unix))
   105  	fmt.Println(path.Match("/home/\\*", "/home/*", path.Unix))
   106  
   107  	// Output:
   108  	// On Unix:
   109  	// true <nil>
   110  	// false <nil>
   111  	// true <nil>
   112  	// true <nil>
   113  }
   114  
   115  func ExampleBase() {
   116  	fmt.Println("On Unix:")
   117  	fmt.Println(path.Base("/foo/bar/baz.js", path.Unix))
   118  	fmt.Println(path.Base("/foo/bar/baz", path.Unix))
   119  	fmt.Println(path.Base("/foo/bar/baz/", path.Unix))
   120  	fmt.Println(path.Base("dev.txt", path.Unix))
   121  	fmt.Println(path.Base("../todo.txt", path.Unix))
   122  	fmt.Println(path.Base("..", path.Unix))
   123  	fmt.Println(path.Base(".", path.Unix))
   124  	fmt.Println(path.Base("/", path.Unix))
   125  	fmt.Println(path.Base("", path.Unix))
   126  
   127  	// Output:
   128  	// On Unix:
   129  	// baz.js
   130  	// baz
   131  	// baz
   132  	// dev.txt
   133  	// todo.txt
   134  	// ..
   135  	// .
   136  	// /
   137  	// .
   138  }
   139  
   140  func ExampleDir() {
   141  	fmt.Println("On Unix:")
   142  	fmt.Println(path.Dir("/foo/bar/baz.js", path.Unix))
   143  	fmt.Println(path.Dir("/foo/bar/baz", path.Unix))
   144  	fmt.Println(path.Dir("/foo/bar/baz/", path.Unix))
   145  	fmt.Println(path.Dir("/dirty//path///", path.Unix))
   146  	fmt.Println(path.Dir("dev.txt", path.Unix))
   147  	fmt.Println(path.Dir("../todo.txt", path.Unix))
   148  	fmt.Println(path.Dir("..", path.Unix))
   149  	fmt.Println(path.Dir(".", path.Unix))
   150  	fmt.Println(path.Dir("/", path.Unix))
   151  	fmt.Println(path.Dir("", path.Unix))
   152  
   153  	// Output:
   154  	// On Unix:
   155  	// /foo/bar
   156  	// /foo/bar
   157  	// /foo/bar/baz
   158  	// /dirty/path
   159  	// .
   160  	// ..
   161  	// .
   162  	// .
   163  	// /
   164  	// .
   165  }
   166  
   167  func ExampleIsAbs() {
   168  	fmt.Println("On Unix:")
   169  	fmt.Println(path.IsAbs("/home/gopher", path.Unix))
   170  	fmt.Println(path.IsAbs(".bashrc", path.Unix))
   171  	fmt.Println(path.IsAbs("..", path.Unix))
   172  	fmt.Println(path.IsAbs(".", path.Unix))
   173  	fmt.Println(path.IsAbs("/", path.Unix))
   174  	fmt.Println(path.IsAbs("", path.Unix))
   175  
   176  	// Output:
   177  	// On Unix:
   178  	// true
   179  	// false
   180  	// false
   181  	// false
   182  	// true
   183  	// false
   184  }
   185  

View as plain text