Skip to content

Commit

Permalink
Increase test coverage of class CommentUtil (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalre committed Jan 16, 2023
1 parent 1fa57cd commit fbc9652
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 12 deletions.
91 changes: 89 additions & 2 deletions src/test/suite/util/comment-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,41 @@ suite("Test CommentUtil - append()", () => {
})
})

// TODO: add suit for insertCommentBetween
suite("Test CommentUtil - insert()", () => {
test("when comment is found should insert", () => {
const text =
'lorem ipsum\n' +
'dolor sit\n' +
'amet, consetetur'
const commentutil = new CommentUtil(text)
const comment = ['#foo', 'dolor sit']

commentutil.insert(comment)

const expected =
'lorem ipsum\n' +
'#foo\n' +
'dolor sit\n' +
'amet, consetetur'
equal(commentutil.text, expected)
})
test("when comment is not found should do nothing", () => {
const text =
'lorem ipsum\n' +
'dolor sit\n' +
'amet, consetetur'
const commentutil = new CommentUtil(text)
const comment = ['#foo', 'do not find']

commentutil.insert(comment)

const expected =
'lorem ipsum\n' +
'dolor sit\n' +
'amet, consetetur'
equal(commentutil.text, expected)
})
})

suite("Test CommentUtil - getIndexOfString()", () => {
test("should return index of line", () => {
Expand All @@ -158,10 +192,63 @@ suite("Test CommentUtil - getIndexOfString()", () => {
const commentutil = new CommentUtil(text)

equal(commentutil.getIndexOfString(line), 12)
equal(commentutil.getIndexOfString("vscode-yaml-sort.lastLine"), commentutil.text.length)
})
})

// TODO: Add suite for search, searchExactMatch, searchFuzzyForTrimmedText, searchFuzzyForKeyword
suite("Test CommentUtil - search()", () => {
test("should return last index of text", () => {
const text =
'foo: lorem ipsum\n' +
'bar: dolor sit\n' +
'baz: amet, consetetur'
const commentutil = new CommentUtil(text)

equal(commentutil.search('foo: lorem ipsum'), 0)
equal(commentutil.search(' bar: dolor sit '), 17)
equal(commentutil.search('baz: "amet, consetetur"'), 32)
})
})

suite("Test CommentUtil - searchExactMatch()", () => {
test("should return last index of text", () => {
const line = 'bar: dolor sit'
const text =
'foo: lorem ipsum\n' +
`${line}\n` +
`${line}\n` +
'baz: amet, consetetur'
const commentutil = new CommentUtil(text)

equal(commentutil.searchExactMatch(line), 32)
})
})

suite("Test CommentUtil - searchFuzzyForTrimmedText()", () => {
test("should return last index of keyword", () => {
const line = ' bar: dolor sit '
const text =
'foo: lorem ipsum\n' +
`${line.trim()}\n` +
'baz: amet, consetetur'
const commentutil = new CommentUtil(text)

equal(commentutil.searchFuzzyForTrimmedText(line), 17)
})
})

suite("Test CommentUtil - searchFuzzyForKeyword()", () => {
test("should return last index of keyword", () => {
const line = 'bar: dolor sit'
const text =
'foo: lorem ipsum\n' +
`${line}\n` +
'baz: amet, consetetur'
const commentutil = new CommentUtil(text)

equal(commentutil.searchFuzzyForKeyword(line), 17)
})
})

suite("Test CommentUtil - isCommentFound()", () => {
test("when index is -1 should return true", () => {
Expand Down
16 changes: 6 additions & 10 deletions src/util/comment-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,20 @@ export class CommentUtil {
if (comment[1] === "vscode-yaml-sort.lastLine") {
this.append(comment[0])
} else {
this.insertCommentBetween(comment)
this.insert(comment)
}
}

append(line: string) {
this.text = `${this.text}\n${line}`
}

insertCommentBetween(comment: string[]) {
insert(comment: string[]) {
const indexOfComment = this.getIndexOfString(comment[1])
if (CommentUtil.isCommentFound(indexOfComment)) {
const textAfter = this.text.slice(indexOfComment)
if (textAfter.trim() === "") {
this.append(comment[0])
} else {
const textBefore = this.text.slice(0, indexOfComment)
this.text = `${textBefore}${comment[0]}\n${textAfter}`
}
const textBefore = this.text.slice(0, indexOfComment)
this.text = `${textBefore}${comment[0]}\n${textAfter.trimEnd()}`
}
}

Expand Down Expand Up @@ -104,11 +100,11 @@ export class CommentUtil {
}

searchFuzzyForTrimmedText(text: string): number {
return this.text.lastIndexOf(text.trim())
return this.searchExactMatch(text.trim())
}

searchFuzzyForKeyword(text: string): number {
return this.text.lastIndexOf(text.split(":")[0])
return this.searchExactMatch(text.split(":")[0])
}

static isCommentFound(index: number): boolean {
Expand Down

0 comments on commit fbc9652

Please sign in to comment.