2011-11-27 16:36:30 +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.
|
|
|
|
*/
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
package index_test
|
2011-11-27 16:36:30 +00:00
|
|
|
|
|
|
|
import (
|
2012-03-28 08:33:20 +00:00
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2011-11-27 16:36:30 +00:00
|
|
|
"testing"
|
2011-11-27 18:14:53 +00:00
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
"camlistore.org/pkg/index"
|
|
|
|
"camlistore.org/pkg/index/indextest"
|
2011-11-27 16:36:30 +00:00
|
|
|
)
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
func TestReverseTimeString(t *testing.T) {
|
|
|
|
in := "2011-11-27T01:23:45Z"
|
|
|
|
got := index.ExpReverseTimeString(in)
|
|
|
|
want := "rt7988-88-72T98:76:54Z"
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("reverseTimeString = %q, want %q", got, want)
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
2012-02-26 12:49:03 +00:00
|
|
|
back := index.ExpUnreverseTimeString(got)
|
|
|
|
if back != in {
|
|
|
|
t.Fatalf("unreverseTimeString = %q, want %q", back, in)
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndex_Memory(t *testing.T) {
|
2012-11-04 11:18:11 +00:00
|
|
|
indextest.Index(t, index.NewMemoryIndex)
|
2011-11-27 18:14:53 +00:00
|
|
|
}
|
|
|
|
|
2011-12-25 20:30:56 +00:00
|
|
|
func TestPathsOfSignerTarget_Memory(t *testing.T) {
|
2012-11-04 11:18:11 +00:00
|
|
|
indextest.PathsOfSignerTarget(t, index.NewMemoryIndex)
|
2011-12-01 18:43:57 +00:00
|
|
|
}
|
|
|
|
|
2011-12-25 20:30:56 +00:00
|
|
|
func TestFiles_Memory(t *testing.T) {
|
2012-11-04 11:18:11 +00:00
|
|
|
indextest.Files(t, index.NewMemoryIndex)
|
2011-11-27 16:36:30 +00:00
|
|
|
}
|
2012-03-28 08:33:20 +00:00
|
|
|
|
2012-11-05 09:29:42 +00:00
|
|
|
func TestEdgesTo_Memory(t *testing.T) {
|
|
|
|
indextest.EdgesTo(t, index.NewMemoryIndex)
|
|
|
|
}
|
|
|
|
|
2012-03-28 08:33:20 +00:00
|
|
|
var (
|
|
|
|
// those dirs are not packages implementing indexers,
|
|
|
|
// hence we do not want to check them.
|
2012-04-13 21:12:04 +00:00
|
|
|
excludedDirs = []string{"indextest", "testdata", "sqlindex"}
|
2012-03-28 08:33:20 +00:00
|
|
|
// A map is used in hasAllRequiredTests to note which required
|
|
|
|
// tests have been found in a package, by setting the corresponding
|
|
|
|
// booleans to true. Those are the keys for this map.
|
2012-11-05 09:29:42 +00:00
|
|
|
requiredTests = []string{"TestIndex_", "TestPathsOfSignerTarget_", "TestFiles_", "TestEdgesTo_"}
|
2012-03-28 08:33:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This function checks that all the functions using the tests
|
|
|
|
// defined in indextest, namely:
|
|
|
|
// TestIndex_, TestPathOfSignerTarget_, TestFiles_
|
|
|
|
// do exist in the provided package.
|
|
|
|
func hasAllRequiredTests(path string, t *testing.T) error {
|
|
|
|
tests := make(map[string]bool)
|
|
|
|
for _, v := range requiredTests {
|
|
|
|
tests[v] = false
|
|
|
|
}
|
|
|
|
dir, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
names, err := dir.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
for _, name := range names {
|
2012-03-29 00:15:37 +00:00
|
|
|
if strings.HasPrefix(name, ".") || !strings.HasSuffix(name, "_test.go") {
|
2012-03-28 08:33:20 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, err := parser.ParseFile(fset, filepath.Join(path, name), nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v: %v", filepath.Join(path, name), err)
|
|
|
|
}
|
|
|
|
ast.Inspect(f, func(n ast.Node) bool {
|
|
|
|
switch x := n.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
name := x.Name.Name
|
|
|
|
for k, _ := range tests {
|
|
|
|
if strings.HasPrefix(name, k) {
|
|
|
|
tests[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range tests {
|
|
|
|
if !v {
|
|
|
|
return fmt.Errorf("%v not implemented in %v", k, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each package implementing an indexer, this checks that
|
|
|
|
// all the required tests are present in its test suite.
|
|
|
|
func TestIndexerTestsCompleteness(t *testing.T) {
|
|
|
|
cwd, err := os.Open(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cwd.Close()
|
|
|
|
files, err := cwd.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2012-03-28 23:45:54 +00:00
|
|
|
|
2012-03-28 08:33:20 +00:00
|
|
|
for _, file := range files {
|
2012-03-28 23:45:54 +00:00
|
|
|
name := file.Name()
|
2012-03-29 00:15:37 +00:00
|
|
|
if !file.IsDir() || skipDir(name) {
|
2012-03-28 08:33:20 +00:00
|
|
|
continue
|
|
|
|
}
|
2012-03-28 23:45:54 +00:00
|
|
|
if err := hasAllRequiredTests(name, t); err != nil {
|
2012-03-29 00:15:37 +00:00
|
|
|
t.Error(err)
|
2012-03-28 08:33:20 +00:00
|
|
|
}
|
2012-03-28 23:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipDir(name string) bool {
|
2012-03-29 00:15:37 +00:00
|
|
|
if strings.HasPrefix(name, "_") {
|
|
|
|
return true
|
|
|
|
}
|
2012-03-28 23:45:54 +00:00
|
|
|
for _, v := range excludedDirs {
|
|
|
|
if v == name {
|
|
|
|
return true
|
2012-03-28 08:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-28 23:45:54 +00:00
|
|
|
return false
|
2012-03-28 08:33:20 +00:00
|
|
|
}
|