Skip to content

Commit e355f84

Browse files
authored
💥 Make unicode json values the default (#5030)
**Description** <!-- Please provide a short description and potentially linked issues justifying the need for this PR --> **💥 Breaking change** With the introduction of the flag `noUnicodeString` on both `json` and `jsonValue` we paved the way for unicode strings to be the default when asking for json entries. This PR toggles the flag to false by default, meaning that any legacy arbitrary not passing the flag will move to unicode strings for keys and value of the produced jsons. As such the breaking change should be rather minor except if the fact not to request unicode was on purpose. <!-- * Your PR is fixing a bug or regression? Check for existing issues related to this bug and link them --> <!-- * Your PR is adding a new feature? Make sure there is a related issue or discussion attached to it --> <!-- You can provide any additional context to help into understanding what's this PR is attempting to solve: reproduction of a bug, code snippets... --> **Checklist** — _Don't delete this checklist and make sure you do the following before opening the PR_ - [x] The name of my PR follows [gitmoji](https://gitmoji.dev/) specification - [x] My PR references one of several related issues (if any) - [x] New features or breaking changes must come with an associated Issue or Discussion - [x] My PR does not add any new dependency without an associated Issue or Discussion - [x] My PR includes bumps details, please run `yarn bump` and flag the impacts properly - [x] My PR adds relevant tests and they would have failed without my PR (when applicable) <!-- More about contributing at https://github.com/dubzzz/fast-check/blob/main/CONTRIBUTING.md --> **Advanced** <!-- How to fill the advanced section is detailed below! --> - [x] Category: 💥 Breaking change - [x] Impacts: Change defaults <!-- [Category] Please use one of the categories below, it will help us into better understanding the urgency of the PR --> <!-- * ✨ Introduce new features --> <!-- * 📝 Add or update documentation --> <!-- * ✅ Add or update tests --> <!-- * 🐛 Fix a bug --> <!-- * 🏷️ Add or update types --> <!-- * ⚡️ Improve performance --> <!-- * _Other(s):_ ... --> <!-- [Impacts] Please provide a comma separated list of the potential impacts that might be introduced by this change --> <!-- * Generated values: Can your change impact any of the existing generators in terms of generated values, if so which ones? when? --> <!-- * Shrink values: Can your change impact any of the existing generators in terms of shrink values, if so which ones? when? --> <!-- * Performance: Can it require some typings changes on user side? Please give more details --> <!-- * Typings: Is there a potential performance impact? In which cases? -->
1 parent a6c0823 commit e355f84

File tree

3 files changed

+16
-36
lines changed

3 files changed

+16
-36
lines changed

packages/fast-check/src/arbitrary/_internals/helpers/JsonConstraintsBuilder.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Arbitrary } from '../../../check/arbitrary/definition/Arbitrary';
21
import { boolean } from '../../boolean';
32
import { constant } from '../../constant';
43
import { double } from '../../double';
54
import type { StringConstraints } from '../../string';
5+
import { string } from '../../string';
66
import type { DepthSize } from './MaxLengthFromMinLength';
77
import type { ObjectConstraints } from './QualifiedObjectConstraints';
88

@@ -28,13 +28,6 @@ export interface JsonSharedConstraints {
2828
* @remarks Since 2.5.0
2929
*/
3030
maxDepth?: number;
31-
/**
32-
* Only generate instances having keys and values made of ascii strings (when true)
33-
* @deprecated Prefer using `stringUnit` to customize the kind of strings that will be generated by default.
34-
* @defaultValue true
35-
* @remarks Since 3.19.0
36-
*/
37-
noUnicodeString?: boolean;
3831
/**
3932
* Replace the default unit for strings.
4033
* @defaultValue undefined
@@ -48,11 +41,9 @@ export interface JsonSharedConstraints {
4841
* @internal
4942
*/
5043

51-
export function jsonConstraintsBuilder(
52-
stringArbitrary: Arbitrary<string>,
53-
constraints: JsonSharedConstraints,
54-
): ObjectConstraints {
55-
const { depthSize, maxDepth } = constraints;
44+
export function jsonConstraintsBuilder(constraints: JsonSharedConstraints): ObjectConstraints {
45+
const { depthSize, maxDepth, stringUnit } = constraints;
46+
const stringArbitrary = string({ unit: stringUnit });
5647
const key = stringArbitrary;
5748
const values = [
5849
boolean(), // any boolean
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
2-
import { string } from './string';
32
import type { JsonSharedConstraints, JsonValue } from './_internals/helpers/JsonConstraintsBuilder';
43
import { jsonConstraintsBuilder } from './_internals/helpers/JsonConstraintsBuilder';
54
import { anything } from './anything';
6-
import { fullUnicodeString } from './fullUnicodeString';
75

86
export type { JsonSharedConstraints, JsonValue };
97

@@ -21,12 +19,5 @@ export type { JsonSharedConstraints, JsonValue };
2119
* @public
2220
*/
2321
export function jsonValue(constraints: JsonSharedConstraints = {}): Arbitrary<JsonValue> {
24-
const noUnicodeString = constraints.noUnicodeString === undefined || constraints.noUnicodeString === true;
25-
const stringArbitrary =
26-
'stringUnit' in constraints
27-
? string({ unit: constraints.stringUnit })
28-
: noUnicodeString
29-
? string()
30-
: fullUnicodeString();
31-
return anything(jsonConstraintsBuilder(stringArbitrary, constraints)) as Arbitrary<JsonValue>;
22+
return anything(jsonConstraintsBuilder(constraints)) as Arbitrary<JsonValue>;
3223
}

website/docs/core-blocks/arbitraries/fake-data/file.md

+11-13
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ All the string values (from keys to values) are generated using `fc.string()`.
5757
**Signatures:**
5858

5959
- `fc.json()`
60-
- `fc.json({depthSize?, maxDepth?, noUnicodeString?, stringUnit?})`
60+
- `fc.json({depthSize?, maxDepth?, stringUnit?})`
6161

6262
**with:**
6363

6464
- `depthSize?` — default: `undefined` [more](/docs/configuration/larger-entries-by-default/#depth-size-explained)_how much we allow our recursive structures to be deep?_
6565
- `maxDepth?` — default: `Number.POSITIVE_INFINITY`_maximal depth of generated objects_
66-
- `noUnicodeString?` — default: `true`_toggle on/off the generation of strings used either as keys or values of the instance and including non-ascii characters — shadowed by `stringUnit`_
6766
- `stringUnit?` — default: `undefined`_customize the set of characters being used by the `string` arbitrary_
6867

6968
**Usages:**
@@ -78,12 +77,12 @@ fc.json();
7877
// • "[null,-1.5485504457576672e+192,null,{},-1.417727947024272e-287,null,null,null]"
7978
// • …
8079

81-
fc.json({ noUnicodeString: false });
80+
fc.json({ stringUnit: 'grapheme' });
8281
// Examples of generated values:
8382
// • "{}"
84-
// • "[{\"󜁳򁿳򎗯􈤘񖇅\":null,\"򈷩𫨹􏥃򤵪񥉨񢦜꣙\":[null,\"򉲨򛨰𜥲񆠉򁀿񇆾􀤯񾱄\"],\"__def\":\"񥛡\",\"𴂏򰷳𩪎񡨫\":true,\"2􏿺\":\"\",\"􍥚󛂾𓴒\":false},[3.5931489320423776e+139,[true,\"󌘅񪜆󗛃󎩻𙹖򞠚򺳵񨶖\",false,{\"􊆪򓔝򘥬𔧥󴓌򩁆\":null,\"\":\"󌽡𗀥󚨿󊭹򎻎񀓜򧅘򏜣󨓚񯄈\",\"𽸧򽂵񯆎񷡰𑴵񞱒\":[true,\"򀲑򿒦\",true,\"􊔹񒚡𣉟𳡸񮋳󳝶\",false,-4.119935921393037e+259,null,-8.9364525362984475e+248]},\"򸀿󳿴񥘡򪠾򃰧򣖏\",\"󱝇򹢖𬂏񠤫󴕠򒐧\"]],[false,-6.0502670401327095e+112,1.1096547717393745e-177,null,null,null,false,[null,\"󘳑㨦𭦄񱹂𚃜򅅪󪃗򟓓󊕝򠗺\",1.288654068889961e-213,null,1.6406299790913147e-206]]]"
85-
// • "\"򁤇𫍯􏿬$񞋰%򟱉򳟔󽐾\""
86-
// • "[null,[{\"\":true,\"𮀳񠍞󗈌\":\"耕򰶤䰅𸬣\",\"\":null,\"𘥣񯙝𖹟󗨟𯵽򿈤􊇦󣌙󸫨󸅔\":true,\"󒾠򈄕󬀘𚨶󍋤񃞜𮢌􇶸񏭘\":null,\"񮹷񀚤󷅓󰪼􀆌𥰂𫃩𧆔𹷹󭼜\":true,\"󛶋򣄚񼇏򡭇󹃤󢁬𞲢\":-4.059178361848322e-91,\"򉁀򠾫𓦞𑬞󵫽򏥷񹺏􌗈\":true},null],[3.6448982683876056e+131]]"
83+
// • "[{\"ᠢ፲ǻ𝕛𑊄\":null,\"㛮𝈷ﴪẠ㊕\\\\ն\":[null,\"𝟗ꊹ🀟ฒᗋ𒈋𔗉ᑐ\"],\"__def\":\"🌶\",\"ꉐ⯚𐹤Ὣ\":true,\"&𝅘𝅥𝅰\":\"\",\"༭𝂽í\":false},[3.5931489320423776e+139,[true,\"౦𒌻🙥Ĥꄯ𝦑🦐ᮚ\",false,{\"𝌝𐾀𑋶𑿕𐧠ᮯ\":null,\"\":\"𝤈𝌰ࢾ𖤤㉛⮳𖢣𝄜ㅲ𖨓\",\"𑐢𖢠爛𓋸𐕜🅕\":[true,\"𒕀𛋲\",true,\"ͽà꠷ĘDzᨸṯ\",false,-4.119935921393037e+259,null,-8.9364525362984475e+248]},\"📡🔎ʋʘ𘥤𐍕\",\"ᾉ𖡡𓐗𝄾ꅑะ\"]],[false,-6.0502670401327095e+112,1.1096547717393745e-177,null,null,null,false,[null,\"✵𒈃☨🈴𞲚𐃩߆𔖕⍣𒀷\",1.288654068889961e-213,null,1.6406299790913147e-206]]]"
84+
// • "\"㉬𒊤𝅘𝅥𝅯#ﰯ!𝣰𛄔𐓩\""
85+
// • "[null,[{\"𛱧\":true,\"𐨠゠ᦹ\":\"𑤑ᣢᾎⰊ\",\"𑘏\":null,\"念𑦰𓇿𝙒ꪠ𘦁𛂖ⴠ𘲽⎺\":true,\"ᤘ🍲𐤀ǧഏ𘪑𐛠🔶𝐃𞋱\":null,\"𘮄Ʈ⮺𝚹𝉅ㅊ𐠢Ⓢ🗣𐘐\":true,\"⽐⪎⫋𓅿𐐓ᢜജ\":-4.059178361848322e-91,\"🨛▟᪆➂ﯜろ♣ὔ\":true},null],[3.6448982683876056e+131]]"
8786
// • "[null,false]"
8887
// • …
8988

@@ -120,13 +119,12 @@ As `JSON.parse` preserves `-0`, `jsonValue` can also have `-0` as a value.
120119
**Signatures:**
121120

122121
- `fc.jsonValue()`
123-
- `fc.jsonValue({depthSize?, maxDepth?, noUnicodeString?, stringUnit?})`
122+
- `fc.jsonValue({depthSize?, maxDepth?, stringUnit?})`
124123

125124
**with:**
126125

127126
- `depthSize?` — default: `undefined` [more](/docs/configuration/larger-entries-by-default/#depth-size-explained)_how much we allow our recursive structures to be deep?_
128127
- `maxDepth?` — default: `Number.POSITIVE_INFINITY`_maximal depth for generated objects (Map and Set included into objects)_
129-
- `noUnicodeString?` — default: `true`_toggle on/off the generation of strings used either as keys or values of the instance and including non-ascii characters — shadowed by `stringUnit`_
130128
- `stringUnit?` — default: `undefined`_customize the set of characters being used by the `string` arbitrary_
131129

132130
**Usages:**
@@ -141,12 +139,12 @@ fc.jsonValue();
141139
// • [null,true,true,"`l+$I","kSros",null]
142140
// • …
143141

144-
fc.jsonValue({ noUnicodeString: false });
142+
fc.jsonValue({ stringUnit: 'grapheme' });
145143
// Examples of generated values:
146-
// • ["򴾼󹤷𡅤񤱓򛗡"]
147-
// • {"􎵔򲁼򀎈𸝔􃌅􊿛񹙦":[false],"򨊗𤮈𡈡󵑑񗀏򏗔𙔔𐸵񇘼":556603.8398649627,"􏿽+򸑽":{"񐀞󴕃󙉅񂊠𴛐󻕀㢋񦔘":true,"񊈒􋚭󷪙𫪀󌧶񉝒𱣆":null,"":5.539268054957889e+74,"򦹷":"񜝍⌳򻍜񇓷񖋦","񥸱񥊔򦹗":4.847354156832373e-25,"񜂑򹏁󞦐":"𻬫𳤲󵹃򕏧񁃵","𓧎𖰦":false,"󛻳򏜚񃛷񌛑𝜀󞅤񪉺":false}}
148-
// • [null,["󿦼񌅡󯻾𝀹򲓋񁆺񐿏󃢰",-2.4628931920258706e-282,null,false,2.681696006505804e-238,"򢰮"]]
149-
// • "򐐩"
144+
// • ["ⳃ◳奈𞹩䷔"]
145+
// • {"𑐗꣼礼⬸ਸ਼𝃢ꙧ":[false],"⸵ᴤ𝚌॰𑌘𒌡ᣊゼ𝙷":556603.8398649627,"𝆺𝅥𝅯*⟱":{"ꑇ𛈯⽂ⱏﺝ䷳ӽ𘩵":true,"ሏ㉒🧌🕊𒑲㍿鐕":null,"":5.539268054957889e+74,"𑇝":"⧉𐜧ᮠ𐠼𓊖","꒭ꡈ✃":4.847354156832373e-25,"𝗄𝨷𐤆":"ӽ⭆𞡗𑫫🀢","ప𔔀":false,"Ⓙ𖼖𐎏𓍱য়𒓫𖺁":false}}
146+
// • [null,["ց𘯶يϋᩐ⨞ꔴℲ",-2.4628931920258706e-282,null,false,2.681696006505804e-238,""]]
147+
// • ""
150148
// • []
151149
// • …
152150

0 commit comments

Comments
 (0)