Debian Codegraph @

cmd/modgraphviz/main_test.go

raw symbols SCIP
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
5 package main
6
7 import (
8 "bytes"
9 "fmt"
10 "reflect"
11 "testing"
12 )
13
14 func TestRun(t *testing.T) {
15 out := &bytes.Buffer{}
16 in := bytes.NewBuffer([]byte(`
17 test.com/[email protected] test.com/[email protected]
18 test.com/[email protected] test.com/[email protected]
19 `))
20 if err := modgraphviz(in, out); err != nil {
21 t.Fatal(err)
22 }
23
24 gotGraph := string(out.Bytes())
25 wantGraph := `digraph gomodgraph {
26 node [ shape=rectangle fontsize=12 ]
27 "test.com/[email protected]" -> "test.com/[email protected]"
28 "test.com/[email protected]" -> "test.com/[email protected]"
29 "test.com/[email protected]" [style = filled, fillcolor = green]
30 "test.com/[email protected]" [style = filled, fillcolor = green]
31 "test.com/[email protected]" [style = filled, fillcolor = green]
32 "test.com/[email protected]" [style = filled, fillcolor = gray]
33 }
34 `
35 if gotGraph != wantGraph {
36 t.Fatalf("\ngot: %s\nwant: %s", gotGraph, wantGraph)
37 }
38 }
39
40 func TestMVSPicking(t *testing.T) {
41 for _, tc := range []struct {
42 name string
43 in []string
44 wantPicked []string
45 wantUnpicked []string
46 }{
47 {
48 name: "single node",
49 in: []string{"[email protected]"},
50 wantPicked: []string{"[email protected]"},
51 wantUnpicked: nil,
52 },
53 {
54 name: "duplicate same node",
55 in: []string{"[email protected]", "[email protected]"},
56 wantPicked: []string{"[email protected]"},
57 wantUnpicked: nil,
58 },
59 {
60 name: "multiple semver - same major",
61 in: []string{"[email protected]", "[email protected]", "[email protected]", "[email protected]"},
62 wantPicked: []string{"[email protected]"},
63 wantUnpicked: []string{"[email protected]", "[email protected]", "[email protected]"},
64 },
65 {
66 name: "multiple semver - multiple major",
67 in: []string{"[email protected]", "[email protected]", "foo/[email protected]", "foo/[email protected]", "[email protected]"},
68 wantPicked: []string{"foo/[email protected]", "[email protected]"},
69 wantUnpicked: []string{"[email protected]", "foo/[email protected]", "[email protected]"},
70 },
71 {
72 name: "semver and pseudo version",
73 in: []string{"[email protected]", "[email protected]", "foo/[email protected]", "foo/[email protected]", "[email protected]", "[email protected]"},
74 wantPicked: []string{"foo/[email protected]", "[email protected]"},
75 wantUnpicked: []string{"[email protected]", "foo/[email protected]", "[email protected]", "[email protected]"},
76 },
77 {
78 name: "multiple pseudo version",
79 in: []string{
80 "[email protected]",
81 "[email protected]",
82 "[email protected]",
83 },
84 wantPicked: []string{"[email protected]"},
85 wantUnpicked: []string{
86 "[email protected]",
87 "[email protected]",
88 },
89 },
90 {
91 name: "semver and suffix",
92 in: []string{"[email protected]", "[email protected]", "[email protected]"},
93 wantPicked: []string{"[email protected]"},
94 wantUnpicked: []string{"[email protected]", "[email protected]"},
95 },
96 } {
97 t.Run(tc.name, func(t *testing.T) {
98 buf := bytes.Buffer{}
99 for _, node := range tc.in {
100 fmt.Fprintf(&buf, "A %s\n", node)
101 }
102
103 g, err := convert(&buf)
104 if err != nil {
105 t.Fatal(err)
106 }
107
108 if !reflect.DeepEqual(g.mvsPicked, tc.wantPicked) {
109 t.Fatalf("picked: got %v, want %v", g.mvsPicked, tc.wantPicked)
110 }
111 if !reflect.DeepEqual(g.mvsUnpicked, tc.wantUnpicked) {
112 t.Fatalf("unpicked: got %v, want %v", g.mvsUnpicked, tc.wantUnpicked)
113 }
114 })
115 }
116 }