All files / toml / _test_utils.ts

100.00% Branches 27/27
100.00% Functions 2/2
100.00% Lines 33/33
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
46
47
48
49
50
51
52
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1261
x1261
x1261
x1261
x1261
 
x1
x1261
x762
x762
x352
x762
x271
x762
x64
x762
x28
 
x762
x762
x762
x42
x762
x5
x762
x1261
x126
x499
x373
x373
x474
x474
x373
x373
x1261




















































// Copyright 2018-2026 the Deno authors. MIT license.

type TestCaseValue = {
  type:
    | "string"
    | "integer"
    | "float"
    | "bool"
    | "datetime"
    | "datetime-local"
    | "date-local"
    | "time-local";
  value: string;
};
export type TestCase =
  | TestCaseValue
  | TestCase[]
  | { [key: string]: TestCase };

function isTestCaseValue(v: unknown): v is TestCaseValue {
  return typeof v === "object" && v !== null &&
    "type" in v && typeof v.type === "string" &&
    "value" in v && typeof v.value === "string";
}

export function convertTestCase(v: TestCase): unknown {
  if (isTestCaseValue(v)) {
    switch (v.type) {
      case "string":
        return v.value;
      case "integer":
        return parseInt(v.value, 10);
      case "float":
        return parseFloat(v.value.replace("inf", "Infinity"));
      case "bool":
        return v.value === "true";
      // TODO: https://github.com/denoland/std/issues/6591
      case "datetime":
      case "datetime-local":
      case "date-local":
        return new Date(v.value);
      case "time-local":
        return v.value;
    }
  } else if (Array.isArray(v)) {
    return v.map(convertTestCase);
  } else {
    const obj: Record<string, unknown> = {};
    for (const [key, value] of Object.entries(v)) {
      obj[key] = convertTestCase(value);
    }
    return obj;
  }
}