napi-rs/examples/napi/__tests__/property-names.spec.ts

65 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from 'ava'
import type {
PropertyNameDigitTest,
PropertyNameSpecialCharsTest,
PropertyNameUnicodeTest,
PropertyNameValidTest,
} from '../index.cjs'
test('Unicode property names should be unquoted', (t) => {
// These should compile without quotes
const obj: PropertyNameUnicodeTest = {
café: 'coffee',
: 'japanese',
Ελληνικά: 'greek',
}
t.is(obj.café, 'coffee')
t.is(obj., 'japanese')
t.is(obj.Ελληνικά, 'greek')
})
test('Special character property names should be quoted', (t) => {
// These require quotes in the type definition
const obj: PropertyNameSpecialCharsTest = {
'kebab-case': 'value1',
'with space': 'value2',
'dot.notation': 'value3',
'xml:lang': 'value4',
$var: 'value5',
}
t.is(obj['kebab-case'], 'value1')
t.is(obj['with space'], 'value2')
t.is(obj['dot.notation'], 'value3')
t.is(obj['xml:lang'], 'value4')
t.is(obj['$var'], 'value5')
})
test('Digit-starting property names should be quoted', (t) => {
// These require quotes because they start with digits
const obj: PropertyNameDigitTest = {
'0invalid': 'value1',
'123': 'value2',
}
t.is(obj['0invalid'], 'value1')
t.is(obj['123'], 'value2')
})
test('Valid identifier property names should be unquoted', (t) => {
// These should compile without quotes
const obj: PropertyNameValidTest = {
camelCase: 'value1',
pascalCase: 'value2',
private: 'value3',
with123Numbers: 'value4',
}
t.is(obj.camelCase, 'value1')
t.is(obj.pascalCase, 'value2')
t.is(obj.private, 'value3')
t.is(obj.with123Numbers, 'value4')
})