...

Source file src/github.com/lestrrat-go/iter/mapiter/mapiter_test.go

Documentation: github.com/lestrrat-go/iter/mapiter

     1  package mapiter_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/lestrrat-go/iter/mapiter"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestIterator(t *testing.T) {
    15  	chSize := 2
    16  
    17  	ch := make(chan *mapiter.Pair, chSize)
    18  	ch <- &mapiter.Pair{Key: "one", Value: 1}
    19  	ch <- &mapiter.Pair{Key: "two", Value: 2}
    20  	close(ch)
    21  
    22  	i := mapiter.New(ch)
    23  
    24  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    25  	defer cancel()
    26  
    27  	var loopCount int
    28  	for i.Next(ctx) {
    29  		loopCount++
    30  		p := i.Pair()
    31  		if !assert.Equal(t, p.Value, loopCount, "expected values to match") {
    32  			return
    33  		}
    34  	}
    35  
    36  	if !assert.Equal(t, chSize, loopCount, "expected to loop for %d times", chSize) {
    37  		return
    38  	}
    39  }
    40  
    41  type MapLike struct {
    42  	Values map[string]int
    43  }
    44  
    45  func (m *MapLike) Iterate(ctx context.Context) mapiter.Iterator {
    46  	ch := make(chan *mapiter.Pair)
    47  	go m.iterate(ctx, ch)
    48  	return mapiter.New(ch)
    49  }
    50  
    51  func (m *MapLike) iterate(ctx context.Context, ch chan *mapiter.Pair) {
    52  	defer close(ch)
    53  	for k, v := range m.Values {
    54  		ch <- &mapiter.Pair{Key: k, Value: v}
    55  	}
    56  }
    57  
    58  func TestAsMap(t *testing.T) {
    59  	t.Run("maps", func(t *testing.T) {
    60  		inputs := []interface{}{
    61  			map[string]string{
    62  				"foo": "one",
    63  				"bar": "two",
    64  				"baz": "three",
    65  			},
    66  		}
    67  		for _, x := range inputs {
    68  			input := x
    69  			t.Run(fmt.Sprintf("%T", input), func(t *testing.T) {
    70  				ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    71  				defer cancel()
    72  				dst := reflect.New(reflect.TypeOf(input))
    73  				dst.Elem().Set(reflect.MakeMap(reflect.TypeOf(input)))
    74  				if !assert.NoError(t, mapiter.AsMap(ctx, input, dst.Interface()), `mapiter.AsMap should succeed`) {
    75  					return
    76  				}
    77  				if !assert.Equal(t, input, dst.Elem().Interface(), `maps should be the same`) {
    78  					return
    79  				}
    80  			})
    81  		}
    82  	})
    83  
    84  	t.Run("Map-like object", func(t *testing.T) {
    85  		src := &MapLike{
    86  			Values: map[string]int{
    87  				"one":   1,
    88  				"two":   2,
    89  				"three": 3,
    90  				"four":  4,
    91  				"five":  5,
    92  			},
    93  		}
    94  
    95  		t.Run("dst is nil", func(t *testing.T) {
    96  			var m map[string]int
    97  			if !assert.NoError(t, mapiter.AsMap(context.Background(), src, &m), `AsMap against nil map should succeed`) {
    98  				return
    99  			}
   100  
   101  			if !assert.Equal(t, src.Values, m, "maps should match") {
   102  				return
   103  			}
   104  		})
   105  		t.Run("dst is nil (elem type does not match)", func(t *testing.T) {
   106  			var m map[string]string
   107  			if assert.Error(t, mapiter.AsMap(context.Background(), src, &m), `AsMap against nil map should fail`) {
   108  				return
   109  			}
   110  		})
   111  		t.Run("dst is not nil", func(t *testing.T) {
   112  			m := make(map[string]int)
   113  			if !assert.NoError(t, mapiter.AsMap(context.Background(), src, &m), `AsMap against nil map should succeed`) {
   114  				return
   115  			}
   116  
   117  			if !assert.Equal(t, src.Values, m, "maps should match") {
   118  				return
   119  			}
   120  		})
   121  	})
   122  }
   123  
   124  func TestGH1(t *testing.T) {
   125  	t.Run("maps", func(t *testing.T) {
   126  		inputs := []interface{}{
   127  			map[string]interface{}{
   128  				"foo": "one",
   129  				"bar": "two",
   130  				"baz": nil,
   131  			},
   132  		}
   133  		for _, x := range inputs {
   134  			input := x
   135  			t.Run(fmt.Sprintf("%T", input), func(t *testing.T) {
   136  				ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
   137  				defer cancel()
   138  				dst := reflect.New(reflect.TypeOf(input))
   139  				dst.Elem().Set(reflect.MakeMap(reflect.TypeOf(input)))
   140  				if !assert.NoError(t, mapiter.AsMap(ctx, input, dst.Interface()), `mapiter.AsMap should succeed`) {
   141  					return
   142  				}
   143  				if !assert.Equal(t, input, dst.Elem().Interface(), `maps should be the same`) {
   144  					return
   145  				}
   146  			})
   147  		}
   148  	})
   149  }
   150  

View as plain text