diff --git a/src/test/groovy/nextflow/lsp/TestLanguageClient.groovy b/src/test/groovy/nextflow/lsp/TestLanguageClient.groovy new file mode 100644 index 0000000..f2104bd --- /dev/null +++ b/src/test/groovy/nextflow/lsp/TestLanguageClient.groovy @@ -0,0 +1,53 @@ +/* + * Copyright 2013-2024, Seqera Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package nextflow.lsp + +import java.util.concurrent.CompletableFuture + +import org.eclipse.lsp4j.MessageActionItem +import org.eclipse.lsp4j.MessageParams +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.ShowMessageRequestParams +import org.eclipse.lsp4j.services.LanguageClient + +/** + * + * @author Ben Sherman + */ +class TestLanguageClient implements LanguageClient { + + @Override + public void telemetryEvent(Object object) { + } + + @Override + public CompletableFuture showMessageRequest(ShowMessageRequestParams requestParams) { + return null + } + + @Override + public void showMessage(MessageParams messageParams) { + } + + @Override + public void publishDiagnostics(PublishDiagnosticsParams diagnostics) { + } + + @Override + public void logMessage(MessageParams message) { + } +} diff --git a/src/test/groovy/nextflow/lsp/file/FileCacheTest.groovy b/src/test/groovy/nextflow/lsp/file/FileCacheTest.groovy index 5afabb8..05f1c94 100644 --- a/src/test/groovy/nextflow/lsp/file/FileCacheTest.groovy +++ b/src/test/groovy/nextflow/lsp/file/FileCacheTest.groovy @@ -67,7 +67,7 @@ class FileCacheTest extends Specification { ) )) then: - 'hello there, friend' == fileCache.getContents(URI.create('file.txt')) + 'hello there, friend' == fileCache.getContents(URI.create('file.txt')) } } diff --git a/src/test/groovy/nextflow/lsp/services/config/ConfigFormattingTest.groovy b/src/test/groovy/nextflow/lsp/services/config/ConfigFormattingTest.groovy new file mode 100644 index 0000000..b08208a --- /dev/null +++ b/src/test/groovy/nextflow/lsp/services/config/ConfigFormattingTest.groovy @@ -0,0 +1,76 @@ +/* + * Copyright 2013-2024, Seqera Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package nextflow.lsp.services.config + +import java.nio.file.Files +import java.nio.file.Path + +import nextflow.lsp.services.util.FormattingOptions +import nextflow.lsp.TestLanguageClient +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentItem +import spock.lang.Specification + +/** + * + * @author Ben Sherman + */ +class ConfigFormattingTest extends Specification { + + String openAndFormat(ConfigService service, Path filePath, String contents) { + def uri = filePath.toUri() + def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow-config', 1, contents) + service.didOpen(new DidOpenTextDocumentParams(textDocumentItem)) + def textEdits = service.formatting(uri, new FormattingOptions(4, true, false)) + return textEdits.first().getNewText() + } + + def 'should format a config file' () { + given: + def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/') + if( !Files.exists(workspaceRoot) ) + workspaceRoot.toFile().mkdirs() + + def service = new ConfigService() + service.connect(new TestLanguageClient()) + service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false) + + when: + def filePath = workspaceRoot.resolve('nextflow.config') + def contents = '''\ + process.cpus = 2 ; process.memory = 8.GB + '''.stripIndent() + then: + openAndFormat(service, filePath, contents) == '''\ + process.cpus = 2 + process.memory = 8.GB + '''.stripIndent() + + when: + contents = '''\ + process.cpus = 2 + process.memory = 8.GB + '''.stripIndent() + then: + openAndFormat(service, filePath, contents) == '''\ + process.cpus = 2 + process.memory = 8.GB + '''.stripIndent() + } + +} diff --git a/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy b/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy new file mode 100644 index 0000000..ba55ae3 --- /dev/null +++ b/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy @@ -0,0 +1,79 @@ +/* + * Copyright 2013-2024, Seqera Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package nextflow.lsp.services.script + +import java.nio.file.Files +import java.nio.file.Path + +import nextflow.lsp.services.util.FormattingOptions +import nextflow.lsp.TestLanguageClient +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentItem +import spock.lang.Specification + +/** + * + * @author Ben Sherman + */ +class ScriptFormattingTest extends Specification { + + String openAndFormat(ScriptService service, Path filePath, String contents) { + def uri = filePath.toUri() + def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow', 1, contents) + service.didOpen(new DidOpenTextDocumentParams(textDocumentItem)) + def textEdits = service.formatting(uri, new FormattingOptions(4, true, false)) + return textEdits.first().getNewText() + } + + def 'should format a script' () { + given: + def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/') + if( !Files.exists(workspaceRoot) ) + workspaceRoot.toFile().mkdirs() + + def service = new ScriptService() + service.connect(new TestLanguageClient()) + service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false) + + when: + def filePath = workspaceRoot.resolve('main.nf') + def contents = '''\ + workflow { println 'Hello!' } + '''.stripIndent() + then: + openAndFormat(service, filePath, contents) == '''\ + workflow { + println('Hello!') + } + '''.stripIndent() + + when: + contents = '''\ + workflow { + println('Hello!') + } + '''.stripIndent() + then: + openAndFormat(service, filePath, contents) == '''\ + workflow { + println('Hello!') + } + '''.stripIndent() + } + +}