2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-12-30 18:17:47 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2011-02-26 21:30:07 +00:00
|
|
|
. "camli/testing"
|
2011-01-01 22:44:19 +00:00
|
|
|
"os"
|
2010-12-30 18:17:47 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type isUtf8Test struct {
|
|
|
|
s string
|
|
|
|
e bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsUtf8(t *testing.T) {
|
|
|
|
tests := []isUtf8Test{
|
|
|
|
{"foo", true},
|
|
|
|
{"Straße", true},
|
|
|
|
{string([]uint8{65, 234, 234, 192, 23, 123}), false},
|
|
|
|
{string([]uint8{65, 97}), true},
|
|
|
|
}
|
|
|
|
for idx, test := range tests {
|
|
|
|
if isValidUtf8(test.s) != test.e {
|
|
|
|
t.Errorf("expected isutf8==%d for test index %d", test.e, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const kExpectedHeader = `{"camliVersion"`
|
|
|
|
|
|
|
|
func TestJson(t *testing.T) {
|
2011-01-01 22:44:19 +00:00
|
|
|
fileName := "schema_test.go"
|
|
|
|
fi, _ := os.Lstat(fileName)
|
|
|
|
m := NewCommonFileMap(fileName, fi)
|
2010-12-30 18:17:47 +00:00
|
|
|
json, err := MapToCamliJson(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("Got json: [%s]\n", json)
|
|
|
|
// TODO: test it parses back
|
|
|
|
|
|
|
|
if !strings.HasPrefix(json, kExpectedHeader) {
|
|
|
|
t.Errorf("JSON does't start with expected header.")
|
|
|
|
}
|
|
|
|
|
2010-12-31 06:37:46 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 20:13:33 +00:00
|
|
|
type rfc3339NanoTest struct {
|
|
|
|
nanos int64
|
|
|
|
e string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRfc3339FromNanos(t *testing.T) {
|
|
|
|
tests := []rfc3339NanoTest{
|
|
|
|
{0, "1970-01-01T00:00:00Z"},
|
|
|
|
{1, "1970-01-01T00:00:00.000000001Z"},
|
|
|
|
{10, "1970-01-01T00:00:00.00000001Z"},
|
|
|
|
{1000, "1970-01-01T00:00:00.000001Z"},
|
|
|
|
}
|
|
|
|
for idx, test := range tests {
|
|
|
|
got := rfc3339FromNanos(test.nanos)
|
|
|
|
if got != test.e {
|
|
|
|
t.Errorf("On test %d got %q; expected %q", idx, got, test.e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-31 06:37:46 +00:00
|
|
|
|
|
|
|
func TestRegularFile(t *testing.T) {
|
2011-02-26 21:30:07 +00:00
|
|
|
fileName := "schema_test.go"
|
|
|
|
fi, err := os.Lstat(fileName)
|
|
|
|
AssertNil(t, err, "test-symlink stat")
|
|
|
|
m := NewCommonFileMap("schema_test.go", fi)
|
2010-12-31 06:37:46 +00:00
|
|
|
json, err := MapToCamliJson(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("Got json for regular file: [%s]\n", json)
|
2011-01-01 21:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlink(t *testing.T) {
|
2011-02-26 21:30:07 +00:00
|
|
|
fileName := "testdata/test-symlink"
|
|
|
|
fi, err := os.Lstat(fileName)
|
|
|
|
AssertNil(t, err, "test-symlink stat")
|
|
|
|
m := NewCommonFileMap(fileName, fi)
|
2011-01-01 21:12:45 +00:00
|
|
|
json, err := MapToCamliJson(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("Got json for symlink file: [%s]\n", json)
|
2010-12-30 18:17:47 +00:00
|
|
|
}
|