Skip to content

Commit

Permalink
Merge pull request #15 from zardoy/refactor-test
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Dec 9, 2022
2 parents 577ebc5 + be51677 commit 4ed6bf9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 50 deletions.
8 changes: 8 additions & 0 deletions src/commaOnEnter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export default () => {
const isGoodEnding =
['}', '"', ']', 'true', 'false'].some(char => prevLineWithoutComments.endsWith(char)) || isNumber(prevLineWithoutComments.at(-1)!)

// ignore last } (if after it only whitespaces)
if (
prevLineWithoutComments.endsWith('}') &&
fileContentWithoutComments.split('\n').slice(prevLine.lineNumber).join('\n').slice(prevLineWithoutComments.length).trimEnd() === ''
) {
continue
}

if (!isGoodEnding) continue

const insertPostion = new vscode.Position(prevLine.lineNumber, prevLineWithoutComments.length)
Expand Down
25 changes: 7 additions & 18 deletions test/integration/suite/commaOnEnter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode'

import { expect } from 'chai'
import { clearEditorText, getTextNormalizedEol, offsetToPosition, stringWithPositions } from './utils'
import { clearEditorText, getTextNormalizedEol, offsetToPosition, prepareFileEditor, stringWithPositions } from './utils'
import dedent from 'string-dedent'

describe('Comma on Enter', () => {
Expand All @@ -25,28 +25,17 @@ describe('Comma on Enter', () => {
}/*|*/
"key7": "value",/*$*/
// Comment with number 3/*$*/
}
}/*$*/
`

const [FULL_FIXTURE_CONTENT, FIXTURE_POSITIONS] = stringWithPositions(FULL_FIXTURE, ['/*|*/', '/*$*/'])

before(done => {
void vscode.workspace
.openTextDocument({
// don't prefil content with \n as vscode won't normalize eol here
content: '',
language: 'jsonc',
})
.then(async newDocument => {
document = newDocument
editor = await vscode.window.showTextDocument(document)
if (process.env.CI) {
await new Promise(resolve => {
setTimeout(resolve, 1000)
})
}
})
.then(done)
prepareFileEditor().then(async () => {
editor = vscode.window.activeTextEditor!
document = editor.document
done()
})
})

const testPosition = (num: number, offset: number, isExpected: boolean, testFn: any = it) => {
Expand Down
13 changes: 4 additions & 9 deletions test/integration/suite/jsonFixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ import * as vscode from 'vscode'

import { expect } from 'chai'
// import delay from 'delay'
import { getTextNormalizedEol, setupFixtureContent, waitForJsonDiagnostics } from './utils'
import { getTextNormalizedEol, prepareFileEditor, setupFixtureContent, waitForJsonDiagnostics } from './utils'
import { jsonFixesFixtures } from '../fixtures/files'
import dedent from 'string-dedent'
import { join } from 'path'
import fs from 'fs'

describe('Json Fixes', () => {
let document: vscode.TextDocument
let editor: vscode.TextEditor
let temporaryFile = join(__dirname, '../temp.json')
fs.writeFileSync(temporaryFile, '', 'utf8')
before(done => {
void vscode.window
.showTextDocument(vscode.Uri.file(temporaryFile))
.then(async newEditor => {
editor = newEditor
prepareFileEditor()
.then(async () => {
editor = vscode.window.activeTextEditor!
document = editor.document
await vscode.workspace.getConfiguration('').update('editor.codeActionsOnSave', { 'source.fixAll': true }, vscode.ConfigurationTarget.Global)
await vscode.workspace.getConfiguration('').update('editor.formatOnSave', true, vscode.ConfigurationTarget.Global)
Expand Down
35 changes: 12 additions & 23 deletions test/integration/suite/quotesOnColon.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as vscode from 'vscode'

import { expect } from 'chai'
import { clearEditorText, waitForJsonDiagnostics } from './utils'
import { clearEditorText, getTextNormalizedEol, prepareFileEditor, waitForJsonDiagnostics } from './utils'

// todo enable back when https://github.com/microsoft/vscode-languageserver-node/issues/1128 is available
describe.skip('Quotes on Colon', () => {
describe('Quotes on Colon', () => {
let document: vscode.TextDocument
let editor: vscode.TextEditor

Expand All @@ -13,23 +12,13 @@ describe.skip('Quotes on Colon', () => {
const FULL_FIXTURE_UNCHANGED = '{key1:, key1:,\nkey1:}'

before(done => {
void vscode.workspace
.openTextDocument({
content: '',
language: 'jsonc',
})
.then(async newDocument => {
document = newDocument
editor = await vscode.window.showTextDocument(document)
if (process.env.CI) {
await new Promise(resolve => {
setTimeout(resolve, 1000)
})
}
// make sure JSON server is started and diagnostics are here
await Promise.all([clearEditorText(editor, '{'), waitForJsonDiagnostics(document)])
})
.then(done)
prepareFileEditor().then(async () => {
editor = vscode.window.activeTextEditor!
document = editor.document
// make sure JSON server is started and diagnostics are here
await Promise.all([clearEditorText(editor, '{'), waitForJsonDiagnostics(document)])
done()
})
})

const typeSequence = async (seq: string) => {
Expand All @@ -52,13 +41,13 @@ describe.skip('Quotes on Colon', () => {
resolve()
})
})
expect(document.getText()).to.equal(FULL_FIXTURE_EXPECTED)
expect(getTextNormalizedEol(document)).to.equal(FULL_FIXTURE_EXPECTED)
} else {
// todo would be better to remove timeout in favor of cleaner solution
await new Promise(resolve => {
setTimeout(resolve, 60)
})
expect(document.getText()).to.equal(TYPE_CONTENT)
expect(getTextNormalizedEol(document)).to.equal(FULL_FIXTURE_UNCHANGED)
}
}
if (title) it(title, cb)
Expand All @@ -70,6 +59,6 @@ describe.skip('Quotes on Colon', () => {

it('Extension setting disabled', async () => {
await vscode.workspace.getConfiguration().update('fixAllJson.insertMissingDoubleQuotesOnColon', false, vscode.ConfigurationTarget.Global)
await execTest('Double quotes basic case', false)
await execTest(undefined, false)
})
})
25 changes: 25 additions & 0 deletions test/integration/suite/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import * as vscode from 'vscode'
import stringDedent from 'string-dedent'
import escapeStringRegexp from 'escape-string-regexp'
import fs from 'fs'
import { join } from 'path'

const testingFile = join(__dirname, '../temp.json')
const testingFileUri = vscode.Uri.file(testingFile)

export const prepareFileEditor = async () => {
if (vscode.window.activeTextEditor?.document.uri.toString() === testingFileUri.toString()) {
await clearEditorText(vscode.window.activeTextEditor)
return
}
fs.writeFileSync(testingFile, '', 'utf8')
await vscode.window.showTextDocument(testingFileUri)
// const document = await vscode.workspace.openTextDocument({
// // don't prefil content with \n as vscode won't normalize eol here
// content: '',
// language: 'jsonc',
// })
// await vscode.window.showTextDocument(document)
// if (process.env.CI) {
// await new Promise(resolve => {
// setTimeout(resolve, 1000)
// })
// }
}

export const clearEditorText = async (editor: vscode.TextEditor, resetContent = '') => {
await new Promise<void>(resolve => {
Expand Down

0 comments on commit 4ed6bf9

Please sign in to comment.