Skip to content

Commit

Permalink
Merge remote-tracking branch 'sgarciac/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hunt committed Nov 4, 2018
2 parents caa558e + ddae9c6 commit 6c8334f
Show file tree
Hide file tree
Showing 54 changed files with 204 additions and 5 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ In the above, `TTYPE` may be one of:
* string
* integer
* float
* datetime
* datetime (for offset date-time)
* datetime-local
* date
* time
* bool
* array

Expand All @@ -101,7 +104,9 @@ and `TVALUE` is always a JSON string, except when `TTYPE` is `array` in which
Empty hashes correspond to empty JSON objects (i.e., `{}`) and empty arrays
correspond to empty JSON arrays (i.e., `[]`).

Datetime should be encoded following RFC 3339.
Offset datetimes should be encoded following RFC 3339. Local datetimes should be
encoded following RFC 3339, without the offset part. Local dates should be
encoded as the date part of RFC 3339 and Local times as the time part.

### Example JSON encoding

Expand Down
48 changes: 46 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"strconv"
"time"
"strings"
)

// compareJson consumes the recursive structure of both `expected` and `test`
Expand Down Expand Up @@ -137,6 +138,8 @@ func (r result) cmpJsonValues(e, t map[string]interface{}) result {
return r.cmpFloats(evalue, tvalue);
case "datetime":
return r.cmpAsDatetimes(evalue, tvalue);
case "datetime-local":
return r.cmpAsLocalDateTimes(evalue, tvalue);
default:
return r.cmpAsStrings(evalue, tvalue);
}
Expand All @@ -151,7 +154,48 @@ func (r result) cmpAsStrings(e, t string) result {
return r
}

func (r result) cmpAsLocalDateTimes(e, t string) result {
ce := strings.Replace(strings.Replace(e, "t", "T", 1), " ", "T", 1)
ct := strings.Replace(strings.Replace(t, "t", "T", 1), " ", "T", 1)

if ce != ct {
return r.failedf("Values for key '%s' don't match. Expected a "+
"value of '%s' but got '%s'.", r.key, e, t)
}
return r
}


func (r result) cmpFloats(e, t string) result {
// Handle Infinity and NaN
tc := strings.ToLower(t)
ec := strings.ToLower(e)

if ec == "nan" || ec == "-nan" || ec == "+nan" {
if tc == "nan" || tc == "-nan" || tc == "+nan" {
return r
} else {
return r.failedf("Value for key '%s' don't match. Expected either nan, -nan or +nan but got '%v'.", r.key, tc)
}
}

if ec == "inf" || ec == "+inf" {
if tc == "inf" || tc == "+inf" {
return r
} else {
return r.failedf("Value for key '%s' don't match. Expected inf or +inf but got '%v'.", r.key, tc)
}
}

if ec == "-inf" {
if tc == "-inf" {
return r
} else {
return r.failedf("Value for key '%s' don't match. Expected -inf but got '%v'.", r.key, tc)
}
}

// Else, compare as regular floats
ef, err := strconv.ParseFloat(e, 64)
if err != nil {
return r.failedf("BUG in test case. Could not read '%s' as a "+
Expand All @@ -173,13 +217,13 @@ func (r result) cmpFloats(e, t string) result {
func (r result) cmpAsDatetimes(e, t string) result {
var err error

ef, err := time.Parse(time.RFC3339Nano, e)
ef, err := time.Parse(time.RFC3339Nano, strings.Replace(e, " ", "T", 1))
if err != nil {
return r.failedf("BUG in test case. Could not read '%s' as a "+
"datetime value for key '%s'.", e, r.key)
}

tf, err := time.Parse(time.RFC3339Nano, t)
tf, err := time.Parse(time.RFC3339Nano, strings.Replace(t, " ", "T", 1))
if err != nil {
return r.failedf("Malformed parser output. Could not read '%s' "+
"as datetime value for key '%s'.", t, r.key)
Expand Down
1 change: 1 addition & 0 deletions tests/invalid/array-7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arr6 = [ 1, 2.0 ] # INVALID
4 changes: 4 additions & 0 deletions tests/invalid/array-of-tables-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# INVALID TOML DOC
fruit = []

[[fruit]] # Not allowed
10 changes: 10 additions & 0 deletions tests/invalid/array-of-tables-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# INVALID TOML DOC
[[fruit]]
name = "apple"

[[fruit.variety]]
name = "red delicious"

# This table conflicts with the previous table
[fruit.variety]
name = "granny smith"
1 change: 1 addition & 0 deletions tests/invalid/bare-key-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bare!key = 123
2 changes: 2 additions & 0 deletions tests/invalid/bare-key-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
barekey
= 123
1 change: 1 addition & 0 deletions tests/invalid/bare-key-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barekey =
1 change: 1 addition & 0 deletions tests/invalid/int-0-padded.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int = 0123
1 change: 1 addition & 0 deletions tests/invalid/key-value-pair-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key = # INVALID
3 changes: 3 additions & 0 deletions tests/invalid/multiple-dot-key.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# THIS IS INVALID
a.b = 1
a.b.c = 2
3 changes: 3 additions & 0 deletions tests/invalid/multiple-key.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DO NOT DO THIS
name = "Tom"
name = "Pradyun"
1 change: 1 addition & 0 deletions tests/invalid/no-key-name.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= "no key name" # INVALID
3 changes: 3 additions & 0 deletions tests/invalid/non-dec-integers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin1 = -0b11010110 # non decimal integers are only for non negative
oct1 = 0o_1
hex3 = 0xdeadbeeg
Binary file added tests/invalid/string-basic-control-1.toml
Binary file not shown.
1 change: 1 addition & 0 deletions tests/invalid/string-basic-control-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "null"
1 change: 1 addition & 0 deletions tests/invalid/string-basic-control-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "null"
1 change: 1 addition & 0 deletions tests/invalid/string-basic-control-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "null"
Binary file added tests/invalid/string-basic-multiline-control-1.toml
Binary file not shown.
1 change: 1 addition & 0 deletions tests/invalid/string-basic-multiline-control-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = """null"""
1 change: 1 addition & 0 deletions tests/invalid/string-basic-multiline-control-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = """null"""
1 change: 1 addition & 0 deletions tests/invalid/string-basic-multiline-control-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = """null"""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = """\UFFFFFFFF"""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = """\U00D80000"""
1 change: 1 addition & 0 deletions tests/invalid/string-basic-multiline-unknown-escape.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = """\@"""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "\UFFFFFFFF"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "\U00D80000"
1 change: 1 addition & 0 deletions tests/invalid/string-basic-unknown-escape.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = "\@"
Binary file added tests/invalid/string-literal-control-1.toml
Binary file not shown.
1 change: 1 addition & 0 deletions tests/invalid/string-literal-control-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 'null'
1 change: 1 addition & 0 deletions tests/invalid/string-literal-control-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 'null'
1 change: 1 addition & 0 deletions tests/invalid/string-literal-control-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 'null'
Binary file not shown.
1 change: 1 addition & 0 deletions tests/invalid/string-literal-multiline-control-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = '''null'''
1 change: 1 addition & 0 deletions tests/invalid/string-literal-multiline-control-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = '''null'''
1 change: 1 addition & 0 deletions tests/invalid/string-literal-multiline-control-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = '''null'''
7 changes: 7 additions & 0 deletions tests/invalid/table-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# DO NOT DO THIS

[a]
b = 1

[a]
c = 2
7 changes: 7 additions & 0 deletions tests/invalid/table-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# DO NOT DO THIS EITHER

[a]
b = 1

[a.b]
c = 2
3 changes: 2 additions & 1 deletion tests/valid/datetime.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"bestdayever": {"type": "datetime", "value": "1987-07-05T17:45:00Z"},
"numoffset": {"type": "datetime", "value": "1977-06-28T12:32:00Z"},
"milliseconds": {"type": "datetime", "value": "1977-12-21T03:32:00.555+00:00"}
"milliseconds": {"type": "datetime", "value": "1977-12-21T03:32:00.555+00:00"},
"bestdayever_with_space": {"type": "datetime", "value": "1987-07-05T17:45:00Z"}
}
1 change: 1 addition & 0 deletions tests/valid/datetime.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bestdayever = 1987-07-05T17:45:00Z
numoffset = 1977-06-28T07:32:00-05:00
milliseconds = 1977-12-21T10:32:00.555+07:00
bestdayever_with_space = 1987-07-05 17:45:00Z
21 changes: 21 additions & 0 deletions tests/valid/dotted-keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": {
"first": {"type": "string", "value": "Tom"},
"last": {"type": "string", "value": "Preston-Werner"}
},
"point": {
"x": {"type": "integer", "value": "1"},
"y": {"type": "integer", "value": "2"}
},
"l1": {
"l2": {
"l3": {
"l4": {
"l5": {
"l6": {"type": "integer", "value": "42"}
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions tests/valid/dotted-keys.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name.first = "Tom"
name.last = "Preston-Werner"
point.x = 1
point.y = 2
l1.l2.l3 = {l4.l5.l6 = 42}
8 changes: 8 additions & 0 deletions tests/valid/infinity-and-nan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"nan": {"type": "float", "value": "nan"},
"nan_neg": {"type": "float", "value": "nan"},
"nan_plus": {"type": "float", "value": "+nan"},
"infinity": {"type": "float", "value": "inf"},
"infinity_neg": {"type": "float", "value": "-inf"},
"infinity_plus": {"type": "float", "value": "+inf"}
}
7 changes: 7 additions & 0 deletions tests/valid/infinity-and-nan.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nan = nan
nan_neg = -nan
nan_plus = +nan
infinity = inf
infinity_neg = -inf
infinity_plus = +inf

3 changes: 3 additions & 0 deletions tests/valid/local-date.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bestdayever": {"type": "date", "value": "1987-07-05"}
}
1 change: 1 addition & 0 deletions tests/valid/local-date.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bestdayever = 1987-07-05
5 changes: 5 additions & 0 deletions tests/valid/local-datetime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bestdayever": {"type": "datetime-local", "value": "1987-07-05T17:45:00"},
"milliseconds": {"type": "datetime-local", "value": "1977-12-21T10:32:00.555"},
"bestdayever_with_space": {"type": "datetime-local", "value": "1987-07-05T17:45:00"}
}
3 changes: 3 additions & 0 deletions tests/valid/local-datetime.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bestdayever = 1987-07-05T17:45:00
milliseconds = 1977-12-21T10:32:00.555
bestdayever_with_space = 1987-07-05 17:45:00
4 changes: 4 additions & 0 deletions tests/valid/local-time.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"besttimeever": {"type": "time", "value": "17:45:00"},
"milliseconds": {"type": "time", "value": "10:32:00.555"}
}
3 changes: 3 additions & 0 deletions tests/valid/local-time.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
besttimeever = 17:45:00
milliseconds = 10:32:00.555

6 changes: 6 additions & 0 deletions tests/valid/multiline-string-accidental-whitespace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"three_lines": {
"type": "string",
"value": "The quick brown fox jumps over the lazy dog."
}
}
5 changes: 5 additions & 0 deletions tests/valid/multiline-string-accidental-whitespace.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
three_lines = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""
8 changes: 8 additions & 0 deletions tests/valid/non-dec-integers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bin1": {"type": "integer", "value": "214"},
"oct1": {"type": "integer", "value": "342391"},
"oct2": {"type": "integer", "value": "493"},
"hex1": {"type": "integer", "value": "3735928559"},
"hex2": {"type": "integer", "value": "3735928559" },
"hex3": {"type": "integer", "value": "3735928559"}
}
6 changes: 6 additions & 0 deletions tests/valid/non-dec-integers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin1 = 0b11010110
oct1 = 0o01234567
oct2 = 0o755
hex1 = 0xDEADBEEF
hex2 = 0xdeadbeef
hex3 = 0xdead_beef

0 comments on commit 6c8334f

Please sign in to comment.