github.com/hattya/nazuna@v0.7.1-0.20240331055452-55e14c275c1c/util_test.go (about)

     1  //
     2  // nazuna :: util_test.go
     3  //
     4  //   Copyright (c) 2018-2022 Akinori Hattori <hattya@gmail.com>
     5  //
     6  //   SPDX-License-Identifier: MIT
     7  //
     8  
     9  package nazuna_test
    10  
    11  import (
    12  	"os"
    13  	"path/filepath"
    14  	"reflect"
    15  	"testing"
    16  
    17  	"github.com/hattya/nazuna"
    18  )
    19  
    20  func TestIsDir(t *testing.T) {
    21  	sandbox(t)
    22  
    23  	if !nazuna.IsDir(".") {
    24  		t.Errorf("IsDir(%q) = false, expected true", ".")
    25  	}
    26  
    27  	p := "file"
    28  	if err := touch(p); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	if nazuna.IsDir(p) {
    32  		t.Errorf("IsDir(%q) = true, expected false", p)
    33  	}
    34  }
    35  
    36  func TestIsEmptyDir(t *testing.T) {
    37  	sandbox(t)
    38  
    39  	if !nazuna.IsEmptyDir(".") {
    40  		t.Errorf("IsEmptyDir(%q) = false, expected true", ".")
    41  	}
    42  
    43  	p := "file"
    44  	if err := touch(p); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	if nazuna.IsEmptyDir(p) {
    48  		t.Errorf("IsEmptyDir(%q) = true, expected false", p)
    49  	}
    50  	if nazuna.IsEmptyDir(".") {
    51  		t.Errorf("IsEmptyDir(%q) = true, expected false", ".")
    52  	}
    53  }
    54  
    55  func TestSplitPath(t *testing.T) {
    56  	sep := string(os.PathSeparator)
    57  	for _, p := range []string{
    58  		"dir" + sep + "file",
    59  		"dir" + sep + sep + "file",
    60  		"dir/file",
    61  		"dir//file",
    62  	} {
    63  		dir, name := nazuna.SplitPath(p)
    64  		if g, e := []string{dir, name}, []string{"dir", "file"}; !reflect.DeepEqual(g, e) {
    65  			t.Errorf("expected %v, got %v", e, g)
    66  		}
    67  	}
    68  }
    69  
    70  func TestSortKeys(t *testing.T) {
    71  	var m interface{}
    72  	m = map[string]string{
    73  		"a": "a",
    74  		"z": "z",
    75  	}
    76  	if g, e := nazuna.SortKeys(m), []string{"a", "z"}; !reflect.DeepEqual(g, e) {
    77  		t.Errorf("expected %v, got %v", e, g)
    78  	}
    79  	// not map
    80  	e := []string(nil)
    81  	if g := nazuna.SortKeys(nil); !reflect.DeepEqual(g, e) {
    82  		t.Errorf("expected %v, got %v", e, g)
    83  	}
    84  	// not map[string]
    85  	m = make(map[int]int)
    86  	if g := nazuna.SortKeys(m); !reflect.DeepEqual(g, e) {
    87  		t.Errorf("expected %v, got %v", e, g)
    88  	}
    89  	m = map[int]int{
    90  		0: 0,
    91  		9: 9,
    92  	}
    93  	if g := nazuna.SortKeys(m); !reflect.DeepEqual(g, e) {
    94  		t.Errorf("expected %v, got %v", e, g)
    95  	}
    96  }
    97  
    98  func TestMarshalError(t *testing.T) {
    99  	sandbox(t)
   100  
   101  	if err := mkdir(".nzn", "r", ".git"); err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	repo, err := nazuna.Open(nil, ".")
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	p := filepath.Join(repo.Root(), ".nzn", "r", "nazuna.json")
   109  
   110  	if err := nazuna.Marshal(repo, filepath.Base(p), nil); err == nil {
   111  		t.Error("expected error")
   112  	}
   113  	if err := nazuna.Marshal(repo, p, nazuna.Marshal); err == nil {
   114  		t.Error("expected error")
   115  	}
   116  	if err := mkdir(p); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	if err := nazuna.Marshal(repo, p, nil); err == nil {
   120  		t.Error("expected error")
   121  	}
   122  }
   123  
   124  func TestUnmarshalError(t *testing.T) {
   125  	sandbox(t)
   126  
   127  	if err := mkdir(".nzn", "r", ".git"); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	repo, err := nazuna.Open(nil, ".")
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	p := filepath.Join(repo.Root(), ".nzn", "r", "nazuna.json")
   135  
   136  	if err := nazuna.Unmarshal(repo, filepath.Base(p), nil); err == nil {
   137  		t.Error("expected error")
   138  	}
   139  	if err := touch(p); err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	if err := nazuna.Unmarshal(repo, p, nil); err == nil {
   143  		t.Error("expected error")
   144  	}
   145  }