All files / utils/__testUtils__ ConstExprShape.ts

100% Statements 11/11
100% Branches 14/14
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                  97x   28x       30x       3x   1x   3x   18x   1x   13x               11x 15x            
import ELEMENT_STEP from "../../types/ELEMENT_STEP";
import type TConstExpr from "../../types/TConstExpr";
 
/**
 * A compact spelling of a `TConstExpr`, so a test can state an expression's
 * shape in one string (#1175). One helper, not a copy per test file.
 */
class ConstExprShape {
  static of(expr: TConstExpr): string {
    switch (expr.kind) {
      case "literal":
        return expr.typeName === null
          ? expr.digits
          : `${expr.digits}:${expr.typeName}`;
      case "name":
        return [...(expr.root === null ? [] : [expr.root]), ...expr.path]
          .join(".")
          .replaceAll(`.${ELEMENT_STEP}`, ELEMENT_STEP);
      case "sizeof":
        return `sizeof(${expr.typeName})`;
      case "cast":
        return `(${expr.typeName})${ConstExprShape.of(expr.operand)}`;
      case "unary":
        return `${expr.op}${ConstExprShape.of(expr.operand)}`;
      case "binary":
        return `(${ConstExprShape.of(expr.left)} ${expr.op} ${ConstExprShape.of(expr.right)})`;
      case "ternary":
        return `(${ConstExprShape.of(expr.condition)} ? ${ConstExprShape.of(expr.whenTrue)} : ${ConstExprShape.of(expr.whenFalse)})`;
      case "other":
        return `<${expr.what} ${expr.spelling}>`;
    }
  }
 
  /** Each slot's shape, or null where 1.3 already knew the size */
  static list(
    exprs: ReadonlyArray<TConstExpr | null> | undefined,
  ): (string | null)[] | undefined {
    return exprs?.map((expr) =>
      expr === null ? null : ConstExprShape.of(expr),
    );
  }
}
 
export default ConstExprShape;