using Febe.Tests.Desktop.Support; using Febe.Tests.Web.Support; namespace Febe.Tests.Web; /// /// The modules of the interface load: a mistake that stops one module from parsing breaks every page importing it, and /// the Chromium tests that would see it run only in the CI. reads them without a browser. /// [TestClass] public sealed class ScriptSyntaxTests { [TestMethod] public void EveryModuleOfTheInterfaceReadsWithoutTheMistakesThatStopItLoading() { var root = Repository.PathOf("src", "Febe.Web", "wwwroot"); var files = Directory.GetFiles(root, "*.js", SearchOption.AllDirectories); Assert.IsGreaterThan(30, files.Length, "the modules of the interface"); var problems = new List(); foreach (var file in files) { var name = Path.GetRelativePath(root, file); problems.AddRange(ScriptCheck.Problems(File.ReadAllText(file)).Select(p => $"{name}, {p}")); } Assert.IsEmpty(problems, string.Join(Environment.NewLine, problems)); } [TestMethod] public void EveryStaticImportNamesSomethingTheOtherModuleExports() { var root = Repository.PathOf("src", "Febe.Web", "wwwroot"); var problems = new List(); var checkedNames = 0; foreach (var file in Directory.GetFiles(root, "*.js", SearchOption.AllDirectories)) { foreach (var (from, names, line) in ScriptCheck.Imports(File.ReadAllText(file))) { if (!from.StartsWith('.')) { continue; } var target = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(file)!, from)); var where = $"{Path.GetRelativePath(root, file)}, riga {line}"; if (!File.Exists(target)) { problems.Add($"{where}: «{from}» non esiste"); continue; } var exports = ScriptCheck.Exports(File.ReadAllText(target)); foreach (var name in names) { checkedNames++; if (!exports.Contains(name)) { problems.Add($"{where}: «{from}» non esporta «{name}»"); } } } } Assert.IsGreaterThan(50, checkedNames); Assert.IsEmpty(problems, string.Join(Environment.NewLine, problems)); } [TestMethod] [DataRow("export function box(saved, page) {\n if (!saved) return null;\n let saved = null;\n}", "«saved» dichiarato di nuovo con let (già parametro", DisplayName = "parametro ridichiarato (connection.js, b29b35a)")] [DataRow("const show = (value) => { const value = 1; };", "«value» dichiarato di nuovo", DisplayName = "parametro di una freccia")] [DataRow("items.map(x => { let x = 2; return x; });", "«x» dichiarato di nuovo", DisplayName = "parametro senza parentesi")] [DataRow("function f() { let a = 1; if (a) { let b; } const a = 2; }", "«a» dichiarato di nuovo con const", DisplayName = "due volte nello stesso blocco")] [DataRow("import { h } from './dom.js';\nconst h = 1;", "«h» dichiarato di nuovo con const (già import", DisplayName = "nome già importato")] [DataRow("class A { m(p) { class p {} } }", "«p» dichiarato di nuovo con class", DisplayName = "classe con il nome di un parametro")] [DataRow("function f() { if (x) { return 1; }", "mai chiusa", DisplayName = "parentesi graffa mai chiusa")] [DataRow("const a = [1, 2);", "chiude «[»", DisplayName = "parentesi che non corrispondono")] [DataRow("const s = 'non chiusa;\n", "stringa non chiusa", DisplayName = "stringa non chiusa")] [DataRow("const s = `a ${b} c;\n", "modello fra apici inversi non chiuso", DisplayName = "modello non chiuso")] public void TheCheckFindsTheMistakesThatStopAModuleLoading(string source, string expected) { var problems = ScriptCheck.Problems(source); Assert.IsTrue(problems.Any(p => p.Contains(expected, StringComparison.Ordinal)), string.Join(" | ", problems.DefaultIfEmpty("(nessuno)"))); } [TestMethod] [DataRow("export function box(saved, page) {\n let stored = null;\n const show = (value) => { stored = value; };\n}", DisplayName = "la correzione di connection.js")] [DataRow("for (let i = 0; i < 3; i++) { let i2 = i; }\nfor (let i = 0; i < 3; i++) { const j = i; }", DisplayName = "variabili dei cicli")] [DataRow("const re = /a{2}[/}]\\//g; const half = 4 / 2; const t = `x ${ { a: 1 }.a } ${`y ${1}`}`;", DisplayName = "espressioni regolari, divisioni e modelli annidati")] [DataRow("function f(a) { { let a = 1; } if (a) { const a = 2; } }", DisplayName = "lo stesso nome in un blocco interno")] [DataRow("const o = { let: 1, const: 2 }; o.let = o.const;", DisplayName = "proprietà con nomi di parole chiave")] [DataRow("try { x(); } catch (error) { console.log(error); }\nconst error = 1;", DisplayName = "parametro di catch e nome fuori")] [DataRow("// a comment with ' and ` and {\n/* and } */ const a = \"}\";", DisplayName = "commenti e stringhe con parentesi")] public void TheCheckLeavesValidCodeAlone(string source) { var problems = ScriptCheck.Problems(source); Assert.IsEmpty(problems, string.Join(" | ", problems)); } [TestMethod] public void ImportsAndExportsAreReadByTheirNames() { var exports = ScriptCheck.Exports("export function a() {}\nexport async function b() {}\nexport const c = 1;\nexport class D {}\nconst e = 1, f = 2;\nexport { e, f as g };\nexport default a;"); CollectionAssert.AreEquivalent(new[] { "a", "b", "c", "D", "e", "g", "default" }, exports.ToArray()); var imports = ScriptCheck.Imports("import { a, b as c } from './x.js';\nimport * as y from './y.js';\nconst z = await import('./z.js');"); Assert.HasCount(2, imports); Assert.AreEqual("./x.js", imports[0].From); CollectionAssert.AreEqual(new[] { "a", "b" }, imports[0].Names); Assert.IsEmpty(imports[1].Names); } }