This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.

150 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-07-29 19:38:21 +02:00
import { describe, expect, it } from "vitest"
2024-08-15 14:14:21 +01:00
import { capitalize, reduceConsecutiveCharacters, trimAny } from "../strings.ts"
2024-07-29 19:38:21 +02:00
describe("capitalize", () => {
it("should capitalize the first letter of a string", () => {
// Arrange - Given
const input = "hello, world!"
// Act - When
const output = capitalize(input)
// Assert - Then
const expected = "Hello, world!"
expect(output).toEqual(expected)
})
it("should return an empty string when the input is an empty string", () => {
// Arrange - Given
const input = ""
// Act - When
const output = capitalize(input)
// Assert - Then
const expected = ""
expect(output).toEqual(expected)
})
it("should return the same string when the first letter is already capitalized", () => {
// Arrange - Given
const input = "Hello, world!"
// Act - When
const output = capitalize(input)
// Assert - Then
const expected = "Hello, world!"
expect(output).toEqual(expected)
})
})
describe("trimAny", () => {
it("should trim any of the specified characters from the start and end of a string", () => {
// Arrange - Given
const input = "_____foo bar "
const characters = [" ", "_"]
// Act - When
const output = trimAny(input, characters)
// Assert - Then
const expected = "foo bar"
expect(output).toEqual(expected)
})
it("should trim any of the specified characters from the start and end of a string even if the start and end characters are different", () => {
// Arrange - Given
const input = "_ __ _foo bar _"
const characters = [" ", "_"]
// Act - When
const output = trimAny(input, characters)
// Assert - Then
const expected = "foo bar"
expect(output).toEqual(expected)
})
it("should return the same string when the input does not start or end with any of the specified characters", () => {
// Arrange - Given
const input = "foo bar"
const characters = [" ", "_"]
// Act - When
const output = trimAny(input, characters)
// Assert - Then
const expected = "foo bar"
expect(output).toEqual(expected)
})
it("should return an empty string when the input is an empty string", () => {
// Arrange - Given
const input = ""
const characters = [" ", "_"]
// Act - When
const output = trimAny(input, characters)
// Assert - Then
const expected = ""
expect(output).toEqual(expected)
})
it("should return an empty string when the input starts and ends with the specified characters", () => {
// Arrange - Given
const input = " _ "
const characters = [" ", "_"]
// Act - When
const output = trimAny(input, characters)
// Assert - Then
const expected = ""
expect(output).toEqual(expected)
})
})
describe("reduceConsecutiveCharacters", () => {
it("should reduce consecutive occurrences of specified characters in a string to a single occurrence", () => {
// Arrange - Given
const input = "Hello___there!!"
const characters = ["_", "!"]
// Act - When
const output = reduceConsecutiveCharacters(input, characters)
// Assert - Then
const expected = "Hello_there!"
expect(output).toEqual(expected)
})
it("should return the same string when there are no consecutive occurrences of specified characters", () => {
// Arrange - Given
const input = "Hello there!"
const characters = ["_", "!"]
// Act - When
const output = reduceConsecutiveCharacters(input, characters)
// Assert - Then
const expected = "Hello there!"
expect(output).toEqual(expected)
})
it("should return an empty string when the input is an empty string", () => {
// Arrange - Given
const input = ""
const characters = ["_", "!"]
// Act - When
const output = reduceConsecutiveCharacters(input, characters)
// Assert - Then
const expected = ""
expect(output).toEqual(expected)
})
})