main
cs 82 lines 4.59 KB
Raw
1 // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3 namespace WixToolsetTest.Core
4 {
5 using System.Collections.Generic;
6 using System.Linq;
7 using WixInternal.TestSupport;
8 using WixToolset.Core;
9 using WixToolset.Data;
10 using WixToolset.Data.Bind;
11 using WixToolset.Extensibility.Services;
12 using Xunit;
13
14 public class VariableResolverFixture
15 {
16 [Fact]
17 public void CanRecursivelyResolveVariables()
18 {
19 var serviceProvider = WixToolsetServiceProviderFactory.CreateServiceProvider();
20 var variableResolver = serviceProvider.GetService<IVariableResolver>();
21
22 var variables = new BindVariable[]
23 {
24 new() { Id = "ProductName", Value = "Localized Product Name" },
25 new() { Id = "ProductNameEdition", Value = "!(loc.ProductName) Enterprise Edition" },
26 new() { Id = "ProductNameEditionVersion", Value = "!(loc.ProductNameEdition) v1.2.3" },
27 new() { Id = "Dotted.Loc.Variable", Value = "Dotted.Loc.Variable = !(loc.ProductNameEditionVersion)" },
28 new() { Id = "NestedDotted.Loc.Variable", Value = "!(loc.Dotted.Loc.Variable) worked" },
29 }.ToDictionary(b => b.Id);
30
31 var localization = new Localization(0, null, "x-none", variables, new Dictionary<string, LocalizedControl>());
32
33 variableResolver.AddLocalization(localization);
34
35 var result = variableResolver.ResolveVariables(null, "These are not the loc strings you're looking for.");
36 WixAssert.StringEqual("These are not the loc strings you're looking for.", result.Value);
37 Assert.False(result.UpdatedValue);
38
39 result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductName)");
40 WixAssert.StringEqual("Welcome to Localized Product Name", result.Value);
41 Assert.True(result.UpdatedValue);
42
43 result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEdition)");
44 WixAssert.StringEqual("Welcome to Localized Product Name Enterprise Edition", result.Value);
45 Assert.True(result.UpdatedValue);
46
47 result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEditionVersion)");
48 WixAssert.StringEqual("Welcome to Localized Product Name Enterprise Edition v1.2.3", result.Value);
49 Assert.True(result.UpdatedValue);
50
51 result = variableResolver.ResolveVariables(null, "start !(loc.NestedDotted.Loc.Variable) end");
52 WixAssert.StringEqual("start Dotted.Loc.Variable = Localized Product Name Enterprise Edition v1.2.3 worked end", result.Value);
53 Assert.True(result.UpdatedValue);
54
55 result = variableResolver.ResolveVariables(null, "Welcome to !(bind.property.ProductVersion)");
56 WixAssert.StringEqual("Welcome to !(bind.property.ProductVersion)", result.Value);
57 Assert.False(result.UpdatedValue);
58 Assert.True(result.DelayedResolve);
59
60 var withUnknownLocString = "Welcome to !(loc.UnknownLocalizationVariable)";
61 Assert.Throws<WixException>(() => variableResolver.ResolveVariables(null, withUnknownLocString));
62
63 result = variableResolver.ResolveVariables(null, withUnknownLocString, errorOnUnknown: false);
64 WixAssert.StringEqual(withUnknownLocString, result.Value);
65 Assert.False(result.UpdatedValue);
66
67 result = variableResolver.ResolveVariables(null, "Welcome to !!(loc.UnknownLocalizationVariable)");
68 WixAssert.StringEqual("Welcome to !(loc.UnknownLocalizationVariable)", result.Value);
69 Assert.True(result.UpdatedValue);
70
71 result = variableResolver.ResolveVariables(null, "Welcome to !!(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)");
72 WixAssert.StringEqual("Welcome to !(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)", result.Value);
73 Assert.True(result.UpdatedValue);
74 Assert.True(result.DelayedResolve);
75
76 result = variableResolver.ResolveVariables(null, "Welcome to !(loc.ProductNameEditionVersion) !!(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)");
77 WixAssert.StringEqual("Welcome to Localized Product Name Enterprise Edition v1.2.3 !(loc.UnknownLocalizationVariable) v!(bind.property.ProductVersion)", result.Value);
78 Assert.True(result.UpdatedValue);
79 Assert.True(result.DelayedResolve);
80 }
81 }
82 }