github.heygears.com/openimsdk/tools@v0.0.49/env/env_test.go (about)

     1  // Copyright © 2024 OpenIM open source community. All rights reserved.
     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  package env
    16  
    17  import (
    18  	"os"
    19  	"strconv"
    20  	"testing"
    21  )
    22  
    23  func TestGetString(t *testing.T) {
    24  	const expected = "foo"
    25  
    26  	key := "STRING_SET_VAR"
    27  	os.Setenv(key, expected)
    28  	if e, a := expected, GetString(key, "~"+expected); e != a {
    29  		t.Fatalf("expected %#v==%#v", e, a)
    30  	}
    31  
    32  	key = "STRING_UNSET_VAR"
    33  	if e, a := expected, GetString(key, expected); e != a {
    34  		t.Fatalf("expected %#v==%#v", e, a)
    35  	}
    36  }
    37  
    38  func TestGetInt(t *testing.T) {
    39  	const expected = 1
    40  	const defaultValue = 2
    41  
    42  	key := "INT_SET_VAR"
    43  	os.Setenv(key, strconv.Itoa(expected))
    44  	returnVal, _ := GetInt(key, defaultValue)
    45  	if e, a := expected, returnVal; e != a {
    46  		t.Fatalf("expected %#v==%#v", e, a)
    47  	}
    48  
    49  	key = "INT_UNSET_VAR"
    50  	returnVal, _ = GetInt(key, defaultValue)
    51  	if e, a := defaultValue, returnVal; e != a {
    52  		t.Fatalf("expected %#v==%#v", e, a)
    53  	}
    54  
    55  	key = "INT_SET_VAR"
    56  	os.Setenv(key, "not-an-int")
    57  	returnVal, err := GetInt(key, defaultValue)
    58  	if e, a := defaultValue, returnVal; e != a {
    59  		t.Fatalf("expected %#v==%#v", e, a)
    60  	}
    61  	if err == nil {
    62  		t.Error("expected error")
    63  	}
    64  }