|
| 1 | +/** |
| 2 | + * Copyright (C) 2025 the original author or authors. |
| 3 | + * See the notice.md file distributed with this work for additional |
| 4 | + * information regarding copyright ownership. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.beust.jcommander.defaultprovider; |
| 20 | + |
| 21 | +import static org.testng.Assert.*; |
| 22 | +import static org.testng.Assert.assertEquals; |
| 23 | + |
| 24 | +import java.util.function.Function; |
| 25 | + |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import com.beust.jcommander.IDefaultProvider; |
| 29 | + |
| 30 | +public final class SequenceDefaultProviderTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + public final void shouldProvideFirstNonNullValue() { |
| 34 | + // given |
| 35 | + final IDefaultProvider defaultProvider = IDefaultProvider.sequenceOf( |
| 36 | + always(null), |
| 37 | + always("one"), |
| 38 | + always("two")); |
| 39 | + |
| 40 | + // when |
| 41 | + final var defaultValue = defaultProvider.getDefaultValueFor("test"); |
| 42 | + |
| 43 | + // then |
| 44 | + assertEquals(defaultValue, "one"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public final void shouldProvideNullWhenNoProviderProducesSomeValue() { |
| 49 | + // given |
| 50 | + final IDefaultProvider defaultProvider = IDefaultProvider.sequenceOf( |
| 51 | + always(null), |
| 52 | + always(null), |
| 53 | + always(null)); |
| 54 | + |
| 55 | + // when |
| 56 | + final var defaultValue = defaultProvider.getDefaultValueFor("test"); |
| 57 | + |
| 58 | + // then |
| 59 | + assertEquals(defaultValue, null); |
| 60 | + } |
| 61 | + |
| 62 | + private static final IDefaultProvider always(final String defaultValue) { |
| 63 | + return new IDefaultProvider() { |
| 64 | + @Override |
| 65 | + public String getDefaultValueFor(final String optionName) { |
| 66 | + return defaultValue; |
| 67 | + } |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments