How to use untoggledValue method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

MixedCaseArbitrary.spec.ts

Source:MixedCaseArbitrary.spec.ts Github

copy

Full Screen

1import fc from 'fast-check';2import {3 assertProduceValuesShrinkableWithoutContext,4 assertProduceCorrectValues,5 assertShrinkProducesSameValueWithoutInitialContext,6 assertShrinkProducesStrictlySmallerValue,7 assertProduceSameValueGivenSameSeed,8} from '../__test-helpers__/ArbitraryAssertions';9import { MixedCaseArbitrary } from '../../../../src/arbitrary/_internals/MixedCaseArbitrary';10import { stringOf } from '../../../../src/arbitrary/stringOf';11import { nat } from '../../../../src/arbitrary/nat';12import * as BigUintNMock from '../../../../src/arbitrary/bigUintN';13import { fakeArbitrary } from '../__test-helpers__/ArbitraryHelpers';14import { Value } from '../../../../src/check/arbitrary/definition/Value';15import { fakeRandom } from '../__test-helpers__/RandomHelpers';16function beforeEachHook() {17 jest.resetModules();18 jest.restoreAllMocks();19 fc.configureGlobal({ beforeEach: beforeEachHook });20}21beforeEach(beforeEachHook);22describe('MixedCaseArbitrary (integration)', () => {23 if (typeof BigInt === 'undefined') {24 it('no test', () => {25 expect(true).toBe(true);26 });27 return;28 }29 describe('generate', () => {30 it('should not toggle any character if flags equal zero', () => {31 // Arrange32 const { instance: mrng } = fakeRandom();33 const { bigUintN, stringInstance } = mockSourceArbitrariesForGenerate(BigInt(0), 'azerty');34 const toggleCase = jest.fn().mockImplementation((c) => c.toUpperCase());35 const untoggleAll = jest.fn().mockImplementation((s) => s.toLowerCase());36 // Act37 const arb = new MixedCaseArbitrary(stringInstance, toggleCase, untoggleAll);38 const g = arb.generate(mrng, undefined);39 // Assert40 expect(g.value).toBe('azerty');41 expect(bigUintN).toHaveBeenCalledWith(6); // num toggleable chars in string = 642 expect(toggleCase).toHaveBeenCalledTimes(6); // length string = 6, to be toggled = 043 expect(untoggleAll).not.toHaveBeenCalled();44 });45 it('should toggle characters according to flags', () => {46 // Arrange47 const { instance: mrng } = fakeRandom();48 const { bigUintN, stringInstance } = mockSourceArbitrariesForGenerate(BigInt(9) /* 001001 */, 'azerty');49 const toggleCase = jest.fn().mockImplementation((c) => c.toUpperCase());50 const untoggleAll = jest.fn().mockImplementation((s) => s.toLowerCase());51 // Act52 const arb = new MixedCaseArbitrary(stringInstance, toggleCase, untoggleAll);53 const g = arb.generate(mrng, undefined);54 // Assert55 expect(g.value).toBe('azErtY');56 expect(bigUintN).toHaveBeenCalledWith(6); // num toggleable chars in string = 657 expect(toggleCase).toHaveBeenCalledTimes(6 + 2); // length string = 6, to be toggled = 258 expect(untoggleAll).not.toHaveBeenCalled();59 });60 it('should not try to toggle characters that do not have toggled versions', () => {61 // Arrange62 const { instance: mrng } = fakeRandom();63 const { bigUintN, stringInstance } = mockSourceArbitrariesForGenerate(BigInt(10) /* 1010 */, 'az01ty');64 const toggleCase = jest.fn().mockImplementation((c) => c.toUpperCase());65 const untoggleAll = jest.fn().mockImplementation((s) => s.toLowerCase());66 // Act67 const arb = new MixedCaseArbitrary(stringInstance, toggleCase, untoggleAll);68 const g = arb.generate(mrng, undefined);69 // Assert70 expect(g.value).toBe('Az01Ty');71 expect(bigUintN).toHaveBeenCalledWith(4); // // num toggleable chars in string = 4 as 01 upper version is the same -> only 4 can be toggled not 672 expect(toggleCase).toHaveBeenCalledTimes(6 + 2); // length string = 6, to be toggled = 273 expect(untoggleAll).not.toHaveBeenCalled();74 });75 it('should properly deal with toggle mapping to multiple characters', () => {76 // Arrange77 const { instance: mrng } = fakeRandom();78 const { bigUintN, stringInstance } = mockSourceArbitrariesForGenerate(BigInt(63) /* 111111 */, 'azerty');79 const toggleCase = jest.fn().mockImplementation((c: string) => {80 if (c === 'a' || c === 't') return '<Hello>';81 else return c;82 });83 const untoggleAll = jest.fn().mockImplementation((s) => s.toLowerCase());84 // Act85 const arb = new MixedCaseArbitrary(stringInstance, toggleCase, untoggleAll);86 const g = arb.generate(mrng, undefined);87 // Assert88 expect(g.value).toBe('<Hello>zer<Hello>y');89 expect(bigUintN).toHaveBeenCalledWith(2); // num toggleable chars in string = 2, only a and t90 expect(toggleCase).toHaveBeenCalledTimes(6 + 2); // length string = 6, to be toggled = 291 expect(untoggleAll).not.toHaveBeenCalled();92 });93 });94 describe('canShrinkWithoutContext', () => {95 it('should always check against the arbitrary of string with raw when no untoggleAll', () => {96 fc.assert(97 fc.property(fc.string(), fc.boolean(), fc.func(fc.string()), (rawValue, isShrinkable, toggleCase) => {98 // Arrange99 const { instance, canShrinkWithoutContext } = fakeArbitrary();100 canShrinkWithoutContext.mockReturnValueOnce(isShrinkable);101 // Act102 const arb = new MixedCaseArbitrary(instance, toggleCase, undefined);103 const out = arb.canShrinkWithoutContext(rawValue);104 // Assert105 expect(out).toBe(isShrinkable);106 expect(canShrinkWithoutContext).toHaveBeenCalledTimes(1);107 expect(canShrinkWithoutContext).toHaveBeenCalledWith(rawValue);108 })109 );110 });111 it('should always check against the arbitrary of string with untoggled when untoggleAll', () => {112 fc.assert(113 fc.property(114 fc.string(),115 fc.string(),116 fc.boolean(),117 fc.func(fc.string()),118 (rawValue, untoggledValue, isShrinkable, toggleCase) => {119 // Arrange120 const { instance, canShrinkWithoutContext } = fakeArbitrary();121 canShrinkWithoutContext.mockReturnValueOnce(isShrinkable);122 const untoggleAll = jest.fn();123 untoggleAll.mockReturnValue(untoggledValue);124 // Act125 const arb = new MixedCaseArbitrary(instance, toggleCase, untoggleAll);126 const out = arb.canShrinkWithoutContext(rawValue);127 // Assert128 expect(out).toBe(isShrinkable);129 expect(canShrinkWithoutContext).toHaveBeenCalledTimes(1);130 expect(canShrinkWithoutContext).toHaveBeenCalledWith(untoggledValue);131 }132 )133 );134 });135 });136});137describe('MixedCaseArbitrary (integration)', () => {138 if (typeof BigInt === 'undefined') {139 it('no test', () => {140 expect(true).toBe(true);141 });142 return;143 }144 type Extra = { withoutToggle: boolean };145 const extraParameters: fc.Arbitrary<Extra> = fc.record({ withoutToggle: fc.boolean() });146 const mixedCaseBaseChars = ['A', 'B', '|', '~'];147 const isCorrect = (value: string, extra: Extra) => {148 const acceptedChars = extra.withoutToggle149 ? mixedCaseBaseChars150 : [...mixedCaseBaseChars, ...mixedCaseBaseChars.map((c) => c.toLowerCase())];151 return typeof value === 'string' && [...value].every((c) => acceptedChars.includes(c));152 };153 const isStrictlySmaller = (v1: string, v2: string) => v1.length < v2.length || v1 < v2; /* 'A' < 'a' < '|' < '~' */154 const mixedCaseBuilder = (extra: Extra) =>155 new MixedCaseArbitrary(156 stringOf(157 nat(mixedCaseBaseChars.length - 1).map(158 (id) => mixedCaseBaseChars[id],159 (c) => mixedCaseBaseChars.indexOf(c as string)160 )161 ),162 extra.withoutToggle ? (rawChar) => rawChar : (rawChar) => rawChar.toLowerCase(),163 (rawString) => rawString.toUpperCase()164 );165 it('should produce the same values given the same seed', () => {166 assertProduceSameValueGivenSameSeed(mixedCaseBuilder, { extraParameters });167 });168 it('should only produce correct values', () => {169 assertProduceCorrectValues(mixedCaseBuilder, isCorrect, { extraParameters });170 });171 it('should produce values seen as shrinkable without any context', () => {172 assertProduceValuesShrinkableWithoutContext(mixedCaseBuilder, { extraParameters });173 });174 it('should be able to shrink to the same values without initial context (if underlyings do)', () => {175 assertShrinkProducesSameValueWithoutInitialContext(mixedCaseBuilder, { extraParameters });176 });177 it('should preserve strictly smaller ordering in shrink (if underlyings do)', () => {178 assertShrinkProducesStrictlySmallerValue(mixedCaseBuilder, isStrictlySmaller, { extraParameters });179 });180});181// Helpers182function mockSourceArbitrariesForGenerate(bigIntOutput: bigint, stringOutput: string) {183 const { instance: bigUintNInstance, generate: bigUintNGenerate } = fakeArbitrary();184 const bigUintN = jest.spyOn(BigUintNMock, 'bigUintN');185 bigUintN.mockReturnValue(bigUintNInstance);186 bigUintNGenerate.mockReturnValueOnce(new Value(bigIntOutput, undefined));187 const { instance: stringInstance, generate: stringGenerate } = fakeArbitrary();188 stringGenerate.mockReturnValueOnce(new Value(stringOutput, undefined));189 return { bigUintN, stringInstance };...

Full Screen

Full Screen

TripleStateToggler.jsx

Source:TripleStateToggler.jsx Github

copy

Full Screen

1import React, { useState } from 'react';2import { Box, Paper, Switch } from '@material-ui/core';3import { makeStyles, withStyles } from '@material-ui/core/styles';4import { ToggleButton, ToggleButtonGroup } from '@material-ui/lab';5import { useTheme } from '@material-ui/styles';6export default function TripleStateToggler({7 name,8 value,9 handleChange,10 disabled,11}) {12 const { palette } = useTheme();13 const [state, setState] = useState(value);14 const [switchState, setSwitchState] = useState(value);15 const handleSwitchChange = (e) => {16 setSwitchState(e.target.checked);17 handleChange(name, e.target.checked);18 };19 const handleToggleChange = (e, value) => {20 setState(value);21 setSwitchState(value);22 handleChange(name, value);23 };24 const StyledToggleButtonGroup = withStyles(() => ({25 root: {26 width: 40,27 backgroundColor: palette.common.white,28 color: palette.text.primary,29 borderRadius: 10,30 },31 grouped: {32 height: 20,33 width: 20,34 border: 'none',35 padding: 16,36 borderRadius: 16,37 margin: '-6px',38 '&:not(:first-child)': { marginLeft: 0 },39 '&:hover': {40 borderRadius: 10,41 backgroundColor: 'rgba(0, 0, 0, 0.04)',42 },43 '&.Mui-selected': {44 borderRadius: 10,45 },46 },47 }))(ToggleButtonGroup);48 const useStyles = makeStyles(() => ({49 paper: {50 display: 'flex',51 flexWrap: 'wrap',52 borderRadius: '10px !important',53 },54 divider: {},55 }));56 const classes = useStyles();57 const defaultSwitchStyle = {58 position: 'absolute',59 zIndex: '1',60 transition: '0.3s all',61 };62 const untoggledValue = null || undefined;63 return (64 <>65 <Paper elevation={0} className={classes.paper}>66 <Box67 style={68 state === untoggledValue69 ? {70 ...defaultSwitchStyle,71 pointerEvents: 'none',72 opacity: '0',73 }74 : { ...defaultSwitchStyle, opacity: '1' }75 }76 >77 <Switch78 className={'Mui-checked'}79 checked={state === untoggledValue ? false : switchState}80 onChange={(e) => handleSwitchChange(e)}81 disabled={disabled}82 style={{83 opacity: disabled ? 0.5 : 1,84 }}85 icon={86 <i87 className="material-icons-outlined"88 style={{89 fontSize: 16,90 backgroundColor: palette.grey[400],91 color: palette.common.white,92 // color: palette.text.primary,93 borderRadius: 10,94 height: 20,95 width: 20,96 display: 'flex',97 alignItems: 'center',98 justifyContent: 'center',99 }}100 >101 close102 </i>103 }104 checkedIcon={105 <i106 className="material-icons-outlined"107 style={{108 fontSize: 16,109 backgroundColor: palette.success.main,110 color: palette.success.contrastText,111 borderRadius: 10,112 height: 20,113 width: 20,114 display: 'flex',115 alignItems: 'center',116 justifyContent: 'center',117 }}118 >119 done120 </i>121 }122 />123 </Box>124 <>125 <Paper126 elevation={state === untoggledValue ? 2 : 0}127 style={{ marginRight: 18, borderRadius: 10 }}128 >129 <StyledToggleButtonGroup130 onChange={handleToggleChange}131 size="small"132 exclusive133 >134 <ToggleButton135 value={false}136 style={{137 padding: 16,138 borderRadius: 16,139 margin: -6,140 }}141 // disabled={disabled}142 >143 <i144 className="material-icons-outlined"145 style={{ fontSize: 16 }}146 >147 close148 </i>149 </ToggleButton>150 <ToggleButton151 value={true}152 style={{153 padding: 16,154 borderRadius: 16,155 margin: '-6px -7px',156 }}157 // disabled={disabled}158 >159 <i160 className="material-icons-outlined"161 style={{ fontSize: 16 }}162 >163 done164 </i>165 </ToggleButton>166 </StyledToggleButtonGroup>167 </Paper>168 </>169 </Paper>170 </>171 );...

Full Screen

Full Screen

MixedCaseArbitrary.ts

Source:MixedCaseArbitrary.ts Github

copy

Full Screen

1import { Random } from '../../random/generator/Random';2import { Stream } from '../../stream/Stream';3import { bigUintN } from '../bigUintN';4import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';5import { Value } from '../../check/arbitrary/definition/Value';6import { makeLazy } from '../../stream/LazyIterableIterator';7import {8 applyFlagsOnChars,9 computeFlagsFromChars,10 computeNextFlags,11 computeTogglePositions,12} from './helpers/ToggleFlags';13import { safeJoin, safeSlice } from '../../utils/globals';14import { BigInt } from '../../utils/globals';15/** @internal */16type MixedCaseArbitraryContext = {17 rawString: string;18 rawStringContext: unknown;19 flags: bigint;20 flagsContext: unknown;21};22/** @internal */23export class MixedCaseArbitrary extends Arbitrary<string> {24 constructor(25 private readonly stringArb: Arbitrary<string>,26 private readonly toggleCase: (rawChar: string) => string,27 private readonly untoggleAll: ((toggledString: string) => string) | undefined28 ) {29 super();30 }31 /**32 * Create a proper context33 * @param rawStringValue34 * @param flagsValue35 */36 private buildContextFor(rawStringValue: Value<string>, flagsValue: Value<bigint>): MixedCaseArbitraryContext {37 return {38 rawString: rawStringValue.value,39 rawStringContext: rawStringValue.context,40 flags: flagsValue.value,41 flagsContext: flagsValue.context,42 };43 }44 generate(mrng: Random, biasFactor: number | undefined): Value<string> {45 const rawStringValue = this.stringArb.generate(mrng, biasFactor);46 const chars = [...rawStringValue.value]; // split into valid unicode (keeps surrogate pairs)47 const togglePositions = computeTogglePositions(chars, this.toggleCase);48 const flagsArb = bigUintN(togglePositions.length);49 const flagsValue = flagsArb.generate(mrng, undefined); // true => toggle the char, false => keep it as-is50 applyFlagsOnChars(chars, flagsValue.value, togglePositions, this.toggleCase);51 return new Value(safeJoin(chars, ''), this.buildContextFor(rawStringValue, flagsValue));52 }53 canShrinkWithoutContext(value: unknown): value is string {54 if (typeof value !== 'string') {55 return false;56 }57 return this.untoggleAll !== undefined58 ? this.stringArb.canShrinkWithoutContext(this.untoggleAll(value))59 : // If nothing was toggled or if the underlying generator can still shrink it, we consider it shrinkable60 this.stringArb.canShrinkWithoutContext(value);61 }62 shrink(value: string, context?: unknown): Stream<Value<string>> {63 let contextSafe: MixedCaseArbitraryContext;64 if (context !== undefined) {65 contextSafe = context as MixedCaseArbitraryContext;66 } else {67 // As user should have called canShrinkWithoutContext first;68 // We know that the untoggled string can be shrunk without any context69 if (this.untoggleAll !== undefined) {70 const untoggledValue = this.untoggleAll(value);71 const valueChars = [...value];72 const untoggledValueChars = [...untoggledValue];73 const togglePositions = computeTogglePositions(untoggledValueChars, this.toggleCase);74 contextSafe = {75 rawString: untoggledValue,76 rawStringContext: undefined,77 flags: computeFlagsFromChars(untoggledValueChars, valueChars, togglePositions),78 flagsContext: undefined,79 };80 } else {81 contextSafe = {82 rawString: value,83 rawStringContext: undefined,84 flags: BigInt(0),85 flagsContext: undefined,86 };87 }88 }89 const rawString = contextSafe.rawString;90 const flags = contextSafe.flags;91 return this.stringArb92 .shrink(rawString, contextSafe.rawStringContext)93 .map((nRawStringValue) => {94 const nChars = [...nRawStringValue.value];95 const nTogglePositions = computeTogglePositions(nChars, this.toggleCase);96 const nFlags = computeNextFlags(flags, nTogglePositions.length);97 // Potentially new value for nTogglePositions.length, new value for nFlags98 // so flagsContext is not applicable anymore99 applyFlagsOnChars(nChars, nFlags, nTogglePositions, this.toggleCase);100 // Remark: Value nFlags can be attached to a context equal to undefined101 // as `canShrinkWithoutContext(nFlags) === true` for the bigint arbitrary102 return new Value(safeJoin(nChars, ''), this.buildContextFor(nRawStringValue, new Value(nFlags, undefined)));103 })104 .join(105 makeLazy(() => {106 const chars = [...rawString];107 const togglePositions = computeTogglePositions(chars, this.toggleCase);108 return bigUintN(togglePositions.length)109 .shrink(flags, contextSafe.flagsContext)110 .map((nFlagsValue) => {111 const nChars = safeSlice(chars); // cloning chars112 applyFlagsOnChars(nChars, nFlagsValue.value, togglePositions, this.toggleCase);113 return new Value(114 safeJoin(nChars, ''),115 this.buildContextFor(new Value(rawString, contextSafe.rawStringContext), nFlagsValue)116 );117 });118 })119 );120 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { untoggledValue } = require('fast-check-monorepo');2const value = untoggledValue();3const { untoggledValue } = require('fast-check-monorepo');4const value = untoggledValue();5const { untoggledValue } = require('fast-check-monorepo');6const value = untoggledValue();7const { untoggledValue } = require('fast-check-monorepo');8const value = untoggledValue();9const { untoggledValue } = require('fast-check-monorepo');10const value = untoggledValue();11const { untoggledValue } = require('fast-check-monorepo');12const value = untoggledValue();13const { untoggledValue } = require('fast-check-monorepo');14const value = untoggledValue();15const { untoggledValue } = require('fast-check-monorepo');16const value = untoggledValue();17const { untoggledValue } = require('fast-check-monorepo');18const value = untoggledValue();19const { untoggledValue } = require('fast-check-monorepo');20const value = untoggledValue();21const { untoggledValue } = require('fast-check-monorepo');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { untoggledValue } = require('fast-check-monorepo');2console.log(untoggledValue(10));3const { untoggledValue } = require('fast-check-monorepo');4console.log(untoggledValue(10));5const { untoggledValue } = require('fast-check-monorepo');6console.log(untoggledValue(10));7const { untoggledValue } = require('fast-check-monorepo');8console.log(untoggledValue(10));9const { untoggledValue } = require('fast-check-monorepo');10console.log(untoggledValue(10));11const { untoggledValue } = require('fast-check-monorepo');12console.log(untoggledValue(10));13const { untoggledValue } = require('fast-check-monorepo');14console.log(untoggledValue(10));15const { untoggledValue } = require('fast-check-monorepo');16console.log(untoggledValue(10));

Full Screen

Using AI Code Generation

copy

Full Screen

1const { untoggledValue } = require("fast-check-monorepo");2const { it } = require("mocha");3const { expect } = require("chai");4describe("test3", () => {5 it("should return the untoggled value", () => {6 expect(untoggledValue(1)).to.equal(2);7 });8});9const { untoggledValue } = require("fast-check-monorepo");10const { it } = require("mocha");11const { expect } = require("chai");12describe("test4", () => {13 it("should return the untoggled value", () => {14 expect(untoggledValue(1)).to.equal(2);15 });16});17const { untoggledValue } = require("fast-check-monorepo");18const { it } = require("mocha");19const { expect } = require("chai");20describe("test5", () => {21 it("should return the untoggled value", () => {22 expect(untoggledValue(1)).to.equal(2);23 });24});25const { untoggledValue } = require("fast-check-monorepo");26const { it } = require("mocha");27const { expect } = require("chai");28describe("test6", () => {29 it("should return the untoggled value", () => {30 expect(untoggledValue(1)).to.equal(2);31 });32});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { untoggledValue } = require('fast-check-monorepo');2const value = untoggledValue();3console.log(`Value: ${value}`);4const { untoggledValue } = require('fast-check-monorepo');5const value = untoggledValue();6console.log(`Value: ${value}`);7test('value is greater than 5', () => {8 expect(value).toBeGreaterThan(5);9});10const { untoggledValue } = require('fast-check-monorepo');11const value = untoggledValue();12console.log(`Value: ${value}`);13test('value is greater than 5', () => {14 expect(value).toBeGreaterThan(5);15});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful