Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hunt committed Nov 4, 2018
2 parents d2950e0 + 39bb76d commit caa558e
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 38 deletions.
27 changes: 17 additions & 10 deletions COPYING
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
The MIT License (MIT)

Copyright (C) 2004 Sam Hocevar <[email protected]>
Copyright (c) 2018 TOML authors

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ compared. Note though that encoders have their own set of invalid tests in the
invalid-encoder directory. The JSON given to a TOML encoder is in the same
format as the JSON that a TOML decoder should output.

Version: v0.2.0 (in sync with TOML)
Version: v0.4.0 (in sync with TOML)

Compatible with TOML version
[v0.2.0](https://github.com/mojombo/toml/blob/master/versions/toml-v0.2.0.md)
[v0.4.0](https://github.com/mojombo/toml/blob/master/versions/toml-v0.4.0.md)

Dependencies: [Go](http://golang.org).

Expand Down Expand Up @@ -101,6 +101,7 @@ 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.

### Example JSON encoding

Expand Down Expand Up @@ -210,8 +211,12 @@ note the commit SHA1 or version tag that your parser supports in your `README`.
* C++ (@skystrife) - https://github.com/skystrife/cpptoml
* Go (@thompelletier) - https://github.com/pelletier/go-toml
* Go w/ Reflection (@BurntSushi) - https://github.com/BurntSushi/toml/tree/master/cmd/toml-test-decoder
* LabVIEW (@dbtaylor) - https://github.com/erdosmiller/lv-toml
* Node.js/Browser (@redhotvengeance) - https://github.com/redhotvengeance/topl
* PHP (@leonelquinteros) - https://github.com/leonelquinteros/php-toml
* Python (@uiri) - https://github.com/uiri/toml
* Python (@marksteve) - https://github.com/marksteve/toml-ply
* Racket (@greghendershott) - https://github.com/greghendershott/toml
* Ruby (@jm, @cespare) - https://gist.github.com/cespare/5052442
* Rust (@mneumann) - https://github.com/mneumann/rust-toml

Expand All @@ -229,4 +234,4 @@ messages.
* Julia (@pygy) - https://github.com/pygy/TOML.jl
* PHP (@yosymfony) - https://github.com/yosymfony/toml
* Python (@f03lipe) - https://github.com/f03lipe/toml-python

* JavaScript (@iarna) - https://github.com/iarna/iarna-toml
71 changes: 51 additions & 20 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"strconv"
"time"
)

// compareJson consumes the recursive structure of both `expected` and `test`
Expand Down Expand Up @@ -114,44 +115,53 @@ func (r result) cmpJsonValues(e, t map[string]interface{}) result {
// equality.
if etype == "array" {
return r.cmpJsonArrays(e["value"], t["value"])
}

// Floats need special attention too. Not every language can
// represent the same floats, and sometimes the string version of
// a float can be wonky with extra zeroes and what not.
if etype == "float" {
enum, ok := e["value"].(string)
} else {
// Atomic values are always strings
evalue, ok := e["value"].(string)
if !ok {
return r.failedf("BUG in test case. 'value' should be a string, "+
"but it is a %T.", e["value"])
return r.failedf("BUG in test case. 'value' "+
"should be a string, but it is a %T.",
e["value"])
}
tnum, ok := t["value"].(string)
tvalue, ok := t["value"].(string)
if !ok {
return r.failedf("Malformed parser output. 'value' should be a "+
"string but it is a %T.", t["value"])
return r.failedf("Malformed parser output. 'value' "+
"should be a string but it is a %T.",
t["value"])
}

// Excepting floats and datetimes, other values can be
// compared as strings.
switch etype {
case "float":
return r.cmpFloats(evalue, tvalue);
case "datetime":
return r.cmpAsDatetimes(evalue, tvalue);
default:
return r.cmpAsStrings(evalue, tvalue);
}
return r.cmpFloats(enum, tnum)
}
}

// Otherwise, we can do simple string equality.
if e["value"] != t["value"] {
func (r result) cmpAsStrings(e, t string) result {
if e != t {
return r.failedf("Values for key '%s' don't match. Expected a "+
"value of '%s' but got '%s'.", r.key, e["value"], t["value"])
"value of '%s' but got '%s'.", r.key, e, t)
}
return r
}

func (r result) cmpFloats(e, t string) result {
ef, err := strconv.ParseFloat(e, 64)
if err != nil {
return r.failedf("BUG in test case. Could not read '%s' as a float "+
"value for key '%s'.", e, r.key)
return r.failedf("BUG in test case. Could not read '%s' as a "+
"float value for key '%s'.", e, r.key)
}

tf, err := strconv.ParseFloat(t, 64)
if err != nil {
return r.failedf("Malformed parser output. Could not read '%s' as "+
"a float value for key '%s'.", t, r.key)
return r.failedf("Malformed parser output. Could not read '%s' "+
"as a float value for key '%s'.", t, r.key)
}
if ef != tf {
return r.failedf("Values for key '%s' don't match. Expected a "+
Expand All @@ -160,6 +170,27 @@ func (r result) cmpFloats(e, t string) result {
return r
}

func (r result) cmpAsDatetimes(e, t string) result {
var err error

ef, err := time.Parse(time.RFC3339Nano, e)
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)
if err != nil {
return r.failedf("Malformed parser output. Could not read '%s' "+
"as datetime value for key '%s'.", t, r.key)
}
if !ef.Equal(tf) {
return r.failedf("Values for key '%s' don't match. Expected a "+
"value of '%v' but got '%v'.", r.key, ef, tf)
}
return r
}

func isValue(m map[string]interface{}) bool {
if len(m) != 2 {
return false
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
// If no test directory was specified, let's look for it automatically.
// Assumes `toml-test` was installed with the Go tool.
if len(flagTestdir) == 0 {
imp := path.Join("github.com", "uiri", "toml-test", "tests")
imp := path.Join("github.com", "BurntSushi", "toml-test", "tests")
for _, dir := range build.Default.SrcDirs() {
if readable(path.Join(dir, imp)) {
flagTestdir = path.Join(dir, imp)
Expand Down Expand Up @@ -68,8 +68,8 @@ func usage() {
path.Base(os.Args[0]))
log.Println(`
parser-cmd should be a program that accepts TOML data on stdin until EOF,
and outputs the corresponding JSON encoding on stdout. Please see 'README.md'
for details on how to satisfy the interface expected by 'toml-test' with your
and outputs the corresponding JSON encoding on stdout. Please see 'README.md'
for details on how to satisfy the interface expected by 'toml-test' with your
own parser.
The 'testdir' directory should have two sub-directories: 'invalid' and 'valid'.
Expand Down
1 change: 1 addition & 0 deletions tests/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.toml -text
4 changes: 4 additions & 0 deletions tests/invalid/multi-line-inline-table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json_like = {
first = "Tom",
last = "Preston-Werner"
}
1 change: 1 addition & 0 deletions tests/invalid/string-bad-codepoint.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid-codepoint = "This string contains a non scalar unicode codepoint \uD801"
1 change: 1 addition & 0 deletions tests/invalid/string-bad-slash-escape.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid-escape = "This string has a bad \/ escape character."
7 changes: 7 additions & 0 deletions tests/valid/arrays.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@
{"type": "datetime", "value": "1979-05-27T07:32:00Z"},
{"type": "datetime", "value": "2006-06-01T11:00:00Z"}
]
},
"comments": {
"type": "array",
"value": [
{"type": "integer", "value": "1"},
{"type": "integer", "value": "2"}
]
}
}
4 changes: 4 additions & 0 deletions tests/valid/arrays.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ dates = [
1979-05-27T07:32:00Z,
2006-06-01T11:00:00Z,
]
comments = [
1,
2, #this is ok
]
4 changes: 3 additions & 1 deletion tests/valid/datetime.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"bestdayever": {"type": "datetime", "value": "1987-07-05T17:45:00Z"}
"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"}
}
2 changes: 2 additions & 0 deletions tests/valid/datetime.toml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
bestdayever = 1987-07-05T17:45:00Z
numoffset = 1977-06-28T07:32:00-05:00
milliseconds = 1977-12-21T10:32:00.555+07:00
6 changes: 6 additions & 0 deletions tests/valid/exponent-part-float.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"million": {"type": "float", "value": "1000000"},
"minustenth": {"type": "float", "value": "-0.1"},
"beast": {"type": "float", "value": "666"}
}

3 changes: 3 additions & 0 deletions tests/valid/exponent-part-float.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
million = 1e6
minustenth = -1E-1
beast = 6.66E2
16 changes: 16 additions & 0 deletions tests/valid/inline-table-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"people": [
{
"first_name": {"type": "string", "value": "Bruce"},
"last_name": {"type": "string", "value": "Springsteen"}
},
{
"first_name": {"type": "string", "value": "Eric"},
"last_name": {"type": "string", "value": "Clapton"}
},
{
"first_name": {"type": "string", "value": "Bob"},
"last_name": {"type": "string", "value": "Seger"}
}
]
}
3 changes: 3 additions & 0 deletions tests/valid/inline-table-array.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
people = [{first_name = "Bruce", last_name = "Springsteen"},
{first_name = "Eric", last_name = "Clapton"},
{first_name = "Bob", last_name = "Seger"}]
8 changes: 8 additions & 0 deletions tests/valid/inline-table.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"name": {
"first": {"type": "string", "value": "Tom"},
"last": {"type": "string", "value": "Preston-Werner"}
},
"point": {
"x": {"type": "integer", "value": "1"},
"y": {"type": "integer", "value": "2"}
},
"simple": { "a": {"type": "integer", "value": "1"} },
"str-key": { "a": {"type": "integer", "value": "1"} },
"table-array": [
Expand Down
2 changes: 2 additions & 0 deletions tests/valid/inline-table.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
simple = { a = 1 }
str-key = { "a" = 1 }
table-array = [{ "a" = 1 }, { "b" = 2 }]
2 changes: 1 addition & 1 deletion tests/valid/integer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"answer": {"type": "integer", "value": "42"},
"posanswer": {"type": "integer", "value": "42"},
"neganswer": {"type": "integer", "value": "-42"},
"posanswer": {"type": "integer", "value": "42"},
"zero": {"type": "integer", "value": "0"}
}
4 changes: 4 additions & 0 deletions tests/valid/newline-crlf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"os": {"type": "string", "value": "DOS"},
"newline": {"type": "string", "value": "crlf"}
}
2 changes: 2 additions & 0 deletions tests/valid/newline-crlf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
os = "DOS"
newline = "crlf"
4 changes: 4 additions & 0 deletions tests/valid/newline-lf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"os": {"type": "string", "value": "unix"},
"newline": {"type": "string", "value": "lf"}
}
2 changes: 2 additions & 0 deletions tests/valid/newline-lf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
os = "unix"
newline = "lf"
3 changes: 3 additions & 0 deletions tests/valid/underscored-float.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"electron_mass": {"type": "float", "value": "9.109109383e-31"}
}
1 change: 1 addition & 0 deletions tests/valid/underscored-float.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron_mass = 9_109.109_383e-3_4
3 changes: 3 additions & 0 deletions tests/valid/underscored-integer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"million": {"type": "integer", "value": "1000000"}
}
1 change: 1 addition & 0 deletions tests/valid/underscored-integer.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
million = 1_000_000

0 comments on commit caa558e

Please sign in to comment.