# Copyright 2025 The go-yaml Project Contributors # SPDX-License-Identifier: Apache-2.0 # Scanner test cases for libyaml scanner # # Purpose: Tests the YAML scanner/tokenizer that converts YAML text into tokens. # # Format: Test cases use type-as-key format: # - test-type: # name: Test case name # ... # # Test Types: # scan-tokens - Verifies token sequence only # scan-tokens-detailed - Verifies complete token properties (value, style, etc.) # scan-error - Validates error detection for invalid input # # Common Keys: # name - Test case name (string) # yaml - Input YAML string to scan # want - Expected result (format varies by test type): # * For scan-error: boolean (defaults to true if omitted; set to false if no error expected) # * For scan-tokens: list of token type names (scalars or type-as-key format) # * For scan-tokens-detailed: list of token specifications (type-as-key format) # # Token Specification Format (for want lists): # Simplified (type only): - TOKEN_TYPE # Detailed (with fields): - TOKEN_TYPE: # value: ... # style: ... # Simple token type tests - scan-tokens: name: Simple scalar yaml: |- hello want: - STREAM_START_TOKEN - SCALAR_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Simple mapping yaml: |- key: value want: - STREAM_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - SCALAR_TOKEN - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Block sequence yaml: | - item1 - item2 want: - STREAM_START_TOKEN - BLOCK_SEQUENCE_START_TOKEN - BLOCK_ENTRY_TOKEN - SCALAR_TOKEN - BLOCK_ENTRY_TOKEN - SCALAR_TOKEN - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Flow sequence yaml: |- [1, 2, 3] want: - STREAM_START_TOKEN - FLOW_SEQUENCE_START_TOKEN - SCALAR_TOKEN - FLOW_ENTRY_TOKEN - SCALAR_TOKEN - FLOW_ENTRY_TOKEN - SCALAR_TOKEN - FLOW_SEQUENCE_END_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Flow mapping yaml: |- {a: 1, b: 2} want: - STREAM_START_TOKEN - FLOW_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - SCALAR_TOKEN - FLOW_ENTRY_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - SCALAR_TOKEN - FLOW_MAPPING_END_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Document markers yaml: | --- key: value ... want: - STREAM_START_TOKEN - DOCUMENT_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - SCALAR_TOKEN - BLOCK_END_TOKEN - DOCUMENT_END_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Empty input yaml: |- want: - STREAM_START_TOKEN - STREAM_END_TOKEN - scan-tokens: name: Complex nesting yaml: | parent: - item1 - nested: key: value want: - STREAM_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - BLOCK_SEQUENCE_START_TOKEN - BLOCK_ENTRY_TOKEN - SCALAR_TOKEN - BLOCK_ENTRY_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN - VALUE_TOKEN - SCALAR_TOKEN - BLOCK_END_TOKEN - BLOCK_END_TOKEN - BLOCK_END_TOKEN - BLOCK_END_TOKEN - STREAM_END_TOKEN # Detailed token tests (checking token properties) - scan-tokens-detailed: name: Anchor and alias yaml: | - &anchor value - *anchor want: - STREAM_START_TOKEN - BLOCK_SEQUENCE_START_TOKEN - BLOCK_ENTRY_TOKEN - ANCHOR_TOKEN: value: anchor - SCALAR_TOKEN: value: value - BLOCK_ENTRY_TOKEN - ALIAS_TOKEN: value: anchor - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens-detailed: name: Tag yaml: |- !!str value want: - STREAM_START_TOKEN - TAG_TOKEN: value: '!!' - SCALAR_TOKEN: value: value - STREAM_END_TOKEN - scan-tokens-detailed: name: Single quoted scalar yaml: |- 'hello world' want: - STREAM_START_TOKEN - SCALAR_TOKEN: style: SINGLE_QUOTED_SCALAR_STYLE value: hello world - STREAM_END_TOKEN - scan-tokens-detailed: name: Double quoted scalar yaml: |- "hello world" want: - STREAM_START_TOKEN - SCALAR_TOKEN: style: DOUBLE_QUOTED_SCALAR_STYLE value: hello world - STREAM_END_TOKEN - scan-tokens-detailed: name: Literal scalar yaml: | key: | line1 line2 want: - STREAM_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: key - VALUE_TOKEN - SCALAR_TOKEN: style: LITERAL_SCALAR_STYLE value: "line1\nline2\n" - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens-detailed: name: Folded scalar yaml: | key: > folded text want: - STREAM_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: key - VALUE_TOKEN - SCALAR_TOKEN: style: FOLDED_SCALAR_STYLE value: "folded text\n" - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens-detailed: name: Version directive yaml: | %YAML 1.1 --- key: value want: - STREAM_START_TOKEN - VERSION_DIRECTIVE_TOKEN - DOCUMENT_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: key - VALUE_TOKEN - SCALAR_TOKEN: value: value - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens-detailed: name: Tag directive yaml: | %TAG !yaml! tag:yaml.org,2002: --- key: value want: - STREAM_START_TOKEN - TAG_DIRECTIVE_TOKEN - DOCUMENT_START_TOKEN - BLOCK_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: key - VALUE_TOKEN - SCALAR_TOKEN: value: value - BLOCK_END_TOKEN - STREAM_END_TOKEN - scan-tokens-detailed: name: Escape sequences yaml: |- "hello\nworld" want: - STREAM_START_TOKEN - SCALAR_TOKEN: style: DOUBLE_QUOTED_SCALAR_STYLE value: "hello\nworld" - STREAM_END_TOKEN - scan-tokens-detailed: name: Flow mapping colon followed by comma, bracket, or brace yaml: |- {a:,b: [c:],d: {e:}} want: - STREAM_START_TOKEN - FLOW_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: a - VALUE_TOKEN - FLOW_ENTRY_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: b - VALUE_TOKEN - FLOW_SEQUENCE_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: c - VALUE_TOKEN - FLOW_SEQUENCE_END_TOKEN - FLOW_ENTRY_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: d - VALUE_TOKEN - FLOW_MAPPING_START_TOKEN - KEY_TOKEN - SCALAR_TOKEN: value: e - VALUE_TOKEN - FLOW_MAPPING_END_TOKEN - FLOW_MAPPING_END_TOKEN - STREAM_END_TOKEN # Error test - scan-error: name: Invalid character yaml: "\x01" # Flow indicator error tests - scan-error: name: Comma in tag yaml: "!foo,bar value" like: found character ',' that is not allowed in a YAML tag - scan-error: name: Left bracket in tag yaml: "!foo[bar value" like: found character '\[' that is not allowed in a YAML tag - scan-error: name: Right bracket in tag yaml: "!foo]bar value" like: found character '\]' that is not allowed in a YAML tag - scan-error: name: Left brace in tag yaml: "!foo{bar value" like: found character '\{' that is not allowed in a YAML tag - scan-error: name: Right brace in tag yaml: "!foo}bar value" like: found character '\}' that is not allowed in a YAML tag - scan-error: name: Hash in tag yaml: "!foo#bar value" like: found character '#' that is not allowed in a YAML tag - scan-error: name: Double quote in tag yaml: '!foo"bar value' like: found character '"' that is not allowed in a YAML tag - scan-error: name: Less than in tag yaml: "!foobar value" like: found character '>' that is not allowed in a YAML tag - scan-error: name: Backslash in tag yaml: '!foo\bar value' like: found character '\\' that is not allowed in a YAML tag # Copyright 2025 The go-yaml Project Contributors # SPDX-License-Identifier: Apache-2.0 # Character classification and utility function tests # # Purpose: Tests internal character classification and conversion functions used # throughout the YAML parser and emitter. # # Format: Test cases use type-as-key format: # - test-type: # name: Test case name # ... # # Test Types: # char-predicate - Tests is* functions that return boolean # char-convert - Tests as* and width functions that return integer # # Common Keys: # name - Test case name (string) # func - Function to call (isAlpha, isDigit, asHex, width, etc.) # data - Input bytes (string or hex bytes like [0x00]) # index - Byte index to test (integer, defaults to 0) # want - Expected result: # * For char-predicate: boolean (defaults to true if omitted; set to false if needed) # * For char-convert: integer (required) # isAlpha tests - char-predicate: name: Is alpha - letters func: isAlpha data: abc - char-predicate: name: Is alpha - uppercase func: isAlpha data: ABC - char-predicate: name: Is alpha - digits func: isAlpha data: '123' - char-predicate: name: Is alpha - underscore func: isAlpha data: _- - char-predicate: name: Is alpha - dash at index 1 func: isAlpha data: _- index: 1 - char-predicate: name: Is alpha - space func: isAlpha data: ' ' want: false - char-predicate: name: Is alpha - exclamation func: isAlpha data: '!' want: false - char-predicate: name: Is alpha - at sign func: isAlpha data: '@' want: false # isFlowIndicator tests - char-predicate: name: Is flow indicator - left bracket func: isFlowIndicator data: '[' - char-predicate: name: Is flow indicator - right bracket func: isFlowIndicator data: ']' - char-predicate: name: Is flow indicator - left brace func: isFlowIndicator data: '{' - char-predicate: name: Is flow indicator - right brace func: isFlowIndicator data: '}' - char-predicate: name: Is flow indicator - comma func: isFlowIndicator data: ',' - char-predicate: name: Is flow indicator - letter func: isFlowIndicator data: a want: false - char-predicate: name: Is flow indicator - colon func: isFlowIndicator data: ':' want: false # isAnchorChar tests - char-predicate: name: Is anchor char - letters func: isAnchorChar data: abc - char-predicate: name: Is anchor char - digits func: isAnchorChar data: '123' - char-predicate: name: Is anchor char - underscore dash func: isAnchorChar data: _- - char-predicate: name: Is anchor char - colon func: isAnchorChar data: ':' want: false - char-predicate: name: Is anchor char - left bracket func: isAnchorChar data: '[' want: false - char-predicate: name: Is anchor char - space func: isAnchorChar data: ' ' want: false - char-predicate: name: Is anchor char - newline func: isAnchorChar data: "\n" want: false - char-predicate: name: Is anchor char - BOM func: isAnchorChar data: [0xEF, 0xBB, 0xBF] want: false # isColon tests - char-predicate: name: Is colon - colon func: isColon data: ':' - char-predicate: name: Is colon - letter func: isColon data: a want: false # isDigit tests - char-predicate: name: Is digit - zero func: isDigit data: '0' - char-predicate: name: Is digit - five func: isDigit data: '5' - char-predicate: name: Is digit - nine func: isDigit data: '9' - char-predicate: name: Is digit - letter func: isDigit data: a want: false - char-predicate: name: Is digit - space func: isDigit data: ' ' want: false # isHex tests - char-predicate: name: Is hex - zero func: isHex data: '0' - char-predicate: name: Is hex - nine func: isHex data: '9' - char-predicate: name: Is hex - uppercase A func: isHex data: A - char-predicate: name: Is hex - uppercase F func: isHex data: F - char-predicate: name: Is hex - lowercase a func: isHex data: a - char-predicate: name: Is hex - lowercase f func: isHex data: f - char-predicate: name: Is hex - uppercase G func: isHex data: G want: false - char-predicate: name: Is hex - lowercase g func: isHex data: g want: false # isASCII tests - char-predicate: name: Is ASCII - letter func: isASCII data: a - char-predicate: name: Is ASCII - 0x7F func: isASCII data: [0x7F] - char-predicate: name: Is ASCII - 0x80 func: isASCII data: [0x80] want: false - char-predicate: name: Is ASCII - 0xFF func: isASCII data: [0xFF] want: false # isPrintable tests - char-predicate: name: Is printable - LF func: isPrintable data: [0x0A] - char-predicate: name: Is printable - space func: isPrintable data: [0x20] - char-predicate: name: Is printable - tilde func: isPrintable data: [0x7E] - char-predicate: name: Is printable - nbsp func: isPrintable data: [0xC2, 0xA0] - char-predicate: name: Is printable - null func: isPrintable data: [0x00] want: false - char-predicate: name: Is printable - control char func: isPrintable data: [0x19] want: false # isZeroChar tests - char-predicate: name: Is zero char - null func: isZeroChar data: [0x00] - char-predicate: name: Is zero char - letter func: isZeroChar data: a want: false # isBOM tests - char-predicate: name: Is BOM - UTF-8 BOM func: isBOM data: [0xEF, 0xBB, 0xBF] - char-predicate: name: Is BOM - regular text func: isBOM data: abc want: false # isSpace tests - char-predicate: name: Is space - space func: isSpace data: ' ' - char-predicate: name: Is space - letter func: isSpace data: a want: false # isTab tests - char-predicate: name: Is tab - tab func: isTab data: "\t" - char-predicate: name: Is tab - space func: isTab data: ' ' want: false # isBlank tests - char-predicate: name: Is blank - space func: isBlank data: ' ' - char-predicate: name: Is blank - tab func: isBlank data: "\t" - char-predicate: name: Is blank - letter func: isBlank data: a want: false - char-predicate: name: Is blank - newline func: isBlank data: "\n" want: false # isLineBreak tests - char-predicate: name: Is line break - CR func: isLineBreak data: "\r" - char-predicate: name: Is line break - LF func: isLineBreak data: "\n" - char-predicate: name: Is line break - NEL func: isLineBreak data: [0xC2, 0x85] - char-predicate: name: Is line break - LS func: isLineBreak data: [0xE2, 0x80, 0xA8] - char-predicate: name: Is line break - PS func: isLineBreak data: [0xE2, 0x80, 0xA9] - char-predicate: name: Is line break - letter func: isLineBreak data: a want: false - char-predicate: name: Is line break - space func: isLineBreak data: ' ' want: false # isCRLF tests - char-predicate: name: Is CRLF - CRLF func: isCRLF data: "\r\n" - char-predicate: name: Is CRLF - LF only func: isCRLF data: [0x0A, 0x00] want: false - char-predicate: name: Is CRLF - CR only func: isCRLF data: [0x0D, 0x00] want: false # isBreakOrZero tests - char-predicate: name: Is break or zero - CR func: isBreakOrZero data: "\r" - char-predicate: name: Is break or zero - LF func: isBreakOrZero data: "\n" - char-predicate: name: Is break or zero - null func: isBreakOrZero data: [0x00] - char-predicate: name: Is break or zero - NEL func: isBreakOrZero data: [0xC2, 0x85] - char-predicate: name: Is break or zero - letter func: isBreakOrZero data: a want: false # isSpaceOrZero tests - char-predicate: name: Is space or zero - space func: isSpaceOrZero data: ' ' - char-predicate: name: Is space or zero - CR func: isSpaceOrZero data: "\r" - char-predicate: name: Is space or zero - LF func: isSpaceOrZero data: "\n" - char-predicate: name: Is space or zero - null func: isSpaceOrZero data: [0x00] - char-predicate: name: Is space or zero - letter func: isSpaceOrZero data: a want: false # isBlankOrZero tests - char-predicate: name: Is blank or zero - space func: isBlankOrZero data: ' ' - char-predicate: name: Is blank or zero - tab func: isBlankOrZero data: "\t" - char-predicate: name: Is blank or zero - CR func: isBlankOrZero data: "\r" - char-predicate: name: Is blank or zero - LF func: isBlankOrZero data: "\n" - char-predicate: name: Is blank or zero - null func: isBlankOrZero data: [0x00] - char-predicate: name: Is blank or zero - letter func: isBlankOrZero data: a want: false # Converter functions - asDigit tests - char-convert: name: As digit - zero func: asDigit data: '0' want: 0 - char-convert: name: As digit - five func: asDigit data: '5' want: 5 - char-convert: name: As digit - nine func: asDigit data: '9' want: 9 # asHex tests - char-convert: name: As hex - zero func: asHex data: '0' want: 0 - char-convert: name: As hex - nine func: asHex data: '9' want: 9 - char-convert: name: As hex - uppercase A func: asHex data: A want: 10 - char-convert: name: As hex - uppercase F func: asHex data: F want: 15 - char-convert: name: As hex - lowercase a func: asHex data: a want: 10 - char-convert: name: As hex - lowercase f func: asHex data: f want: 15 # width tests - char-convert: name: Width - 0x00 func: width data: [0x00] want: 1 - char-convert: name: Width - 0x7F func: width data: [0x7F] want: 1 - char-convert: name: Width - 0xC0 func: width data: [0xC0] want: 2 - char-convert: name: Width - 0xDF func: width data: [0xDF] want: 2 - char-convert: name: Width - 0xE0 func: width data: [0xE0] want: 3 - char-convert: name: Width - 0xEF func: width data: [0xEF] want: 3 - char-convert: name: Width - 0xF0 func: width data: [0xF0] want: 4 - char-convert: name: Width - 0xF7 func: width data: [0xF7] want: 4 - char-convert: name: Width - 0xF8 func: width data: [0xF8] want: 0 # isTagURIChar tests - char-predicate: name: Is tag URI char - alphanumeric (non-verbatim) func: isTagURIChar data: abc123 args: [false] - char-predicate: name: Is tag URI char - underscore (non-verbatim) func: isTagURIChar data: _ args: [false] - char-predicate: name: Is tag URI char - dash (non-verbatim) func: isTagURIChar data: '-' args: [false] - char-predicate: name: Is tag URI char - semicolon (non-verbatim) func: isTagURIChar data: ; args: [false] - char-predicate: name: Is tag URI char - slash (non-verbatim) func: isTagURIChar data: / args: [false] - char-predicate: name: Is tag URI char - question mark (non-verbatim) func: isTagURIChar data: '?' args: [false] - char-predicate: name: Is tag URI char - colon (non-verbatim) func: isTagURIChar data: ':' args: [false] - char-predicate: name: Is tag URI char - at sign (non-verbatim) func: isTagURIChar data: '@' args: [false] - char-predicate: name: Is tag URI char - ampersand (non-verbatim) func: isTagURIChar data: '&' args: [false] - char-predicate: name: Is tag URI char - equals (non-verbatim) func: isTagURIChar data: = args: [false] - char-predicate: name: Is tag URI char - plus (non-verbatim) func: isTagURIChar data: + args: [false] - char-predicate: name: Is tag URI char - dollar (non-verbatim) func: isTagURIChar data: $ args: [false] - char-predicate: name: Is tag URI char - dot (non-verbatim) func: isTagURIChar data: . args: [false] - char-predicate: name: Is tag URI char - exclamation (non-verbatim) func: isTagURIChar data: '!' args: [false] - char-predicate: name: Is tag URI char - tilde (non-verbatim) func: isTagURIChar data: '~' args: [false] - char-predicate: name: Is tag URI char - asterisk (non-verbatim) func: isTagURIChar data: '*' args: [false] - char-predicate: name: Is tag URI char - single quote (non-verbatim) func: isTagURIChar data: "'" args: [false] - char-predicate: name: Is tag URI char - left paren (non-verbatim) func: isTagURIChar data: ( args: [false] - char-predicate: name: Is tag URI char - right paren (non-verbatim) func: isTagURIChar data: ) args: [false] - char-predicate: name: Is tag URI char - percent (non-verbatim) func: isTagURIChar data: '%' args: [false] - char-predicate: name: Is tag URI char - comma (non-verbatim) func: isTagURIChar data: ',' args: [false] want: false - char-predicate: name: Is tag URI char - left bracket (non-verbatim) func: isTagURIChar data: '[' args: [false] want: false - char-predicate: name: Is tag URI char - right bracket (non-verbatim) func: isTagURIChar data: ']' args: [false] want: false - char-predicate: name: Is tag URI char - left brace (non-verbatim) func: isTagURIChar data: '{' args: [false] want: false - char-predicate: name: Is tag URI char - right brace (non-verbatim) func: isTagURIChar data: '}' args: [false] want: false - char-predicate: name: Is tag URI char - comma (verbatim) func: isTagURIChar data: ',' args: [true] - char-predicate: name: Is tag URI char - left bracket (verbatim) func: isTagURIChar data: '[' args: [true] - char-predicate: name: Is tag URI char - right bracket (verbatim) func: isTagURIChar data: ']' args: [true] - char-predicate: name: Is tag URI char - left brace (verbatim) func: isTagURIChar data: '{' args: [true] - char-predicate: name: Is tag URI char - right brace (verbatim) func: isTagURIChar data: '}' args: [true] - char-predicate: name: Is tag URI char - space (non-verbatim) func: isTagURIChar data: ' ' args: [false] want: false - char-predicate: name: Is tag URI char - newline (non-verbatim) func: isTagURIChar data: [0x0A] args: [false] want: false - char-predicate: name: Is tag URI char - hash (non-verbatim) func: isTagURIChar data: '#' args: [false] want: false