github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/os/env_test.go (about)

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os_test
     6  
     7  import (
     8  	. "os"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  // testGetenv gives us a controlled set of variables for testing Expand.
    14  func testGetenv(s string) string {
    15  	switch s {
    16  	case "*":
    17  		return "all the args"
    18  	case "#":
    19  		return "NARGS"
    20  	case "$":
    21  		return "PID"
    22  	case "1":
    23  		return "ARGUMENT1"
    24  	case "HOME":
    25  		return "/usr/gopher"
    26  	case "H":
    27  		return "(Value of H)"
    28  	case "home_1":
    29  		return "/usr/foo"
    30  	case "_":
    31  		return "underscore"
    32  	}
    33  	return ""
    34  }
    35  
    36  var expandTests = []struct {
    37  	in, out string
    38  }{
    39  	{"", ""},
    40  	{"$*", "all the args"},
    41  	{"$$", "PID"},
    42  	{"${*}", "all the args"},
    43  	{"$1", "ARGUMENT1"},
    44  	{"${1}", "ARGUMENT1"},
    45  	{"now is the time", "now is the time"},
    46  	{"$HOME", "/usr/gopher"},
    47  	{"$home_1", "/usr/foo"},
    48  	{"${HOME}", "/usr/gopher"},
    49  	{"${H}OME", "(Value of H)OME"},
    50  	{"A$$$#$1$H$home_1*B", "APIDNARGSARGUMENT1(Value of H)/usr/foo*B"},
    51  }
    52  
    53  func TestExpand(t *testing.T) {
    54  	for _, test := range expandTests {
    55  		result := Expand(test.in, testGetenv)
    56  		if result != test.out {
    57  			t.Errorf("Expand(%q)=%q; expected %q", test.in, result, test.out)
    58  		}
    59  	}
    60  }
    61  
    62  func TestConsistentEnviron(t *testing.T) {
    63  	e0 := Environ()
    64  	for i := 0; i < 10; i++ {
    65  		e1 := Environ()
    66  		if !reflect.DeepEqual(e0, e1) {
    67  			t.Fatalf("environment changed")
    68  		}
    69  	}
    70  }