Debian Codegraph @

external/nuget-buildtasks/src/Microsoft.NuGet.Build.Tasks.Tests/PreprocessorTests.cs

raw symbols SCIP
1 // Copyright (c) .NET Foundation. All rights reserved.
2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using Microsoft.NuGet.Build.Tasks;
11 using Xunit;
12
13 namespace Microsoft.NuGet.Build.Tasks.Tests
14 {
15 public class PreprocessorTests
16 {
17 private string Preprocess(string text, IReadOnlyDictionary<string, string> values)
18 {
19 var writer = new StringWriter();
20 Preprocessor.Preprocess(new StringReader(text), writer, values);
21 return writer.ToString();
22 }
23
24 [Fact]
25 public void EmptyStringYieldsEmptyString()
26 {
27 Assert.Equal("", Preprocess("", new Dictionary<string, string>()));
28 }
29
30 [Fact]
31 public void NonEmptyStringWithNoTokensYieldsIdentity()
32 {
33 Assert.Equal("Hello, World!", Preprocess("Hello, World!", new Dictionary<string, string>()));
34 }
35
36 [Fact]
37 public void NonEmptyStringWithNoTokensYieldsIdentityWithValues()
38 {
39 Assert.Equal("Hello, World!", Preprocess("Hello, World!", new Dictionary<string, string>() { { "CompanyName", "Microsoft" } }));
40 }
41
42 [Fact]
43 public void DoubleDollarIsEscape()
44 {
45 Assert.Equal("Pay Jason $42", Preprocess("Pay Jason $$42", new Dictionary<string, string>()));
46 }
47
48 [Fact]
49 public void ReplaceWithSimpleValue()
50 {
51 Assert.Equal("Microsoft", Preprocess("$CompanyName$", new Dictionary<string, string>() { { "CompanyName", "Microsoft" } }));
52 }
53
54 [Fact]
55 public void UnterminatedIsNotAToken()
56 {
57 Assert.Equal("$CompanyName", Preprocess("$CompanyName", new Dictionary<string, string>() { { "CompanyName", "Microsoft" } }));
58 }
59
60 [Fact]
61 public void SimpleDollarIsTrivial()
62 {
63 Assert.Equal("$", Preprocess("$", new Dictionary<string, string>() { }));
64 }
65
66 [Fact]
67 public void UnterminatedIsNotAToken2()
68 {
69 Assert.Equal("$CompanyName $CompanyName", Preprocess("$CompanyName $CompanyName", new Dictionary<string, string>() { { "CompanyName", "Microsoft" } }));
70 }
71
72 [Fact]
73 public void ReplaceWithTwoValues()
74 {
75 Assert.Equal("Microsoft", Preprocess("$CompanyName1$$CompanyName2$", new Dictionary<string, string>() { { "CompanyName1", "Micro" }, { "CompanyName2", "soft" } }));
76 }
77
78 [Fact]
79 public void ReplaceWithSimpleValueTwice()
80 {
81 Assert.Equal("MicrosoftMicrosoft", Preprocess("$CompanyName$$CompanyName$", new Dictionary<string, string>() { { "CompanyName", "Microsoft" } }));
82 }
83
84 [Fact]
85 public void ReplaceWithOtherTextBeforeAndAfter()
86 {
87 Assert.Equal(" Microsoft ", Preprocess(" $CompanyName$ ", new Dictionary<string, string>() { { "CompanyName", "Microsoft" } }));
88 }
89
90 [Fact]
91 public void ReplaceWithInvalidTokenThrows()
92 {
93 Assert.Throws<ExceptionFromResource>(() => Preprocess("$UnknownToken$", new Dictionary<string, string>()));
94 }
95 }
96 }