Add go report card
This commit is contained in:
parent
b2713a0653
commit
60dd11deee
@ -64,7 +64,7 @@ func init() {
|
||||
value = Any(number, groupExpr)
|
||||
}
|
||||
|
||||
func Calc(input string) (float64, error) {
|
||||
func calc(input string) (float64, error) {
|
||||
result, err := Run(y, input)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -7,48 +7,48 @@ import (
|
||||
)
|
||||
|
||||
func TestNumbers(t *testing.T) {
|
||||
result, err := Calc(`1`)
|
||||
result, err := calc(`1`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 1, result)
|
||||
}
|
||||
|
||||
func TestAddition(t *testing.T) {
|
||||
result, err := Calc(`1+1`)
|
||||
result, err := calc(`1+1`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 2, result)
|
||||
}
|
||||
|
||||
func TestSubtraction(t *testing.T) {
|
||||
result, err := Calc(`1-1`)
|
||||
result, err := calc(`1-1`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 0, result)
|
||||
}
|
||||
|
||||
func TestDivision(t *testing.T) {
|
||||
result, err := Calc(`1/2`)
|
||||
result, err := calc(`1/2`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, .5, result)
|
||||
}
|
||||
|
||||
func TestMultiplication(t *testing.T) {
|
||||
result, err := Calc(`1*2`)
|
||||
result, err := calc(`1*2`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 2, result)
|
||||
}
|
||||
|
||||
func TestOrderOfOperations(t *testing.T) {
|
||||
result, err := Calc(`1+10*2`)
|
||||
result, err := calc(`1+10*2`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 21, result)
|
||||
}
|
||||
func TestParenthesis(t *testing.T) {
|
||||
result, err := Calc(`(1+10)*2`)
|
||||
result, err := calc(`(1+10)*2`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 22, result)
|
||||
}
|
||||
|
||||
func TestRecursive(t *testing.T) {
|
||||
result, err := Calc(`(1+(2*(3-(4/(5)))))`)
|
||||
result, err := calc(`(1+(2*(3-(4/(5)))))`)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 5.4, result)
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ func TestMap(t *testing.T) {
|
||||
return Result{Result: htmlTag{n.Child[1].Token}}
|
||||
})
|
||||
|
||||
t.Run("sucess", func(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
result, _ := runParser("<html>", parser)
|
||||
require.Equal(t, htmlTag{"html"}, result.Result)
|
||||
})
|
||||
@ -149,7 +149,7 @@ func TestMap(t *testing.T) {
|
||||
func TestBind(t *testing.T) {
|
||||
parser := Bind("true", true)
|
||||
|
||||
t.Run("sucess", func(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
result, _ := runParser("true", parser)
|
||||
require.Equal(t, true, result.Result)
|
||||
})
|
||||
@ -187,7 +187,7 @@ func TestMerge(t *testing.T) {
|
||||
bracer = Seq("(", Maybe(&bracer), ")")
|
||||
parser := Merge(bracer)
|
||||
|
||||
t.Run("sucess", func(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
result, _ := runParser("((()))", parser)
|
||||
require.Equal(t, "((()))", result.Token)
|
||||
})
|
||||
|
@ -18,9 +18,9 @@ func getPackageName(f runtime.Frame) string {
|
||||
|
||||
if parts[pl-2][0] == '(' {
|
||||
return strings.Join(parts[0:pl-2], ".")
|
||||
} else {
|
||||
return strings.Join(parts[0:pl-1], ".")
|
||||
}
|
||||
|
||||
return strings.Join(parts[0:pl-1], ".")
|
||||
}
|
||||
|
||||
func getVarName(filename string, lineNo int) string {
|
||||
|
@ -4,11 +4,11 @@ import (
|
||||
. "github.com/vektah/goparsify"
|
||||
)
|
||||
|
||||
func Parse(input string) (result interface{}, err error) {
|
||||
func parse(input string) (result interface{}, err error) {
|
||||
return Run(tag, input)
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
type htmlTag struct {
|
||||
Name string
|
||||
Attributes map[string]string
|
||||
Body []interface{}
|
||||
@ -49,7 +49,7 @@ var (
|
||||
func init() {
|
||||
tag = Map(Seq(tstart, Cut(), elements, tend), func(node Result) Result {
|
||||
openTag := node.Child[0]
|
||||
return Result{Result: Tag{
|
||||
return Result{Result: htmlTag{
|
||||
Name: openTag.Child[1].Token,
|
||||
Attributes: openTag.Child[3].Result.(map[string]string),
|
||||
Body: node.Child[2].Result.([]interface{}),
|
||||
|
@ -7,10 +7,10 @@ import (
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
result, err := Parse(`<body>hello <p color="blue">world</p></body>`)
|
||||
result, err := parse(`<body>hello <p color="blue">world</p></body>`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, Tag{Name: "body", Attributes: map[string]string{}, Body: []interface{}{
|
||||
require.Equal(t, htmlTag{Name: "body", Attributes: map[string]string{}, Body: []interface{}{
|
||||
"hello ",
|
||||
Tag{Name: "p", Attributes: map[string]string{"color": "blue"}, Body: []interface{}{"world"}},
|
||||
htmlTag{Name: "p", Attributes: map[string]string{"color": "blue"}, Body: []interface{}{"world"}},
|
||||
}}, result)
|
||||
}
|
||||
|
@ -36,6 +36,6 @@ func init() {
|
||||
_value = Any(_null, _true, _false, _string, _number, _array, _object)
|
||||
}
|
||||
|
||||
func Unmarshal(input string) (interface{}, error) {
|
||||
func unmarshal(input string) (interface{}, error) {
|
||||
return Run(_value, input, ASCIIWhitespace)
|
||||
}
|
||||
|
@ -11,31 +11,31 @@ import (
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
t.Run("basic types", func(t *testing.T) {
|
||||
result, err := Unmarshal(`true`)
|
||||
result, err := unmarshal(`true`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, true, result)
|
||||
|
||||
result, err = Unmarshal(`false`)
|
||||
result, err = unmarshal(`false`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, false, result)
|
||||
|
||||
result, err = Unmarshal(`null`)
|
||||
result, err = unmarshal(`null`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, nil, result)
|
||||
|
||||
result, err = Unmarshal(`"true"`)
|
||||
result, err = unmarshal(`"true"`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "true", result)
|
||||
})
|
||||
|
||||
t.Run("array", func(t *testing.T) {
|
||||
result, err := Unmarshal(`[true, null, false, -1.23e+4]`)
|
||||
result, err := unmarshal(`[true, null, false, -1.23e+4]`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []interface{}{true, nil, false, -1.23e+4}, result)
|
||||
})
|
||||
|
||||
t.Run("object", func(t *testing.T) {
|
||||
result, err := Unmarshal(`{"true":true, "false":false, "null": null, "number": 404} `)
|
||||
result, err := unmarshal(`{"true":true, "false":false, "null": null, "number": 404} `)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, map[string]interface{}{"true": true, "false": false, "null": nil, "number": int64(404)}, result)
|
||||
})
|
||||
@ -54,7 +54,7 @@ func BenchmarkUnmarshalParsec(b *testing.B) {
|
||||
|
||||
func BenchmarkUnmarshalParsify(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := Unmarshal(benchmarkString)
|
||||
_, err := unmarshal(benchmarkString)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
goparsify.DumpDebugStats()
|
||||
|
@ -48,7 +48,7 @@ func main() {
|
||||
}
|
||||
|
||||
for i := 0; i < max; i++ {
|
||||
_, err := json.Unmarshal(benchmarkString)
|
||||
_, err := json.unmarshal(benchmarkString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -171,7 +171,7 @@ func parseRepetition(defaultMin, defaultMax int, repetition ...int) (min int, ma
|
||||
min = repetition[0]
|
||||
max = repetition[1]
|
||||
default:
|
||||
panic(fmt.Errorf("Dont know what %d repetion args mean", len(repetition)))
|
||||
panic(fmt.Errorf("Dont know what %d repetition args mean", len(repetition)))
|
||||
}
|
||||
return min, max
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
goparsify [![CircleCI](https://circleci.com/gh/Vektah/goparsify/tree/master.svg?style=shield)](https://circleci.com/gh/Vektah/goparsify/tree/master) [![godoc](http://b.repl.ca/v1/godoc-reference-blue.png)](https://godoc.org/github.com/Vektah/goparsify)
|
||||
goparsify [![CircleCI](https://circleci.com/gh/Vektah/goparsify/tree/master.svg?style=shield)](https://circleci.com/gh/Vektah/goparsify/tree/master) [![godoc](http://b.repl.ca/v1/godoc-reference-blue.png)](https://godoc.org/github.com/Vektah/goparsify) [![Go Report Card](https://goreportcard.com/badge/github.com/vektah/goparsify)](https://goreportcard.com/report/github.com/vektah/goparsify)
|
||||
=========
|
||||
|
||||
A parser-combinator library for building easy to test, read and maintain parsers using functional composition.
|
||||
|
Loading…
Reference in New Issue
Block a user