-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IDefaultValueProviderExtension (#1994)
to make EmptyOrDummyResponse extensible for types that need special handling.
- Loading branch information
Showing
15 changed files
with
435 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...re/src/main/java/org/spockframework/runtime/extension/IDefaultValueProviderExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.runtime.extension; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import org.spockframework.mock.IMockMethod; | ||
import org.spockframework.util.Beta; | ||
import org.spockframework.util.Nullable; | ||
import org.spockframework.util.ThreadSafe; | ||
|
||
/** | ||
* Allows to enhance {@link org.spockframework.mock.EmptyOrDummyResponse} with custom default values. | ||
* <p> | ||
* Will be instantiated via the {@link java.util.ServiceLoader} mechanism. | ||
* <p> | ||
* Implementations must be thread-safe and should not have any state. | ||
* <p> | ||
* This extension is intended for framework authors and users who want to provide default values for their own types. | ||
* The extension will only be called if no sensible default value can be provided by the default mechanism. | ||
* If you want to change the default behavior for all types, you should implement a custom {@link org.spockframework.mock.IDefaultResponse}. | ||
* | ||
* @since 2.4 | ||
* @see org.spockframework.mock.EmptyOrDummyResponse | ||
* @see org.spockframework.mock.IDefaultResponse | ||
*/ | ||
@Beta | ||
@ThreadSafe | ||
public interface IDefaultValueProviderExtension { | ||
/** | ||
* Provides a default value for the given type. | ||
* <p> | ||
* This method will be called for every `EmptyOrDummyResponse` non-default type, the returned values are not cached. | ||
* | ||
* @param type the type for which a default value should be provided, see {@link IMockMethod#getReturnType()} | ||
* @param exactType the exact type for which a default value should be provided, see {@link IMockMethod#getExactReturnType()} | ||
* @return the value or null if no default value can be provided | ||
*/ | ||
@Nullable | ||
Object provideDefaultValue(Class<?> type, Type exactType); | ||
} |
31 changes: 31 additions & 0 deletions
31
spock-specs/src/test-java-ge-17/java/org/spockframework/smoke/mock/IEither.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.smoke.mock; | ||
|
||
public sealed interface IEither<L, R> { | ||
record Left<L, R>(L value) implements IEither<L, R> { | ||
} | ||
|
||
record Right<L, R>(R value) implements IEither<L, R> { | ||
} | ||
|
||
static <L, R> IEither<L, R> left(L value) { | ||
return new Left<>(value); | ||
} | ||
|
||
static <L, R> IEither<L, R> right(R value) { | ||
return new Right<>(value); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
spock-specs/src/test-java-ge-17/java/org/spockframework/smoke/mock/IMaybe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.smoke.mock; | ||
|
||
public sealed interface IMaybe<T> { | ||
|
||
record Some<T>(T value) implements IMaybe<T> { | ||
} | ||
|
||
record None<T>() implements IMaybe<T> { | ||
} | ||
|
||
static <T> IMaybe<T> some(T value) { | ||
return new Some<>(value); | ||
} | ||
|
||
static <T> IMaybe<T> none() { | ||
return new None<>(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
spock-specs/src/test-java-le-11/java/org/spockframework/smoke/mock/IEither.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.smoke.mock; | ||
|
||
public interface IEither<L, R> { | ||
class Left<L, R> implements IEither<L, R> { | ||
private final L value; | ||
|
||
public Left(L value) { | ||
this.value = value; | ||
} | ||
|
||
public L value() { | ||
return value; | ||
} | ||
} | ||
|
||
class Right<L, R> implements IEither<L, R> { | ||
private final R value; | ||
|
||
public Right(R value) { | ||
this.value = value; | ||
} | ||
|
||
public R value() { | ||
return value; | ||
} | ||
} | ||
|
||
static <L, R> IEither<L, R> left(L value) { | ||
return new Left<>(value); | ||
} | ||
|
||
static <L, R> IEither<L, R> right(R value) { | ||
return new Right<>(value); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
spock-specs/src/test-java-le-11/java/org/spockframework/smoke/mock/IMaybe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.spockframework.smoke.mock; | ||
|
||
public interface IMaybe<T> { | ||
|
||
class Some<T> implements IMaybe<T> { | ||
private final T value; | ||
|
||
Some(T value) { | ||
this.value = value; | ||
} | ||
|
||
public T getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
class None<T> implements IMaybe<T> { | ||
} | ||
|
||
static <T> IMaybe<T> some(T value) { | ||
return new Some<>(value); | ||
} | ||
|
||
static <T> IMaybe<T> none() { | ||
return new None<>(); | ||
} | ||
} |
Oops, something went wrong.