diff --git a/specs/396-add-greeting-utf32-length-library-helper/design.md b/specs/396-add-greeting-utf32-length-library-helper/design.md new file mode 100644 index 0000000..c5d64d8 --- /dev/null +++ b/specs/396-add-greeting-utf32-length-library-helper/design.md @@ -0,0 +1,59 @@ +# Design: Add greeting_utf32_length library helper + +**Issue**: #396 +**Date**: 2026-11-30 +**Status**: Approved +**Author**: NMG + +## Overview + +The new helper follows `greeting_utf16_length`. It calls `greet(name)`, which validates the name, then encodes the completed greeting with the BOM-free `utf-32-le` codec and returns the byte length divided by four. It is exported from the package root and documented in the README Library section. It needs no new import, no new module, and no runtime dependency. + +## Architecture + +In `src/nmg_sdlc_smoke/greet.py`, add the following immediately after `greeting_utf8_length` and before `greeting_is_ascii`, matching the spacing of the neighbouring length helpers: + +```python +def greeting_utf32_length(name: str) -> int: + return len(greet(name).encode("utf-32-le")) // 4 +``` + +- **Validation.** `greet(name)` runs first, so invalid names propagate its `ValueError("name must not be blank")` unchanged. +- **Encoding.** `utf-32-le` emits no byte-order mark, so every code point is exactly four bytes and the result equals the code-point count. No Unicode normalization. Lone surrogates are not among the specified inputs. +- **Return type.** Integer floor division of an `int` is an `int`. + +In `src/nmg_sdlc_smoke/__init__.py`: +- add `from .greet import greeting_utf32_length as greeting_utf32_length` immediately after the `greeting_utf16_length` import line; +- add `"greeting_utf32_length"` to `__all__` immediately after `"greeting_utf16_length"`; +- keep every existing import and `__all__` entry. + +In the `README.md` `## Library` section: +- add `greeting_utf32_length,` to the import list immediately after `greeting_utf8_length,`; +- add the example `greeting_utf32_length("Ada") # 10` immediately after `greeting_utf8_length("Ada") # 10`; +- add this exact prose line immediately after the `greeting_utf8_length` prose line, separated by one blank line: + +```markdown +`greeting_utf32_length` returns the number of UTF-32 code units in the complete greeting (not the name alone), including the `Hello, ` prefix and excluding any byte-order mark, so every code point, including a character outside the Basic Multilingual Plane, counts as one unit (for example `Ada` gives `10` and `😀` gives `8`); no Unicode normalization is applied, so precomposed and decomposed spellings of a name can produce different lengths, and it inherits `greet`'s `ValueError("name must not be blank")` for empty, whitespace-only, and non-string names. +``` + +Tests: four acceptance scenarios go in `tests/features/add_greeting_utf32_length_library_helper.feature`, with their steps in `tests/features/steps/test_greeting_utf32_length_steps.py`. + +## API / Interface Changes + +| Interface | Input | Output / Error | Behavior | +|-----------|-------|----------------|----------| +| `nmg_sdlc_smoke.greeting_utf32_length(name: str) -> int` | Valid nonblank string name | `int`. Invalid input raises `ValueError("name must not be blank")` | UTF-32 code units of `greet(name)`: `"Ada"` → `10`, `"😀"` → `8` | + +## Alternatives Considered + +| Option | Decision | Reason | +|--------|----------|--------| +| `len(greet(name).encode("utf-32-le")) // 4` | Selected | Standard library, one line, mirrors `greeting_utf16_length` | +| `len(greet(name))` | Rejected | Same result, but hides the encoding contract the helper names | +| `encode("utf-32")` | Rejected | Prepends a 4-byte byte-order mark, giving one extra unit | + +## Change History + +| Issue | Date | Summary | +|-------|------|---------| +| #396 | 2026-11-30 | Initial feature design | diff --git a/specs/396-add-greeting-utf32-length-library-helper/feature.gherkin b/specs/396-add-greeting-utf32-length-library-helper/feature.gherkin new file mode 100644 index 0000000..67112e5 --- /dev/null +++ b/specs/396-add-greeting-utf32-length-library-helper/feature.gherkin @@ -0,0 +1,35 @@ +**Issue**: #396 +**Date**: 2026-11-30 +**Status**: Approved +**Author**: NMG + +# File: tests/features/add_greeting_utf32_length_library_helper.feature +# Generated from: specs/396-add-greeting-utf32-length-library-helper/requirements.md +Feature: Return the UTF-32 code-unit length of the completed greeting + + @SCN001 + Scenario: Return the UTF-32 code-unit length of the greeting + Given the installed nmg_sdlc_smoke package + When greeting_utf32_length is called with Ada, Zo U+00EB, U+674E U+96F7, Ada space U+1F600, and U+1F600 + Then the results are 10, then 10, then 9, then 12, then 8 + And each result is an int equal to the UTF-32-LE encoded greeting length divided by 4 + + @SCN002 + Scenario: Count supplementary characters as one unit and apply no normalization + Given the installed nmg_sdlc_smoke package + When greeting_utf32_length is called with U+1F600 and with Zoe U+0308 + Then U+1F600 gives 8, which differs from the UTF-16 length 9 + And Zoe U+0308 gives 11, which differs from the precomposed result 10 + + @SCN003 + Scenario: Preserve invalid-name behavior + Given the invalid names empty, space-tab, None, and 42 + When greeting_utf32_length is called with each invalid name + Then each call raises ValueError with message name must not be blank + + @SCN004 + Scenario: Expose and document the helper + Given the README Library section + When the README Library example runs and prints greeting_utf32_length of Ada + Then it prints 10 and greeting_utf32_length is listed in nmg_sdlc_smoke.__all__ with every prior export + And the README documents the UTF-32 code-unit count of the complete greeting, no byte-order mark, no normalization, and the inherited invalid-name error diff --git a/specs/396-add-greeting-utf32-length-library-helper/requirements.md b/specs/396-add-greeting-utf32-length-library-helper/requirements.md new file mode 100644 index 0000000..a8aad25 --- /dev/null +++ b/specs/396-add-greeting-utf32-length-library-helper/requirements.md @@ -0,0 +1,73 @@ +# Requirements: Add greeting_utf32_length library helper + +**Issue**: #396 +**Date**: 2026-11-30 +**Status**: Approved +**Author**: NMG + +## User Story + +**As a** caller of the Python greeting library +**I want** a public `greeting_utf32_length(name)` helper that returns the number of UTF-32 code units in the completed greeting +**So that** I can get the greeting's fixed-width code-unit size without re-implementing the greeting or the encoding + +## Background + +For valid names, `greet(name)` returns `Hello, {name}`. For empty, whitespace-only, and non-string names, it raises `ValueError("name must not be blank")`. + +`greeting_utf32_length` encodes the complete greeting (not the name alone), including the `Hello, ` prefix, as UTF-32 without a byte-order mark and returns the number of 4-byte code units as an `int`. Every code point, including characters outside the Basic Multilingual Plane, counts as exactly one unit. No Unicode normalization is applied. + +`greeting_utf16_length` (UTF-16 code units) and `greeting_utf8_length` (UTF-8 bytes) are the existing encoded-length helpers. Each public helper has a definition in `src/nmg_sdlc_smoke/greet.py`, a self-aliased re-export and `__all__` entry in `nmg_sdlc_smoke`, and a README Library entry with an import, an example, and a prose contract line. No helper returns the UTF-32 code-unit length. + +## Acceptance Criteria + +### AC1: Return the UTF-32 code-unit length of the greeting + +**Given** the valid names `Ada`, `Zoë` (precomposed U+00EB), `李雷` (U+674E U+96F7), `"Ada \U0001F600"`, and `"\U0001F600"` (U+1F600 GRINNING FACE) +**When** a caller invokes `greeting_utf32_length` with each name +**Then** they return `10`, `10`, `9`, `12`, and `8` respectively +**And** `type(result) is int` for each result +**And** each result equals `len(greet(name).encode("utf-32-le")) // 4` + +### AC2: Count supplementary characters as one unit and apply no normalization + +**Given** the valid names `"\U0001F600"` and `"Zoë"` spelled decomposed (`e` + U+0308 COMBINING DIAERESIS) +**When** a caller invokes `greeting_utf32_length` with each one +**Then** `"\U0001F600"` returns `8`, which differs from `greeting_utf16_length("\U0001F600")` (`9`) +**And** decomposed `"Zoë"` returns `11`, which differs from the precomposed result `10` + +### AC3: Preserve invalid-name behavior + +**Given** the invalid names `""`, `" \t"`, `None`, and `42` +**When** a caller invokes `greeting_utf32_length` with each one +**Then** each call raises `ValueError` with the message `name must not be blank` + +### AC4: Expose and document the helper + +**Given** the installed `nmg_sdlc_smoke` package and the README Library section +**When** a caller runs `from nmg_sdlc_smoke import greeting_utf32_length` and follows the README example +**Then** the import succeeds and the README example `greeting_utf32_length("Ada")` gives `10` +**And** `"greeting_utf32_length"` is listed in `nmg_sdlc_smoke.__all__` alongside every previously listed name +**And** the README says that it returns the UTF-32 code-unit count of the complete greeting, including the `Hello, ` prefix, with no byte-order mark and without normalization, and inherits `ValueError("name must not be blank")` + +## Functional Requirements + +| ID | Requirement | Priority | +|----|-------------|----------| +| FR1 | Add `greeting_utf32_length(name: str) -> int`, returning `len(greet(name).encode("utf-32-le")) // 4` | Must | +| FR2 | Raise `greet`'s `ValueError("name must not be blank")` for empty, whitespace-only, and non-string names | Must | +| FR3 | Re-export `greeting_utf32_length` from the `nmg_sdlc_smoke` package with a self-alias and list it in `__all__` without removing existing exports | Must | +| FR4 | Document `greeting_utf32_length` in the README Library section (import list, usage example, behavior sentence), following `greeting_utf8_length` | Should | + +## Out of Scope + +- A `nmg-smoke` CLI flag or option that exposes the code-unit length +- Byte counts (rather than code units) for UTF-32, a byte-order mark, or a configurable encoding +- Unicode normalization before encoding +- Changes to `greet`, `greeting_length`, `greeting_utf8_length`, `greeting_utf16_length`, or any other existing helper + +## Change History + +| Issue | Date | Summary | +|-------|------|---------| +| #396 | 2026-11-30 | Initial feature spec | diff --git a/specs/396-add-greeting-utf32-length-library-helper/tasks.md b/specs/396-add-greeting-utf32-length-library-helper/tasks.md new file mode 100644 index 0000000..909f8e5 --- /dev/null +++ b/specs/396-add-greeting-utf32-length-library-helper/tasks.md @@ -0,0 +1,56 @@ +# Tasks: Add greeting_utf32_length library helper + +**Issue**: #396 +**Date**: 2026-11-30 +**Status**: Approved +**Author**: NMG + +## Implementation Tasks + +### T001: Implement, export, and document greeting_utf32_length + +**File(s)**: `src/nmg_sdlc_smoke/greet.py`, `src/nmg_sdlc_smoke/__init__.py`, `README.md` +**Type**: Modify +**Depends**: none +**Acceptance**: +- In `greet.py`, add `greeting_utf32_length(name: str) -> int` exactly as in design.md, immediately after `greeting_utf8_length` (AC1, AC2, AC3, FR1, FR2). +- In `__init__.py`: + - add `from .greet import greeting_utf32_length as greeting_utf32_length` immediately after the `greeting_utf16_length` import; + - add the `__all__` entry `"greeting_utf32_length"` immediately after `"greeting_utf16_length"`; + - remove no existing export (AC4, FR3). +- In README `## Library`: + - add `greeting_utf32_length,` after `greeting_utf8_length,` in the import list; + - add `greeting_utf32_length("Ada") # 10` after the `greeting_utf8_length("Ada") # 10` example; + - add the exact `greeting_utf32_length` prose line from design.md after the `greeting_utf8_length` prose line (AC4, FR4). +- `python -m ruff check .` passes. + +### T002: Implement the `feature.gherkin` scenarios + +**File(s)**: `tests/features/add_greeting_utf32_length_library_helper.feature`, `tests/features/steps/test_greeting_utf32_length_steps.py` +**Type**: Create +**Depends**: T001 +**Acceptance**: +- Copy the four tagged scenarios from this spec's `feature.gherkin` into the executable `.feature`, without frontmatter or source comments. +- Implement deterministic steps with `scenarios("../add_greeting_utf32_length_library_helper.feature")` and public `nmg_sdlc_smoke` imports. Follow `tests/features/steps/test_greeting_utf8_length_steps.py`. Write non-ASCII inputs as Python `\N{...}` escapes. +- `@SCN001` (AC1): + - assert `10` for `"Ada"`, `10` for `"Zo\N{LATIN SMALL LETTER E WITH DIAERESIS}"`, `9` for `"\N{CJK UNIFIED IDEOGRAPH-674E}\N{CJK UNIFIED IDEOGRAPH-96F7}"`, `12` for `"Ada \N{GRINNING FACE}"`, and `8` for `"\N{GRINNING FACE}"`; + - assert `type(result) is int` for each; + - assert each result equals `len(greet(name).encode("utf-32-le")) // 4`. +- `@SCN002` (AC2): + - assert `8` for `"\N{GRINNING FACE}"` and that it differs from `greeting_utf16_length("\N{GRINNING FACE}")`, which is `9`; + - assert `11` for `"Zoe\N{COMBINING DIAERESIS}"` and that it differs from the precomposed result `10`. +- `@SCN003` (AC3): assert `ValueError` with the exact message `name must not be blank` for `""`, `" \t"`, `None`, and `42`. +- `@SCN004` (AC4): + - `from nmg_sdlc_smoke import greeting_utf32_length` succeeds; + - read the first fenced `python` block of `README.md`, resolved from `Path(__file__).resolve().parents[3]`; + - append `print(greeting_utf32_length("Ada"))` and run the result with `sys.executable -c` and `encoding="utf-8"`, with `PYTHONIOENCODING=utf-8` in the child environment; + - the run gives `(returncode, stdout, stderr) == (0, "10\n", "")`; + - `"greeting_utf32_length" in nmg_sdlc_smoke.__all__`, and every name previously in `__all__` remains, including `greeting_utf8_length`, `greeting_utf16_length`, `greeting_length`, and `greeting_crc_hqx`; + - the exact `greeting_utf32_length` prose line from design.md is one of the lines of `README.md`. +- Run `python -m pytest tests/features/steps/test_greeting_utf32_length_steps.py`. + +## Change History + +| Issue | Date | Summary | +|-------|------|---------| +| #396 | 2026-11-30 | Initial feature tasks |