Monorepo for Tangled
tangled.org
1package codesearch
2
3import (
4 "testing"
5
6 "github.com/sourcegraph/zoekt/query"
7)
8
9func TestAsRepoSearch(t *testing.T) {
10 cases := []struct {
11 query string
12 wantOK bool
13 wantStr string // rewritten repo-search query, only checked when wantOK
14 }{
15 {"repo:foo", true, "foo"},
16 {"repo:foo repo:bar", true, "foo bar"},
17 {"repo:foo lang:go", true, "foo lang:Go"},
18 {"lang:go", false, ""},
19 {"repo:foo bar", false, ""},
20 {"repo:foo file:x", false, ""},
21 {"file:x", false, ""},
22 {"type:repo foo", true, "foo"},
23 {"foo", false, ""},
24 {"branch:main", false, ""},
25 }
26
27 for _, tc := range cases {
28 t.Run(tc.query, func(t *testing.T) {
29 q, err := query.Parse(tc.query)
30 if err != nil {
31 t.Fatalf("parse %q: %v", tc.query, err)
32 }
33 rs, ok := asRepoSearch(q)
34 if ok != tc.wantOK {
35 t.Fatalf("asRepoSearch(%q) ok = %v, want %v", tc.query, ok, tc.wantOK)
36 }
37 if ok && rs.Query() != tc.wantStr {
38 t.Errorf("asRepoSearch(%q).Query() = %q, want %q", tc.query, rs.Query(), tc.wantStr)
39 }
40 })
41 }
42}
43
44func TestExtractDID(t *testing.T) {
45 cases := []struct {
46 tmpl string
47 want string
48 }{
49 {"https://tangled.org/did:plc:abc123/blob/{{.Version}}/{{.Path}}", "did:plc:abc123"},
50 {"http://localhost:3000/did:web:example.com/blob/{{.Version}}/{{.Path}}", "did:web:example.com"},
51 {"", ""},
52 }
53
54 for _, tc := range cases {
55 t.Run(tc.tmpl, func(t *testing.T) {
56 if got := extractDID(tc.tmpl); string(got) != tc.want {
57 t.Errorf("extractDID(%q) = %q, want %q", tc.tmpl, got, tc.want)
58 }
59 })
60 }
61}