| 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.Converters |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Xml.Linq; |
| 8 | using WixToolset.Converters; |
| 9 | using WixToolset.Extensibility.Services; |
| 10 | using WixToolsetTest.Converters.Mocks; |
| 11 | using Xunit; |
| 12 | |
| 13 | public class FormatFixture : BaseConverterFixture |
| 14 | { |
| 15 | [Fact] |
| 16 | public void CanFixWhitespace() |
| 17 | { |
| 18 | var parse = String.Join(Environment.NewLine, |
| 19 | "<?xml version='1.0' encoding='utf-8'?>", |
| 20 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", |
| 21 | " <Fragment>", |
| 22 | " <Property Id='Prop'", |
| 23 | " Value='Val'>", |
| 24 | " </Property>", |
| 25 | " </Fragment>", |
| 26 | "</Wix>"); |
| 27 | |
| 28 | var expected = String.Join(Environment.NewLine, |
| 29 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", |
| 30 | " <Fragment>", |
| 31 | " <Property Id=\"Prop\" Value=\"Val\" />", |
| 32 | " </Fragment>", |
| 33 | "</Wix>"); |
| 34 | |
| 35 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); |
| 36 | |
| 37 | var messaging = new MockMessaging(); |
| 38 | var converter = new WixConverter(messaging, 4, null, null); |
| 39 | |
| 40 | var errors = converter.FormatDocument(document); |
| 41 | |
| 42 | var actual = UnformattedDocumentString(document); |
| 43 | |
| 44 | Assert.Equal(expected, actual); |
| 45 | Assert.Equal(5, errors); |
| 46 | } |
| 47 | |
| 48 | [Fact] |
| 49 | public void CanPreserveNewLines() |
| 50 | { |
| 51 | var parse = String.Join(Environment.NewLine, |
| 52 | "<?xml version='1.0' encoding='utf-8'?>", |
| 53 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", |
| 54 | " <Fragment>", |
| 55 | "", |
| 56 | " <Property Id='Prop' Value='Val' />", |
| 57 | "", |
| 58 | " </Fragment>", |
| 59 | "</Wix>"); |
| 60 | |
| 61 | var expected = String.Join(Environment.NewLine, |
| 62 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", |
| 63 | " <Fragment>", |
| 64 | "", |
| 65 | " <Property Id=\"Prop\" Value=\"Val\" />", |
| 66 | "", |
| 67 | " </Fragment>", |
| 68 | "</Wix>"); |
| 69 | |
| 70 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); |
| 71 | |
| 72 | var messaging = new MockMessaging(); |
| 73 | var converter = new WixConverter(messaging, 4, null, null); |
| 74 | |
| 75 | var conversions = converter.FormatDocument(document); |
| 76 | |
| 77 | var actual = UnformattedDocumentString(document); |
| 78 | |
| 79 | Assert.Equal(expected, actual); |
| 80 | Assert.Equal(4, conversions); |
| 81 | } |
| 82 | |
| 83 | [Fact] |
| 84 | public void CanFormatWithNewLineAtEndOfFile() |
| 85 | { |
| 86 | var parse = String.Join(Environment.NewLine, |
| 87 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", |
| 88 | " <Fragment>", |
| 89 | "", |
| 90 | " <Property Id='Prop' Value='Val' />", |
| 91 | "", |
| 92 | " </Fragment>", |
| 93 | "</Wix>", |
| 94 | ""); |
| 95 | |
| 96 | var expected = String.Join(Environment.NewLine, |
| 97 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", |
| 98 | " <Fragment>", |
| 99 | "", |
| 100 | " <Property Id=\"Prop\" Value=\"Val\" />", |
| 101 | "", |
| 102 | " </Fragment>", |
| 103 | "</Wix>", |
| 104 | ""); |
| 105 | |
| 106 | var document = XDocument.Parse(parse, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); |
| 107 | |
| 108 | var messaging = new MockMessaging(); |
| 109 | var converter = new WixConverter(messaging, 4, null, null); |
| 110 | |
| 111 | var conversions = converter.FormatDocument(document); |
| 112 | |
| 113 | var actual = UnformattedDocumentString(document); |
| 114 | |
| 115 | Assert.Equal(expected, actual); |
| 116 | Assert.Equal(3, conversions); |
| 117 | } |
| 118 | [Fact] |
| 119 | public void CanSaveInPlace() |
| 120 | { |
| 121 | var parse = String.Join(Environment.NewLine, |
| 122 | "<?xml version='1.0' encoding='utf-8'?>", |
| 123 | "<Wix xmlns='http://wixtoolset.org/schemas/v4/wxs'>", |
| 124 | " <Fragment>", |
| 125 | " <Property Id='Prop'", |
| 126 | " Value='Val'>", |
| 127 | " </Property>", |
| 128 | " </Fragment>", |
| 129 | "</Wix>"); |
| 130 | |
| 131 | var expected = String.Join(Environment.NewLine, |
| 132 | "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">", |
| 133 | " <Fragment>", |
| 134 | " <Property Id=\"Prop\" Value=\"Val\" />", |
| 135 | " </Fragment>", |
| 136 | "</Wix>"); |
| 137 | var tempFileName = Path.GetTempFileName(); |
| 138 | File.WriteAllText(tempFileName, parse, System.Text.Encoding.UTF8); |
| 139 | var messaging = new MockMessaging(); |
| 140 | var converter = new WixConverter(messaging, 4, null, null); |
| 141 | converter.FormatFile(tempFileName, true); |
| 142 | var actual = File.ReadAllText(tempFileName, System.Text.Encoding.UTF8); |
| 143 | Assert.Equal(expected, actual); |
| 144 | } |
| 145 | } |
| 146 | } |