Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mholt/json-to-go
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 93ef0cf49770cfbd6340706e29e91770c8ca48e8
Choose a base ref
..
head repository: mholt/json-to-go
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 342ae50a25131b5c71534718b0ba11a55c0283df
Choose a head ref
Showing with 68 additions and 6 deletions.
  1. +56 −0 .github/workflows/node-tests.yml
  2. +2 −2 json-to-go.js
  3. +10 −4 json-to-go.test.js
56 changes: 56 additions & 0 deletions .github/workflows/node-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
name: json-to-go tests

on: # yamllint disable-line rule:truthy
workflow_call:
workflow_dispatch:
push:
branches:
- "master"
- "main"
pull_request:

jobs:
run-tests:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Run tests
run: |
node json-to-go.test.js
- name: Run json-to-go using stdin
shell: bash
run: |
set -eEuo pipefail
got=$(node json-to-go.js < tests/double-nested-objects.json)
exp=$(cat tests/double-nested-objects.go)
echo "got: '${got}'"
[[ "${got}" == "${exp}" ]]
- name: Run json-to-go with -big using stdin
shell: bash
run: |
set -eEuo pipefail
got=$(node json-to-go.js -big < tests/double-nested-objects.json)
exp=$(cat tests/double-nested-objects.go)
echo "got: '${got}'"
[[ "${got}" == "${exp}" ]]
- name: Run json-to-go with a file
shell: bash
run: |
set -eEuo pipefail
got=$(node json-to-go.js tests/double-nested-objects.json)
exp=$(cat tests/double-nested-objects.go)
echo "got: '${got}'"
[[ "${got}" == "${exp}" ]]
4 changes: 2 additions & 2 deletions json-to-go.js
Original file line number Diff line number Diff line change
@@ -507,9 +507,9 @@ if (typeof module != 'undefined') {
process.stdout.write(jsonToGo(json).go)
})
} else if (process.argv.length === 3) {
const fs = require('node:fs');
const fs = require('fs');
const json = fs.readFileSync(process.argv[2], 'utf8');
console.log(jsonToGo(json).go)
process.stdout.write(jsonToGo(json).go)
} else {
process.stdin.on('data', function(buf) {
const json = buf.toString('utf8')
14 changes: 10 additions & 4 deletions json-to-go.test.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ function quote(str) {
return "'" + str
.replace(/\t/g, "\\t")
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/'/g, "\\'") + "'"
}

@@ -141,12 +142,14 @@ function test(includeExampleData) {
const got = jsonToGo(testCase.input, null, null, includeExampleData);
if (got.error) {
console.assert(!got.error, `format('${testCase.input}'): ${got.error}`);
process.exitCode = 16
} else {
const exp = includeExampleData ? testCase.expectedWithExample : testCase.expected
console.assert(
got.go === exp,
const success = got.go === exp
console.assert(success,
`format('${testCase.input}'): \n\tgot: ${quote(got.go)}\n\twant: ${quote(exp)}`
);
if(!success) process.exitCode = 17
}
}
console.log(includeExampleData ? "done testing samples with data" : "done testing samples without data")
@@ -170,14 +173,17 @@ function testFiles() {
const got = jsonToGo(jsonData);
if (got.error) {
console.assert(!got.error, `format('${jsonData}'): ${got.error}`);
process.exitCode = 18
} else {
console.assert(
got.go === expectedGoData,
const success = got.go === expectedGoData
console.assert(success,
`format('${jsonData}'): \n\tgot: ${quote(got.go)}\n\twant: ${quote(expectedGoData)}`
);
if(!success) process.exitCode = 19
}
} catch (err) {
console.error(err);
process.exitCode = 20
}
}
console.log("done testing files")