blob: 246123c5f8d4dd9e6b3e5cc60aa6712007f17516 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package swift
import (
func newVdlRootTestSwiftContext(srcDirs []string) *swiftContext {
srcDirsSet := map[string]bool{}
for _, srcDir := range srcDirs {
srcDirsSet[srcDir] = true
ctx := swiftContext{nil, nil, map[string]string{}, srcDirsSet, map[*compile.TypeDef]string{}}
return &ctx
func TestVdlRootForFilePath(t *testing.T) {
tests := []struct {
Name string
AbsolutePath string
Ctx *swiftContext
WantRoot string
WantErr error
{"Must be absolute path", "not/absolute", newVdlRootTestSwiftContext(nil), "", errors.New("Path not absolute or unsupported file system")},
"/home/user/vanadium/release/go/src/", nil},
{"Basic two places",
newVdlRootTestSwiftContext([]string{"/home/user/vanadium/release/go/src/", "/home/users/vanadium/third-party"}),
"/home/user/vanadium/release/go/src/", nil},
{"Longest path wins",
newVdlRootTestSwiftContext([]string{"/home/user/vanadium/release/go/src/", "/home/user/vanadium/release/go/src/"}),
"/home/user/vanadium/release/go/src/", nil},
{"Vdlroot NOT found",
"", errors.New("Hit the root before any vdl paths were found")},
for _, test := range tests {
gotRoot, gotErr := test.Ctx.vdlRootForFilePath(test.AbsolutePath)
if gotRoot != test.WantRoot {
t.Errorf("%s\n GOT %s\nWANT %s", test.Name, gotRoot, test.WantRoot)
// Errors can be nil in want or what's producted. Test if they're not both nil or not nil (XOR), or if is not expected IF we want an error
if (gotErr != nil) != (test.WantErr != nil) || (test.WantErr != nil && gotErr.Error() != test.WantErr.Error()) {
t.Errorf("%s\n GOT ERROR %v\nWANT ERROR %v", test.Name, gotErr, test.WantErr)