Skip to content

Commit f675a17

Browse files
Improve coverage and fix lint errors (#6)
* Add more badges into README * Fix lint issues * Add coverage badge * Add more comments * Improve test coverage more * More test coverage improvements
1 parent 3cbdc12 commit f675a17

13 files changed

+137
-49
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[![Circle CI](https://circleci.com/gh/segmentio/golines.svg?style=svg&circle-token=b1d01d8b035ef0aa71ccd183580586a80cd85271)](https://circleci.com/gh/segmentio/golines)
2+
[![Go Report Card](https://goreportcard.com/badge/github.com/segmentio/golines)](https://goreportcard.com/report/github.com/segmentio/golines)
3+
[![GoDoc](https://godoc.org/github.com/segmentio/golines?status.svg)](https://godoc.org/github.com/segmentio/golines)
4+
[![Coverage](https://img.shields.io/badge/Go%20Coverage-78%25-brightgreen.svg?longCache=true&style=flat)](https://github.com/jpoles1/gopherbadger)
5+
26
# golines
37
Golines is a golang formatter that shortens long lines, in addition to all
48
of the formatting fixes done by [`gofmt`](https://golang.org/cmd/gofmt/).

_fixtures/end_to_end.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func shortFunc(a int, b int) error {
7373
}
7474

7575
if a > 5 {
76-
panic(fmt.Sprintf(">>>>>>>>>>>>>>>>>>> %s %s %s", "really long argument", "another really long argument", "a third really long arguement"))
76+
panic(fmt.Sprintf(">>>>>>>>>>>>>>>>>>> %s %s %s %s", "really long argument", "another really long argument", "a third really long arguement", abc[1:2]))
7777
}
7878

7979
return nil

_fixtures/end_to_end__exp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ func shortFunc(a int, b int) error {
143143
if a > 5 {
144144
panic(
145145
fmt.Sprintf(
146-
">>>>>>>>>>>>>>>>>>> %s %s %s",
146+
">>>>>>>>>>>>>>>>>>> %s %s %s %s",
147147
"really long argument",
148148
"another really long argument",
149149
"a third really long arguement",
150+
abc[1:2],
150151
),
151152
)
152153
}

_fixtures/interface.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
package fixtures
22

3+
import "fmt"
4+
35
type MyEmptyInterface interface{}
46

57
type MyInterface2 interface {
68
aReallyLongFunctionName(argument1 string, argument2 string, argument3 string, argument4 string, argument5 string, argument6 string) (string, error)
79
shortFunc(x int, y int) error
810
}
11+
12+
func interfaceFuncs() {
13+
var m MyEmptyInterface
14+
_, ok := m.(MyInterface2)
15+
fmt.Println(ok)
16+
17+
switch m.(type) {
18+
case MyEmptyInterface:
19+
fmt.Println("Got MyEmptyInterface")
20+
}
21+
}

_fixtures/interface__exp.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fixtures
22

3+
import "fmt"
4+
35
type MyEmptyInterface interface{}
46

57
type MyInterface2 interface {
@@ -13,3 +15,14 @@ type MyInterface2 interface {
1315
) (string, error)
1416
shortFunc(x int, y int) error
1517
}
18+
19+
func interfaceFuncs() {
20+
var m MyEmptyInterface
21+
_, ok := m.(MyInterface2)
22+
fmt.Println(ok)
23+
24+
switch m.(type) {
25+
case MyEmptyInterface:
26+
fmt.Println("Got MyEmptyInterface")
27+
}
28+
}

annotation.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ import (
1010

1111
const annotationPrefix = "// __golines:shorten:"
1212

13+
// CreateAnnotation generates the text of a comment that will annotate long lines.
1314
func CreateAnnotation(length int) string {
1415
return fmt.Sprintf(
15-
"%s:%d",
16+
"%s%d",
1617
annotationPrefix,
1718
length,
1819
)
1920
}
2021

22+
// IsAnnotation determines whether the given line is an annotation created with CreateAnnotation.
2123
func IsAnnotation(line string) bool {
2224
return strings.HasPrefix(
2325
strings.Trim(line, " \t"),
2426
annotationPrefix,
2527
)
2628
}
2729

30+
// HasAnnotation determines whether the given AST node has a line length annotation on it.
2831
func HasAnnotation(node dst.Node) bool {
2932
startDecorations := node.Decorations().Start.All()
3033
return len(startDecorations) > 0 &&
@@ -61,6 +64,8 @@ func HasAnnotationRecursive(node dst.Node) bool {
6164
return false
6265
}
6366

67+
// ParseAnnotation returns the line length encoded in a golines annotation. If none is found,
68+
// it returns -1.
6469
func ParseAnnotation(line string) int {
6570
if IsAnnotation(line) {
6671
components := strings.SplitN(line, ":", 3)

annotation_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/dave/dst"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAnnotationStrings(t *testing.T) {
11+
assert.Equal(t, "// __golines:shorten:5", CreateAnnotation(5))
12+
assert.Equal(t, 5, ParseAnnotation("// __golines:shorten:5"))
13+
assert.Equal(t, -1, ParseAnnotation("// __golines:shorten:not_a_number"))
14+
assert.Equal(t, -1, ParseAnnotation("// not an annotation"))
15+
assert.True(t, IsAnnotation("// __golines:shorten:5"))
16+
assert.False(t, IsAnnotation("// not an annotation"))
17+
}
18+
19+
func TestHasAnnotation(t *testing.T) {
20+
node1 := &dst.Ident{
21+
Name: "x",
22+
Decs: dst.IdentDecorations{
23+
NodeDecs: dst.NodeDecs{
24+
Start: []string{
25+
"// not an annotation",
26+
CreateAnnotation(55),
27+
},
28+
},
29+
},
30+
}
31+
assert.True(t, HasAnnotation(node1))
32+
33+
node2 := &dst.Ident{
34+
Name: "x",
35+
Decs: dst.IdentDecorations{
36+
NodeDecs: dst.NodeDecs{
37+
Start: []string{
38+
"// not an annotation",
39+
},
40+
},
41+
},
42+
}
43+
assert.False(t, HasAnnotation(node2))
44+
}

diff_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPrettyDiff(t *testing.T) {
10+
// For now, this just tests that the script runs without error
11+
err := PrettyDiff(
12+
"test_path.txt",
13+
[]byte("line 1\nline 2"),
14+
[]byte("line 1\nline 2 modified"),
15+
)
16+
assert.Nil(t, err)
17+
}

generate/graph.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sort"
55

66
"github.com/dave/dst/gendst/data"
7-
. "github.com/dave/jennifer/jen"
7+
"github.com/dave/jennifer/jen"
88
)
99

1010
const (
@@ -15,69 +15,69 @@ const (
1515
// graph node. The latter can then be used to generate a dot file for debugging
1616
// purposes.
1717
func genNodeToGraphNode() error {
18-
f := NewFile("main")
18+
f := jen.NewFile("main")
1919
f.HeaderComment("Generated by generate/main.go. DO NOT EDIT.")
2020

2121
// Sort the keys in the dst map so that output is in stable order
2222
dataKeys := []string{}
2323

24-
for key, _ := range data.Info {
24+
for key := range data.Info {
2525
dataKeys = append(dataKeys, key)
2626
}
2727

2828
sort.Strings(dataKeys)
2929

3030
f.Func().Id("NodeToGraphNode").Params(
31-
Id("node").Qual(dstPath, "Node"),
31+
jen.Id("node").Qual(dstPath, "Node"),
3232
).Op("*").Id("GraphNode").BlockFunc(
33-
func(g *Group) {
33+
func(g *jen.Group) {
3434
g.Id("gNode").Op(":=").Op("&").Id("GraphNode").Values(
35-
Id("Node").Op(":").Id("node"),
36-
Id("Edges").Op(":").Index().Op("*").Id("GraphEdge").Values(),
35+
jen.Id("Node").Op(":").Id("node"),
36+
jen.Id("Edges").Op(":").Index().Op("*").Id("GraphEdge").Values(),
3737
)
3838

3939
g.Var().Id("child").Op("*").Id("GraphNode")
4040

41-
g.Switch(Id("n").Op(":=").Id("node").Assert(Id("type"))).BlockFunc(
42-
func(g *Group) {
41+
g.Switch(jen.Id("n").Op(":=").Id("node").Assert(jen.Id("type"))).BlockFunc(
42+
func(g *jen.Group) {
4343
for _, key := range dataKeys {
4444
parts := data.Info[key]
4545

46-
g.Case(Op("*").Qual(dstPath, key)).BlockFunc(
47-
func(g *Group) {
46+
g.Case(jen.Op("*").Qual(dstPath, key)).BlockFunc(
47+
func(g *jen.Group) {
4848
g.Id("gNode").Dot("Type").Op("=").Lit(key)
4949

5050
for _, part := range parts {
5151
switch p := part.(type) {
5252
case data.Node:
5353
g.If(p.Field.Get("n").Op("!=").Nil()).Block(
54-
Id("child").Op("=").Id("NodeToGraphNode").
54+
jen.Id("child").Op("=").Id("NodeToGraphNode").
5555
Call(
5656
p.Field.Get("n"),
5757
),
58-
Id("gNode").Dot("Edges").Op("=").Append(
59-
Id("gNode").Dot("Edges"),
60-
Op("&").Id("GraphEdge").Values(
61-
Id("Dest").Op(":").Id("child"),
62-
Id("Relationship").Op(":").Lit(p.Name),
58+
jen.Id("gNode").Dot("Edges").Op("=").Append(
59+
jen.Id("gNode").Dot("Edges"),
60+
jen.Op("&").Id("GraphEdge").Values(
61+
jen.Id("Dest").Op(":").Id("child"),
62+
jen.Id("Relationship").Op(":").Lit(p.Name),
6363
),
6464
),
6565
)
6666
case data.List:
6767
g.If(p.Field.Get("n").Op("!=").Nil()).Block(
68-
For(
69-
List(Id("_"), Id("obj")).
68+
jen.For(
69+
jen.List(jen.Id("_"), jen.Id("obj")).
7070
Op(":=").Range().Add(p.Field.Get("n")),
7171
).Block(
72-
Id("child").Op("=").Id("NodeToGraphNode").
72+
jen.Id("child").Op("=").Id("NodeToGraphNode").
7373
Call(
74-
Id("obj"),
74+
jen.Id("obj"),
7575
),
76-
Id("gNode").Dot("Edges").Op("=").Append(
77-
Id("gNode").Dot("Edges"),
78-
Op("&").Id("GraphEdge").Values(
79-
Id("Dest").Op(":").Id("child"),
80-
Id("Relationship").Op(":").Lit(p.Name),
76+
jen.Id("gNode").Dot("Edges").Op("=").Append(
77+
jen.Id("gNode").Dot("Edges"),
78+
jen.Op("&").Id("GraphEdge").Values(
79+
jen.Id("Dest").Op(":").Id("child"),
80+
jen.Id("Relationship").Op(":").Lit(p.Name),
8181
),
8282
),
8383
),
@@ -92,8 +92,8 @@ func genNodeToGraphNode() error {
9292
)
9393
}
9494
g.Default().Block(
95-
Qual("log", "Println").Call(
96-
Lit("Unrecognized type"),
95+
jen.Qual("log", "Println").Call(
96+
jen.Lit("Unrecognized type"),
9797
),
9898
)
9999
},

graph_generated.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
package main
44

55
import (
6-
"log"
7-
86
dst "github.com/dave/dst"
7+
"log"
98
)
109

1110
func NodeToGraphNode(node dst.Node) *GraphNode {

0 commit comments

Comments
 (0)