Skip to content

Commit 086c488

Browse files
committed
parseColorDecision, test that fails
1 parent d5e14e2 commit 086c488

File tree

5 files changed

+140
-4
lines changed

5 files changed

+140
-4
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { State } from "./colorOptions";
2+
3+
//export type Context= "background" | "border" | "text" | "illustration";
4+
5+
//export type Usage = "action" |
6+
//"active" | "disabled" | "error" | "success" |
7+
//"default" | "title" | "label" | "contrast";
8+
9+
//export type Usage = "default" | "alt";
10+
11+
//Not in path
12+
//export type Variant = "high" | "low";
13+
14+
const contexts = ["background", "text", "border", "artwork"] as const;
15+
16+
type Context = typeof contexts[number];
17+
18+
const usages = [
19+
"default",
20+
"alt",
21+
"contrast",
22+
"flat",
23+
"action",
24+
"active",
25+
"open",
26+
"disabled",
27+
"raised",
28+
"altRaised",
29+
"altOverlap",
30+
"contrastRaised"
31+
] as const;
32+
33+
//type Usage = typeof usages[number];
34+
35+
export type ParsedColorDecisionName = {
36+
context: Context;
37+
usage: string; //Usage In reality use string because it's susceptible to change.
38+
variant: "high" | "low" | undefined;
39+
colorName: string; // "grey" "blueFrance"
40+
state: State | undefined;
41+
};
42+
43+
export function createParseColorDecisionName(params: {
44+
/** Like [ "grey", "blueFrance", ... ]
45+
* All the the color name in camel case that we deduce from Options
46+
* it help parsing without making assumption on what is a valid Usage
47+
*/
48+
colorNames: string[];
49+
}) {
50+
const { colorNames } = params;
51+
52+
function parseColorDecisionName(desicsionName: `--${string}`): ParsedColorDecisionName {
53+
return null as any;
54+
}
55+
56+
return { parseColorDecisionName };
57+
}

src/bin/css_to_ts/colorOptions.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ export const data_fr_theme = "data-fr-theme";
1313

1414
export type Variant = "main" | "sun" | "moon";
1515

16-
export const states = ["hover", "active"] as const;
17-
18-
export type State = typeof states[number];
16+
export type State = "hover" | "active";
1917

2018
export type BrightnessIndex = {
2119
value: number;
@@ -327,7 +325,6 @@ export type ColorOption = {
327325
};
328326
};
329327

330-
/** Exported only for tests */
331328
export function parseColorOptions(rawCssCode: string): ColorOption[] {
332329
const parsedCss = css.parse(rawCssCode);
333330

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./parseColorDecisionName.test";
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { createParseColorDecisionName } from "../../../bin/css_to_ts/colorDecisions";
2+
import type { ParsedColorDecisionName } from "../../../bin/css_to_ts/colorDecisions";
3+
import { assert } from "tsafe/assert";
4+
import { same } from "evt/tools/inDepth/same";
5+
6+
console.log(`Running test ${__filename}`);
7+
8+
const colorNames = ["grey", "orangeTerreBattue"];
9+
10+
const { parseColorDecisionName } = createParseColorDecisionName({ colorNames });
11+
12+
{
13+
const expected: ParsedColorDecisionName = {
14+
"context": "background",
15+
"usage": "default",
16+
"variant": undefined,
17+
"colorName": "grey",
18+
"state": "hover"
19+
};
20+
21+
const got = parseColorDecisionName("--background-default-grey-hover");
22+
23+
assert(same(got, expected));
24+
}
25+
26+
{
27+
const expected: ParsedColorDecisionName = {
28+
"context": "background",
29+
"usage": "default",
30+
"variant": undefined,
31+
"colorName": "grey",
32+
"state": undefined
33+
};
34+
35+
const got = parseColorDecisionName("--background-default-grey");
36+
37+
assert(same(got, expected));
38+
}
39+
40+
{
41+
const expected: ParsedColorDecisionName = {
42+
"context": "border",
43+
"usage": "action",
44+
"variant": "low",
45+
"colorName": "orange-terre-battue",
46+
"state": undefined
47+
};
48+
49+
const got = parseColorDecisionName("--border-action-low-orange-terre-battue");
50+
51+
assert(same(got, expected));
52+
}
53+
54+
{
55+
const expected: ParsedColorDecisionName = {
56+
"context": "background",
57+
"usage": "altRaised",
58+
"variant": undefined,
59+
"colorName": "grey",
60+
"state": "hover"
61+
};
62+
63+
const got = parseColorDecisionName("--background-alt-raised-grey-hover");
64+
65+
assert(same(got, expected));
66+
}
67+
68+
{
69+
const expected: ParsedColorDecisionName = {
70+
"context": "background",
71+
"usage": "contrastOverlap",
72+
"variant": undefined,
73+
"colorName": "grey",
74+
"state": undefined
75+
};
76+
77+
const got = parseColorDecisionName("--background-contrast-overlap-grey");
78+
79+
assert(same(got, expected));
80+
}

src/test/bin/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
import "./colorOptions";
2+
import "./colorDecisions";

0 commit comments

Comments
 (0)