|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Devicie Pty Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of PSAppDeployToolkit. |
| 5 | + * |
| 6 | + * PSAppDeployToolkit is free software: you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public License |
| 8 | + * as published by the Free Software Foundation, either version 3 |
| 9 | + * of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * PSAppDeployToolkit is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * |
| 15 | + * See the GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with PSAppDeployToolkit. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +using PSADT.ProcessManagement; |
| 22 | +using PSAppDeployToolkit.CodeGen; |
| 23 | +using Xunit; |
| 24 | + |
| 25 | +namespace PSAppDeployToolkit.Tests.CodeGen.Converters |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Contains unit tests for verifying the serialization behavior of ProcessDefinition objects using the |
| 29 | + /// PowerShellSyntaxSerializer. |
| 30 | + /// </summary> |
| 31 | + /// <remarks>These tests ensure that the PowerShellSyntaxSerializer correctly serializes ProcessDefinition |
| 32 | + /// instances, including handling of required and optional properties, proper formatting for PowerShell hashtable |
| 33 | + /// syntax, support for compressed output, and correct escaping of special characters. The tests validate that the |
| 34 | + /// serializer produces output suitable for use in PowerShell scripts and adheres to expected conventions.</remarks> |
| 35 | + public sealed class ProcessDefinitionSyntaxConverterTests |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// Verifies that serializing a ProcessDefinition with only a name emits a 'Name' key and does not include a |
| 39 | + /// 'Description' key. |
| 40 | + /// </summary> |
| 41 | + /// <remarks>This test ensures that the PowerShellSyntaxSerializer correctly serializes minimal |
| 42 | + /// ProcessDefinition objects by including only the relevant properties in the output.</remarks> |
| 43 | + [Fact] |
| 44 | + public void Serialize_NameOnly_EmitsNameKey() |
| 45 | + { |
| 46 | + ProcessDefinition pd = new("notepad"); |
| 47 | + string result = PowerShellSyntaxSerializer.Serialize(pd); |
| 48 | + Assert.Contains("Name = 'notepad'", result); |
| 49 | + Assert.DoesNotContain("Description", result); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Verifies that serializing a ProcessDefinition with both name and description properties emits both keys in |
| 54 | + /// the output. |
| 55 | + /// </summary> |
| 56 | + /// <remarks>This test ensures that the PowerShellSyntaxSerializer correctly includes both the |
| 57 | + /// Name and Description fields when serializing a ProcessDefinition instance. It checks that the resulting |
| 58 | + /// string contains the expected key-value pairs.</remarks> |
| 59 | + [Fact] |
| 60 | + public void Serialize_NameAndDescription_EmitsBothKeys() |
| 61 | + { |
| 62 | + ProcessDefinition pd = new("notepad", "A text editor"); |
| 63 | + string result = PowerShellSyntaxSerializer.Serialize(pd); |
| 64 | + Assert.Contains("Name = 'notepad'", result); |
| 65 | + Assert.Contains("Description = 'A text editor'", result); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Verifies that serializing a ProcessDefinition with only a name results in a string wrapped in a PowerShell |
| 70 | + /// hashtable syntax. |
| 71 | + /// </summary> |
| 72 | + /// <remarks>This test ensures that the Serialize method produces output that starts with '@{' and |
| 73 | + /// ends with '}', indicating correct hashtable formatting for PowerShell scripts.</remarks> |
| 74 | + [Fact] |
| 75 | + public void Serialize_NameOnly_IsWrappedInHashtable() |
| 76 | + { |
| 77 | + ProcessDefinition pd = new("calc"); |
| 78 | + string result = PowerShellSyntaxSerializer.Serialize(pd); |
| 79 | + Assert.StartsWith("@{", result); |
| 80 | + Assert.EndsWith("}", result); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Verifies that the PowerShellSyntaxSerializer.Serialize method produces a single-line, compressed output when |
| 85 | + /// the compress parameter is set to true. |
| 86 | + /// </summary> |
| 87 | + /// <remarks>This test ensures that serialization with compression enabled results in a compact, |
| 88 | + /// single-line string representation of the ProcessDefinition object. It checks for correct formatting and |
| 89 | + /// absence of line breaks in the output.</remarks> |
| 90 | + [Fact] |
| 91 | + public void Serialize_Compressed_EmitsSingleLine() |
| 92 | + { |
| 93 | + ProcessDefinition pd = new("notepad", "Editor"); |
| 94 | + string result = PowerShellSyntaxSerializer.Serialize(pd, compress: true); |
| 95 | + Assert.Equal("@{ Name = 'notepad'; Description = 'Editor' }", result); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Verifies that the serializer correctly escapes single quotes in process names when serializing. |
| 100 | + /// </summary> |
| 101 | + /// <remarks>This test ensures that embedded single quotes in the process name are properly |
| 102 | + /// escaped according to PowerShell syntax conventions. It checks that the serialized output contains the |
| 103 | + /// expected escaped value.</remarks> |
| 104 | + [Fact] |
| 105 | + public void Serialize_NameWithEmbeddedQuotes_EscapesCorrectly() |
| 106 | + { |
| 107 | + ProcessDefinition pd = new("it's"); |
| 108 | + string result = PowerShellSyntaxSerializer.Serialize(pd); |
| 109 | + Assert.Contains("Name = 'it''s'", result); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments