From 4dc5bf4899bc7055712b507fb0a7524e4077991f Mon Sep 17 00:00:00 2001 From: Daniel Hillmann Date: Sat, 7 Sep 2024 14:32:22 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20make=20yaml=20line=20len?= =?UTF-8?q?gths=20configurable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit make yaml line breaks configurable when converting --- package-lock.json | 254 ++++++++++++++++++ package.json | 2 + src/config.ts | 10 +- src/helpers.ts | 8 +- .../fixtures/longLinesExpectedLimited.yaml | 6 + .../fixtures/longLinesExpectedunlimited.yaml | 2 + src/test/fixtures/longLinesInput.json | 4 + src/test/suite/helpers.test.ts | 53 ++++ 8 files changed, 332 insertions(+), 7 deletions(-) create mode 100644 src/test/fixtures/longLinesExpectedLimited.yaml create mode 100644 src/test/fixtures/longLinesExpectedunlimited.yaml create mode 100644 src/test/fixtures/longLinesInput.json diff --git a/package-lock.json b/package-lock.json index 9025e50..59d2da6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@types/glob": "^8.1.0", "@types/mocha": "^10.0.7", "@types/node": "^22.5.4", + "@types/sinon": "^17.0.3", "@types/vscode": "^1.93.0", "@types/yaml": "1.9.7", "@typescript-eslint/eslint-plugin": "^8.4.0", @@ -24,6 +25,7 @@ "glob": "^11.0.0", "globals": "^15.9.0", "mocha": "^10.7.3", + "sinon": "18.0.0", "ts-loader": "^9.5.1", "typescript": "^5.5.4", "typescript-eslint": "^8.4.0", @@ -416,6 +418,50 @@ "node": ">=14" } }, + "node_modules/@sinonjs/commons": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.1.tgz", + "integrity": "sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, + "node_modules/@sinonjs/fake-timers": { + "version": "11.3.1", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-11.3.1.tgz", + "integrity": "sha512-EVJO7nW5M/F5Tur0Rf2z/QoMo+1Ia963RiMtapiQrEWvY0iBUvADo8Beegwjpnle5BHkyHuoxSTW3jF43H1XRA==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^3.0.1" + } + }, + "node_modules/@sinonjs/samsam": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-8.0.0.tgz", + "integrity": "sha512-Bp8KUVlLp8ibJZrnvq2foVhP0IVX2CIprMJPK0vqGqgrDa0OHVKeZyBykqskkrdxV6yKBPmGasO8LVjAKR3Gew==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^2.0.0", + "lodash.get": "^4.4.2", + "type-detect": "^4.0.8" + } + }, + "node_modules/@sinonjs/samsam/node_modules/@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, + "node_modules/@sinonjs/text-encoding": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.3.tgz", + "integrity": "sha512-DE427ROAphMQzU4ENbliGYrBSYPXF+TtLg9S8vzeA+OF4ZKzoDdzfL8sxuMUGS/lgRhM6j1URSk9ghf7Xo1tyA==", + "dev": true + }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", @@ -459,6 +505,21 @@ "undici-types": "~6.19.2" } }, + "node_modules/@types/sinon": { + "version": "17.0.3", + "resolved": "https://registry.npmjs.org/@types/sinon/-/sinon-17.0.3.tgz", + "integrity": "sha512-j3uovdn8ewky9kRBG19bOwaZbexJu/XjtkHyjvUgt4xfPFz18dcORIMqnYh66Fx3Powhcr85NT5+er3+oViapw==", + "dev": true, + "dependencies": { + "@types/sinonjs__fake-timers": "*" + } + }, + "node_modules/@types/sinonjs__fake-timers": { + "version": "8.1.5", + "resolved": "https://registry.npmjs.org/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-8.1.5.tgz", + "integrity": "sha512-mQkU2jY8jJEF7YHjHvsQO8+3ughTL1mcnn96igfhONmR+fUPSKIkefQYpSe8bsly2Ep7oQbn/6VG5/9/0qcArQ==", + "dev": true + }, "node_modules/@types/vscode": { "version": "1.93.0", "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.93.0.tgz", @@ -2355,6 +2416,12 @@ "setimmediate": "^1.0.5" } }, + "node_modules/just-extend": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-6.2.0.tgz", + "integrity": "sha512-cYofQu2Xpom82S6qD778jBDpwvvy39s1l/hrYij2u9AMdQcGRpaBu6kY4mVhuno5kJVi1DAz4aiphA2WI1/OAw==", + "dev": true + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -2419,6 +2486,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==", + "dev": true + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -2616,6 +2689,19 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node_modules/nise": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/nise/-/nise-6.0.0.tgz", + "integrity": "sha512-K8ePqo9BFvN31HXwEtTNGzgrPpmvgciDsFz8aztFjt4LqKO/JeFD8tBOeuDiCMXrIl/m1YvfH8auSpxfaD09wg==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^3.0.0", + "@sinonjs/fake-timers": "^11.2.2", + "@sinonjs/text-encoding": "^0.7.2", + "just-extend": "^6.2.0", + "path-to-regexp": "^6.2.1" + } + }, "node_modules/node-releases": { "version": "2.0.14", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz", @@ -2903,6 +2989,12 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/path-to-regexp": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.2.tgz", + "integrity": "sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==", + "dev": true + }, "node_modules/picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", @@ -3269,6 +3361,36 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/sinon": { + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-18.0.0.tgz", + "integrity": "sha512-+dXDXzD1sBO6HlmZDd7mXZCR/y5ECiEiGCBSGuFD/kZ0bDTofPYc6JaeGmPSF+1j1MejGUWkORbYOLDyvqCWpA==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^3.0.1", + "@sinonjs/fake-timers": "^11.2.2", + "@sinonjs/samsam": "^8.0.0", + "diff": "^5.2.0", + "nise": "^6.0.0", + "supports-color": "^7" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/sinon" + } + }, + "node_modules/sinon/node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -3537,6 +3659,15 @@ "node": ">= 0.8.0" } }, + "node_modules/type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/typescript": { "version": "5.5.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", @@ -4180,6 +4311,52 @@ "dev": true, "optional": true }, + "@sinonjs/commons": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.1.tgz", + "integrity": "sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + }, + "@sinonjs/fake-timers": { + "version": "11.3.1", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-11.3.1.tgz", + "integrity": "sha512-EVJO7nW5M/F5Tur0Rf2z/QoMo+1Ia963RiMtapiQrEWvY0iBUvADo8Beegwjpnle5BHkyHuoxSTW3jF43H1XRA==", + "dev": true, + "requires": { + "@sinonjs/commons": "^3.0.1" + } + }, + "@sinonjs/samsam": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-8.0.0.tgz", + "integrity": "sha512-Bp8KUVlLp8ibJZrnvq2foVhP0IVX2CIprMJPK0vqGqgrDa0OHVKeZyBykqskkrdxV6yKBPmGasO8LVjAKR3Gew==", + "dev": true, + "requires": { + "@sinonjs/commons": "^2.0.0", + "lodash.get": "^4.4.2", + "type-detect": "^4.0.8" + }, + "dependencies": { + "@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + } + } + }, + "@sinonjs/text-encoding": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.3.tgz", + "integrity": "sha512-DE427ROAphMQzU4ENbliGYrBSYPXF+TtLg9S8vzeA+OF4ZKzoDdzfL8sxuMUGS/lgRhM6j1URSk9ghf7Xo1tyA==", + "dev": true + }, "@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", @@ -4223,6 +4400,21 @@ "undici-types": "~6.19.2" } }, + "@types/sinon": { + "version": "17.0.3", + "resolved": "https://registry.npmjs.org/@types/sinon/-/sinon-17.0.3.tgz", + "integrity": "sha512-j3uovdn8ewky9kRBG19bOwaZbexJu/XjtkHyjvUgt4xfPFz18dcORIMqnYh66Fx3Powhcr85NT5+er3+oViapw==", + "dev": true, + "requires": { + "@types/sinonjs__fake-timers": "*" + } + }, + "@types/sinonjs__fake-timers": { + "version": "8.1.5", + "resolved": "https://registry.npmjs.org/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-8.1.5.tgz", + "integrity": "sha512-mQkU2jY8jJEF7YHjHvsQO8+3ughTL1mcnn96igfhONmR+fUPSKIkefQYpSe8bsly2Ep7oQbn/6VG5/9/0qcArQ==", + "dev": true + }, "@types/vscode": { "version": "1.93.0", "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.93.0.tgz", @@ -5576,6 +5768,12 @@ "setimmediate": "^1.0.5" } }, + "just-extend": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-6.2.0.tgz", + "integrity": "sha512-cYofQu2Xpom82S6qD778jBDpwvvy39s1l/hrYij2u9AMdQcGRpaBu6kY4mVhuno5kJVi1DAz4aiphA2WI1/OAw==", + "dev": true + }, "keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -5625,6 +5823,12 @@ "p-locate": "^5.0.0" } }, + "lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==", + "dev": true + }, "lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -5775,6 +5979,19 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "nise": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/nise/-/nise-6.0.0.tgz", + "integrity": "sha512-K8ePqo9BFvN31HXwEtTNGzgrPpmvgciDsFz8aztFjt4LqKO/JeFD8tBOeuDiCMXrIl/m1YvfH8auSpxfaD09wg==", + "dev": true, + "requires": { + "@sinonjs/commons": "^3.0.0", + "@sinonjs/fake-timers": "^11.2.2", + "@sinonjs/text-encoding": "^0.7.2", + "just-extend": "^6.2.0", + "path-to-regexp": "^6.2.1" + } + }, "node-releases": { "version": "2.0.14", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz", @@ -5976,6 +6193,12 @@ "minipass": "^7.1.2" } }, + "path-to-regexp": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.2.tgz", + "integrity": "sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==", + "dev": true + }, "picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", @@ -6234,6 +6457,31 @@ "integrity": "sha512-MY2/qGx4enyjprQnFaZsHib3Yadh3IXyV2C321GY0pjGfVBu4un0uDJkwgdxqO+Rdx8JMT8IfJIRwbYVz3Ob3Q==", "dev": true }, + "sinon": { + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-18.0.0.tgz", + "integrity": "sha512-+dXDXzD1sBO6HlmZDd7mXZCR/y5ECiEiGCBSGuFD/kZ0bDTofPYc6JaeGmPSF+1j1MejGUWkORbYOLDyvqCWpA==", + "dev": true, + "requires": { + "@sinonjs/commons": "^3.0.1", + "@sinonjs/fake-timers": "^11.2.2", + "@sinonjs/samsam": "^8.0.0", + "diff": "^5.2.0", + "nise": "^6.0.0", + "supports-color": "^7" + }, + "dependencies": { + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -6412,6 +6660,12 @@ "prelude-ls": "^1.2.1" } }, + "type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", + "dev": true + }, "typescript": { "version": "5.5.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", diff --git a/package.json b/package.json index adbcfec..68fe392 100644 --- a/package.json +++ b/package.json @@ -269,6 +269,7 @@ "@types/glob": "^8.1.0", "@types/mocha": "^10.0.7", "@types/node": "^22.5.4", + "@types/sinon": "^17.0.3", "@types/vscode": "^1.93.0", "@types/yaml": "1.9.7", "@typescript-eslint/eslint-plugin": "^8.4.0", @@ -278,6 +279,7 @@ "glob": "^11.0.0", "globals": "^15.9.0", "mocha": "^10.7.3", + "sinon": "18.0.0", "ts-loader": "^9.5.1", "typescript": "^5.5.4", "typescript-eslint": "^8.4.0", diff --git a/src/config.ts b/src/config.ts index 65c897f..dcf4cf0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,12 +2,13 @@ import * as vscode from 'vscode'; export enum ConfigId { ConvertOnRename = 'convertOnRename', - YamlSchema = 'yamlSchema', - YamlIndent = 'yamlIndent', - FileExtensionsYaml = 'fileExtensions.yaml', FileExtensionsJson = 'fileExtensions.json', + FileExtensionsYaml = 'fileExtensions.yaml', KeepOriginalFiles = 'keepOriginalFiles', - OverwriteExistentFiles = 'overwriteExistentFiles' + OverwriteExistentFiles = 'overwriteExistentFiles', + YamlIndent = 'yamlIndent', + YamlSchema = 'yamlSchema', + YamlLineWidth = 'yamlLineWidth', } export type Configs = { @@ -15,6 +16,7 @@ export type Configs = { overwriteExistentFiles: 'ask' | 'always'; YamlSchema: 'core' | 'failsafe' | 'json' | 'yaml-1.1'; YamlIndent: number; + YamlLineWidth: number; }; enum ConfigIdLegacy { diff --git a/src/helpers.ts b/src/helpers.ts index 91179a9..cb0d693 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -19,14 +19,16 @@ export function showError(error: unknown) { export function getYamlFromJson(json: string): string { const indent = getConfig(ConfigId.YamlIndent); const schema = getConfig(ConfigId.YamlSchema); + const lineWidth = getConfig(ConfigId.YamlLineWidth); try { const jsonObject = JSON.parse(json); return YAML.stringify(jsonObject, { - ...(indent && { indent }), - ...(schema && { schema }), - merge: true + ...(indent !== undefined && { indent }), + ...(schema !== undefined && { schema }), + ...(lineWidth !== undefined && { lineWidth }), + merge: true, }); } catch (error) { console.error(error); diff --git a/src/test/fixtures/longLinesExpectedLimited.yaml b/src/test/fixtures/longLinesExpectedLimited.yaml new file mode 100644 index 0000000..3679bc2 --- /dev/null +++ b/src/test/fixtures/longLinesExpectedLimited.yaml @@ -0,0 +1,6 @@ +one: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been + the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of + type and scrambled it to make a type specimen book +two: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been + the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of + type and scrambled it to make a type specimen book diff --git a/src/test/fixtures/longLinesExpectedunlimited.yaml b/src/test/fixtures/longLinesExpectedunlimited.yaml new file mode 100644 index 0000000..2ce73c4 --- /dev/null +++ b/src/test/fixtures/longLinesExpectedunlimited.yaml @@ -0,0 +1,2 @@ +one: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book +two: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book diff --git a/src/test/fixtures/longLinesInput.json b/src/test/fixtures/longLinesInput.json new file mode 100644 index 0000000..84bd2f4 --- /dev/null +++ b/src/test/fixtures/longLinesInput.json @@ -0,0 +1,4 @@ +{ + "one": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book", + "two": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book" +} diff --git a/src/test/suite/helpers.test.ts b/src/test/suite/helpers.test.ts index a039cbe..f6c525e 100644 --- a/src/test/suite/helpers.test.ts +++ b/src/test/suite/helpers.test.ts @@ -1,7 +1,10 @@ import * as assert from 'assert'; +import * as sinon from 'sinon';; +import * as vscode from 'vscode'; import { getJsonFromYaml, getYamlFromJson } from '../../helpers'; import { loadFixture, stripNewLines } from '../testHelpers'; +import { ConfigId } from '../../config'; suite('helpers', () => { suite('getYamlFromJson()', () => { @@ -15,6 +18,42 @@ suite('helpers', () => { assert.deepStrictEqual(stripNewLines(actualYaml), stripNewLines(expectedYaml)); }); + + suite('when json lines are long and line width is not limited', () => { + let vscodeWorkspaceStub: sinon.SinonStub; + suiteSetup(() => vscodeWorkspaceStub = createLongLinesConfigStub(0)); + + suiteTeardown(() => vscodeWorkspaceStub.restore()); + + test('should convert json to yaml without line breaks', async () => { + const [inputJson, expectedYaml] = await Promise.all([ + loadFixture('longLinesInput.json'), + loadFixture('longLinesExpectedUnlimited.yaml'), + ]); + + const actualYaml = getYamlFromJson(inputJson); + + assert.deepStrictEqual(stripNewLines(actualYaml), stripNewLines(expectedYaml)); + }); + }); + + suite('when json lines are long and line width is limited', () => { + let vscodeWorkspaceStub: sinon.SinonStub; + suiteSetup(() => vscodeWorkspaceStub = createLongLinesConfigStub(100)); + + suiteTeardown(() => vscodeWorkspaceStub.restore()); + + test('should convert json to yaml and apply line breaks', async () => { + const [inputJson, expectedYaml] = await Promise.all([ + loadFixture('longLinesInput.json'), + loadFixture('longLinesExpectedLimited.yaml'), + ]); + + const actualYaml = getYamlFromJson(inputJson); + + assert.deepStrictEqual(actualYaml, expectedYaml); + }); + }); }); suite('getJsonFromYaml', async () => { @@ -41,3 +80,17 @@ suite('helpers', () => { }); }); }); + +function createLongLinesConfigStub(lineWidth: number) { + const stub = sinon.stub(vscode.workspace, 'getConfiguration'); + + const configMock = { + [ConfigId.YamlLineWidth]: lineWidth, + }; + + stub.returns({ + get: (configKey: ConfigId.YamlLineWidth) => configMock[configKey], + } as vscode.WorkspaceConfiguration); + + return stub +}