59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package filesystem
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCachedFile_Serialization(t *testing.T) {
|
|
now := time.Now().Truncate(time.Second) // JSON marshal of time might lose precision
|
|
cf := File{
|
|
name: "test.md",
|
|
size: 123,
|
|
mode: 0o644,
|
|
modTime: now,
|
|
isDir: false,
|
|
contentType: "text/markdown",
|
|
path: "/test.md",
|
|
}
|
|
|
|
data, err := json.Marshal(cf)
|
|
if err != nil {
|
|
t.Fatalf("Marshal failed: %v", err)
|
|
}
|
|
|
|
var cf2 File
|
|
if err := json.Unmarshal(data, &cf2); err != nil {
|
|
t.Fatalf("Unmarshal failed: %v", err)
|
|
}
|
|
|
|
if cf.name != cf2.name {
|
|
t.Errorf("Name mismatch: %s != %s", cf.name, cf2.name)
|
|
}
|
|
if cf.size != cf2.size {
|
|
t.Errorf("Size mismatch: %d != %d", cf.size, cf2.size)
|
|
}
|
|
if cf.mode != cf2.mode {
|
|
t.Errorf("Mode mismatch: %v != %v", cf.mode, cf2.mode)
|
|
}
|
|
if !cf.modTime.Equal(cf2.modTime) {
|
|
t.Errorf("ModTime mismatch: %v != %v", cf.modTime, cf2.modTime)
|
|
}
|
|
if cf.isDir != cf2.isDir {
|
|
t.Errorf("IsDir mismatch: %v != %v", cf.isDir, cf2.isDir)
|
|
}
|
|
if cf.contentType != cf2.contentType {
|
|
t.Errorf("ContentType mismatch: %s != %s", cf.contentType, cf2.contentType)
|
|
}
|
|
if cf.path != cf2.path {
|
|
t.Errorf("Path mismatch: %s != %s", cf.path, cf2.path)
|
|
}
|
|
}
|
|
|
|
func TestCachedFile_Interface(t *testing.T) {
|
|
// Verify CachedFile implements os.FileInfo
|
|
var _ os.FileInfo = File{}
|
|
}
|