github.com/ethersphere/bee/v2@v2.2.0/pkg/manifest/simple/walker_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package simple_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/manifest/simple"
    12  )
    13  
    14  func TestWalkEntry(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	for _, tc := range makeTestCases(t) {
    18  		tc := tc
    19  		t.Run(tc.name, func(t *testing.T) {
    20  			t.Parallel()
    21  
    22  			m := simple.NewManifest()
    23  
    24  			// add entries
    25  			for _, e := range tc.entries {
    26  				err := m.Add(e.path, e.reference.String(), e.metadata)
    27  				if err != nil {
    28  					t.Fatal(err)
    29  				}
    30  			}
    31  
    32  			manifestLen := m.Length()
    33  
    34  			if len(tc.entries) != manifestLen {
    35  				t.Fatalf("expected %d entries, found %d", len(tc.entries), manifestLen)
    36  			}
    37  
    38  			walkedCount := 0
    39  
    40  			walker := func(path string, entry simple.Entry, err error) error {
    41  				walkedCount++
    42  
    43  				pathFound := false
    44  
    45  				for i := 0; i < len(tc.entries); i++ {
    46  					p := tc.entries[i].path
    47  					if path == p {
    48  						pathFound = true
    49  						break
    50  					}
    51  				}
    52  
    53  				if !pathFound {
    54  					return fmt.Errorf("walkFn returned unknown path: %s", path)
    55  				}
    56  
    57  				return nil
    58  			}
    59  			// Expect no errors.
    60  			err := m.WalkEntry("", walker)
    61  			if err != nil {
    62  				t.Fatalf("no error expected, found: %s", err)
    63  			}
    64  
    65  			if len(tc.entries) != walkedCount {
    66  				t.Errorf("expected %d nodes, got %d", len(tc.entries), walkedCount)
    67  			}
    68  		})
    69  	}
    70  }