file/file_test.go

59 lines
1.1 KiB
Go

package file
import (
"fmt"
"strconv"
"sync"
"testing"
)
func TestFile(t *testing.T) {
ext := Ext("path/to/file.ext")
if ext != "ext" {
t.Error("(1)")
}
ext = Ext("path/to.file/file")
if ext != "" {
t.Error("(2)")
}
ext = Ext("path/to.file/file.ext")
if ext != "ext" {
t.Error("(3)")
}
}
func TestFileCreate(t *testing.T) {
if err := Save("test/test.txt", []byte("Test first string\n")); err != nil {
t.Error(err.Error())
}
if err := Append("test/test.txt", []byte("Test second string\n")); err != nil {
t.Error(err.Error())
}
//= Threaded write >
tw := &sync.WaitGroup{}
for ti := 0; ti < 10; ti++ {
tw.Add(1)
go func(ti int, t *testing.T) {
defer tw.Done()
for i := 0; i < 50; i++ {
if err := Append("test/test.txt", []byte("Thread "+strconv.Itoa(ti)+" line "+strconv.Itoa(i)+"\n")); err != nil {
t.Error(err)
}
}
}(ti, t)
}
tw.Wait()
//< Threaded write =
}
func TestClearPath(t *testing.T) {
exp := "./path/to/file/with.ext"
fn := "....//path/////to/////file/with....ext"
r := ClearPath(fn)
if exp != r {
t.Errorf("%s not match %s", exp, r)
}
fmt.Println(r)
}