public void Hint_Name_Must_Be_Unique(string hintName1, string hintName2)
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection();

            asc.Add(hintName1, SourceText.From("", Encoding.UTF8));
            Assert.Throws <ArgumentException>("hintName", () => asc.Add(hintName2, SourceText.From("", Encoding.UTF8)));
        }
        public void AddedSources_Are_Deterministic()
        {
            // a few manual simple ones
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection(".cs");

            asc.Add("file3.cs", SourceText.From("", Encoding.UTF8));
            asc.Add("file1.cs", SourceText.From("", Encoding.UTF8));
            asc.Add("file2.cs", SourceText.From("", Encoding.UTF8));
            asc.Add("file5.cs", SourceText.From("", Encoding.UTF8));
            asc.Add("file4.cs", SourceText.From("", Encoding.UTF8));

            var sources   = asc.ToImmutableAndFree();
            var hintNames = sources.Select(s => s.HintName).ToArray();

            Assert.Equal(
                new[] { "file3.cs", "file1.cs", "file2.cs", "file5.cs", "file4.cs" },
                hintNames
                );

            // generate a long random list, remembering the order we added them
            Random r = new Random();

            string[] names = new string[1000];
            asc = new AdditionalSourcesCollection(".cs");
            for (int i = 0; i < 1000; i++)
            {
                names[i] = CSharpTestBase.GetUniqueName() + ".cs";
                asc.Add(names[i], SourceText.From("", Encoding.UTF8));
            }

            sources   = asc.ToImmutableAndFree();
            hintNames = sources.Select(s => s.HintName).ToArray();
            Assert.Equal(names, hintNames);
        }
        public void SourceTextRequiresEncoding()
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection();

            // fine
            asc.Add("file1.cs", SourceText.From("", Encoding.UTF8));
            asc.Add("file2.cs", SourceText.From("", Encoding.UTF32));
            asc.Add("file3.cs", SourceText.From("", Encoding.Unicode));

            // no encoding
            Assert.Throws <ArgumentException>(() => asc.Add("file4.cs", SourceText.From("")));

            // explicit null encoding
            Assert.Throws <ArgumentException>(() => asc.Add("file5.cs", SourceText.From("", encoding: null)));
        }
Exemple #4
0
        public void Contains(string addHintName, string checkHintName)
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection(".cs");

            asc.Add(addHintName, SourceText.From("", Encoding.UTF8));
            Assert.True(asc.Contains(checkHintName));
        }
Exemple #5
0
        public void Hint_Name_Must_Be_Unique_When_Combining_Sources()
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection(".cs");

            asc.Add("hintName1", SourceText.From("", Encoding.UTF8));
            asc.Add("hintName2", SourceText.From("", Encoding.UTF8));

            AdditionalSourcesCollection asc2 = new AdditionalSourcesCollection(".cs");

            asc2.Add("hintName3", SourceText.From("", Encoding.UTF8));
            asc2.Add("hintName1", SourceText.From("", Encoding.UTF8));

            var exception = Assert.Throws <ArgumentException>("hintName", () => asc.CopyTo(asc2));

            Assert.Contains("hintName1", exception.Message);
        }
        [InlineData("abc\u00A0.cs")] // unicode non-breaking space
        public void HintName_InvalidValues(string hintName)
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection(".cs");

            Assert.Throws <ArgumentException>(
                nameof(hintName),
                () => asc.Add(hintName, SourceText.From("public class D{}", Encoding.UTF8))
                );
        }
Exemple #7
0
        public void Remove(string addHintName, string removeHintName)
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection(".cs");

            asc.Add(addHintName, SourceText.From("", Encoding.UTF8));
            asc.RemoveSource(removeHintName);
            var sources = asc.ToImmutableAndFree();

            Assert.Empty(sources);
        }
        public void HintName_ValidValues(string hintName)
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection();

            asc.Add(hintName, SourceText.From("public class D{}", Encoding.UTF8));
            Assert.True(asc.Contains(hintName));

            var sources = asc.ToImmutableAndFree();

            Assert.True(sources[0].HintName.EndsWith(".cs"));
        }
Exemple #9
0
        public void SourceTextRequiresEncoding()
        {
            AdditionalSourcesCollection asc = new AdditionalSourcesCollection(".cs");

            // fine
            asc.Add("file1.cs", SourceText.From("", Encoding.UTF8));
            asc.Add("file2.cs", SourceText.From("", Encoding.UTF32));
            asc.Add("file3.cs", SourceText.From("", Encoding.Unicode));

            // no encoding
            Assert.Throws <ArgumentException>(() => asc.Add("file4.cs", SourceText.From("")));

            // explicit null encoding
            Assert.Throws <ArgumentException>(() => asc.Add("file5.cs", SourceText.From("", encoding: null)));

            var exception = Assert.Throws <ArgumentException>(() => asc.Add("file5.cs", SourceText.From("", encoding: null)));

            // check the exception contains the expected hintName
            Assert.Contains("file5.cs", exception.Message);
        }
 private void AddSourceForAdditionalFile(AdditionalSourcesCollection sources, AdditionalText file) => sources.Add(GetGeneratedFileName(file.Path), SourceText.From("", Encoding.UTF8));
Exemple #11
0
 /// <summary>
 /// Adds a <see cref="SourceText"/> to the compilation
 /// </summary>
 /// <param name="hintName">An identifier that can be used to reference this source text, must be unique within this generator</param>
 /// <param name="sourceText">The <see cref="SourceText"/> to add to the compilation</param>
 public void AddSource(string hintName, SourceText sourceText) => Sources.Add(hintName, sourceText);