Example #1
0
    public void Compile_I18nTextTypedClassWasGenerated_Test(string langCode, bool disableSubNameSpace)
    {
        using var workSpace = new WorkSpace();

        var srcFiles = "*.json;*.csv".Split(';')
                       .SelectMany(pattern => Directory.GetFiles(workSpace.I18nTextDir, pattern, SearchOption.AllDirectories))
                       .Select(path => new I18nTextSourceFile(path, Encoding.UTF8));
        var options = new I18nTextCompilerOptions(workSpace.ProjectDir)
        {
            FallBackLanguage    = langCode,
            OutDirectory        = workSpace.TextResJsonsDir,
            DisableSubNameSpace = disableSubNameSpace
        };
        var compiler = new I18nTextCompiler();
        var success  = compiler.Compile(srcFiles, options);

        success.IsTrue();

        // Compiled an i18n text type file should exist.
        const string nameSpace = "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.";
        var          fooBarClass = nameSpace + "Foo.Bar";
        var          fizzBuzzClass = nameSpace + (disableSubNameSpace ? "Buzz" : "Fizz.Buzz");
        var          csFileNames = new[] { fooBarClass + ".cs", fizzBuzzClass + ".cs" }.OrderBy(n => n);

        Directory.Exists(workSpace.TypesDir).IsTrue();
        Directory.GetFiles(workSpace.TypesDir)
        .Select(path => Path.GetFileName(path))
        .OrderBy(n => n)
        .Is(csFileNames);

        // the i18n text type file should be valid C# code.
        this.ValidateGeneratedCSharpCode(workSpace, langCode, "l47c0gpbnx", fooBarClass + ".cs", fooBarClass, new[] { "HelloWorld", "Exit", "GreetingOfJA" });
        this.ValidateGeneratedCSharpCode(workSpace, langCode, "o246f7as05", fizzBuzzClass + ".cs", fizzBuzzClass, new[] { "Text1", "Text2" });
    }
        public void Compile_I18nTextJsonFilesWereGenerated_Test()
        {
            var srcFiles = "*.json;*.csv".Split(';')
                           .SelectMany(pattern => Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "i18ntext"), pattern, SearchOption.AllDirectories))
                           .Select(path => new I18nTextSourceFile(path, Encoding.UTF8));
            var options  = new I18nTextCompilerOptions();
            var compiler = new I18nTextCompiler();
            var success  = compiler.Compile(srcFiles, options);

            success.IsTrue();

            // Compiled i18n text json files should exist.
            var jsonDir = Path.Combine(_WwwRootDir, "content", "i18ntext");

            Directory.Exists(jsonDir).IsTrue();
            Directory.GetFiles(jsonDir)
            .Select(path => Path.GetFileName(path))
            .OrderBy(name => name)
            .Is("Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.en.json",
                "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.ja.json");

            var enJsonText = File.ReadAllText(Path.Combine(jsonDir, "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.en.json"));
            var enTexts    = JsonConvert.DeserializeObject <Dictionary <string, string> >(enJsonText);

            enTexts["HelloWorld"].Is("Hello World!");
            enTexts["Exit"].Is("Exit");
            enTexts["GreetingOfJA"].Is("こんにちは");

            var jaJsonText = File.ReadAllText(Path.Combine(jsonDir, "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.ja.json"));
            var jaTexts    = JsonConvert.DeserializeObject <Dictionary <string, string> >(jaJsonText);

            jaTexts["HelloWorld"].Is("こんにちは世界!");
            jaTexts["Exit"].Is("Exit");
            jaTexts["GreetingOfJA"].Is("こんにちは");
        }
Example #3
0
    public void Compile_SweepTextJsonFiles_Test()
    {
        using var workSpace = new WorkSpace();

        if (Directory.Exists(workSpace.TextResJsonsDir))
        {
            Directory.Delete(workSpace.TextResJsonsDir, recursive: true);
        }
        Directory.CreateDirectory(workSpace.TextResJsonsDir);
        File.WriteAllLines(Path.Combine(workSpace.TextResJsonsDir, "Bar.json"), new[] { "{\"Key\":\"Value\"}" });

        var srcPath  = Path.Combine(workSpace.I18nTextDir, "Foo.Bar.en.json");
        var srcFiles = new[] { new I18nTextSourceFile(srcPath, Encoding.UTF8) };
        var options  = new I18nTextCompilerOptions(workSpace.ProjectDir)
        {
            OutDirectory = workSpace.TextResJsonsDir
        };
        var compiler = new I18nTextCompiler();
        var suceess  = compiler.Compile(srcFiles, options);

        suceess.IsTrue();

        // "Bar.json" should be sweeped.
        Directory.GetFiles(workSpace.TextResJsonsDir)
        .Select(path => Path.GetFileName(path))
        .OrderBy(name => name)
        .Is("Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.en.json");
    }
Example #4
0
        public void Compile_SweepTypeFiles_Test()
        {
            if (Directory.Exists(_TypesDir))
            {
                Directory.Delete(_TypesDir, recursive: true);
            }
            Directory.CreateDirectory(_TypesDir);
            File.WriteAllLines(Path.Combine(_TypesDir, "Bar.cs"), new[] { "// <auto-generated by=\"the Blazor I18n Text compiler\" />" });
            File.WriteAllLines(Path.Combine(_TypesDir, "Bar.cs.bak"), new[] { "// <auto-generated by=\"the Blazor I18n Text compiler\" />" });
            File.WriteAllLines(Path.Combine(_TypesDir, "Fizz.cs"), new[] { "public class Fizz {}" });

            var srcPath  = Path.Combine(Environment.CurrentDirectory, "i18ntext", "Foo.Bar.en.json");
            var srcFiles = new[] { new I18nTextSourceFile(srcPath, Encoding.UTF8) };
            var options  = new I18nTextCompilerOptions {
                OutDirectory = _TextResJsonsDir
            };
            var compiler = new I18nTextCompiler();
            var suceess  = compiler.Compile(srcFiles, options);

            suceess.IsTrue();

            Directory.GetFiles(_TypesDir)
            .Select(path => Path.GetFileName(path))
            .OrderBy(name => name)
            .Is(
                // "Bar.cs" should be sweeped.
                "Bar.cs.bak",
                "Fizz.cs",
                "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.cs");

            File.ReadLines(Path.Combine(_TypesDir, "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.cs"))
            .FirstOrDefault()
            .Is("// <auto-generated by=\"the Blazor I18n Text compiler\" />");
        }
        public void Compile_NoSrcFiles_Test()
        {
            var options  = new I18nTextCompilerOptions();
            var compiler = new I18nTextCompiler();
            var success  = compiler.Compile(Enumerable.Empty <I18nTextSourceFile>(), options);

            success.IsTrue();
            Directory.Exists(_TypesDir).IsFalse();
            Directory.Exists(_WwwRootDir).IsFalse();
        }
Example #6
0
    public void Compile_NoSrcFiles_Test()
    {
        using var workSpace = new WorkSpace();
        var options  = new I18nTextCompilerOptions(workSpace.ProjectDir);
        var compiler = new I18nTextCompiler();
        var success  = compiler.Compile(Enumerable.Empty <I18nTextSourceFile>(), options);

        success.IsTrue();
        Directory.Exists(workSpace.TypesDir).IsFalse();
        Directory.Exists(workSpace.TextResJsonsDir).IsFalse();
    }
        public void Compile_Error_FallbackLangNotExist()
        {
            var srcFiles = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "i18ntext"), "*.json", SearchOption.AllDirectories)
                           .Select(path => new I18nTextSourceFile(path, Encoding.UTF8));
            var logErr  = new List <string>();
            var options = new I18nTextCompilerOptions
            {
                FallBackLanguage = "fr",
                LogError         = msg => logErr.Add(msg)
            };
            var compiler = new I18nTextCompiler();
            var suceess  = compiler.Compile(srcFiles, options);

            suceess.IsFalse();
            logErr.Is("IN1001: Could not find an I18n source text file of fallback language 'fr', for 'Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar'.");
        }
Example #8
0
    public void Compile_I18nTextJsonFilesWereGenerated_Test(bool disableSubNameSpace)
    {
        using var workSpace = new WorkSpace();

        var srcFiles = "*.json;*.csv".Split(';')
                       .SelectMany(pattern => Directory.GetFiles(workSpace.I18nTextDir, pattern, SearchOption.AllDirectories))
                       .Select(path => new I18nTextSourceFile(path, Encoding.UTF8));
        var options = new I18nTextCompilerOptions(workSpace.ProjectDir)
        {
            OutDirectory        = workSpace.TextResJsonsDir,
            DisableSubNameSpace = disableSubNameSpace
        };
        var compiler = new I18nTextCompiler();
        var success  = compiler.Compile(srcFiles, options);

        success.IsTrue();

        // Compiled i18n text json files should exist.
        Directory.Exists(workSpace.TextResJsonsDir).IsTrue();
        Directory.GetFiles(workSpace.TextResJsonsDir)
        .Select(path => Path.GetFileName(path))
        .OrderBy(name => name)
        .Is(new[] {
            $"Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.{(disableSubNameSpace ? "" : "Fizz.")}Buzz.en.json",
            $"Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.{(disableSubNameSpace ? "" : "Fizz.")}Buzz.ja.json",
            $"Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.en.json",
            $"Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.ja.json"
        }.OrderBy(n => n));

        var enJsonText = File.ReadAllText(Path.Combine(workSpace.TextResJsonsDir, "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.en.json"));
        var enTexts    = JsonConvert.DeserializeObject <Dictionary <string, string> >(enJsonText) ?? new();

        enTexts["HelloWorld"].Is("Hello World!");
        enTexts["Exit"].Is("Exit");
        enTexts["GreetingOfJA"].Is("こんにちは");

        var jaJsonText = File.ReadAllText(Path.Combine(workSpace.TextResJsonsDir, "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.ja.json"));
        var jaTexts    = JsonConvert.DeserializeObject <Dictionary <string, string> >(jaJsonText) ?? new();

        jaTexts["HelloWorld"].Is("こんにちは世界!");
        jaTexts["Exit"].Is("Exit");
        jaTexts["GreetingOfJA"].Is("こんにちは");
    }
Example #9
0
    public void Compile_Error_FallbackLangNotExist()
    {
        using var workSpace = new WorkSpace();

        var srcFiles = Directory.GetFiles(workSpace.I18nTextDir, "*.json", SearchOption.AllDirectories)
                       .Select(path => new I18nTextSourceFile(path, Encoding.UTF8));
        var logErrors = new List <string>();
        var options   = new I18nTextCompilerOptions(workSpace.ProjectDir)
        {
            FallBackLanguage = "fr",
            LogError         = e => logErrors.Add(e.Message),
            OutDirectory     = workSpace.TextResJsonsDir
        };
        var compiler = new I18nTextCompiler();
        var suceess  = compiler.Compile(srcFiles, options);

        suceess.IsFalse();
        logErrors.Count.Is(1);
        logErrors.First()
        .StartsWith("Could not find an I18n source text file of fallback language 'fr', for 'Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.")
        .IsTrue();
    }
        public void Compile_I18nTextTypedClassWasGenerated_Test(string langCode)
        {
            var srcFiles = "*.json;*.csv".Split(';')
                           .SelectMany(pattern => Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "i18ntext"), pattern, SearchOption.AllDirectories))
                           .Select(path => new I18nTextSourceFile(path, Encoding.UTF8));
            var options = new I18nTextCompilerOptions {
                FallBackLanguage = langCode
            };
            var compiler = new I18nTextCompiler();
            var success  = compiler.Compile(srcFiles, options);

            success.IsTrue();

            // Compiled an i18n text type file should exist.
            var csFileName = "Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar.cs";

            Directory.Exists(_TypesDir).IsTrue();
            Directory.GetFiles(_TypesDir).Select(path => Path.GetFileName(path)).Is(csFileName);

            // the i18n text type file should be valid C# code.
            var typeCode     = File.ReadAllText(Path.Combine(_TypesDir, csFileName));
            var syntaxTree   = CSharpSyntaxTree.ParseText(typeCode);
            var assemblyName = Path.GetRandomFileName();
            var references   = new[] {
                MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location),
                MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").Location),
                MetadataReference.CreateFromFile(typeof(I18nTextFallbackLanguage).GetTypeInfo().Assembly.Location),
            };
            var compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            var compiledType = default(Type);

            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms);
                result.Success.IsTrue();

                ms.Seek(0, SeekOrigin.Begin);
                var assembly = AssemblyLoadContext.Default.LoadFromStream(ms);
                compiledType = assembly.GetType("Toolbelt.Blazor.I18nTextCompileTask.Test.I18nText.Foo.Bar");
            }

            // the i18n text type file should contain the i18n text typed public class.
            compiledType.IsNotNull();
            compiledType.IsClass.IsTrue();
            compiledType.IsPublic.IsTrue();

            // the i18n text typed class has fileds that are combined all languages files.
            var fields = compiledType.GetFields(BindingFlags.Instance | BindingFlags.Public);

            fields.Where(f => f.FieldType == typeof(string)).Any(f => f.Name == "HelloWorld").IsTrue();
            fields.Where(f => f.FieldType == typeof(string)).Any(f => f.Name == "Exit").IsTrue();
            fields.Where(f => f.FieldType == typeof(string)).Any(f => f.Name == "GreetingOfJA").IsTrue();

            var textTableObj = Activator.CreateInstance(compiledType);

            textTableObj.IsNotNull();
            (textTableObj as I18nTextFallbackLanguage).FallBackLanguage.Is(langCode);
            (textTableObj as I18nTextLateBinding)["HelloWorld"].Is("HelloWorld");
        }