Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from tdakkota/test/improve-coverage
fix: float decoding, improve coverage
- Loading branch information
Showing
19 changed files
with
510 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package jx | ||
|
||
// Bool reads a json object as Bool | ||
func (d *Decoder) Bool() (bool, error) { | ||
var buf [4]byte | ||
if err := d.readExact4(&buf); err != nil { | ||
return false, err | ||
} | ||
|
||
switch string(buf[:]) { | ||
case "true": | ||
return true, nil | ||
case "fals": | ||
c, err := d.byte() | ||
if err != nil { | ||
return false, err | ||
} | ||
if c != 'e' { | ||
return false, badToken(c) | ||
} | ||
return false, nil | ||
default: | ||
switch c := buf[0]; c { | ||
case 't': | ||
const encodedTrue = 't' | 'r'<<8 | 'u'<<16 | 'e'<<24 | ||
return false, findInvalidToken4(buf, encodedTrue) | ||
case 'f': | ||
const encodedAlse = 'a' | 'l'<<8 | 's'<<16 | 'e'<<24 | ||
return false, findInvalidToken4(buf, encodedAlse) | ||
default: | ||
return false, badToken(c) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jx | ||
|
||
import "testing" | ||
|
||
func TestDecoder_Bool(t *testing.T) { | ||
runTestCases(t, testBools, func(t *testing.T, d *Decoder) error { | ||
_, err := d.Bool() | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package jx | ||
|
||
import "github.com/go-faster/errors" | ||
|
||
// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9 | ||
const maxDepth = 10000 | ||
|
||
var errMaxDepth = errors.New("depth: maximum") | ||
|
||
func (d *Decoder) incDepth() error { | ||
d.depth++ | ||
if d.depth > maxDepth { | ||
return errMaxDepth | ||
} | ||
return nil | ||
} | ||
|
||
var errNegativeDepth = errors.New("depth: negative") | ||
|
||
func (d *Decoder) decDepth() error { | ||
d.depth-- | ||
if d.depth < 0 { | ||
return errNegativeDepth | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package jx | ||
|
||
// Null reads a json object as null and | ||
// returns whether it's a null or not. | ||
func (d *Decoder) Null() error { | ||
var buf [4]byte | ||
if err := d.readExact4(&buf); err != nil { | ||
return err | ||
} | ||
|
||
if string(buf[:]) != "null" { | ||
const encodedNull = 'n' | 'u'<<8 | 'l'<<16 | 'l'<<24 | ||
return findInvalidToken4(buf, encodedNull) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.