Skip to content

Commit

Permalink
Harden tests to account for new fast path
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 10, 2024
1 parent 8f9becc commit 6488907
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/lexicon/tests/_scaffolds/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ const lexicons: LexiconDoc[] = [
},
},
},
{
lexicon: 1,
id: 'com.example.stringLengthNoMinLength',
defs: {
main: {
type: 'record',
record: {
type: 'object',
properties: {
string: {
type: 'string',
maxLength: 4,
},
},
},
},
},
},
{
lexicon: 1,
id: 'com.example.stringLengthGrapheme',
Expand Down
101 changes: 101 additions & 0 deletions packages/lexicon/tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,107 @@ describe('Record validation', () => {
).toThrow('Record/string must not be longer than 4 characters')
})

it('Applies string length constraint (no minLength)', () => {
// Shorter than two UTF8 characters
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '',
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'a',
})

// Two to four UTF8 characters
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'ab',
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '\u0301', // Combining acute accent (2 bytes)
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'a\u0301', // 'a' + combining acute accent (1 + 2 bytes = 3 bytes)
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'aé', // 'a' (1 byte) + 'é' (2 bytes) = 3 bytes
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'abc',
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '一', // CJK character (3 bytes)
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '\uD83D', // Unpaired high surrogate (3 bytes)
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'abcd',
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'éé', // 'é' + 'é' (2 + 2 bytes = 4 bytes)
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'aaé', // 1 + 1 + 2 = 4 bytes
})
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '👋', // 4 bytes
})

expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'abcde',
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'a\u0301\u0301', // 1 + (2 * 2) = 5 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '\uD83D\uD83D', // Two unpaired high surrogates (3 * 2 = 6 bytes)
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: 'ééé', // 2 + 2 + 2 bytes = 6 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '👋a', // 4 + 1 bytes = 5 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '👨👨', // 4 + 4 = 8 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLengthNoMinLength', {
$type: 'com.example.stringLengthNoMinLength',
string: '👨‍👩‍👧‍👧', // 4 emojis × 4 bytes + 3 ZWJs × 3 bytes = 25 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
})

it('Applies grapheme string length constraint', () => {
// Shorter than two graphemes
expect(() =>
Expand Down

0 comments on commit 6488907

Please sign in to comment.