Files
regorus/tests/azure_policy_builtins/cases/format.yaml
Anand Krishnamoorthi 5b60daabd9 feat: add Azure Policy builtins with YAML test suite (#630)
* feat: add Azure Policy builtins with YAML test suite

Implement ARM template functions for Azure Policy evaluation:

Builtins:
- String: indexOf, lastIndexOf, trim, format, split, startsWith, endsWith,
  padLeft, concat, replace, toLower, toUpper, substring, guid, uniqueString
- DateTime: dateTimeAdd, dateTimeFromEpoch, dateTimeToEpoch, addDays
- Collection: intersection, union, take, skip, first, last, min, max,
  range, items, tryGet, tryIndexFromEnd, empty, array, createObject
- Encoding: base64, base64ToString, base64ToJson, uri, uriComponent,
  uriComponentToString, dataUri, dataUriToString
- Numeric: int, float, intDiv, intMod
- Misc: json, join, bool, string, coalesce, if, getParameter, resolveField
- Logic: logicAll, logicAny

Key implementation details:
- Unicode case-insensitive search via ICU4X case folding with single-pass
  fold_with_char_map() for indexOf/lastIndexOf
- .NET composite formatting (System.String.Format) with alignment, standard
  and custom datetime format specifiers, numeric format specifiers
- DateTime round-trip preserves input shape (Z vs +00:00, T vs space,
  fractional seconds) when no explicit output format is supplied
- Zero-cost as_str() helper borrows directly from Value::String(Rc<str>)
- BTreeSet<&Value> in array union avoids redundant cloning

Test suite:
- 53 YAML test files exercising all builtins via direct BUILTINS registry
- Coverage for edge cases: empty inputs, Unicode, fractional seconds,
  invalid alignment, unknown format specifiers, RFC3339 offset shapes

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* fix: address PR review comments

- Fix percent_encode to only uppercase hex digits, not entire string
- Remove guid/uniqueString (unsupported); delete custom SHA-1 impl
- Replace unwrap_or(0) with proper error in format placeholder parsing
- Hoist CaseMapper into static CaseMapperBorrowed for zero per-call overhead
- Pre-allocate Vec in range() with_capacity
- Update bindings/ffi and bindings/ruby Cargo.lock
- Fix uri_component test expectations for correct case preservation

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* fix: address second round of PR review comments

- float(): return Undefined when as_f64() fails instead of leaking
  the original non-f64 representation
- createObject(): reject odd number of arguments with an error
  (ARM-template parity)
- format(): error on unknown numeric format specifiers instead of
  silently passing through (matches .NET FormatException behavior)
- format(): cap alignment width at 10,000 to prevent DoS from
  user-controlled format strings like {0,1000000000}
- Add YAML test cases for all new error behaviors

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* fix: address third round of PR review comments

- percent_decode: reject incomplete % escapes (e.g. "%", "%2") instead
  of treating them as literal characters
- parse_iso8601_duration: reject leftover digits without a unit designator
  at T boundary and end-of-input (e.g. "P1", "P1T2H")
- yaml_to_value: panic on unsupported YAML numeric representations instead
  of silently mapping to Null
- Revert unused src/languages/mod.rs changes (module is defined inline in
  lib.rs)

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* fix: add missing edge-case tests and fix empty-delimiter panic

- fn_split: return input as single-element array for empty string
  delimiter instead of panicking (Rust's str::split("") panics)
- format: add test for F3 higher precision ({0:F3} + 1.23456 → 1.235)
- format: add test for N2 float with thousands separator
- format: add test for negative index error ({-1})
- split: add test for empty-string delimiter
- uri: add tests for query string and fragment in relative URI
- createObject: add test for non-string (numeric) keys

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* fix: address fourth round of PR review comments

- Add MAX_VARIADIC_ARGS (64) constant for variadic builtin arity
  instead of registering with 0 (logic_all, logic_any, min, max,
  format, intersection, union, coalesce, createObject); set
  dateTimeAdd to exact arity 3

- Switch indexOf/lastIndexOf to UTF-16 code-unit indices to match
  .NET String.IndexOf semantics (track ch.len_utf16() in
  fold_with_char_map, use encode_utf16().count() for empty-needle
  lastIndexOf)

- Use DateTime::<Utc>::from_timestamp for explicit timezone type

- Remove stale docs/azure-policy/casing.md link from module doc

- Fix misleading comment in want_error test branch (code bails on
  Undefined, not accepts it)

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

---------

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
2026-03-25 17:32:39 -05:00

132 lines
3.1 KiB
YAML

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Tests for azure.policy.fn.format
builtin: azure.policy.fn.format
cases:
- note: format_single_placeholder
args: ["Hello {0}!", "world"]
want: "Hello world!"
- note: format_multiple_placeholders
args: ["{0} is {1}", "sky", "blue"]
want: "sky is blue"
- note: format_repeated_placeholder
args: ["{0} and {0}", "hello"]
want: "hello and hello"
- note: format_number_arg
args: ["Count: {0}", 42]
want: "Count: 42"
- note: format_bool_arg
args: ["Value: {0}", true]
want: "Value: True"
- note: format_no_placeholders
args: ["no placeholders"]
want: "no placeholders"
- note: format_three_args
args: ["{0}-{1}-{2}", "a", "b", "c"]
want: "a-b-c"
- note: format_missing_placeholder_error
args: ["{0} and {1}", "only_first"]
want_error: "placeholder {1} references argument index 1"
- note: format_empty_template
args: [""]
want: ""
- note: format_escaped_braces
args: ["{{literal braces}}"]
want: "{literal braces}"
- note: format_mixed_escaped_and_placeholder
args: ["{{{0}}}", "value"]
want: "{value}"
- note: format_alignment_right
args: ["{0,10}", "hi"]
want: " hi"
- note: format_alignment_left
args: ["{0,-10}", "hi"]
want: "hi "
- note: format_numeric_fixed_point
args: ["{0:F2}", 3.14159]
want: "3.14"
- note: format_numeric_with_thousands
args: ["{0:N0}", 1234567]
want: "1,234,567"
- note: format_float_with_thousands
args: ["{0:N2}", 1234.5678]
want: "1,234.57"
- note: format_hex_upper
args: ["{0:X}", 255]
want: "FF"
- note: format_hex_lower_padded
args: ["{0:x4}", 255]
want: "00ff"
- note: format_decimal_padded
args: ["{0:D5}", 42]
want: "00042"
- note: format_percent
args: ["{0:P1}", 0.1234]
want: "12.3 %"
- note: format_unmatched_closing_brace
args: ["hello } world"]
want_error: "unmatched closing brace"
- note: format_invalid_placeholder_no_index
args: ["{abc}"]
want_error: "invalid placeholder"
- note: format_unmatched_opening_brace
args: ["{0", "value"]
want_error: "unmatched opening brace"
- note: format_alignment_non_ascii
args: ["{0,6}", "café"]
want: " café"
# Invalid alignment clauses (must be rejected)
- note: format_alignment_empty
args: ["{0,}", "hi"]
want_error: "empty alignment value"
- note: format_alignment_non_numeric
args: ["{0,abc}", "hi"]
want_error: "invalid alignment"
- note: format_alignment_trailing_junk
args: ["{0, 1x}", "hi"]
want_error: "invalid alignment"
- note: format_unknown_numeric_specifier_error
args: ["{0:Z}", 42]
want_error: "invalid numeric format specifier"
- note: format_alignment_width_exceeds_max
args: ["{0,1000000000}", "hi"]
want_error: "exceeds maximum allowed"
- note: format_fixed_point_higher_precision
args: ["{0:F3}", 1.23456]
want: "1.235"
- note: format_negative_index_error
args: ["{-1}", "value"]
want_error: "invalid placeholder"