當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。