Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Dec 26, 2024
1 parent ab879eb commit e729314
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Tests/MessagingInApp/Gist/Utilities/StringPercentEncodeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@testable import CioMessagingInApp
import XCTest

class StringPercentEncodeTests: XCTestCase {
func test_givenValidCharacter_expectEncodedURL() {
let input = "https://example.com/path#fragment"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "https://example.com/path%23fragment")
}

func test_givenMultipleOccurrences_expectAllEncoded() {
let input = "https://example.com/path#fragment#another"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "https://example.com/path%23fragment%23another")
}

func test_givenMultipleCharacter_expectEncodedGivenOnly() {
let input = "https://example.com/path#source=link&medium=email"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "https://example.com/path%23source=link&medium=email")
}

func test_givenCharacterInAllowedSet_expectUnchangedString() {
let input = "https://example.com/path/fragment"

// '/' is allowed in `.urlPathAllowed`
let encoded = input.percentEncode(character: "/")

XCTAssertEqual(encoded, input, "The '/' character should not be encoded as it is allowed in the path")
}

func test_givenCustomAllowedCharacterSet_expectEncodedSpaces() {
let input = "https://example.com/path with spaces"

let encoded = input.percentEncode(character: " ", withAllowedCharacters: .alphanumerics)

XCTAssertEqual(encoded, "https://example.com/path%20with%20spaces")
}

func test_givenNoTargetCharacter_expectUnchangedString() {
let input = "https://example.com/path/fragment"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, input)
}

func test_givenEmptyTargetCharacter_expectUnchangedString() {
let input = "https://example.com/path/fragment"

let encoded = input.percentEncode(character: "")

XCTAssertEqual(encoded, input)
}

func test_givenEmptyInput_expectEmptyString() {
let input = ""

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "")
}
}

0 comments on commit e729314

Please sign in to comment.