github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/environment/map_test.go (about)

     1  package environment
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // TestToMap tests ToMap.
     8  func TestToMap(t *testing.T) {
     9  	// Set test parameters.
    10  	input := []string{
    11  		"KEY=VALUE",
    12  		"KEY=duplicate",
    13  		"OTHER=2",
    14  		"IGNORED",
    15  	}
    16  	expected := map[string]string{
    17  		"KEY":   "duplicate",
    18  		"OTHER": "2",
    19  	}
    20  
    21  	// Perform conversion.
    22  	output := ToMap(input)
    23  
    24  	// Validate results.
    25  	if len(output) != len(expected) {
    26  		t.Fatal("output length does not match expected:", len(output), "!=", len(expected))
    27  	}
    28  	for key, value := range output {
    29  		if expectedValue, ok := expected[key]; !ok {
    30  			t.Errorf("output key \"%s\" not expected", key)
    31  		} else if value != expectedValue {
    32  			t.Error("output value does not match expected:", value, "!=", expectedValue)
    33  		}
    34  	}
    35  }
    36  
    37  // TestFromMap tests FromMap.
    38  func TestFromMap(t *testing.T) {
    39  	// Set test parameters.
    40  	input := map[string]string{
    41  		"KEY":   "duplicate",
    42  		"OTHER": "2",
    43  		"HEY":   "THERE",
    44  	}
    45  
    46  	// Perform conversion to a slice and then back to a map so that we can
    47  	// compare based on map contents.
    48  	output := ToMap(FromMap(input))
    49  
    50  	// Validate results.
    51  	if len(output) != len(input) {
    52  		t.Fatal("output length does not match expected:", len(output), "!=", len(input))
    53  	}
    54  	for key, value := range output {
    55  		if expectedValue, ok := input[key]; !ok {
    56  			t.Errorf("output key \"%s\" not expected", key)
    57  		} else if value != expectedValue {
    58  			t.Error("output value does not match expected:", value, "!=", expectedValue)
    59  		}
    60  	}
    61  }
    62  
    63  // TestFromMapNilMap tests FromMap with a nil input.
    64  func TestFromMapNilMap(t *testing.T) {
    65  	if FromMap(nil) != nil {
    66  		t.Fatal("FromMap returned non-nil result with nil input")
    67  	}
    68  }
    69  
    70  // TestFromMapEmptyMap tests FromMap with an empty input map.
    71  func TestFromMapEmptyMap(t *testing.T) {
    72  	result := FromMap(make(map[string]string))
    73  	if result == nil {
    74  		t.Fatal("FromMap returned nil result with non-nil input")
    75  	} else if len(result) != 0 {
    76  		t.Fatal("FromMap returned non-empty result with empty input")
    77  	}
    78  }