github.com/while1malloc0/docdir@v0.0.0-20220830001304-722ec0f2cf3a/dirtree/dirtree_test.go (about)

     1  package dirtree_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/while1malloc0/docdir/dirtree"
     7  )
     8  
     9  func TestRead(t *testing.T) {
    10  	got, err := dirtree.New("testdata/a", false)
    11  
    12  	if err != nil {
    13  		t.Fatalf("unexpected error: %v", err)
    14  	}
    15  
    16  	if got.Name != "a" {
    17  		t.Fatalf("expected name 'a', got %v", got.Name)
    18  	}
    19  }
    20  
    21  func TestChildren(t *testing.T) {
    22  	root, err := dirtree.New("testdata/a", false)
    23  
    24  	if err != nil {
    25  		t.Fatalf("unexpected error: %v", err)
    26  	}
    27  
    28  	children := root.Children
    29  	if len(children) != 2 {
    30  		t.Fatalf("expected node A to have 2 children, had %d", len(children))
    31  	}
    32  
    33  	if children[0].Name != "aa" {
    34  		t.Fatalf("expected first child of node A to have name aa, had %s", children[0].Name)
    35  	}
    36  
    37  	if children[1].Name != "bb" {
    38  		t.Fatalf("expected second child of node A to have name bb, had %s", children[1].Name)
    39  	}
    40  }
    41  
    42  func TestGrandChildren(t *testing.T) {
    43  	root, err := dirtree.New("testdata/a", false)
    44  
    45  	if err != nil {
    46  		t.Fatalf("unexpected error: %v", err)
    47  	}
    48  
    49  	grandchildren := root.Children[0].Children
    50  	if len(grandchildren) != 3 {
    51  		t.Fatalf("expected node A to have 3 grandchildren from node AA, had %d", len(grandchildren))
    52  	}
    53  
    54  	if grandchildren[0].Name != "aaa" {
    55  		t.Fatalf("expected first child of node AA to have name aaa, had %s", grandchildren[0].Name)
    56  	}
    57  
    58  	if grandchildren[1].Name != "bbb" {
    59  		t.Fatalf("expected second child of node AA to have name bbb, had %s", grandchildren[1].Name)
    60  	}
    61  
    62  	if grandchildren[2].Name != "ccc" {
    63  		t.Fatalf("expected third child of node AA to have name ccc, had %s", grandchildren[2].Name)
    64  	}
    65  }
    66  
    67  func TestDescription(t *testing.T) {
    68  	subject, err := dirtree.New("testdata/a", false)
    69  	if err != nil {
    70  		t.Fatalf("unexpected error reading testdata: %v", err)
    71  	}
    72  	want := "a is the root testing directory"
    73  	got := subject.Description
    74  	if got != want {
    75  		t.Fatalf("expected %s got %s", want, got)
    76  	}
    77  }
    78  
    79  func TestSkip(t *testing.T) {
    80  	subject, err := dirtree.New("testdata/a", true)
    81  	if err != nil {
    82  		t.Fatalf("unexpected error reading testdata: %v", err)
    83  	}
    84  	if len(subject.Children) != 0 {
    85  		t.Fatalf("expected Node A to have no children due to skipping. Had %d children", len(subject.Children))
    86  	}
    87  }