11import * as assert from 'assert' ;
22import * as path from 'path' ;
33import * as vscode from 'vscode' ;
4+ import { Range , TextDocument } from 'vscode' ;
5+ import { adjustedRangeWithMinimumIndentation , contentOfLinesWithAdjustedIndentation , endOfLineCharacter , linesForIndexes , minimumIndentationForLineIndexes } from '../../../lib/documentHelpers' ;
46
5- import { minimumIndentationForLineIndexes , contentOfLinesWithAdjustedIndentation } from '../../../lib/documentHelpers' ;
67
78const fixturesPath = '/../../../../src/test/fixtures/' ;
89const uri = vscode . Uri . file (
910 path . join ( __dirname + fixturesPath + 'javascript-example.js' )
1011) ;
1112
1213describe ( 'Document Helpers' , function ( ) {
13- context ( 'minimumIndentationLevelForLineIndexes' , async ( ) => {
14- let document : vscode . TextDocument ;
14+ let document : TextDocument ;
15+
16+ before ( async ( ) => {
17+ document = await vscode . workspace . openTextDocument ( uri ) ;
18+ } ) ;
1519
16- before ( async ( ) => {
17- document = await vscode . workspace . openTextDocument ( uri ) ;
20+ context ( 'linesForIndexes' , ( ) => {
21+ it ( 'returns the correct lines' , ( ) => {
22+ const lines = linesForIndexes ( document , [ 0 , 1 ] ) ;
23+ assert . equal ( lines . length , 2 ) ;
24+ assert . equal ( lines [ 0 ] . lineNumber , 0 ) ;
25+ assert . equal ( lines [ 0 ] . text , 'class MyThing {' ) ;
26+ assert . equal ( lines [ 1 ] . lineNumber , 1 ) ;
27+ assert . equal ( lines [ 1 ] . text , ' doSomething(aValue) {' ) ;
1828 } ) ;
29+ } ) ;
1930
31+ context ( 'minimumIndentationLevelForLineIndexes' , async ( ) => {
2032 it ( 'calculates the correct minimum indentation level for a single line' , ( ) => {
21- assert . equal ( 4 , minimumIndentationForLineIndexes ( document , [ 2 ] ) ) ;
33+ assert . equal ( minimumIndentationForLineIndexes ( document , [ 3 ] ) , 6 ) ;
2234 } ) ;
2335
2436 it ( 'calculates the correct minimum indentation level for multiple lines' , ( ) => {
25- assert . equal ( 2 , minimumIndentationForLineIndexes ( document , [ 1 , 2 , 3 ] ) ) ;
37+ assert . equal ( minimumIndentationForLineIndexes ( document , [ 1 , 2 , 3 ] ) , 2 ) ;
2638 } ) ;
2739 } ) ;
2840
2941 context ( 'contentOfLinesWithAdjustedIndentation' , async ( ) => {
30- let document : vscode . TextDocument ;
31-
32- before ( async ( ) => {
33- document = await vscode . workspace . openTextDocument ( uri ) ;
34- } ) ;
35-
3642 it ( 'returns multiline text with the indentation adjusted correctly' , ( ) => {
37- assert . equal ( 'if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}' , contentOfLinesWithAdjustedIndentation ( document , [ 1 , 2 , 3 ] , 2 ) ) ;
43+ assert . equal ( contentOfLinesWithAdjustedIndentation ( document , [ 2 , 3 , 4 ] , 4 ) , 'if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}' ) ;
3844 } ) ;
3945
4046 it ( 'returns single line text with the indentation adjusted correctly' , ( ) => {
41- assert . equal ( 'console.log(`Doing something with ${aValue}!`);' , contentOfLinesWithAdjustedIndentation ( document , [ 2 ] , 4 ) ) ;
47+ assert . equal ( contentOfLinesWithAdjustedIndentation ( document , [ 3 ] , 6 ) , 'console.log(`Doing something with ${aValue}!`);' ) ;
4248 } ) ;
4349
4450 it ( 'returns text with CRLF characters if file is using them' , async ( ) => {
@@ -47,7 +53,30 @@ describe('Document Helpers', function () {
4753 ) ;
4854 let crlfDocument = await vscode . workspace . openTextDocument ( uri ) ;
4955
50- assert . equal ( 'def polish\r\n puts "Polishing"\r\nend' , contentOfLinesWithAdjustedIndentation ( crlfDocument , [ 1 , 2 , 3 ] , 2 ) ) ;
56+ assert . equal ( contentOfLinesWithAdjustedIndentation ( crlfDocument , [ 1 , 2 , 3 ] , 2 ) , 'def polish\r\n puts "Polishing"\r\nend' ) ;
57+ } ) ;
58+ } ) ;
59+
60+ context ( 'adjustedRangeWithMinimumIndentation' , ( ) => {
61+ it ( 'adjusts the range' , ( ) => {
62+ const adjustedRange = adjustedRangeWithMinimumIndentation ( new Range ( 2 , 0 , 2 , 17 ) , 4 ) ;
63+ assert . equal ( adjustedRange . start . line , 2 ) ;
64+ assert . equal ( adjustedRange . start . character , 4 ) ;
65+ assert . equal ( adjustedRange . end . line , 2 ) ;
66+ assert . equal ( adjustedRange . end . character , 17 ) ;
67+ } ) ;
68+ } ) ;
69+
70+ context ( 'endOfLineCharacter' , ( ) => {
71+ it ( 'correctly returns LF' , async ( ) => {
72+ assert . equal ( endOfLineCharacter ( document ) , '\n' ) ;
73+ } ) ;
74+
75+ it ( 'correctly returns CRLF' , async ( ) => {
76+ const uri = vscode . Uri . file (
77+ path . join ( __dirname + fixturesPath + 'crlf-ruby-example.rb' )
78+ ) ;
79+ assert . equal ( endOfLineCharacter ( await vscode . workspace . openTextDocument ( uri ) ) , '\r\n' ) ;
5180 } ) ;
5281 } ) ;
5382} ) ;
0 commit comments