Skip to content

Commit

Permalink
Add supports generating Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo committed Dec 30, 2021
1 parent 49fc22a commit 929ca9a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Stream;

Expand All @@ -46,6 +47,8 @@ public static <T> Arbitrary<T> build(Class<T> clazz, List<ArbitraryNode> nodes)
return MapEntryBuilder.INSTANCE.build(nodes);
} else if (isOptional(clazz)) {
return OptionalBuilder.INSTANCE.build(nodes);
} else if (isQueue(clazz)) {
return QueueBuilder.INSTANCE.build(nodes);
} else {
throw new IllegalArgumentException("Not implemented collection.");
}
Expand Down Expand Up @@ -79,4 +82,8 @@ private static <T> boolean isMapEntry(Class<T> clazz) {
private static <T> boolean isOptional(Class<T> clazz) {
return clazz.isAssignableFrom(Optional.class);
}

private static <T> boolean isQueue(Class<T> clazz) {
return clazz.isAssignableFrom(Queue.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* 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
*
* http://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 com.navercorp.fixturemonkey.generator;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import net.jqwik.api.Arbitrary;
import net.jqwik.api.Combinators;

import com.navercorp.fixturemonkey.arbitrary.ArbitraryNode;

final class QueueBuilder {
public static QueueBuilder INSTANCE = new QueueBuilder();

@SuppressWarnings({"rawtypes", "unchecked"})
<T> Arbitrary<T> build(List<ArbitraryNode> nodes) {
Combinators.BuilderCombinator<CollectionBuilderFrame> queueBuilderCombinator =
Combinators.withBuilder(QueueBuilderFrame::new);

if (nodes.isEmpty()) {
return (Arbitrary<T>)queueBuilderCombinator.build(CollectionBuilderFrame::build);
}

for (ArbitraryNode<?> node : nodes) {
queueBuilderCombinator = queueBuilderCombinator
.use(node.getArbitrary()).in(CollectionBuilderFrame::add);
}

return (Arbitrary<T>)queueBuilderCombinator.build(CollectionBuilderFrame::build);
}

private static class QueueBuilderFrame extends CollectionBuilderFrame {
public QueueBuilderFrame() {
super(new LinkedList<>());
}

@Override
Collection<Object> build() {
return new LinkedList<>(collection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.MapKeyIntegerValueInteger;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.MapKeyIntegerValueString;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.MockInterface;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.NestedStringQueue;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.NestedStringWithNotBlankList;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.StringAndInt;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.StringQueue;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.StringWithNotBlank;
import com.navercorp.fixturemonkey.test.FixtureMonkeyTestSpecs.StringWithNullable;

Expand Down Expand Up @@ -1323,4 +1325,16 @@ void setSpec() {

then(actual.getValue1().getValue()).isEqualTo("test");
}

@Property
@Domain(FixtureMonkeyTestSpecs.class)
void giveMeQueue(@ForAll StringQueue expected) {
then(expected.getValues()).isNotNull();
}

@Property
@Domain(FixtureMonkeyTestSpecs.class)
void giveMeNestedQueue(@ForAll NestedStringQueue expected) {
then(expected.getValues()).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -63,6 +64,8 @@ class FixtureMonkeyTestSpecs extends AbstractDomainContextBase {
registerArbitrary(StringAndInt.class, stringAndInt());
registerArbitrary(NestedStringWithNotBlankList.class, nestedStringWithNotBlankList());
registerArbitrary(ListWithAnnotation.class, listWithAnnotation());
registerArbitrary(StringQueue.class, stringQueue());
registerArbitrary(NestedStringQueue.class, nestedStringQueue());
}

@Provide
Expand Down Expand Up @@ -262,6 +265,26 @@ Arbitrary<ListWithAnnotation> listWithAnnotation() {
return SUT.giveMeArbitrary(ListWithAnnotation.class);
}

@Data
public static class StringQueue {
private Queue<String> values;
}

@Provide
Arbitrary<StringQueue> stringQueue() {
return SUT.giveMeArbitrary(StringQueue.class);
}

@Data
public static class NestedStringQueue {
private Queue<StringQueue> values;
}

@Provide
Arbitrary<NestedStringQueue> nestedStringQueue() {
return SUT.giveMeArbitrary(NestedStringQueue.class);
}

public static class DefaultArbitraryGroup {
public DefaultArbitraryGroup() {
}
Expand Down

0 comments on commit 929ca9a

Please sign in to comment.