github.com/kaydxh/golang@v0.0.131/go/os/env_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package os_test
    23  
    24  import (
    25  	"os"
    26  	"testing"
    27  
    28  	"github.com/google/uuid"
    29  	os_ "github.com/kaydxh/golang/go/os"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestGetEnvAsStringOrFallback(t *testing.T) {
    34  	const expected = "foo"
    35  
    36  	assert := assert.New(t)
    37  	key := "FLOCKER_SET_VAR"
    38  	os.Setenv(key, expected)
    39  
    40  	assert.Equal(expected, os_.GetEnvAsStringOrFallback(key, "~"+expected))
    41  
    42  	key = "FLOCKER_UNSET_VAR"
    43  	assert.Equal(expected, os_.GetEnvAsStringOrFallback(key, expected))
    44  	assert.NotEqual(expected, os_.GetEnvAsStringOrFallback(key, "~"+expected))
    45  }
    46  
    47  //gotests -w  -all go/os/env.go
    48  //gotests -only GetEnvAsIntOrFallback go/os/env.go
    49  func TestUUID(t *testing.T) {
    50  	id := uuid.New().String()
    51  	t.Logf("uuid: %v ", id)
    52  }
    53  
    54  func TestGetEnvAsIntOrFallback(t *testing.T) {
    55  	type args struct {
    56  		key          string
    57  		defaultValue int
    58  	}
    59  	tests := []struct {
    60  		name    string
    61  		args    args
    62  		want    int
    63  		wantErr bool
    64  	}{
    65  		// TODO: Add test cases.
    66  		{
    67  			name: "test1",
    68  			args: args{
    69  				key:          "FLOCKER_SET_VAR_INT",
    70  				defaultValue: 0,
    71  			},
    72  			want:    0,
    73  			wantErr: false,
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			got, err := os_.GetEnvAsIntOrFallback(tt.args.key, tt.args.defaultValue)
    79  			if (err != nil) != tt.wantErr {
    80  				t.Errorf("GetEnvAsIntOrFallback() error = %v, wantErr %v", err, tt.wantErr)
    81  				return
    82  			}
    83  			if got != tt.want {
    84  				t.Errorf("GetEnvAsIntOrFallback() = %v, want %v", got, tt.want)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestGetEnvAsFloat64OrFallback(t *testing.T) {
    91  	type args struct {
    92  		key          string
    93  		defaultValue float64
    94  	}
    95  	tests := []struct {
    96  		name    string
    97  		args    args
    98  		want    float64
    99  		wantErr bool
   100  	}{
   101  		// TODO: Add test cases.
   102  		{
   103  			name: "test1",
   104  			args: args{
   105  				key:          "FLOCKER_SET_VAR_FLOAT",
   106  				defaultValue: 1.0,
   107  			},
   108  			want:    1.0,
   109  			wantErr: false,
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			got, err := os_.GetEnvAsFloat64OrFallback(tt.args.key, tt.args.defaultValue)
   115  			if (err != nil) != tt.wantErr {
   116  				t.Errorf("GetEnvAsFloat64OrFallback() error = %v, wantErr %v", err, tt.wantErr)
   117  				return
   118  			}
   119  			if got != tt.want {
   120  				t.Errorf("GetEnvAsFloat64OrFallback() = %v, want %v", got, tt.want)
   121  			}
   122  		})
   123  	}
   124  }