v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/maps/flat_test.go (about)

     1  package maps
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestFlatMap(t *testing.T) {
     9  	t.Run("nil map", func(t *testing.T) {
    10  		var m map[int]int
    11  		if got := FlatMap(m); got != nil {
    12  			t.Errorf("FlatMap(%v) = %v, want nil", m, got)
    13  		}
    14  	})
    15  
    16  	t.Run("empty map", func(t *testing.T) {
    17  		m := map[int]int{}
    18  		if got := FlatMap(m); !reflect.DeepEqual(got, []KeyValue[int, int]{}) {
    19  			t.Errorf("FlatMap(%v) = %v, want empty", m, got)
    20  		}
    21  	})
    22  
    23  	t.Run("1-element map", func(t *testing.T) {
    24  		m := map[int]int{1: 1}
    25  		want := []KeyValue[int, int]{{1, 1}}
    26  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    27  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    28  		}
    29  	})
    30  
    31  	t.Run("2-element map", func(t *testing.T) {
    32  		m := map[int]int{1: 1, 2: 2}
    33  		want := []KeyValue[int, int]{{1, 1}, {2, 2}}
    34  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    35  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    36  		}
    37  	})
    38  
    39  	t.Run("3-element map", func(t *testing.T) {
    40  		m := map[int]int{1: 1, 2: 2, 3: 3}
    41  		want := []KeyValue[int, int]{{1, 1}, {2, 2}, {3, 3}}
    42  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    43  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    44  		}
    45  	})
    46  
    47  	t.Run("4-element map", func(t *testing.T) {
    48  		m := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
    49  		want := []KeyValue[int, int]{{1, 1}, {2, 2}, {3, 3}, {4, 4}}
    50  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    51  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    52  		}
    53  	})
    54  
    55  	t.Run("string-int map", func(t *testing.T) {
    56  		m := map[string]int{"a": 1, "b": 2, "c": 3}
    57  		want := []KeyValue[string, int]{{"a", 1}, {"b", 2}, {"c", 3}}
    58  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    59  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    60  		}
    61  	})
    62  
    63  	t.Run("int-string map", func(t *testing.T) {
    64  		m := map[int]string{1: "a", 2: "b", 3: "c"}
    65  		want := []KeyValue[int, string]{{1, "a"}, {2, "b"}, {3, "c"}}
    66  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    67  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    68  		}
    69  	})
    70  
    71  	t.Run("float-int map", func(t *testing.T) {
    72  		m := map[float64]int{1.0: 1, 2.0: 2, 3.0: 3}
    73  		want := []KeyValue[float64, int]{{1.0, 1}, {2.0, 2}, {3.0, 3}}
    74  		if got := FlatMap(m); !reflect.DeepEqual(got, want) {
    75  			t.Errorf("FlatMap(%v) = %v, want %v", m, got, want)
    76  		}
    77  	})
    78  }