|
| 1 | +{ *************************************************************************** |
| 2 | +
|
| 3 | + Copyright (c) 2016-2026 Kike Pérez |
| 4 | +
|
| 5 | + Unit : Quick.Parameters.Tests |
| 6 | + Description : Tests for Quick.Parameters |
| 7 | + Author : Kike Pérez |
| 8 | + Version : 1.0 |
| 9 | + Created : 01/03/2026 |
| 10 | + Modified : 01/03/2026 |
| 11 | +
|
| 12 | + This file is part of QuickLib: https://github.com/exilon/QuickLib |
| 13 | +
|
| 14 | + *************************************************************************** |
| 15 | +
|
| 16 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | + you may not use this file except in compliance with the License. |
| 18 | + You may obtain a copy of the License at |
| 19 | +
|
| 20 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +
|
| 22 | + Unless required by applicable law or agreed to in writing, software |
| 23 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | + See the License for the specific language governing permissions and |
| 26 | + limitations under the License. |
| 27 | +
|
| 28 | + *************************************************************************** } |
| 29 | + |
| 30 | +unit Quick.Parameters.Tests; |
| 31 | + |
| 32 | +{$i QuickLib.inc} |
| 33 | + |
| 34 | +interface |
| 35 | + |
| 36 | +uses |
| 37 | + DUnitX.TestFramework, |
| 38 | + Quick.Parameters; |
| 39 | + |
| 40 | +type |
| 41 | + // Minimal TParameters subclass with no published properties, |
| 42 | + // so ParseParams does not raise ERequiredParameterNotFound. |
| 43 | + TTestParameters = class(TParameters) |
| 44 | + published |
| 45 | + end; |
| 46 | + |
| 47 | + [TestFixture] |
| 48 | + TQuickParametersTests = class |
| 49 | + private |
| 50 | + fParams : TTestParameters; |
| 51 | + public |
| 52 | + [Setup] |
| 53 | + procedure SetUp; |
| 54 | + [TearDown] |
| 55 | + procedure TearDown; |
| 56 | + |
| 57 | + // ExistsParam single-arg overload (default '=' separator) |
| 58 | + [Test] |
| 59 | + procedure Test_ExistsParam_NonExistentParam_ReturnsFalse; |
| 60 | + [Test] |
| 61 | + procedure Test_ExistsParam_EmptyParam_ReturnsFalse; |
| 62 | + [Test] |
| 63 | + procedure Test_ExistsParam_DoesNotRaise; |
| 64 | + |
| 65 | + // ExistsParam two-arg overload (custom separator — issue #135) |
| 66 | + [Test] |
| 67 | + procedure Test_ExistsParam_WithCustomSeparator_NonExistentParam_ReturnsFalse; |
| 68 | + [Test] |
| 69 | + procedure Test_ExistsParam_WithColonSeparator_DoesNotRaise; |
| 70 | + [Test] |
| 71 | + procedure Test_ExistsParam_WithSpaceSeparator_DoesNotRaise; |
| 72 | + [Test] |
| 73 | + procedure Test_ExistsParam_DefaultAndCustomSeparator_SameResultForAbsentParam; |
| 74 | + end; |
| 75 | + |
| 76 | +implementation |
| 77 | + |
| 78 | +procedure TQuickParametersTests.SetUp; |
| 79 | +begin |
| 80 | + fParams := TTestParameters.Create(False); |
| 81 | +end; |
| 82 | + |
| 83 | +procedure TQuickParametersTests.TearDown; |
| 84 | +begin |
| 85 | + fParams.Free; |
| 86 | +end; |
| 87 | + |
| 88 | +procedure TQuickParametersTests.Test_ExistsParam_NonExistentParam_ReturnsFalse; |
| 89 | +begin |
| 90 | + Assert.IsFalse(fParams.ExistsParam('__nonexistent_param_xyz__'), |
| 91 | + 'A param that is not on the command line must return False'); |
| 92 | +end; |
| 93 | + |
| 94 | +procedure TQuickParametersTests.Test_ExistsParam_EmptyParam_ReturnsFalse; |
| 95 | +begin |
| 96 | + Assert.IsFalse(fParams.ExistsParam(''), |
| 97 | + 'An empty param name must return False'); |
| 98 | +end; |
| 99 | + |
| 100 | +procedure TQuickParametersTests.Test_ExistsParam_DoesNotRaise; |
| 101 | +begin |
| 102 | + Assert.WillNotRaise( |
| 103 | + procedure begin fParams.ExistsParam('someParam'); end, |
| 104 | + nil, |
| 105 | + 'ExistsParam with a valid param name must not raise an exception'); |
| 106 | +end; |
| 107 | + |
| 108 | +procedure TQuickParametersTests.Test_ExistsParam_WithCustomSeparator_NonExistentParam_ReturnsFalse; |
| 109 | +begin |
| 110 | + // Issue #135: the two-arg overload must honour the custom separator. |
| 111 | + // For a param that does not exist, the result must still be False. |
| 112 | + Assert.IsFalse(fParams.ExistsParam('__nonexistent_param_xyz__', ':'), |
| 113 | + 'A param that is not on the command line must return False even with a custom separator'); |
| 114 | +end; |
| 115 | + |
| 116 | +procedure TQuickParametersTests.Test_ExistsParam_WithColonSeparator_DoesNotRaise; |
| 117 | +begin |
| 118 | + Assert.WillNotRaise( |
| 119 | + procedure begin fParams.ExistsParam('someParam', ':'); end, |
| 120 | + nil, |
| 121 | + 'ExistsParam with colon separator must not raise an exception'); |
| 122 | +end; |
| 123 | + |
| 124 | +procedure TQuickParametersTests.Test_ExistsParam_WithSpaceSeparator_DoesNotRaise; |
| 125 | +begin |
| 126 | + Assert.WillNotRaise( |
| 127 | + procedure begin fParams.ExistsParam('someParam', ' '); end, |
| 128 | + nil, |
| 129 | + 'ExistsParam with space separator must not raise an exception'); |
| 130 | +end; |
| 131 | + |
| 132 | +procedure TQuickParametersTests.Test_ExistsParam_DefaultAndCustomSeparator_SameResultForAbsentParam; |
| 133 | +var |
| 134 | + resultDefault : Boolean; |
| 135 | + resultCustom : Boolean; |
| 136 | +begin |
| 137 | + // For a param that is not present, both overloads should agree. |
| 138 | + resultDefault := fParams.ExistsParam('__absent__'); |
| 139 | + resultCustom := fParams.ExistsParam('__absent__', ':'); |
| 140 | + Assert.AreEqual(resultDefault, resultCustom, |
| 141 | + 'Both overloads must return the same result when the param is absent'); |
| 142 | +end; |
| 143 | + |
| 144 | +initialization |
| 145 | + TDUnitX.RegisterTestFixture(TQuickParametersTests); |
| 146 | + |
| 147 | +end. |
0 commit comments