utils.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright (c) 2020-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Panagiotis Tourlas <panangiot_tourlov@hotmail.com>
  6. * @author Koya Sakuma <koya.sakuma.work@gmail.com>
  7. */
  8. import { Transpiler } from '../transpiler';
  9. import { KeywordDict, PropertyDict, OperatorList } from '../types';
  10. export function testKeywords(keywords: KeywordDict, transpiler: Transpiler) {
  11. for (const name in keywords) {
  12. it(name, () => {
  13. const k = keywords[name];
  14. if (k.map) {
  15. const expr = transpiler(name);
  16. expect(expr).toEqual(k.map());
  17. } else {
  18. const transpile = () => transpiler(name);
  19. expect(transpile).toThrow();
  20. expect(transpile).not.toThrowError(RangeError);
  21. }
  22. });
  23. }
  24. }
  25. export function testProperties(properties: PropertyDict, transpiler: Transpiler) {
  26. for (const name in properties) {
  27. const p = properties[name];
  28. p['@examples'].forEach(example => {
  29. it(name, () => {
  30. if (!p.isUnsupported) {
  31. transpiler(example);
  32. } else {
  33. const transpile = () => transpiler(example);
  34. expect(transpile).toThrow();
  35. expect(transpile).not.toThrowError(RangeError);
  36. }
  37. });
  38. });
  39. it(name, () => {
  40. if (!p['@examples'].length) {
  41. throw Error(`'${name}' property has no example(s)`);
  42. }
  43. });
  44. }
  45. }
  46. export function testOperators(operators: OperatorList, transpiler: Transpiler) {
  47. operators.forEach(o => {
  48. o['@examples'].forEach(example => {
  49. it(o.name, () => {
  50. if (!o.isUnsupported) {
  51. transpiler(example);
  52. } else {
  53. const transpile = () => transpiler(example);
  54. expect(transpile).toThrow();
  55. expect(transpile).not.toThrowError(RangeError);
  56. }
  57. });
  58. });
  59. it(o.name, () => {
  60. if (!o['@examples'].length) {
  61. throw Error(`'${o.name}' operator has no example(s)`);
  62. }
  63. });
  64. });
  65. }