230 lines
8.6 KiB
Ziran
230 lines
8.6 KiB
Ziran
// Shared by the behavior tests: a sheet, its built rules, and checks that
|
|
// print what failed. Each test's main returns test_failures.
|
|
#import "css_parser"
|
|
#import "css_sheet"
|
|
#import "kryon/style"
|
|
#import "kryon/style_sheet"
|
|
#import "kryon/geometry"
|
|
#import "kryon/drawing_props"
|
|
#import "css_export"
|
|
|
|
libc :: #system_library "libc";
|
|
RawWrite :: (descriptor: s32, input: *u8, count: u64) -> s64 #foreign libc "write";
|
|
|
|
test_failures: s32 = 0;
|
|
test_sheet: CssSheet;
|
|
test_rules: StyleRules;
|
|
test_diagnostic: [256]u8;
|
|
|
|
TestSay :: (text: string) {
|
|
chunk: [512]u8
|
|
at: s32 = 0
|
|
while at < cast(s32)text.count {
|
|
count: s32 = cast(s32)text.count - at
|
|
if count > 512 { count = 512 }
|
|
for index: cast(s32)0..count - 1 { chunk[index] = text[at + index] }
|
|
if RawWrite(2, *chunk[0], cast(u64)count) <= 0 { return }
|
|
at += count
|
|
}
|
|
}
|
|
|
|
Check :: (ok: bool, label: string) {
|
|
if ok { return }
|
|
TestSay("FAIL ")
|
|
TestSay(label)
|
|
TestSay("\n")
|
|
test_failures += 1
|
|
}
|
|
|
|
Contains :: (text: string, fragment: string) -> bool {
|
|
if fragment.count == 0 { return true }
|
|
if fragment.count > text.count { return false }
|
|
for start: cast(s32)0..cast(s32)(text.count - fragment.count) {
|
|
if text[start:start + cast(s32)fragment.count] == fragment { return true }
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Imports the tests may name.
|
|
TestImports :: (name: string) -> CssImportAnswer {
|
|
answer: CssImportAnswer
|
|
if name == "base" {
|
|
answer.found = true
|
|
answer.source = "Button { color: #010203; gap: 1px; }"
|
|
} else if name == "tokens" {
|
|
answer.found = true
|
|
answer.source = ":root { --ink: #445566; }"
|
|
} else if name == "loop" {
|
|
answer.found = true
|
|
answer.source = "@import \"loop\";"
|
|
} else if name == "deep" {
|
|
answer.found = true
|
|
answer.source = "@import \"base\";\nText { color: red; }"
|
|
}
|
|
return answer
|
|
}
|
|
|
|
Diagnostic :: () -> string {
|
|
return CssSheetDiagnostic(*test_sheet, test_diagnostic[:])
|
|
}
|
|
|
|
Parse :: (source: string) -> bool {
|
|
CssSheetReset(*test_sheet)
|
|
return CssSheetParse(*test_sheet, source, "sheet.css", TestImports)
|
|
}
|
|
|
|
Env :: (theme: string) -> CssEnvironment {
|
|
return CssEnvironmentWithNames(CssDefaultEnvironment(), theme, "", "", "", "")
|
|
}
|
|
|
|
// Parse and build for `env`; false on either failure.
|
|
BuildIn :: (source: string, env: CssEnvironment) -> bool {
|
|
if !Parse(source) { return false }
|
|
return CssSheetBuild(*test_sheet, env, null, *test_rules)
|
|
}
|
|
|
|
Build :: (source: string) -> bool {
|
|
return BuildIn(source, CssDefaultEnvironment())
|
|
}
|
|
|
|
// The sheet must fail to parse, saying `fragment`.
|
|
ParseFails :: (source: string, fragment: string, label: string) {
|
|
if Parse(source) {
|
|
Check(false, label)
|
|
return
|
|
}
|
|
message: string = Diagnostic()
|
|
if !Contains(message, fragment) {
|
|
TestSay(" diagnostic: ")
|
|
TestSay(message)
|
|
TestSay("\n")
|
|
Check(false, label)
|
|
}
|
|
}
|
|
|
|
// The sheet parses but must fail to build, saying `fragment`.
|
|
BuildFails :: (source: string, fragment: string, label: string) {
|
|
if !Parse(source) {
|
|
TestSay(" diagnostic: ")
|
|
TestSay(Diagnostic())
|
|
TestSay("\n")
|
|
Check(false, label)
|
|
return
|
|
}
|
|
if CssSheetBuild(*test_sheet, CssDefaultEnvironment(), null, *test_rules) {
|
|
Check(false, label)
|
|
return
|
|
}
|
|
message: string = Diagnostic()
|
|
if !Contains(message, fragment) {
|
|
TestSay(" diagnostic: ")
|
|
TestSay(message)
|
|
TestSay("\n")
|
|
Check(false, label)
|
|
}
|
|
}
|
|
|
|
// Like Build, printing the diagnostic when it fails.
|
|
Builds :: (source: string, label: string) -> bool {
|
|
if Build(source) { return true }
|
|
TestSay(" diagnostic: ")
|
|
TestSay(Diagnostic())
|
|
TestSay("\n")
|
|
Check(false, label)
|
|
return false
|
|
}
|
|
|
|
Facts :: (kind: s32, class_name: s32) -> StyleFacts {
|
|
facts: StyleFacts = StyleDefaultFacts(kind)
|
|
facts.class_name = class_name
|
|
return facts
|
|
}
|
|
|
|
Resolve :: (facts: StyleFacts, state: s32) -> StyleData {
|
|
base: StyleData
|
|
return ResolveStyleRules(test_rules, base, facts, state)
|
|
}
|
|
|
|
// Geometry and color assertions shared by the behavior tests and the TSX
|
|
// fixture.
|
|
ColorIs :: (color: Color, value: u32) -> bool {
|
|
return color.r == cast(u8)(value >> cast(u32)24) &&
|
|
color.g == cast(u8)(value >> cast(u32)16) &&
|
|
color.b == cast(u8)(value >> cast(u32)8) && color.a == cast(u8)value
|
|
}
|
|
|
|
Near :: (bounds: Rectangle, x: float32, y: float32, width: float32, height: float32) -> bool {
|
|
return bounds.x == x && bounds.y == y && bounds.width == width && bounds.height == height
|
|
}
|
|
|
|
// The foreground the context selectors resolve for `class_name` right now,
|
|
// against the containers the fixture holds open.
|
|
ContextColor :: (kind: s32, class_name: string, state: s32) -> u32 {
|
|
facts := StyleDefaultFacts(kind)
|
|
if class_name != "" { facts.class_name = StyleClass(class_name) }
|
|
return ResolveInstalledStyleRules(StyleData.{}, facts, state).foreground
|
|
}
|
|
|
|
// Context-selector stages the TSX fixture observes while its containers are
|
|
// open; every line is the original behavior assertion, unchanged.
|
|
CheckContextStage :: (stage: s32) {
|
|
text := StyleKindText()
|
|
any := StyleStateAny()
|
|
if stage == 0 {
|
|
Check(ContextColor(text, "", any) != cast(u32)0x010101ff, "no ancestor outside the panel")
|
|
} else if stage == 1 {
|
|
Check(ContextColor(text, "", any) == cast(u32)0x010101ff, ".panel Text matches inside the panel")
|
|
Check(ContextColor(text, "direct", any) == cast(u32)0x020202ff, ".panel > .direct matches its child")
|
|
Check(ContextColor(text, "lead", any) == cast(u32)0x030303ff, ":first-child before any child")
|
|
} else if stage == 2 {
|
|
Check(ContextColor(text, "lead", any) == cast(u32)0x010101ff, "no first child after a sibling")
|
|
} else if stage == 3 {
|
|
Check(ContextColor(text, "direct", any) == cast(u32)0x010101ff, "a grandchild is a descendant, not a child")
|
|
}
|
|
}
|
|
|
|
// Sibling-selector stages across the fixture's four frames.
|
|
CheckSiblingStage :: (stage: s32) {
|
|
text := StyleKindText()
|
|
any := StyleStateAny()
|
|
if stage == 0 {
|
|
Check(ContextColor(text, "f1", any) == cast(u32)0x101010ff, ":first-child before any child")
|
|
} else if stage == 1 {
|
|
Check(ContextColor(text, "f6", any) == cast(u32)0x606060ff, ":first-of-type before any child")
|
|
} else if stage == 2 {
|
|
Check(ContextColor(text, "f2", any) != cast(u32)0x202020ff, ":nth-child(2) misses 1")
|
|
} else if stage == 3 {
|
|
Check(ContextColor(text, "f4", any) != cast(u32)0x404040ff, "a new container has no committed last child")
|
|
} else if stage == 4 {
|
|
Check(ContextColor(text, "f2", any) == cast(u32)0x202020ff, ":nth-child(2) follows any kind of sibling")
|
|
} else if stage == 5 {
|
|
Check(ContextColor(text, "f6", any) == cast(u32)0x606060ff, "a box is not of the text type")
|
|
} else if stage == 6 {
|
|
Check(ContextColor(text, "f1", any) != cast(u32)0x101010ff, ":first-child misses 2")
|
|
} else if stage == 7 {
|
|
Check(ContextColor(text, "f3", any) == cast(u32)0x303030ff, ":nth-child(odd) at 3")
|
|
} else if stage == 8 {
|
|
Check(ContextColor(text, "f5", any) != cast(u32)0x505050ff, "a new container has no committed only child")
|
|
} else if stage == 9 {
|
|
Check(ContextColor(text, "f6", any) == cast(u32)0x606060ff, ":first-of-type keeps matching the text type")
|
|
} else if stage == 10 {
|
|
Check(ContextColor(text, "f7", any) != cast(u32)0x707070ff, "the committed row holds more than one text")
|
|
} else if stage == 11 {
|
|
Check(ContextColor(text, "f4", any) == cast(u32)0x404040ff, ":last-child converges at the committed count")
|
|
} else if stage == 12 {
|
|
Check(ContextColor(text, "f7", any) == cast(u32)0x707070ff, ":last-of-type converges at the committed count")
|
|
} else if stage == 13 {
|
|
Check(ContextColor(text, "f5", any) == cast(u32)0x505050ff, ":only-child converges on the committed row")
|
|
} else if stage == 14 {
|
|
Check(ContextColor(text, "f8", any) == cast(u32)0x808080ff, ":only-of-type converges on the committed row")
|
|
} else if stage == 15 {
|
|
Check(ContextColor(text, "f4", any) == cast(u32)0x404040ff, "the stale committed count still converges the last position")
|
|
} else if stage == 16 {
|
|
Check(ContextColor(text, "f5", any) != cast(u32)0x505050ff, "two children are not an only child")
|
|
} else if stage == 17 {
|
|
Check(ContextColor(text, "f4", any) == cast(u32)0x404040ff, "the shorter row's last position converges")
|
|
} else if stage == 18 {
|
|
Check(ContextColor(text, "f4", any) != cast(u32)0x404040ff, "a position past the committed row waits one frame")
|
|
}
|
|
}
|