Skip to content

Test suite relies on JavaScript object traversal #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
deepilla opened this issue Mar 29, 2018 · 1 comment
Closed

Test suite relies on JavaScript object traversal #179

deepilla opened this issue Mar 29, 2018 · 1 comment

Comments

@deepilla
Copy link
Contributor

I'm running the Go port of JSONata against the jsonata-js test suite and a few tests are failing because of assumptions that hold true in JavaScript but not in Go. As the test suite aims to be language-agnostic, it would be good to resolve these issues.

The Problem

The underlying problem is object traversal. When you iterate over an object in JavaScript, the values come back in source order. This code, for example, always outputs "a", "b", and "c" in that order.

var obj = {"a": 1, "b": 2, "c": 3};

for (var key in obj) {
    console.log(key);
}

In Go, object traversal is randomised. The equivalent Go code returns the same values, but the order is scrambled each time it runs.

obj := map[string]int{"a": 1, "b": 2, "c": 3}

for key := range obj {
    fmt.Println(key)
}

This isn't an issue in JSONata itself. The JSON spec defines an object as an unordered list of name/value pairs and that's exactly how Go maps work. However some of the tests in the test suite rely on JavaScript-style object traversal.

Affected Tests

The following tests all traverse objects and put the items into an array. The resulting array is assumed to be in a particular order. This order is not guaranteed in Go (or implied by the JSON spec).

The following tests do the same thing but add a predicate to retrieve a specific item from the array. Again, this relies on the items being in a known order.

Finally, converting an object to a string can fall foul of the same issue. What order should the fields appear in?

Possible Fixes

The first group of issues could be fixed by adding a flag to the test case JSON. Setting this flag to true would indicate that the order of the test results is unspecified. Or maybe we need a more general field that indicates the type of comparison to use for a test. Unordered array comparison could be one such type.

The second group is more tricky. Combining a predicate with a wildcard like this is non-deterministic in Go. I would recommend moving these tests out of the language-agnostic test suite.

I don't have a suggestion for the last issue but it's the least important of the ones here. I can live with that test failing.

@andrew-coleman
Copy link
Member

Resolved by #241
Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants