Skip to content

Commit

Permalink
update & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Jun 9, 2017
1 parent c349e0b commit 85f065e
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions assets/failStr7a_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
# invalid string containing a newline
foo : "
"
}
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions assets/mltabs_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar\tjoe\noki\tdoki\n\t\ttwo tabs"
}
3 changes: 0 additions & 3 deletions assets/pass5_test.json

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions assets/sorted/strings_result.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
number: 5
yes: true
}
notml1: "\n"
notml2: " \n"
notml3: "\n \n \n \n"
notml4: "\t\n"
special:
{
false: "false"
Expand Down
4 changes: 4 additions & 0 deletions assets/sorted/strings_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"number": 5,
"yes": true
},
"notml1": "\n",
"notml2": " \n",
"notml3": "\n \n \n \n",
"notml4": "\t\n",
"special": {
"false": "false",
"minus": "-3",
Expand Down
4 changes: 4 additions & 0 deletions assets/strings_result.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
text4a: " untrimmed "
text4b: " untrimmed"
text4c: "untrimmed "
notml1: "\n"
notml2: " \n"
notml3: "\n \n \n \n"
notml4: "\t\n"
multiline1:
'''
first line
Expand Down
4 changes: 4 additions & 0 deletions assets/strings_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"text4a": " untrimmed ",
"text4b": " untrimmed",
"text4c": "untrimmed ",
"notml1": "\n",
"notml2": " \n",
"notml3": "\n \n \n \n",
"notml4": "\t\n",
"multiline1": "first line\n indented line\nlast line",
"multiline2": "first line\n indented line\nlast line",
"multiline3": "first line\n indented line\nlast line\n",
Expand Down
5 changes: 5 additions & 0 deletions assets/strings_test.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
text4b: " untrimmed"
text4c: "untrimmed "

notml1: "\n"
notml2: " \n"
notml3: "\n \n \n \n"
notml4: "\t\n"

# multiline string

multiline1:
Expand Down
12 changes: 10 additions & 2 deletions assets/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,24 @@ failStr6a_test.hjson
failStr6b_test.hjson
failStr6c_test.hjson
failStr6d_test.hjson
failStr7a_test.hjson
kan_test.hjson
keys_test.hjson
mltabs_test.json
oa_test.hjson
pass1_test.json
pass2_test.json
pass3_test.json
pass4_test.json
pass5_test.json
passSingle_test.hjson
root_test.hjson
stringify1_test.hjson
strings_test.hjson
trail_test.hjson
trail_test.hjson
stringify/quotes_all_test.hjson
stringify/quotes_always_test.hjson
stringify/quotes_keys_test.hjson
stringify/quotes_strings_ml_test.json
stringify/quotes_strings_test.hjson
extra/notabs_test.json
extra/separator_test.json
2 changes: 2 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (p *hjsonParser) readString() (string, error) {
} else {
return "", p.errAt("Bad escape \\" + string(p.ch))
}
} else if p.ch == '\n' || p.ch == '\r' {
return "", p.errAt("Bad string containing newline");
} else {
res.WriteByte(p.ch)
}
Expand Down
4 changes: 2 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func init() {
needsEscape = regexp.MustCompile(`[\\\"\x00-\x1f` + commonRange + `]`)
// needsQuotes tests if the string can be written as a quoteless string (includes needsEscape but without \\ and \")
needsQuotes = regexp.MustCompile(`^\s|^"|^'''|^#|^/\*|^//|^\{|^\}|^\[|^\]|^:|^,|\s$|[\x00-\x1f\x7f-\x9f\x{00ad}\x{0600}-\x{0604}\x{070f}\x{17b4}\x{17b5}\x{200c}-\x{200f}\x{2028}-\x{202f}\x{2060}-\x{206f}\x{feff}\x{fff0}-\x{ffff}]`)
// needsEscapeML tests if the string can be written as a multiline string (includes needsEscape but without \n, \r, \\ and \")
// needsEscapeML tests if the string can be written as a multiline string (like needsEscape but without \n, \r, \\, \", \t)
var x08Or9 = `\x08` // `\x09` for the old behavior
needsEscapeML = regexp.MustCompile(`'''|^[\\s]+$|[\x00-` + x08Or9 + `\x0b\x0c\x0e-\x1f` + commonRange + `]`)
needsEscapeML = regexp.MustCompile(`'''|^[\s]+$|[\x00-` + x08Or9 + `\x0b\x0c\x0e-\x1f` + commonRange + `]`)
// starts with a keyword and optionally is followed by a comment
startsWithKeyword = regexp.MustCompile(`^(true|false|null)\s*((,|\]|\}|#|//|/\*).*)?$`)
needsEscapeName = regexp.MustCompile(`[,\{\[\}\]\s:#"]|//|/\*|'''`)
Expand Down
4 changes: 3 additions & 1 deletion hjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func TestHjson(t *testing.T) {
files := strings.Split(string(getContent("assets/testlist.txt")), "\n")

for _, file := range files {
run(t, file)
if !strings.HasPrefix(file, "stringify/quotes") && !strings.HasPrefix(file, "extra/") {
run(t, file)
}
}
}

0 comments on commit 85f065e

Please sign in to comment.