dati

data and template interface
git clone git://git.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

file_test.go (1330B)


      1 package dati
      2 
      3 import (
      4 	"os"
      5 	"path/filepath"
      6 	"strconv"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestSortFileList(t *testing.T) {
     12 	var err error
     13 	tdir := os.TempDir()
     14 	paths := []string{tdir + "/1", tdir + "/3", tdir + "/2"}
     15 	var sorted []string
     16 
     17 	sorted, err = SortFileList(paths, "filename")
     18 	if err != nil {
     19 		t.Fatal(err)
     20 	}
     21 	for i, p := range sorted {
     22 		if filepath.Base(p) != strconv.Itoa(i+1) {
     23 			t.Fatalf("invalid order returned sorted[%d] is %s", i, p)
     24 		}
     25 	}
     26 
     27 	sorted, err = SortFileList(paths, "filename-desc")
     28 	if err != nil {
     29 		t.Fatal(err)
     30 	}
     31 	j := 3
     32 	for i := 0; i < len(sorted); i++ {
     33 		if filepath.Base(sorted[i]) != strconv.Itoa(j) {
     34 			t.Fatalf("invalid order returned sorted[%d] is %s", i, sorted[i])
     35 		}
     36 		j--
     37 	}
     38 
     39 	for _, path := range paths {
     40 		var f *os.File
     41 		if f, err = os.Create(path); err != nil {
     42 			t.Skip(err)
     43 		}
     44 		defer f.Close()
     45 		time.Sleep(100 * time.Millisecond)
     46 	}
     47 
     48 	sorted, err = SortFileList(paths, "modified")
     49 	if err != nil {
     50 		t.Fatal(err)
     51 	}
     52 	for i := range paths {
     53 		if sorted[i] != paths[i] {
     54 			t.Fatalf("invalid order returned %s - %s", sorted, paths)
     55 		}
     56 	}
     57 
     58 	sorted, err = SortFileList(paths, "modified-desc")
     59 	if err != nil {
     60 		t.Fatal(err)
     61 	}
     62 	j = 2
     63 	for i := 0; i < len(paths); i++ {
     64 		if sorted[i] != paths[j] {
     65 			t.Fatalf("invalid order returned %s - %s", sorted, paths)
     66 		}
     67 		j--
     68 	}
     69 }