001/*
002 * Copyright © 2018. Sir Wellington.
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 *
006 * You may obtain a copy of the License at
007 *     http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License.
014 */
015
016package tech.sirwellington.alchemy.test.junit.runners;
017
018import java.lang.annotation.Retention;
019import java.lang.annotation.Target;
020
021import tech.sirwellington.alchemy.annotations.access.Internal;
022import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
023import tech.sirwellington.alchemy.generator.AlchemyGenerator;
024
025import static java.lang.annotation.ElementType.FIELD;
026import static java.lang.annotation.RetentionPolicy.RUNTIME;
027import static tech.sirwellington.alchemy.generator.NumberGenerators.*;
028import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull;
029import static tech.sirwellington.alchemy.test.Checks.Internal.checkThat;
030
031/**
032 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the
033 * Runtime Injection of Generated Doubles from the {@link AlchemyGenerator} library.
034 * <p>
035 * Example:
036 * <pre>
037 * {@code
038 * `@RunWith(AlchemyTestRunner.class)
039 * public class ExampleTest
040 * {
041 *   `@GenerateDouble(POSITIVE)
042 *    private double percentage;
043 *
044 *    ...
045 * }
046 * }
047 * </pre>
048 * Note, ticks (`) used to escape Javadocs.
049 *
050 * @author SirWellington
051 * @see GenerateInteger
052 * @see GenerateLong
053 * @see GenerateString
054 */
055@Target(FIELD)
056@Retention(RUNTIME)
057public @interface GenerateFloat
058{
059
060    Type value() default Type.POSITIVE;
061
062    float min() default 0.0f;
063
064    float max() default 1.0f;
065
066    enum Type
067    {
068        POSITIVE,
069        NEGATIVE,
070        ANY,
071        RANGE;
072    }
073
074    @Internal
075    @NonInstantiable
076    class Values
077    {
078
079        private Values() throws IllegalAccessException
080        {
081            throw new IllegalAccessException("cannot instantiate");
082        }
083
084        static AlchemyGenerator<Float> createGeneratorFor(GenerateFloat annotation)
085        {
086            checkNotNull(annotation, "missing annotation");
087
088            Type type = annotation.value();
089            checkNotNull(type, "@GenerateDouble missing value");
090
091            if (type == Type.RANGE)
092            {
093                float min = annotation.min();
094                float max = annotation.max();
095                checkThat(min < max, "@GenerateDouble: min must be less than max");
096
097                final AlchemyGenerator<Double> doubles = doubles(min, max);
098
099                return new AlchemyGenerator<Float>()
100                {
101                    @Override
102                    public Float get()
103                    {
104                        return doubles.get().floatValue();
105                    }
106                };
107            }
108
109            //Cover remaining cases
110            switch (type)
111            {
112                case POSITIVE:
113                    return positiveFloats();
114                case NEGATIVE:
115                    return negativeFloats();
116                default:
117                    return anyFloats();
118            }
119        }
120
121    }
122
123}