Add go report card

This commit is contained in:
Adam Scarr 2017-08-11 00:08:08 +10:00
parent b2713a0653
commit 60dd11deee
11 changed files with 31 additions and 31 deletions

View File

@ -64,7 +64,7 @@ func init() {
value = Any(number, groupExpr) value = Any(number, groupExpr)
} }
func Calc(input string) (float64, error) { func calc(input string) (float64, error) {
result, err := Run(y, input) result, err := Run(y, input)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -7,48 +7,48 @@ import (
) )
func TestNumbers(t *testing.T) { func TestNumbers(t *testing.T) {
result, err := Calc(`1`) result, err := calc(`1`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 1, result) require.EqualValues(t, 1, result)
} }
func TestAddition(t *testing.T) { func TestAddition(t *testing.T) {
result, err := Calc(`1+1`) result, err := calc(`1+1`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 2, result) require.EqualValues(t, 2, result)
} }
func TestSubtraction(t *testing.T) { func TestSubtraction(t *testing.T) {
result, err := Calc(`1-1`) result, err := calc(`1-1`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 0, result) require.EqualValues(t, 0, result)
} }
func TestDivision(t *testing.T) { func TestDivision(t *testing.T) {
result, err := Calc(`1/2`) result, err := calc(`1/2`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, .5, result) require.EqualValues(t, .5, result)
} }
func TestMultiplication(t *testing.T) { func TestMultiplication(t *testing.T) {
result, err := Calc(`1*2`) result, err := calc(`1*2`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 2, result) require.EqualValues(t, 2, result)
} }
func TestOrderOfOperations(t *testing.T) { func TestOrderOfOperations(t *testing.T) {
result, err := Calc(`1+10*2`) result, err := calc(`1+10*2`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 21, result) require.EqualValues(t, 21, result)
} }
func TestParenthesis(t *testing.T) { func TestParenthesis(t *testing.T) {
result, err := Calc(`(1+10)*2`) result, err := calc(`(1+10)*2`)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 22, result) require.EqualValues(t, 22, result)
} }
func TestRecursive(t *testing.T) { 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.NoError(t, err)
require.EqualValues(t, 5.4, result) require.EqualValues(t, 5.4, result)
} }

View File

@ -134,7 +134,7 @@ func TestMap(t *testing.T) {
return Result{Result: htmlTag{n.Child[1].Token}} 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) result, _ := runParser("<html>", parser)
require.Equal(t, htmlTag{"html"}, result.Result) require.Equal(t, htmlTag{"html"}, result.Result)
}) })
@ -149,7 +149,7 @@ func TestMap(t *testing.T) {
func TestBind(t *testing.T) { func TestBind(t *testing.T) {
parser := Bind("true", true) parser := Bind("true", true)
t.Run("sucess", func(t *testing.T) { t.Run("success", func(t *testing.T) {
result, _ := runParser("true", parser) result, _ := runParser("true", parser)
require.Equal(t, true, result.Result) require.Equal(t, true, result.Result)
}) })
@ -187,7 +187,7 @@ func TestMerge(t *testing.T) {
bracer = Seq("(", Maybe(&bracer), ")") bracer = Seq("(", Maybe(&bracer), ")")
parser := Merge(bracer) parser := Merge(bracer)
t.Run("sucess", func(t *testing.T) { t.Run("success", func(t *testing.T) {
result, _ := runParser("((()))", parser) result, _ := runParser("((()))", parser)
require.Equal(t, "((()))", result.Token) require.Equal(t, "((()))", result.Token)
}) })

View File

@ -18,9 +18,9 @@ func getPackageName(f runtime.Frame) string {
if parts[pl-2][0] == '(' { if parts[pl-2][0] == '(' {
return strings.Join(parts[0:pl-2], ".") 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 { func getVarName(filename string, lineNo int) string {

View File

@ -4,11 +4,11 @@ import (
. "github.com/vektah/goparsify" . "github.com/vektah/goparsify"
) )
func Parse(input string) (result interface{}, err error) { func parse(input string) (result interface{}, err error) {
return Run(tag, input) return Run(tag, input)
} }
type Tag struct { type htmlTag struct {
Name string Name string
Attributes map[string]string Attributes map[string]string
Body []interface{} Body []interface{}
@ -49,7 +49,7 @@ var (
func init() { func init() {
tag = Map(Seq(tstart, Cut(), elements, tend), func(node Result) Result { tag = Map(Seq(tstart, Cut(), elements, tend), func(node Result) Result {
openTag := node.Child[0] openTag := node.Child[0]
return Result{Result: Tag{ return Result{Result: htmlTag{
Name: openTag.Child[1].Token, Name: openTag.Child[1].Token,
Attributes: openTag.Child[3].Result.(map[string]string), Attributes: openTag.Child[3].Result.(map[string]string),
Body: node.Child[2].Result.([]interface{}), Body: node.Child[2].Result.([]interface{}),

View File

@ -7,10 +7,10 @@ import (
) )
func TestParse(t *testing.T) { 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.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 ", "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) }}, result)
} }

View File

@ -36,6 +36,6 @@ func init() {
_value = Any(_null, _true, _false, _string, _number, _array, _object) _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) return Run(_value, input, ASCIIWhitespace)
} }

View File

@ -11,31 +11,31 @@ import (
func TestUnmarshal(t *testing.T) { func TestUnmarshal(t *testing.T) {
t.Run("basic types", func(t *testing.T) { t.Run("basic types", func(t *testing.T) {
result, err := Unmarshal(`true`) result, err := unmarshal(`true`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, true, result) require.Equal(t, true, result)
result, err = Unmarshal(`false`) result, err = unmarshal(`false`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, false, result) require.Equal(t, false, result)
result, err = Unmarshal(`null`) result, err = unmarshal(`null`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, nil, result) require.Equal(t, nil, result)
result, err = Unmarshal(`"true"`) result, err = unmarshal(`"true"`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "true", result) require.Equal(t, "true", result)
}) })
t.Run("array", func(t *testing.T) { 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.NoError(t, err)
require.Equal(t, []interface{}{true, nil, false, -1.23e+4}, result) require.Equal(t, []interface{}{true, nil, false, -1.23e+4}, result)
}) })
t.Run("object", func(t *testing.T) { 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.NoError(t, err)
require.Equal(t, map[string]interface{}{"true": true, "false": false, "null": nil, "number": int64(404)}, result) 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) { func BenchmarkUnmarshalParsify(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := Unmarshal(benchmarkString) _, err := unmarshal(benchmarkString)
require.NoError(b, err) require.NoError(b, err)
} }
goparsify.DumpDebugStats() goparsify.DumpDebugStats()

View File

@ -48,7 +48,7 @@ func main() {
} }
for i := 0; i < max; i++ { for i := 0; i < max; i++ {
_, err := json.Unmarshal(benchmarkString) _, err := json.unmarshal(benchmarkString)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -171,7 +171,7 @@ func parseRepetition(defaultMin, defaultMax int, repetition ...int) (min int, ma
min = repetition[0] min = repetition[0]
max = repetition[1] max = repetition[1]
default: 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 return min, max
} }

View File

@ -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. A parser-combinator library for building easy to test, read and maintain parsers using functional composition.