/********************************************************************************
 * Copyright (C) 2018 Red Hat, Inc. and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/ import * as assert from 'assert' ; import * as Converter from './type-converters' ; import * as theia from '@theia/plugin' ; import * as types from './types-impl' ; import * as model from '../common/plugin-api-rpc-model' ; import { MarkdownString , isMarkdownString } from './markdown-string' ; import { TaskDto } from '../common/plugin-api-rpc' ; import { TaskGroup } from './types-impl' ; describe ( 'Type converters:' , ( ) => { describe ( 'convert ranges:' , ( ) => { const modelRange = { startLineNumber : 5 , startColumn : 5 , endLineNumber : 10 , endColumn : 20 } ; const pluginRange = new types . Range ( 4 , 4 , 9 , 19 ) ; it ( 'should convert to theia range' , ( ) => { const result = Converter . toRange ( modelRange ) ; assert . deepStrictEqual ( result , pluginRange ) ; } ) ; it ( 'should convert to model range' , ( ) => { const result = Converter . fromRange ( pluginRange ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , modelRange ) ; } ) ; } ) ; describe ( 'markdown:' , ( ) => { describe ( 'type guard:' , ( ) => { it ( 'should recognize markdown string' , ( ) => { const markdownString = new MarkdownString ( '**test**' ) ; const result = isMarkdownString ( markdownString ) ; assert . deepStrictEqual ( result !== false , true ) ; } ) ; it ( 'should recognize markdown object' , ( ) => { const markdownObject = { value : '*test*' } ; const result = isMarkdownString ( markdownObject ) ; assert . deepStrictEqual ( result !== false , true ) ; } ) ; it ( 'should recognize markdown object with redundant fields' , ( ) => { const markdownObject = { field1 : 5 , value : '*test*' , field2 : 'test' } ; const result = isMarkdownString ( markdownObject ) ; assert . deepStrictEqual ( result !== false , true ) ; } ) ; it ( 'should reject non markdown object' , ( ) => { const nonMarkdownObject = { field1 : 5 , field2 : 'test' } ; const result = isMarkdownString ( nonMarkdownObject ) ; assert . deepStrictEqual ( result === false , true ) ; } ) ; it ( 'should reject non markdown object if it contains isTrusted field' , ( ) => { const nonMarkdownObject = { isTrusted : true , field1 : 5 , field2 : 'test' } ; const result = isMarkdownString ( nonMarkdownObject ) ; assert . deepStrictEqual ( result === false , true ) ; } ) ; } ) ; describe ( 'converter: ' , ( ) => { const aStringWithMarkdown = '**test**' ; const pluginMarkdown = new MarkdownString ( aStringWithMarkdown ) ; const aLanguage = 'typescript' ; const aValue = 'const x=5;' ; const codeblock = { language : aLanguage , value : aValue } ; const modelMarkdownWithCode = { value : '```' + aLanguage + '\n' + aValue + '\n```\n' } ; const modelMarkdown = { value : aStringWithMarkdown } ; it ( 'should convert plugin markdown to model markdown' , ( ) => { const result = { ... Converter . fromMarkdown ( pluginMarkdown ) } ; assert . deepStrictEqual ( result , modelMarkdown ) ; } ) ; it ( 'should convert string to model markdown' , ( ) => { const result = Converter . fromMarkdown ( aStringWithMarkdown ) ; assert . deepStrictEqual ( result , modelMarkdown ) ; } ) ; it ( 'should convert codeblock to model markdown' , ( ) => { const result = Converter . fromMarkdown ( codeblock ) ; assert . deepStrictEqual ( result , modelMarkdownWithCode ) ; } ) ; it ( 'should convert array of markups to model markdown' , ( ) => { const markups = [ pluginMarkdown , aStringWithMarkdown , codeblock ] ; const result = Converter . fromManyMarkdown ( markups ) . map ( md => ( { ... md } ) ) ; assert . deepStrictEqual ( Array . isArray ( result ) , true ) ; assert . deepStrictEqual ( result . length , 3 ) ; assert . deepStrictEqual ( result [ 0 ] , modelMarkdown ) ; assert . deepStrictEqual ( result [ 1 ] , modelMarkdown ) ; assert . deepStrictEqual ( result [ 2 ] , modelMarkdownWithCode ) ; } ) ; } ) ; } ) ; describe ( 'convert tasks:' , ( ) => { const customType = 'custom' ; const shellType = 'shell' ; const label = 'yarn build' ; const source = 'source' ; const command = 'yarn' ; const commandLine = 'yarn run build' ; const args = [ 'run' , 'build' ] ; const cwd = '/projects/theia' ; const additionalProperty = 'some property' ; const groupDto = 'build' ; const group = TaskGroup . Build ; const shellTaskDto = { type : shellType , taskType : shellType , label , source , scope : 1 , command , args , options : { cwd } , additionalProperty , presentation : { reveal : 3 , focus : true } , group : groupDto } ; const shellTaskDtoWithCommandLine = { type : shellType , taskType : shellType , label , source , scope : 2 , command : commandLine , options : { cwd } , additionalProperty } ; const shellPluginTask = { name : label , source , scope : 1 , definition : { type : shellType , additionalProperty } , presentationOptions : { reveal : types . TaskRevealKind . Never , focus : true } , group , execution : { command , args , options : { cwd } } } ; const pluginTaskWithCommandLine = { name : label , source , scope : 2 , definition : { type : shellType , additionalProperty } , execution : { commandLine , options : { cwd } } } ; const customTaskDto = { ... shellTaskDto , type : customType } ; const customTaskDtoWithCommandLine = { ... shellTaskDtoWithCommandLine , type : customType } ; const customPluginTask = { ... shellPluginTask , definition : { type : customType , additionalProperty } } ; const customPluginTaskWithCommandLine = { name : label , source , scope : 2 , definition : { type : customType , additionalProperty } , execution : { commandLine , options : { cwd } } } ; it ( 'should convert to task dto' , ( ) => { const result = Converter . fromTask ( shellPluginTask ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , shellTaskDto ) ; } ) ; it ( 'should convert from task dto' , ( ) => { const result = Converter . toTask ( shellTaskDto ) ; assert . strictEqual ( result . execution instanceof types . ShellExecution , true ) ; if ( result . execution instanceof types . ShellExecution ) { assert . strictEqual ( result . execution . commandLine , undefined ) ; result . execution = { args : result . execution . args , options : result . execution . options , command : result . execution . command } ; } assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , shellPluginTask ) ; } ) ; it ( 'should convert to task dto from task with commandline' , ( ) => { const result = Converter . fromTask ( pluginTaskWithCommandLine ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , shellTaskDtoWithCommandLine ) ; } ) ; it ( 'should convert task with custom type to dto' , ( ) => { const result = Converter . fromTask ( customPluginTask ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , customTaskDto ) ; } ) ; it ( 'should convert task with custom type from dto' , ( ) => { const result = Converter . toTask ( customTaskDto ) ; assert . strictEqual ( result . execution instanceof types . ShellExecution , true ) ; if ( result . execution instanceof types . ShellExecution ) { assert . strictEqual ( result . execution . commandLine , undefined ) ; result . execution = { args : result . execution . args , options : result . execution . options , command : result . execution . command } ; } assert . deepStrictEqual ( result , customPluginTask ) ; } ) ; it ( 'should convert to task dto from custom task with commandline' , ( ) => { const result = Converter . fromTask ( customPluginTaskWithCommandLine ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , customTaskDtoWithCommandLine ) ; } ) ; } ) ; describe ( 'Webview Panel Show Options:' , ( ) => { it ( 'should create options from view column ' , ( ) => { const viewColumn = types . ViewColumn . Five ; const showOptions = { area : types . WebviewPanelTargetArea . Main , viewColumn : types . ViewColumn . Four , preserveFocus : false } ; const result = Converter . toWebviewPanelShowOptions ( viewColumn ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , showOptions ) ; } ) ; it ( 'should create options from given "WebviewPanelShowOptions" object ' , ( ) => { const incomingObject = { area : types . WebviewPanelTargetArea . Main , viewColumn : types . ViewColumn . Five , preserveFocus : true } ; const showOptions = { area : types . WebviewPanelTargetArea . Main , viewColumn : types . ViewColumn . Four , preserveFocus : true } ; const result = Converter . toWebviewPanelShowOptions ( incomingObject ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , showOptions ) ; } ) ; it ( 'should set default "main" area' , ( ) => { const incomingObject = { viewColumn : types . ViewColumn . Five , preserveFocus : false } ; const showOptions = { area : types . WebviewPanelTargetArea . Main , viewColumn : types . ViewColumn . Four , preserveFocus : false } ; const result = Converter . toWebviewPanelShowOptions ( incomingObject ) ; assert . notStrictEqual ( result , undefined ) ; assert . deepStrictEqual ( result , showOptions ) ; } ) ; } ) ; } ) ;