当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript wrap-jsverify.oneof函数代码示例

本文整理汇总了TypeScript中@ephox/wrap-jsverify.oneof函数的典型用法代码示例。如果您正苦于以下问题:TypeScript oneof函数的具体用法?TypeScript oneof怎么用?TypeScript oneof使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了oneof函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: converter

UnitTest.test('SizeInputConverterTest', () => {
  const check = (converter: SizeConversion) => (expected: Option<Size>, input: Size) => {
    const result = converter(input);
    expected.fold(() => {
      assert.eq(true, result.isNone(), 'Expected none');
    }, (size) => {
      result.fold(() => {
        assert.fail('Expected size');
      }, (resultSize) => {
        assert.eq(size, resultSize);
      });
    });
  };

  const checkRatio2 = check(ratioSizeConversion(2, 'in'));
  checkRatio2(Option.some(nuSize(1, 'in')), nuSize(0.5, 'in'));
  checkRatio2(Option.some(nuSize(1, 'in')), nuSize(48, 'px'));
  checkRatio2(Option.some(nuSize(1, 'in')), nuSize(6, 'pc'));
  checkRatio2(Option.some(nuSize(1, 'in')), nuSize(36, 'pt'));
  checkRatio2(Option.some(nuSize(2, 'in')), nuSize(2.54, 'cm'));
  checkRatio2(Option.some(nuSize(1, 'in')), nuSize(12.7, 'mm'));

  const checkRatio3 = check(ratioSizeConversion(3, 'in'));
  checkRatio3(Option.some(nuSize(1, 'in')), nuSize(32, 'px'));
  checkRatio3(Option.some(nuSize(1, 'in')), nuSize(4, 'pc'));
  checkRatio3(Option.some(nuSize(1, 'in')), nuSize(24, 'pt'));
  checkRatio3(Option.some(nuSize(3, 'in')), nuSize(2.54, 'cm'));
  checkRatio3(Option.some(nuSize(3, 'in')), nuSize(25.4, 'mm'));

  Jsc.property(
    'ratioSizeConversion is equalivent to multipling when the units match',
    Jsc.nat(100), Jsc.nat(100), Jsc.oneof(Jsc.elements(units)),
    function (scale: number, value: number, unit: SizeUnit) {
      const v = ratioSizeConversion(scale, unit)({ value, unit }).getOrNull();
      return Jsc.eq({ value: scale * value, unit }, v);
    }
  );

  Jsc.property(
    'noSizeConversion always returns none',
    Jsc.number(0, largeSensible), Jsc.oneof(Jsc.elements(units)),
    function (value: number, unit: SizeUnit) {
      return Jsc.eq(true, noSizeConversion({ value, unit }).isNone());
    }
  );

  const checkRatioConv2 = check(makeRatioConverter('96px', '2in'));
  checkRatioConv2(Option.some(nuSize(1, 'in')), nuSize(0.5, 'in'));
  checkRatioConv2(Option.some(nuSize(1, 'in')), nuSize(48, 'px'));
  checkRatioConv2(Option.some(nuSize(1, 'in')), nuSize(6, 'pc'));
  checkRatioConv2(Option.some(nuSize(1, 'in')), nuSize(36, 'pt'));
  checkRatioConv2(Option.some(nuSize(2, 'in')), nuSize(2.54, 'cm'));
  checkRatioConv2(Option.some(nuSize(1, 'in')), nuSize(12.7, 'mm'));

  const checkNoRatio = check(makeRatioConverter('96px', '1em'));
  checkNoRatio(Option.none(), nuSize(1, 'em'));
  checkNoRatio(Option.none(), nuSize(50, 'px'));
  checkNoRatio(Option.none(), nuSize(2, 'cm'));
});
开发者ID:tinymce,项目名称:tinymce,代码行数:59,代码来源:SizeInputConverterTest.ts

示例2: convertUnit

UnitTest.test('SizeInputConvertTest', () => {

  const check = (expected: Option<number>, size: Size, unit: SizeUnit) => {
    const result = convertUnit(size, unit);
    assert.eq(
      true, expected.equals(result),
      'Expected conversion of ' + JSON.stringify(size) +
      ' to ' + unit + ' = ' + result.toString()
    );
  };

  check(Option.some(12), nuSize(12, 'mm'), 'mm');
  check(Option.some(16848), nuSize(234, 'in'), 'pt');
  check(Option.some(75.59055118110236), nuSize(2, 'cm'), 'px');
  check(Option.none(), nuSize(2, 'cm'), 'ch');

  Jsc.property(
    'All units can convert to themselves',
    Jsc.number(0, largeSensible), Jsc.oneof(Jsc.elements(units)),
    function (value: number, unit: SizeUnit) {
      const outValue = convertUnit(nuSize(value, unit), unit).getOrNull();
      return Jsc.eq(value, outValue);
    }
  );

  const approxEq = (value, outValue) => Math.abs(value - outValue) < 0.000001;
  Jsc.property(
    'All convertable units should convert back and forth',
    Jsc.number(0, largeSensible), Jsc.oneof(Jsc.elements(convertableUnits)), Jsc.oneof(Jsc.elements(convertableUnits)),
    function (value: number, unit1: SizeUnit, unit2: SizeUnit) {
      const outValue = convertUnit(nuSize(value, unit1), unit2).bind(
        (unit2Value) => convertUnit(nuSize(unit2Value, unit2), unit1)
      ).getOrNull();
      return outValue !== null && approxEq(value, outValue);
    }
  );

  const nonConvertable = Arr.filter(units, (unit) => !Arr.contains(convertableUnits, unit));
  Jsc.property(
    'All non-convertable units can only convert to themselves',
    Jsc.number(0, largeSensible), Jsc.oneof(Jsc.elements(nonConvertable)), Jsc.oneof(Jsc.elements(units)),
    function (value: number, unit1: SizeUnit, unit2: SizeUnit) {
      return Jsc.eq(unit1 === unit2, convertUnit(nuSize(value, unit1), unit2).isSome());
    }
  );
});
开发者ID:tinymce,项目名称:tinymce,代码行数:46,代码来源:SizeInputConvertTest.ts

示例3: parseSize

UnitTest.test('SizeInputParsingTest', () => {
  const check = (expected: Result<Size, string>, text: string) => {
    const result = parseSize(text);
    expected.fold((err) => {
      result.fold((resultErr) => {
        assert.eq(err, resultErr);
      }, (resultValue) => {
        assert.fail('parseSize should have failed but succeeded with value:\n' + JSON.stringify(resultValue));
      });
    }, (value: Size) => {
      result.fold((resultErr) => {
        assert.fail('parseSize should have succeeded but failed with err: "' + resultErr + '"');
      }, (resultValue) => {
        assert.eq(value, resultValue);
      });
    });
  };

  check(Result.error(''), '');
  check(Result.error('px'), 'px');
  check(Result.error('a'), 'a');
  check(Result.error('blah'), 'blah');
  check(Result.error('1a'), '1a');
  // check negative numbers are not allowed
  check(Result.error('-1px'), '-1px');
  // check the empty unit
  check(Result.value(nuSize(1, '')), '1');
  // check various forms of padding
  check(Result.value(nuSize(1, 'px')), '1px');
  check(Result.value(nuSize(1, 'px')), '    1px');
  check(Result.value(nuSize(1, 'px')), '1px     ');
  check(Result.value(nuSize(1, 'px')), '    1px     ');
  check(Result.value(nuSize(1, 'px')), '    1    px     ');
  check(Result.value(nuSize(1.25, 'px')), '    1.25    px     ');
  // check that all units work
  Arr.each(units, (unit) => {
    check(Result.error(unit), unit);
    check(Result.value(nuSize(4, unit as SizeUnit)), '4' + unit);
  });

  const arbPad = Jsc.array(Jsc.elements(' \t'.split(''))).smap(
    function (arr) { return arr.join(''); },
    function (s) { return s.split(''); }
  );

  Jsc.property(
    'Valid size strings should be parseable',
    arbPad, Jsc.number(0, largeSensible), arbPad, Jsc.oneof(Jsc.elements(units)), arbPad,
    function (pad1: string, nonNegNumber: number, pad2: string, unit: SizeUnit, pad3: string) {
      const str = pad1 + nonNegNumber + pad2 + unit + pad3;
      const parsed = parseSize(str);
      const size = parsed.toOption().getOrNull();
      return Jsc.eq(nuSize(nonNegNumber, unit), size);
    }
  );
});
开发者ID:tinymce,项目名称:tinymce,代码行数:56,代码来源:SizeInputParsingTest.ts

示例4: isEqualEntries

UnitTest.test('ListModelTest', () => {
  const arbitratyContent = Jsc.bless({
    generator: Arbitraries.content('inline').generator.map((el) => [el])
  });

  const arbitraryEntry = Jsc.record({
    isSelected: Jsc.constant(false),
    depth: Jsc.integer(1, 10),
    content: Jsc.small(arbitratyContent),
    listType: Jsc.oneof(Jsc.constant(ListType.OL), Jsc.constant(ListType.UL)),
    listAttributes: Jsc.oneof(Jsc.constant({}), Jsc.constant({style: 'list-style-type: lower-alpha;'})),
    itemAttributes: Jsc.oneof(Jsc.constant({}), Jsc.constant({style: 'color: red;'})),
  });

  const arbitraryEntries = Jsc.array(arbitraryEntry);

  const composeParseProperty = Jsc.forall(arbitraryEntries, (inputEntries: Entry[]) => {
    normalizeEntries(inputEntries);
    const outputEntries = composeParse(inputEntries);
    return isEqualEntries(inputEntries, outputEntries) || errorMessage(inputEntries, outputEntries);
  });

  const composeParse = (entries: Entry[]): Entry[] => {
    return composeList(document, entries)
      .map((list) => parseLists([list], Option.none()))
      .bind(Arr.head)
      .map((entrySet) => entrySet.entries)
      .getOr([]);
  };

  const isEqualEntries = (a: Entry[], b: Entry[]): boolean => {
    return stringifyEntries(a) === stringifyEntries(b);
  };

  const errorMessage = (inputEntries: Entry[], outputEntries: Entry[]): string => {
    return `\nPretty print counterexample:\n` +
    `input: [${stringifyEntries(inputEntries)}\n]\n` +
    `output: [${stringifyEntries(outputEntries)}\n]`;
  };

  const stringifyEntries = (entries: Entry[]): string => {
    return Arr.map(entries, stringifyEntry).join(',');
  };

  const stringifyEntry = (entry: Entry): string => {
    return `\n  {
      depth: ${entry.depth}
      content: ${entry.content.length > 0 ? serializeElements(entry.content) : '[Empty]'}
      listType: ${entry.listType}
      isSelected: ${entry.isSelected}
      listAttributes: ${JSON.stringify(entry.listAttributes)}
      itemAttributes: ${JSON.stringify(entry.itemAttributes)}
    }`;
  };

  const serializeElements = (elms: Element[]): string => {
    return Arr.map(elms, (el) => el.dom().outerHTML).join('');
  };

  Jsc.assert(composeParseProperty, {
    size: 500,
    tests: 500,
    quiet: true
  });

  // Manual testing. To simplify debugging once a counterexample has been found.
  /* const inputEntries: Entry[] = [
    {
      depth: 2,
      content: [Element.fromHtml('<i>stuff</i>')],
      listType: ListType.OL,
      isSelected: false,
      listAttributes: {style: 'list-style-type: lower-alpha;'},
      itemAttributes: {}
    }
  ];
  throw composeParse(inputEntries); */
});
开发者ID:mdgbayly,项目名称:tinymce,代码行数:78,代码来源:ListModelTest.ts


注:本文中的@ephox/wrap-jsverify.oneof函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。