From 6a9f58986d29ec2918a10e600cf8e84b84eac54f Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Fri, 18 Jul 2025 07:15:43 +0200 Subject: [PATCH] Fix pascalcase stealing first character from boundaries By picking the first character out of the input, an artificial boundary is created between first and second character. Fixed by creating a pascalcase specific boundary checking if there is no previous character. --- caseconverter/boundaries.py | 2 +- caseconverter/pascal.py | 13 +++++++++++-- caseconverter/pascal_test.py | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/caseconverter/boundaries.py b/caseconverter/boundaries.py index 783906c..0e9d5d6 100644 --- a/caseconverter/boundaries.py +++ b/caseconverter/boundaries.py @@ -99,7 +99,7 @@ def __init__(self, join_char=""): self._join_char = join_char def is_boundary(self, pc, c): - return pc != None and pc.isalpha() and pc.isupper() and c.isupper() + return pc is not None and pc.isalpha() and pc.isupper() and c.isupper() def handle(self, pc, cc, input_buffer, output_buffer): output_buffer.write(cc) diff --git a/caseconverter/pascal.py b/caseconverter/pascal.py index 79f9655..57bebe9 100644 --- a/caseconverter/pascal.py +++ b/caseconverter/pascal.py @@ -1,16 +1,25 @@ from .caseconverter import CaseConverter from .boundaries import ( + BoundaryHandler, OnDelimeterUppercaseNext, OnUpperPrecededByLowerAppendUpper, OnUpperPrecededByUpperAppendCurrent, ) +class OnFirstCharUpper(BoundaryHandler): + """Boundary handler that ensures the first character is uppercase.""" + + def is_boundary(self, pc, c): + return pc is None + + def handle(self, pc, cc, input_buffer, output_buffer): + output_buffer.write(cc.upper()) + class Pascal(CaseConverter): - def init(self, input_buffer, output_buffer): - output_buffer.write(input_buffer.read(1).upper()) def define_boundaries(self): + self.add_boundary_handler(OnFirstCharUpper()) self.add_boundary_handler(OnDelimeterUppercaseNext(self.delimiters())) self.add_boundary_handler(OnUpperPrecededByLowerAppendUpper()) self.add_boundary_handler(OnUpperPrecededByUpperAppendCurrent()) diff --git a/caseconverter/pascal_test.py b/caseconverter/pascal_test.py index bd28da3..e4756fb 100644 --- a/caseconverter/pascal_test.py +++ b/caseconverter/pascal_test.py @@ -31,6 +31,7 @@ # Alternating character cases ("heLlo WoRld", "HeLloWoRld"), ("helloWORLD", "HelloWORLD"), + ("HELLOWorld", "HELLOWorld"), ], ) def test_pascal_with_default_args(input, output):