| 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; |
| 6 | using System.Xml.Linq; |
| 7 | using WixInternal.TestSupport; |
| 8 | using WixToolset.Core; |
| 9 | using WixToolset.Data; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | using Xunit; |
| 12 | |
| 13 | public class ParserHelperFixture |
| 14 | { |
| 15 | [Fact] |
| 16 | public void CanParseFourPartAttributeVersion() |
| 17 | { |
| 18 | var helper = GetParserHelper(); |
| 19 | |
| 20 | var attribute = CreateAttribute("1.2.3.4"); |
| 21 | var result = helper.GetAttributeVersionValue(null, attribute); |
| 22 | |
| 23 | WixAssert.StringEqual("1.2.3.4", result); |
| 24 | } |
| 25 | |
| 26 | [Fact] |
| 27 | public void CannotParseFivePartAttributeVersion() |
| 28 | { |
| 29 | var helper = GetParserHelper(); |
| 30 | |
| 31 | var attribute = CreateAttribute("1.2.3.4.5"); |
| 32 | var exception = Assert.Throws<WixException>(() => { helper.GetAttributeVersionValue(null, attribute); }); |
| 33 | WixAssert.StringEqual("The Test/@Value attribute's value, '1.2.3.4.5', is not a valid version. Specify a four-part version or semantic version, such as '#.#.#.#' or '#.#.#-label.#'.", exception.Message); |
| 34 | } |
| 35 | |
| 36 | [Fact] |
| 37 | public void CannotParseVersionTooLargeAttributeVersion() |
| 38 | { |
| 39 | var version = "4294967296.2.3.4"; |
| 40 | AssertVersion(version); |
| 41 | } |
| 42 | |
| 43 | private static void AssertVersion(string version) |
| 44 | { |
| 45 | var helper = GetParserHelper(); |
| 46 | var attribute = CreateAttribute(version); |
| 47 | var exception = Assert.Throws<WixException>(() => { helper.GetAttributeVersionValue(null, attribute); }); |
| 48 | WixAssert.StringEqual($"The Test/@Value attribute's value, '{version}', is not a valid version. Specify a four-part version or semantic version, such as '#.#.#.#' or '#.#.#-label.#'.", exception.Message); |
| 49 | } |
| 50 | |
| 51 | [Fact] |
| 52 | public void CanParseSemverAttributeVersion() |
| 53 | { |
| 54 | var helper = GetParserHelper(); |
| 55 | |
| 56 | var attribute = CreateAttribute("10.99.444-preview.0"); |
| 57 | var result = helper.GetAttributeVersionValue(null, attribute); |
| 58 | |
| 59 | WixAssert.StringEqual("10.99.444-preview.0", result); |
| 60 | } |
| 61 | |
| 62 | [Fact] |
| 63 | public void CanParseFourPartSemverAttributeVersion() |
| 64 | { |
| 65 | var helper = GetParserHelper(); |
| 66 | |
| 67 | var attribute = CreateAttribute("1.2.3.4-meta.123-other.456"); |
| 68 | var result = helper.GetAttributeVersionValue(null, attribute); |
| 69 | |
| 70 | WixAssert.StringEqual("1.2.3.4-meta.123-other.456", result); |
| 71 | } |
| 72 | |
| 73 | [Fact] |
| 74 | public void CanParseVersionWithLeadingV() |
| 75 | { |
| 76 | var helper = GetParserHelper(); |
| 77 | |
| 78 | var attribute = CreateAttribute("v1.2.3.4"); |
| 79 | var result = helper.GetAttributeVersionValue(null, attribute); |
| 80 | |
| 81 | WixAssert.StringEqual("v1.2.3.4", result); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | private static IParseHelper GetParserHelper() |
| 86 | { |
| 87 | var sp = WixToolsetServiceProviderFactory.CreateServiceProvider(); |
| 88 | var helper = sp.GetService<IParseHelper>(); |
| 89 | return helper; |
| 90 | } |
| 91 | |
| 92 | private static XAttribute CreateAttribute(string value) |
| 93 | { |
| 94 | var attribute = new XAttribute("Value", value); |
| 95 | _ = new XElement("Test", attribute); |
| 96 | |
| 97 | return attribute; |
| 98 | } |
| 99 | } |
| 100 | } |