Skip to content

Commit

Permalink
Add JSON test.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg committed Jan 21, 2024
1 parent 4c2f1a8 commit 3c2123d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
46 changes: 46 additions & 0 deletions cmd/decouple/decouple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"bytes"
"encoding/json"
"path/filepath"
"reflect"
"testing"
)

func TestRun(t *testing.T) {
buf := new(bytes.Buffer)
if err := run(buf, false, true, []string{"../.."}); err != nil {
t.Fatal(err)
}

var (
got []jtuple
dec = json.NewDecoder(buf)
)
for dec.More() {
var val jtuple
if err := dec.Decode(&val); err != nil {
t.Fatal(err)
}
val.FileName = filepath.Base(val.FileName)
got = append(got, val)
}

want := []jtuple{{
PackageName: "main",
FileName: "main.go",
Line: 100,
FuncName: "showJSON",
Params: []jparam{{
Name: "checker",
Methods: []string{
"NameForMethods",
},
}},
}}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
41 changes: 23 additions & 18 deletions cmd/decouple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"sort"

"github.com/bobg/errors"
"github.com/bobg/go-generics/v3/maps"

"github.com/bobg/decouple"
Expand All @@ -21,28 +23,32 @@ func main() {
flag.BoolVar(&doJSON, "json", false, "output in JSON format")
flag.Parse()

if err := run(os.Stdout, verbose, doJSON, flag.Args()); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func run(w io.Writer, verbose, doJSON bool, args []string) error {
var dir string
switch flag.NArg() {
switch len(args) {
case 0:
dir = "."
case 1:
dir = flag.Arg(0)
dir = args[0]
default:
fmt.Fprintf(os.Stderr, "Usage: %s [-v] [DIR]\n", os.Args[0])
os.Exit(1)
return fmt.Errorf("Usage: %s [-v] [-json] [DIR]", os.Args[0])
}

checker, err := decouple.NewCheckerFromDir(dir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return errors.Wrapf(err, "creating checker for %s", dir)
}
checker.Verbose = verbose

tuples, err := checker.Check()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return errors.Wrapf(err, "checking %s", dir)
}

sort.Slice(tuples, func(i, j int) bool {
Expand All @@ -57,11 +63,8 @@ func main() {
})

if doJSON {
if err := showJSON(checker, tuples); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
err := showJSON(w, checker, tuples)
return errors.Wrap(err, "formatting JSON output")
}

for _, tuple := range tuples {
Expand All @@ -76,24 +79,26 @@ func main() {
}

if !showedFuncName {
fmt.Printf("%s: %s\n", tuple.Pos(), tuple.F.Name.Name)
fmt.Fprintf(w, "%s: %s\n", tuple.Pos(), tuple.F.Name.Name)
showedFuncName = true
}

if intfName := checker.NameForMethods(mm); intfName != "" {
fmt.Printf(" %s: %s\n", param, intfName)
fmt.Fprintf(w, " %s: %s\n", param, intfName)
continue
}

methods := maps.Keys(tuple.M[param])
sort.Strings(methods)
fmt.Printf(" %s: %v\n", param, methods)
fmt.Fprintf(w, " %s: %v\n", param, methods)
}
}

return nil
}

func showJSON(checker decouple.Checker, tuples []decouple.Tuple) error {
enc := json.NewEncoder(os.Stdout)
func showJSON(w io.Writer, checker decouple.Checker, tuples []decouple.Tuple) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")

for _, tuple := range tuples {
Expand Down

0 comments on commit 3c2123d

Please sign in to comment.