utils.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. */
  7. import { parse } from '../../transpile';
  8. import { KeywordDict, PropertyDict, OperatorList } from '../types';
  9. /* FAULTY IMPORTS */
  10. // import compile from '../../compiler';
  11. export function testKeywords(keywords: KeywordDict, language: string) {
  12. for (const name in keywords) {
  13. it(name, () => {
  14. const k = keywords[name];
  15. if (k.map) {
  16. const expr = parse(language, name);
  17. // compile(expr);
  18. expect(expr).toEqual(k.map());
  19. } else {
  20. const transpile = () => parse(language, name);
  21. expect(transpile).toThrow();
  22. expect(transpile).not.toThrowError(RangeError);
  23. }
  24. });
  25. }
  26. }
  27. export function testProperties(properties: PropertyDict, language: string) {
  28. for (const name in properties) {
  29. const p = properties[name];
  30. p['@examples'].forEach(example => {
  31. it(name, () => {
  32. if (!p.isUnsupported) {
  33. const expr = parse(language, example);
  34. expect(expr).toBe(p);
  35. // compile(expr);
  36. } else {
  37. const transpile = () => parse(language, example);
  38. expect(transpile).toThrow();
  39. expect(transpile).not.toThrowError(RangeError);
  40. }
  41. });
  42. });
  43. it(name, () => {
  44. if (!p['@examples'].length) {
  45. throw Error(`'${name}' property has no example(s)`);
  46. }
  47. });
  48. }
  49. }
  50. export function testOperators(operators: OperatorList, language: string) {
  51. operators.forEach(o => {
  52. o['@examples'].forEach(example => {
  53. it(o.name, () => {
  54. if (!o.isUnsupported) {
  55. const expr = parse(language, example);
  56. expect(expr).toBe(o);
  57. } else {
  58. const transpile = () => parse(language, example);
  59. expect(transpile).toThrow();
  60. expect(transpile).not.toThrowError(RangeError);
  61. }
  62. });
  63. });
  64. it(o.name, () => {
  65. if (!o['@examples'].length) {
  66. throw Error(`'${o.name}' operator has no example(s)`);
  67. }
  68. });
  69. });
  70. }