Skip to content

Commit 99d5bb6

Browse files
committed
tests: improve code coverage
1 parent b38b222 commit 99d5bb6

File tree

6 files changed

+52
-25
lines changed

6 files changed

+52
-25
lines changed

dag.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package goabnf
22

3-
import "strings"
3+
import (
4+
"fmt"
5+
"strings"
6+
)
47

58
type node struct {
69
Rulename string
@@ -66,6 +69,18 @@ func appendDeps(deps []string, ndeps ...string) []string {
6669
return deps
6770
}
6871

72+
// Mermaid returns a flowchart of the dependency graph.
73+
func (dg Depgraph) Mermaid() string {
74+
out := "flowchart TD\n"
75+
for _, node := range dg {
76+
for _, dep := range node.Dependencies {
77+
out += fmt.Sprintf("\t%s --> %s\n", node.Rulename, dep)
78+
}
79+
out += "\n"
80+
}
81+
return out
82+
}
83+
6984
// IsDag find Strongly Connected Components using Tarjan's algorithm
7085
// and returns whether it contains cycle or not.
7186
// Could be improved by Nuutila's or Pearce's algorithms, or replaced
@@ -247,7 +262,8 @@ func (c *cycle) strongconnect(v *node) {
247262
for _, dep := range v.Dependencies {
248263
w, ok := c.dg[dep]
249264
if !ok {
250-
// core rules
265+
// core rules, as we know they won't have a cycle thus
266+
// no SCC, we don't need to recurse.
251267
continue
252268
}
253269
if w.index == 0 {

dag_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func Test_U_IsDAG(t *testing.T) {
4444
if !assert.Nil(err) {
4545
t.FailNow()
4646
}
47+
// Side checks that this dependency graph does not produce an
48+
// empty dependency graph. It should not be, at any time,
49+
// as only the diagram header is a non-empty content.
50+
mrmd := g.DependencyGraph().Mermaid()
51+
assert.NotEmpty(mrmd)
52+
// Do the same with the pretty print.
53+
ptp := g.PrettyPrint()
54+
assert.NotEmpty(ptp)
4755

4856
isDag := g.IsDAG()
4957
assert.Equal(tt.ExpectedIsDag, isDag)

grammar_fuzz_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ func FuzzParseABNF_Generate(f *testing.F) {
4343
t.Fatalf("For input %s, got error %s", input, err)
4444
}
4545
return
46-
} else {
47-
if g == nil {
48-
t.Fatal("Expected a path when no error")
49-
}
46+
}
47+
if g == nil {
48+
t.Fatal("Expected a grammar when no error")
49+
return
5050
}
5151
})
5252
}

grammar_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ func Test_U_ParseABNF(t *testing.T) {
157157
assert.NotEmpty(tt.Input)
158158
_, err := goabnf.ParseABNF(tt.Input, goabnf.WithValidation(tt.Validate))
159159

160-
if tt.ExpectErr {
161-
assert.NotNil(err)
162-
} else {
163-
assert.Nil(err)
160+
if (err != nil) != tt.ExpectErr {
161+
t.Fatalf("Expected err: %t ; got: %s", tt.ExpectErr, err)
164162
}
165163
})
166164
}

mermaid.go

-15
This file was deleted.

regex_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ var testsRegex = map[string]struct {
1818
Rulename: "alpha",
1919
ExpectErr: false,
2020
},
21+
"cycle": {
22+
Grammar: mustGrammar(string(cycleAbnf)),
23+
Rulename: "a",
24+
ExpectErr: true,
25+
},
26+
"void": {
27+
Grammar: mustGrammar(string(voidAbnf)),
28+
Rulename: "",
29+
ExpectErr: true,
30+
},
31+
"nocycle": {
32+
Grammar: mustGrammar(string(nocycleAbnf)),
33+
Rulename: "a",
34+
ExpectErr: false,
35+
},
36+
"group-option": {
37+
Grammar: mustGrammar("a = 1*(*[\"b.\"] *3%x61.7a)\r\n"),
38+
Rulename: "a",
39+
ExpectErr: false,
40+
},
2141
}
2242

2343
func Test_U_Regex(t *testing.T) {

0 commit comments

Comments
 (0)