GoPLS Viewer

Home|gopls/internal/imports/mod_cache_test.go
1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package imports
6
7import (
8    "fmt"
9    "reflect"
10    "sort"
11    "testing"
12)
13
14func TestDirectoryPackageInfoReachedStatus(t *testing.T) {
15    tests := []struct {
16        info       directoryPackageInfo
17        target     directoryPackageStatus
18        wantStatus bool
19        wantError  bool
20    }{
21        {
22            infodirectoryPackageInfo{
23                statusdirectoryScanned,
24                err:    nil,
25            },
26            target:     directoryScanned,
27            wantStatustrue,
28        },
29        {
30            infodirectoryPackageInfo{
31                statusdirectoryScanned,
32                err:    fmt.Errorf("error getting to directory scanned"),
33            },
34            target:     directoryScanned,
35            wantStatustrue,
36            wantError:  true,
37        },
38        {
39            info:       directoryPackageInfo{},
40            target:     directoryScanned,
41            wantStatusfalse,
42        },
43    }
44
45    for _tt := range tests {
46        gotStatusgotErr := tt.info.reachedStatus(tt.target)
47        if gotErr != nil {
48            if !tt.wantError {
49                t.Errorf("unexpected error: %s"gotErr)
50            }
51            continue
52        }
53
54        if tt.wantStatus != gotStatus {
55            t.Errorf("reached status expected: %v, got: %v"tt.wantStatusgotStatus)
56        }
57    }
58}
59
60func TestModCacheInfo(t *testing.T) {
61    m := &dirInfoCache{
62        dirsmake(map[string]*directoryPackageInfo),
63    }
64
65    dirInfo := []struct {
66        dir  string
67        info directoryPackageInfo
68    }{
69        {
70            dir"mypackage",
71            infodirectoryPackageInfo{
72                status:                 directoryScanned,
73                dir:                    "mypackage",
74                nonCanonicalImportPath"example.com/mypackage",
75            },
76        },
77        {
78            dir"bad package",
79            infodirectoryPackageInfo{
80                statusdirectoryScanned,
81                err:    fmt.Errorf("bad package"),
82            },
83        },
84        {
85            dir"mypackage/other",
86            infodirectoryPackageInfo{
87                dir:                    "mypackage/other",
88                nonCanonicalImportPath"example.com/mypackage/other",
89            },
90        },
91    }
92
93    for _d := range dirInfo {
94        m.Store(d.dird.info)
95    }
96
97    for _d := range dirInfo {
98        valok := m.Load(d.dir)
99        if !ok {
100            t.Errorf("directory not loaded: %s"d.dir)
101        }
102
103        if !reflect.DeepEqual(d.infoval) {
104            t.Errorf("expected: %v, got: %v"d.infoval)
105        }
106    }
107
108    var wantKeys []string
109    for _d := range dirInfo {
110        wantKeys = append(wantKeysd.dir)
111    }
112    sort.Strings(wantKeys)
113
114    gotKeys := m.Keys()
115    sort.Strings(gotKeys)
116
117    if len(gotKeys) != len(wantKeys) {
118        t.Errorf("different length of keys. expected: %d, got: %d"len(wantKeys), len(gotKeys))
119    }
120
121    for iwant := range wantKeys {
122        if want != gotKeys[i] {
123            t.Errorf("%d: expected %s, got %s"iwantgotKeys[i])
124        }
125    }
126}
127
MembersX
TestModCacheInfo.t
TestModCacheInfo.RangeStmt_1967.d
TestModCacheInfo.gotKeys
TestModCacheInfo.RangeStmt_2476.i
TestModCacheInfo.RangeStmt_2476.want
TestDirectoryPackageInfoReachedStatus
TestModCacheInfo
TestModCacheInfo.dirInfo
TestModCacheInfo.RangeStmt_1909.d
TestModCacheInfo.m
TestModCacheInfo.RangeStmt_1967.BlockStmt.val
TestModCacheInfo.RangeStmt_1967.BlockStmt.ok
TestModCacheInfo.wantKeys
TestDirectoryPackageInfoReachedStatus.t
TestDirectoryPackageInfoReachedStatus.tests
TestDirectoryPackageInfoReachedStatus.RangeStmt_885.tt
TestDirectoryPackageInfoReachedStatus.RangeStmt_885.BlockStmt.gotErr
TestModCacheInfo.RangeStmt_2206.d
TestDirectoryPackageInfoReachedStatus.RangeStmt_885.BlockStmt.gotStatus
Members
X