extends TestCase ## A1: every colour the client draws with comes from Palette (docs/STYLE.md). ## Scripts allowed literal Color() values, and why. const ALLOWED: Dictionary = { "res://src/client/style/palette.gd": "the palette itself", "res://src/client/terrain/terrain_chunk_builder.gd": "splat weights carried in vertex colours, not colours", } ## A Color() made only of number literals, or a named engine colour other than the ## neutral ones used as multipliers. const LITERAL: String = "Color\\(\\s*-?[0-9.]+\\s*,\\s*-?[0-9.]+\\s*,\\s*-?[0-9.]+\\s*(,\\s*-?[0-9.]+\\s*)?\\)" const NAMED: String = "Color\\.(?!WHITE\\b|BLACK\\b|TRANSPARENT\\b)[A-Z_]+\\b|Color8\\(|Color\\.html\\(" func test_client_colours_come_from_the_palette() -> void: var literal: RegEx = RegEx.create_from_string(LITERAL) var named: RegEx = RegEx.create_from_string(NAMED) var paths: Array[String] = _scripts_under("res://src/client") paths.append_array(_scripts_under("res://src/app")) paths.append_array(_scripts_under("res://src/ui")) assert_true(paths.size() > 10, "expected client scripts") for path: String in paths: if ALLOWED.has(path): continue for line: String in FileAccess.get_file_as_string(path).split("\n"): var code: String = line.get_slice("#", 0) if literal.search(code) != null or named.search(code) != null: fail("%s has a colour outside Palette: %s" % [path, code.strip_edges()]) func test_scanner_catches_literals_and_named_colours() -> void: var literal: RegEx = RegEx.create_from_string(LITERAL) var named: RegEx = RegEx.create_from_string(NAMED) assert_not_null(literal.search("var c: Color = Color(0.5, 0.2, 0.1)")) assert_not_null(literal.search("Color(1, 0, 0, 0.5)")) assert_null(literal.search("Color(base.r + shift, 0.0, 1.0)")) assert_not_null(named.search("x = Color.RED")) assert_not_null(named.search("Color.html(\"#fff\")")) assert_null(named.search("tint = Color.WHITE")) func test_palette_matches_the_style_guide() -> void: # Each named colour is listed in docs/STYLE.md, so the guide can't fall behind. var guide: String = FileAccess.get_file_as_string("res://docs/STYLE.md") assert_true(not guide.is_empty(), "docs/STYLE.md should exist") var names: PackedStringArray = PackedStringArray() for constant: String in (Palette as Script).get_script_constant_map(): names.append(constant) assert_true(names.size() > 20, "expected the palette's colours") for constant: String in names: assert_true(guide.contains("`%s`" % constant), "docs/STYLE.md should list `%s`" % constant) func _scripts_under(dir_path: String) -> Array[String]: var found: Array[String] = [] for name: String in DirAccess.get_files_at(dir_path): if name.ends_with(".gd"): found.append(dir_path.path_join(name)) for name: String in DirAccess.get_directories_at(dir_path): found.append_array(_scripts_under(dir_path.path_join(name))) return found