Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contains operator to check if codepoint belongs to specified group #4

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,33 @@ public val CodePoint.category: CodepointCategory
return CodepointCategory.entries.first { it.characterData.contains(this) }
}

public operator fun CodepointCategory.contains(codepoint: CodePoint): Boolean = characterData.contains(codepoint)

public val CodePoint.bidirectionalClass: CodepointBidirectionalClass
get() {
requireCodepointInRange()
return CodepointBidirectionalClass.entries.first { it.characterData.contains(this) }
}

public operator fun CodepointBidirectionalClass.contains(codepoint: CodePoint): Boolean =
characterData.contains(codepoint)

public val CodePoint.joiningType: CodepointJoiningType
get() {
requireCodepointInRange()
return CodepointJoiningType.entries.first { it.contains(this) }
}

public operator fun CodepointJoiningType.contains(codepoint: CodePoint): Boolean = contains(codepoint)

public val CodePoint.derivedProperty: CodepointDerivedProperty
get() {
requireCodepointInRange()
return CodepointDerivedProperty.entries.first { it.contains(this) }
}

public operator fun CodepointDerivedProperty.contains(codepoint: CodePoint): Boolean = contains(codepoint)

private fun CodePoint.requireCodepointInRange() {
require(this in 0..MAX_CODE_POINT) {
"code point must be in [0, 0x${MAX_CODE_POINT.toString(16).uppercase()}]"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.optimumcode.karacteristics.test.extension

import io.github.optimumcode.karacteristics.CodepointBidirectionalClass
import io.github.optimumcode.karacteristics.CodepointCategory
import io.github.optimumcode.karacteristics.CodepointDerivedProperty
import io.github.optimumcode.karacteristics.CodepointJoiningType
import io.github.optimumcode.karacteristics.bidirectionalClass
import io.github.optimumcode.karacteristics.category
import io.github.optimumcode.karacteristics.contains
import io.github.optimumcode.karacteristics.derivedProperty
import io.github.optimumcode.karacteristics.joiningType
import io.kotest.assertions.asClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class ContainsTest :
FunSpec(
{
val codepoint = 0x20
test("category contains") {
codepoint.category.asClue {
(codepoint in CodepointCategory.SPACE_SEPARATOR) shouldBe true
}
}

test("bidirectional class contains") {
codepoint.bidirectionalClass.asClue {
(codepoint in CodepointBidirectionalClass.WHITE_SPACE) shouldBe true
}
}

test("joining type contains") {
codepoint.joiningType.asClue {
(codepoint in CodepointJoiningType.NON_JOINING) shouldBe true
}
}

test("derived property contains") {
codepoint.derivedProperty.asClue {
(codepoint in CodepointDerivedProperty.DISALLOWED) shouldBe true
}
}
},
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package io.github.optimumcode.karacteristics
package io.github.optimumcode.karacteristics.test.extension

import io.github.optimumcode.karacteristics.CodePoint
import io.github.optimumcode.karacteristics.CodepointBidirectionalClass
import io.github.optimumcode.karacteristics.CodepointCategory
import io.github.optimumcode.karacteristics.CodepointDerivedProperty
import io.github.optimumcode.karacteristics.CodepointJoiningType
import io.github.optimumcode.karacteristics.bidirectionalClass
import io.github.optimumcode.karacteristics.category
import io.github.optimumcode.karacteristics.contains
import io.github.optimumcode.karacteristics.derivedProperty
import io.github.optimumcode.karacteristics.joiningType
import io.kotest.assertions.asClue
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
Expand All @@ -24,5 +35,30 @@ class NegativeTest :
}.message shouldBe "code point must be in [0, 0x10FFFF]"
}
}

val codepoint = 0x20
test("category not contains") {
codepoint.category.asClue {
(codepoint in CodepointCategory.SPACING_MARK) shouldBe false
}
}

test("bidirectional class not contains") {
codepoint.bidirectionalClass.asClue {
(codepoint in CodepointBidirectionalClass.ARABIC_LETTER) shouldBe false
}
}

test("joining type not contains") {
codepoint.joiningType.asClue {
(codepoint in CodepointJoiningType.DUAL_JOINING) shouldBe false
}
}

test("derived property not contains") {
codepoint.derivedProperty.asClue {
(codepoint in CodepointDerivedProperty.UNASSIGNED) shouldBe false
}
}
},
)
Loading