All files / toml / _test_utils.ts

100.00% Branches 16/16
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1262
x1262
x1262
x1262
x1262
 
x1
x1262
x2024
x2024
x2376
x2024
x2295
x2024
x2088
x2024
x2052
 
x2024
x2024
x2024
x2066
x2024
x2029
x2024
x1262
x1887
x1761
x2134
x2134
x2608
x2608
x2134
x2134
x1262




















































// Copyright 2018-2025 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;
  }
}