github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/env/env_test.go (about)

     1  package env
     2  
     3  import (
     4  	"net/url"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestInt(t *testing.T) {
    12  	result := Int("nonexistent", 15)
    13  	Parse()
    14  
    15  	if *result != 15 {
    16  		t.Fatalf("expected result=15, got result=%d", *result)
    17  	}
    18  
    19  	err := os.Setenv("int-key", "25")
    20  	if err != nil {
    21  		t.Fatal("unexpected error", err)
    22  	}
    23  
    24  	result = Int("int-key", 15)
    25  	Parse()
    26  
    27  	if *result != 25 {
    28  		t.Fatalf("expected result=25, got result=%d", *result)
    29  	}
    30  }
    31  
    32  func TestIntVar(t *testing.T) {
    33  	var result int
    34  	IntVar(&result, "nonexistent", 15)
    35  	Parse()
    36  
    37  	if result != 15 {
    38  		t.Fatalf("expected result=15, got result=%d", result)
    39  	}
    40  
    41  	err := os.Setenv("int-key", "25")
    42  	if err != nil {
    43  		t.Fatal("unexpected error", err)
    44  	}
    45  
    46  	IntVar(&result, "int-key", 15)
    47  	Parse()
    48  
    49  	if result != 25 {
    50  		t.Fatalf("expected result=25, got result=%d", result)
    51  	}
    52  }
    53  
    54  func TestBool(t *testing.T) {
    55  	result := Bool("nonexistent", true)
    56  	Parse()
    57  
    58  	if *result != true {
    59  		t.Fatalf("expected result=true, got result=%t", *result)
    60  	}
    61  
    62  	err := os.Setenv("bool-key", "true")
    63  	if err != nil {
    64  		t.Fatal("unexpected error", err)
    65  	}
    66  
    67  	result = Bool("bool-key", false)
    68  	Parse()
    69  
    70  	if *result != true {
    71  		t.Fatalf("expected result=true, got result=%t", *result)
    72  	}
    73  }
    74  
    75  func TestBoolVar(t *testing.T) {
    76  	var result bool
    77  	BoolVar(&result, "nonexistent", true)
    78  	Parse()
    79  
    80  	if result != true {
    81  		t.Fatalf("expected result=true, got result=%t", result)
    82  	}
    83  
    84  	err := os.Setenv("bool-key", "true")
    85  	if err != nil {
    86  		t.Fatal("unexpected error", err)
    87  	}
    88  
    89  	BoolVar(&result, "bool-key", false)
    90  	Parse()
    91  
    92  	if result != true {
    93  		t.Fatalf("expected result=true, got result=%t", true)
    94  	}
    95  }
    96  
    97  func TestDuration(t *testing.T) {
    98  	result := Duration("nonexistent", 15*time.Second)
    99  	Parse()
   100  
   101  	if *result != 15*time.Second {
   102  		t.Fatalf("expected result=15s, got result=%v", *result)
   103  	}
   104  
   105  	err := os.Setenv("duration-key", "25s")
   106  	if err != nil {
   107  		t.Fatal("unexpected error", err)
   108  	}
   109  
   110  	result = Duration("duration-key", 15*time.Second)
   111  	Parse()
   112  
   113  	if *result != 25*time.Second {
   114  		t.Fatalf("expected result=25s, got result=%v", *result)
   115  	}
   116  }
   117  
   118  func TestDurationVar(t *testing.T) {
   119  	var result time.Duration
   120  	DurationVar(&result, "nonexistent", 15*time.Second)
   121  	Parse()
   122  
   123  	if result != 15*time.Second {
   124  		t.Fatalf("expected result=15s, got result=%v", result)
   125  	}
   126  
   127  	err := os.Setenv("duration-key", "25s")
   128  	if err != nil {
   129  		t.Fatal("unexpected error", err)
   130  	}
   131  
   132  	DurationVar(&result, "duration-key", 15*time.Second)
   133  	Parse()
   134  
   135  	if result != 25*time.Second {
   136  		t.Fatalf("expected result=25s, got result=%v", result)
   137  	}
   138  }
   139  
   140  func TestURL(t *testing.T) {
   141  	example := "http://example.com"
   142  	newExample := "http://something-new.com"
   143  	result := URL("nonexistent", example)
   144  	Parse()
   145  
   146  	if result.String() != example {
   147  		t.Fatalf("expected result=%s, got result=%v", example, result)
   148  	}
   149  
   150  	err := os.Setenv("url-key", newExample)
   151  	if err != nil {
   152  		t.Fatal("unexpected error", err)
   153  	}
   154  
   155  	result = URL("url-key", example)
   156  	Parse()
   157  
   158  	if result.String() != newExample {
   159  		t.Fatalf("expected result=%v, got result=%v", newExample, result)
   160  	}
   161  }
   162  
   163  func TestURLVar(t *testing.T) {
   164  	example := "http://example.com"
   165  	newExample := "http://something-new.com"
   166  	var result url.URL
   167  	URLVar(&result, "nonexistent", example)
   168  	Parse()
   169  
   170  	if result.String() != example {
   171  		t.Fatalf("expected result=%s, got result=%v", example, result)
   172  	}
   173  
   174  	err := os.Setenv("url-key", newExample)
   175  	if err != nil {
   176  		t.Fatal("unexpected error", err)
   177  	}
   178  
   179  	URLVar(&result, "url-key", example)
   180  	Parse()
   181  
   182  	if result.String() != newExample {
   183  		t.Fatalf("expected result=%v, got result=%v", newExample, result)
   184  	}
   185  }
   186  
   187  func TestString(t *testing.T) {
   188  	result := String("nonexistent", "default")
   189  	Parse()
   190  
   191  	if *result != "default" {
   192  		t.Fatalf("expected result=default, got result=%s", *result)
   193  	}
   194  
   195  	err := os.Setenv("string-key", "something-new")
   196  	if err != nil {
   197  		t.Fatal("unexpected error", err)
   198  	}
   199  
   200  	result = String("string-key", "default")
   201  	Parse()
   202  
   203  	if *result != "something-new" {
   204  		t.Fatalf("expected result=something-new, got result=%s", *result)
   205  	}
   206  }
   207  
   208  func TestStringVar(t *testing.T) {
   209  	var result string
   210  	StringVar(&result, "nonexistent", "default")
   211  	Parse()
   212  
   213  	if result != "default" {
   214  		t.Fatalf("expected result=default, got result=%s", result)
   215  	}
   216  
   217  	err := os.Setenv("string-key", "something-new")
   218  	if err != nil {
   219  		t.Fatal("unexpected error", err)
   220  	}
   221  
   222  	StringVar(&result, "string-key", "default")
   223  	Parse()
   224  
   225  	if result != "something-new" {
   226  		t.Fatalf("expected result=something-new, got result=%s", result)
   227  	}
   228  }
   229  
   230  func TestStringSlice(t *testing.T) {
   231  	result := StringSlice("empty", "hi")
   232  	Parse()
   233  
   234  	exp := []string{"hi"}
   235  	if !reflect.DeepEqual(exp, *result) {
   236  		t.Fatalf("expected %v, got %v", exp, *result)
   237  	}
   238  
   239  	err := os.Setenv("string-slice-key", "hello,world")
   240  	if err != nil {
   241  		t.Fatal("unexpected error", err)
   242  	}
   243  
   244  	result = StringSlice("string-slice-key", "hi")
   245  	Parse()
   246  
   247  	exp = []string{"hello", "world"}
   248  	if !reflect.DeepEqual(exp, *result) {
   249  		t.Fatalf("expected %v, got %v", exp, *result)
   250  	}
   251  }
   252  
   253  func TestStringSliceVar(t *testing.T) {
   254  	var result []string
   255  	StringSliceVar(&result, "empty", "hi")
   256  	Parse()
   257  
   258  	exp := []string{"hi"}
   259  	if !reflect.DeepEqual(exp, result) {
   260  		t.Fatalf("expected %v, got %v", exp, result)
   261  	}
   262  
   263  	err := os.Setenv("string-slice-key", "hello,world")
   264  	if err != nil {
   265  		t.Fatal("unexpected error", err)
   266  	}
   267  
   268  	StringSliceVar(&result, "string-slice-key", "hi", "there")
   269  	Parse()
   270  
   271  	exp = []string{"hello", "world"}
   272  	if !reflect.DeepEqual(exp, result) {
   273  		t.Fatalf("expected %v, got %v", exp, result)
   274  	}
   275  }