github.com/mattolenik/notify@v0.9.1/util_test.go (about)

     1  // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be
     3  // found in the LICENSE file.
     4  
     5  package notify
     6  
     7  import (
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  type caseCanonical struct {
    13  	path string
    14  	full string
    15  }
    16  
    17  func testCanonical(t *testing.T, cases []caseCanonical) {
    18  	for i, cas := range cases {
    19  		full, err := canonical(cas.path)
    20  		if err != nil {
    21  			t.Errorf("want err=nil; got %v (i=%d)", err, i)
    22  			continue
    23  		}
    24  		if full != cas.full {
    25  			t.Errorf("want full=%q; got %q (i=%d)", cas.full, full, i)
    26  			continue
    27  		}
    28  	}
    29  }
    30  
    31  func TestCanonicalNoSymlink(t *testing.T) {
    32  	td := filepath.Join(wd, "testdata")
    33  	cases := [...]caseCanonical{
    34  		{".", wd},
    35  		{"testdata", td},
    36  		{filepath.Join("testdata", ".."), wd},
    37  	}
    38  	testCanonical(t, cases[:])
    39  }
    40  
    41  func TestJoinevents(t *testing.T) {
    42  	cases := [...]struct {
    43  		evs []Event
    44  		ev  Event
    45  	}{
    46  		0: {nil, All},
    47  		1: {[]Event{}, All},
    48  		2: {[]Event{Create}, Create},
    49  		3: {[]Event{Rename}, Rename},
    50  		4: {[]Event{Create, Write, Remove}, Create | Write | Remove},
    51  	}
    52  	for i, cas := range cases {
    53  		if ev := joinevents(cas.evs); ev != cas.ev {
    54  			t.Errorf("want event=%v; got %v (i=%d)", cas.ev, ev, i)
    55  		}
    56  	}
    57  }
    58  
    59  func TestTreeSplit(t *testing.T) {
    60  	cases := [...]struct {
    61  		path string
    62  		dir  string
    63  		base string
    64  	}{
    65  		{"/github.com/rjeczalik/fakerpc", "/github.com/rjeczalik", "fakerpc"},
    66  		{"/home/rjeczalik/src", "/home/rjeczalik", "src"},
    67  		{"/Users/pknap/porn/gopher.avi", "/Users/pknap/porn", "gopher.avi"},
    68  		{"C:/Documents and Users", "C:", "Documents and Users"},
    69  		{"C:/Documents and Users/pblaszczyk/wiertarka.exe", "C:/Documents and Users/pblaszczyk", "wiertarka.exe"},
    70  		{"/home/(╯°□°)╯︵ ┻━┻", "/home", "(╯°□°)╯︵ ┻━┻"},
    71  	}
    72  	for i, cas := range cases {
    73  		dir, base := split(filepath.FromSlash(cas.path))
    74  		if want := filepath.FromSlash(cas.dir); dir != want {
    75  			t.Errorf("want dir=%s; got %s (i=%d)", want, dir, i)
    76  		}
    77  		if want := filepath.FromSlash(cas.base); base != want {
    78  			t.Errorf("want base=%s; got %s (i=%d)", want, base, i)
    79  		}
    80  	}
    81  }
    82  
    83  func TestTreeBase(t *testing.T) {
    84  	cases := [...]struct {
    85  		path string
    86  		base string
    87  	}{
    88  		{"/github.com/rjeczalik/fakerpc", "fakerpc"},
    89  		{"/home/rjeczalik/src", "src"},
    90  		{"/Users/pknap/porn/gopher.avi", "gopher.avi"},
    91  		{"C:/Documents and Users", "Documents and Users"},
    92  		{"C:/Documents and Users/pblaszczyk/wiertarka.exe", "wiertarka.exe"},
    93  		{"/home/(╯°□°)╯︵ ┻━┻", "(╯°□°)╯︵ ┻━┻"},
    94  	}
    95  	for i, cas := range cases {
    96  		if base := base(filepath.FromSlash(cas.path)); base != cas.base {
    97  			t.Errorf("want base=%s; got %s (i=%d)", cas.base, base, i)
    98  		}
    99  	}
   100  }
   101  
   102  func TestTreeIndexSep(t *testing.T) {
   103  	cases := [...]struct {
   104  		path string
   105  		n    int
   106  	}{
   107  		{"github.com/rjeczalik/fakerpc", 10},
   108  		{"home/rjeczalik/src", 4},
   109  		{"Users/pknap/porn/gopher.avi", 5},
   110  		{"C:/Documents and Users", 2},
   111  		{"Documents and Users/pblaszczyk/wiertarka.exe", 19},
   112  		{"(╯°□°)╯︵ ┻━┻/Downloads", 30},
   113  	}
   114  	for i, cas := range cases {
   115  		if n := indexSep(filepath.FromSlash(cas.path)); n != cas.n {
   116  			t.Errorf("want n=%d; got %d (i=%d)", cas.n, n, i)
   117  		}
   118  	}
   119  }
   120  
   121  func TestTreeLastIndexSep(t *testing.T) {
   122  	cases := [...]struct {
   123  		path string
   124  		n    int
   125  	}{
   126  		{"github.com/rjeczalik/fakerpc", 20},
   127  		{"home/rjeczalik/src", 14},
   128  		{"Users/pknap/porn/gopher.avi", 16},
   129  		{"C:/Documents and Users", 2},
   130  		{"Documents and Users/pblaszczyk/wiertarka.exe", 30},
   131  		{"/home/(╯°□°)╯︵ ┻━┻", 5},
   132  	}
   133  	for i, cas := range cases {
   134  		if n := lastIndexSep(filepath.FromSlash(cas.path)); n != cas.n {
   135  			t.Errorf("want n=%d; got %d (i=%d)", cas.n, n, i)
   136  		}
   137  	}
   138  }
   139  
   140  func TestCleanpath(t *testing.T) {
   141  	t.Skip("TODO(rjeczalik)")
   142  }