import groovy.json.JsonSlurper @Grab('org.spockframework:spock-core:1.1-groovy-2.4') import spock.lang.Specification import static ShortJson.printValue import static ShortJson.plain class ShortJsonSpec extends Specification { void "primitive arrays should be flattened"() { expect: plain(printValue([1, 2, 3])) == '[ 1, 2, 3 ]' } void "hash arrays should not be flattened"() { expect: plain(printValue([[:], [:]])) == '''\ [ {}, {} ]'''.stripIndent() } void "simple maps should be flattened"() { expect: plain(printValue([first: 'Rahul', last: 'Somasunderam'])) == '{"first": "Rahul", "last": "Somasunderam"}' } void "complex json regression"() { def str = '''\ { "_args": [["compressible@2.0.10", "/Users/rahul/src/hubot-api-ai-poc"]], "_from": "compressible@2.0.10", "_id": "compressible@2.0.10", "_inBundle": false, "_integrity": "sha1-/tocf3YXkScyspv4zyYlKiC57s0=", "_location": "/compressible", "_phantomChildren": {}, "_requested": {"type": "version", "registry": true, "raw": "compressible@2.0.10", "name": "compressible", "escapedName": "compressible", "saveSpec": null, "fetchSpec": "2.0.10"}, "_requiredBy": [], "_resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.10.tgz", "_spec": "2.0.10", "_where": "/Users/rahul/src/hubot-api-ai-poc", "bugs": {"url": "https://github.com/jshttp/compressible/issues"}, "contributors": [ {"name": "Douglas Christopher Wilson", "email": "doug@somethingdoug.com"}, {"name": "Jonathan Ong", "email": "me@jongleberry.com", "url": "http://jongleberry.com"}, {"name": "Jeremiah Senkpiel", "email": "fishrock123@rocketmail.com", "url": "https://searchbeam.jit.su"} ], "dependencies": {"mime-db": ">= 1.27.0 < 2"}, "description": "Compressible Content-Type / mime checking", "devDependencies": { "eslint": "3.18.0", "eslint-config-standard": "7.1.0", "eslint-plugin-markdown": "1.0.0-beta.4", "eslint-plugin-promise": "3.5.0", "eslint-plugin-standard": "2.1.1", "istanbul": "0.4.5", "mocha": "~1.21.5" }, "engines": {"node": ">= 0.6"}, "files": ["HISTORY.md", "LICENSE", "README.md", "index.js"], "homepage": "https://github.com/jshttp/compressible#readme", "keywords": ["compress", "gzip", "mime", "content-type"], "license": "MIT", "name": "compressible", "repository": {"type": "git", "url": "git+https://github.com/jshttp/compressible.git"}, "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot --check-leaks" }, "version": "2.0.10" }'''.stripIndent() expect: plain(printValue(new JsonSlurper().parseText(str))) == str } }