-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add calculation examples to all shapes
- Loading branch information
Showing
5 changed files
with
304 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,114 @@ | ||
import { test } from "@siteimprove/alfa-test"; | ||
|
||
import { Lexer } from "../../../src/syntax/lexer"; | ||
import { Circle } from "../../../src/value/shape/circle"; | ||
import { Circle, Lexer } from "../../../src"; | ||
|
||
function parse(input: string) { | ||
return Circle.parse(Lexer.lex(input)).map(([_, circle]) => circle.toJSON()); | ||
return Circle.parse(Lexer.lex(input)).getUnsafe()[1].toJSON(); | ||
} | ||
|
||
test("parse() parses a circle with just a radius", (t) => { | ||
t.deepEqual(parse("circle(farthest-side)").getUnsafe(), { | ||
t.deepEqual(parse("circle(farthest-side)"), { | ||
type: "basic-shape", | ||
kind: "circle", | ||
radius: { | ||
type: "basic-shape", | ||
kind: "radius", | ||
value: { | ||
type: "keyword", | ||
value: "farthest-side", | ||
}, | ||
value: { type: "keyword", value: "farthest-side" }, | ||
}, | ||
center: { | ||
type: "position", | ||
vertical: { | ||
type: "keyword", | ||
value: "center", | ||
}, | ||
horizontal: { | ||
type: "keyword", | ||
value: "center", | ||
}, | ||
vertical: { type: "keyword", value: "center" }, | ||
horizontal: { type: "keyword", value: "center" }, | ||
}, | ||
}); | ||
}); | ||
|
||
test("parse() parses a circle with just a center", (t) => { | ||
t.deepEqual(parse("circle(at left)").getUnsafe(), { | ||
t.deepEqual(parse("circle(at left)"), { | ||
type: "basic-shape", | ||
kind: "circle", | ||
radius: { | ||
type: "basic-shape", | ||
kind: "radius", | ||
value: { | ||
type: "keyword", | ||
value: "closest-side", | ||
}, | ||
value: { type: "keyword", value: "closest-side" }, | ||
}, | ||
center: { | ||
type: "position", | ||
vertical: { | ||
type: "keyword", | ||
value: "center", | ||
}, | ||
vertical: { type: "keyword", value: "center" }, | ||
horizontal: { | ||
type: "side", | ||
offset: null, | ||
side: { | ||
type: "keyword", | ||
value: "left", | ||
}, | ||
side: { type: "keyword", value: "left" }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test("parse() parses a circle with both radius and center", (t) => { | ||
t.deepEqual(parse("circle(10px at left)").getUnsafe(), { | ||
t.deepEqual(parse("circle(10px at left)"), { | ||
type: "basic-shape", | ||
kind: "circle", | ||
radius: { | ||
type: "basic-shape", | ||
kind: "radius", | ||
value: { | ||
type: "length", | ||
value: 10, | ||
unit: "px", | ||
}, | ||
value: { type: "length", value: 10, unit: "px" }, | ||
}, | ||
center: { | ||
type: "position", | ||
vertical: { | ||
type: "keyword", | ||
value: "center", | ||
}, | ||
vertical: { type: "keyword", value: "center" }, | ||
horizontal: { | ||
type: "side", | ||
offset: null, | ||
side: { | ||
type: "keyword", | ||
value: "left", | ||
}, | ||
side: { type: "keyword", value: "left" }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test("parse() fails if there is a negative radius", (t) => { | ||
t.deepEqual(parse("circle(-1px)").isErr(), true); | ||
t.deepEqual(Circle.parse(Lexer.lex("circle(-1px)")).isErr(), true); | ||
}); | ||
|
||
test("parse() accepts calculated radius", (t) => { | ||
t.deepEqual(parse("circle(calc(10px + 1%) at left)"), { | ||
type: "basic-shape", | ||
kind: "circle", | ||
radius: { | ||
type: "basic-shape", | ||
kind: "radius", | ||
value: { | ||
type: "length-percentage", | ||
math: { | ||
type: "math expression", | ||
expression: { | ||
type: "calculation", | ||
arguments: [ | ||
{ | ||
type: "sum", | ||
operands: [ | ||
{ | ||
type: "value", | ||
value: { type: "length", value: 10, unit: "px" }, | ||
}, | ||
{ | ||
type: "value", | ||
value: { type: "percentage", value: 0.01 }, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
center: { | ||
type: "position", | ||
vertical: { type: "keyword", value: "center" }, | ||
horizontal: { | ||
type: "side", | ||
offset: null, | ||
side: { type: "keyword", value: "left" }, | ||
}, | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.