├── .gradle
├── 4.4
│ ├── fileChanges
│ │ └── last-build.bin
│ ├── fileHashes
│ │ ├── fileHashes.bin
│ │ └── fileHashes.lock
│ └── taskHistory
│ │ ├── taskHistory.bin
│ │ └── taskHistory.lock
└── buildOutputCleanup
│ ├── buildOutputCleanup.lock
│ ├── cache.properties
│ └── outputFiles.bin
├── .idea
├── gradle.xml
├── kotlinc.xml
├── misc.xml
├── modules
│ ├── stub-generator_main.iml
│ └── stub-generator_test.iml
├── vcs.xml
└── workspace.xml
├── LICENSE.txt
├── README.md
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
└── kotlin
│ └── m
│ └── bonnet
│ └── generator
│ └── StubGenerator.kt
└── test
└── kotlin
└── m
└── bonnet
└── generator
└── StubGeneratorTest.kt
/.gradle/4.4/fileChanges/last-build.bin:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gradle/4.4/fileHashes/fileHashes.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/.gradle/4.4/fileHashes/fileHashes.bin
--------------------------------------------------------------------------------
/.gradle/4.4/fileHashes/fileHashes.lock:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/.gradle/4.4/fileHashes/fileHashes.lock
--------------------------------------------------------------------------------
/.gradle/4.4/taskHistory/taskHistory.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/.gradle/4.4/taskHistory/taskHistory.bin
--------------------------------------------------------------------------------
/.gradle/4.4/taskHistory/taskHistory.lock:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/.gradle/4.4/taskHistory/taskHistory.lock
--------------------------------------------------------------------------------
/.gradle/buildOutputCleanup/buildOutputCleanup.lock:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/.gradle/buildOutputCleanup/buildOutputCleanup.lock
--------------------------------------------------------------------------------
/.gradle/buildOutputCleanup/cache.properties:
--------------------------------------------------------------------------------
1 | #Wed Jul 11 15:18:34 CEST 2018
2 | gradle.version=4.4
3 |
--------------------------------------------------------------------------------
/.gradle/buildOutputCleanup/outputFiles.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/.gradle/buildOutputCleanup/outputFiles.bin
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules/stub-generator_main.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/.idea/modules/stub-generator_test.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | fine
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
119 |
120 |
121 |
124 |
125 |
126 |
148 |
149 |
150 |
359 |
360 |
361 |
368 |
369 |
370 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 | 1531315091024
804 |
805 |
806 | 1531315091024
807 |
808 |
809 | 1531385689680
810 |
811 |
812 |
813 | 1531385689680
814 |
815 |
816 | 1531386653075
817 |
818 |
819 |
820 | 1531386653075
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 |
893 |
894 |
895 |
896 |
897 |
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
909 |
910 |
911 |
912 |
913 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 |
922 |
923 |
924 |
925 |
926 |
927 |
928 |
929 |
930 |
931 |
932 |
933 |
934 |
935 |
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 | 1.8
953 |
954 |
955 |
956 |
957 |
958 |
959 |
960 |
961 |
962 |
963 |
964 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) stub-generator Contributors
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Introduction
2 |
3 | A library to build data classes with default values. It can help you use your data classes in tests without mocking them.
4 |
5 | ## How to use
6 |
7 | ```kotlin
8 | val hugePojo = createStub()
9 | ```
10 |
11 | This creates a `HugePojo` with its values set to default values.
12 | You can then set the properties you want, using the data classes' `copy` function :
13 |
14 | ```kotlin
15 | val myCustomPojo = createStub().copy(usefulBoolean = true)
16 | ```
17 |
18 | ## Get it
19 |
20 | [](https://jitpack.io/#BonnetM/stub-generator)
21 |
22 | Add jitpack dependency :
23 |
24 | ```
25 | allprojects {
26 | repositories {
27 | ...
28 | maven { url 'https://jitpack.io' }
29 | }
30 | }
31 | ```
32 | Add the library dependency :
33 |
34 | ```
35 | dependencies {
36 | testImplementation 'com.github.BonnetM:stub-generator:v1.1'
37 | }
38 | ```
39 | ## Example
40 |
41 | The data classes :
42 |
43 | ```kotlin
44 | data class HugePojo(
45 | val uselessIntA: Int,
46 | val uselessIntB: Int,
47 | val uselessIntC: Int,
48 | val uselessIntD: Int = 175,
49 | val uselessIntE: Int?,
50 | val uselessIntF: Int?,
51 | val list: List,
52 | val set: Set,
53 | val map: Map>>,
54 | val uselessStringA: String,
55 | val uselessStringB: String,
56 | val uselessStringC: String,
57 | val uselessStringD: String,
58 | val uselessBoolean: Boolean,
59 | val usefulBoolean: Boolean, // Only variable that will be useful in my tests
60 | val otherPojo: OtherPojo
61 | )
62 |
63 | data class OtherPojo(val aString: String, val otherotherPojo: OtherOtherPojo)
64 |
65 | data class OtherOtherPojo(val i : List)
66 | ```
67 |
68 | The class to be tested
69 |
70 | ```kotlin
71 | class ClassUnderTest(private val ifTrue: IfTrue, private val ifFalse: IfFalse) {
72 | fun compute(hugePojo: HugePojo) {
73 | if (hugePojo.usefulBoolean) {
74 | ifTrue.compute(hugePojo)
75 | } else {
76 | ifFalse.compute(hugePojo)
77 | }
78 | }
79 | }
80 | ```
81 | Its unit test, in vanilla kotlin:
82 |
83 | ```kotlin
84 | @Test
85 | fun `vanilla kotlin`() {
86 | val hugePojo = HugePojo(
87 | 1,
88 | 2,
89 | 3,
90 | 4,
91 | 5,
92 | 6,
93 | emptyList(),
94 | emptySet(),
95 | emptyMap(),
96 | "",
97 | "",
98 | "",
99 | "",
100 | false,
101 | usefulBoolean = true,
102 | OtherPojo("",
103 | OtherOtherPojo(emptyList()))
104 | )
105 |
106 | classUnderTest.compute(hugePojo)
107 |
108 | verify(ifTrue).compute(hugePojo)
109 | verifyZeroInteractions(ifFalse)
110 | }
111 | ```
112 |
113 | And with the library :
114 |
115 | ```kotlin
116 | @Test
117 | fun `with magic`() {
118 | val hugePojo = createStub().copy(usefulBoolean = false)
119 |
120 | classUnderTest.compute(hugePojo)
121 |
122 | verify(ifFalse).compute(hugePojo)
123 | verifyZeroInteractions(ifTrue)
124 | }
125 | ```
126 | ## Known limitations
127 |
128 | For now, arrays are not supported.
129 |
130 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | ext.kotlin_version = '1.2.31'
3 |
4 | repositories {
5 | mavenCentral()
6 | }
7 | dependencies {
8 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
9 | }
10 | }
11 |
12 | group 'm.bonnet'
13 | version '1.0-SNAPSHOT'
14 |
15 | apply plugin: 'kotlin'
16 |
17 | repositories {
18 | mavenCentral()
19 | }
20 |
21 | dependencies {
22 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
23 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
24 | testCompile 'junit:junit:4.12'
25 | testCompile "org.assertj:assertj-core:2.9.1"
26 | }
27 |
28 | sourceCompatibility = 1.7
29 |
30 | compileKotlin {
31 | kotlinOptions.jvmTarget = "1.6"
32 | }
33 | compileTestKotlin {
34 | kotlinOptions.jvmTarget = "1.6"
35 | }
36 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BonnetM/stub-generator/bb31ff71919a46bff4385820cf10a8dfd7cace93/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jul 11 15:18:47 CEST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'stub-generator'
2 | include 'lib'
3 |
4 |
--------------------------------------------------------------------------------
/src/main/kotlin/m/bonnet/generator/StubGenerator.kt:
--------------------------------------------------------------------------------
1 | package m.bonnet.generator
2 |
3 | import kotlin.reflect.*
4 | import kotlin.reflect.full.createType
5 | import kotlin.reflect.full.isSupertypeOf
6 | import kotlin.reflect.full.primaryConstructor
7 | import kotlin.reflect.full.starProjectedType
8 | import kotlin.reflect.jvm.jvmErasure
9 |
10 | /**
11 | * Creates an instance of type [T] with default values
12 | *
13 | * @param T the type to be created
14 | * @return an instance of type [T] which is constructed with default values
15 | * @throws IllegalStateException if an instance cannot be constructed
16 | */
17 |
18 | inline fun createStub(): T {
19 | return StubGenerator.create(T::class)
20 | }
21 |
22 | object StubGenerator {
23 | /**
24 | * Creates an instance of [KC] with default values
25 | *
26 | * @param T the type to be created
27 | * @param KC the KClass type associated with [T]
28 | * @param kClass the KClass of type [T] to be created
29 | * @return an instance of [KC] which is constructed with default values
30 | * @throws IllegalStateException if an instance cannot be constructed
31 | */
32 | fun > create(kClass: KC): T {
33 | kClass.primaryConstructor?.let { constructor ->
34 | val arguments = constructor.parameters.map { it to getParameterDefaultValue(it, listOf(kClass)) }
35 | return constructor.callBy(arguments.toMap())
36 | } ?: throw IllegalStateException("Cannot create a stub without a primary constructor for class $kClass")
37 | }
38 |
39 | private fun > createAny(kClassAny: KCA, creatingValueOf: List>): Any {
40 | if (creatingValueOf.contains(kClassAny)) {
41 | throw IllegalStateException("Type circular depedency, cannot create value for $kClassAny")
42 | }
43 | val constructor = kClassAny.constructors.firstOrNull()
44 | ?: throw IllegalStateException("Cannot create a stub without a constructor for class $kClassAny")
45 | val arguments = constructor.parameters.map { it to getParameterDefaultValue(it, creatingValueOf.plus(kClassAny)) }
46 | return constructor.callBy(arguments.toMap())
47 | }
48 |
49 | private fun getParameterDefaultValue(kParameter: KParameter, creatingValueOf: List>): Any? {
50 | val kType = kParameter.type
51 | return getTypeDefaultValue(kType, creatingValueOf)
52 | }
53 |
54 | private fun getTypeDefaultValue(kType: KType, creatingValueOf: List>): Any? {
55 | return when {
56 | kType.isMarkedNullable -> null
57 | kType.classifier!!.starProjectedType.isSupertypeOf(List::class.createType(listOf(anyProjection()))) -> emptyList()
58 | kType.classifier!!.starProjectedType.isSupertypeOf(Map::class.createType(listOf(anyProjection(), anyProjection()))) -> emptyMap()
59 | kType.classifier!!.starProjectedType.isSupertypeOf(Set::class.createType(listOf(anyProjection()))) -> emptySet()
60 | else -> getBasicTypeDefaultValue(kType, creatingValueOf)
61 | }
62 | }
63 |
64 | private fun anyProjection() = KTypeProjection(KVariance.INVARIANT, Any::class.createType())
65 |
66 | private fun getBasicTypeDefaultValue(kType: KType, creatingValueOf: List>): Any? {
67 | return when (kType) {
68 | Double::class.createType() -> 0.0
69 | Float::class.createType() -> 0F
70 | Long::class.createType() -> 0L
71 | Int::class.createType() -> 0
72 | Short::class.createType() -> 0.toShort()
73 | Byte::class.createType() -> 0.toByte()
74 | Boolean::class.createType() -> false
75 | Char::class.createType() -> 'a'
76 | String::class.createType() -> "String"
77 | else -> createAny(kType.jvmErasure, creatingValueOf)
78 | }
79 | }
80 | }
--------------------------------------------------------------------------------
/src/test/kotlin/m/bonnet/generator/StubGeneratorTest.kt:
--------------------------------------------------------------------------------
1 | package m.bonnet.generator
2 |
3 | import org.assertj.core.api.Assertions.assertThat
4 | import org.junit.Test
5 |
6 | class StubGeneratorTest {
7 |
8 | @Test(expected = IllegalStateException::class)
9 | fun `create when no primary constructor should throw exception`() {
10 | // When
11 | StubGenerator.create(NoPrimaryConstructor::class)
12 |
13 | // Then
14 | // Should throw exception
15 | }
16 |
17 | @Test
18 | fun `create when only primitive types should assign default value to each parameters`() {
19 | // When
20 | val createdStub = StubGenerator.create(OnlyPrimitive::class)
21 |
22 | // Then
23 | val expected = OnlyPrimitive(0.0, 0F, 0L, 0, 0.toShort(), 0.toByte(), false, 'a', "String")
24 | assertThat(createdStub).isEqualTo(expected)
25 | }
26 |
27 | @Test
28 | fun `create when class has user defined class as parameters should create it`() {
29 | // When
30 | val createdStub = StubGenerator.create(WithCustomClass::class)
31 |
32 | // Then
33 | val expected = WithCustomClass(CustomClass(0))
34 | assertThat(createdStub).isEqualTo(expected)
35 | }
36 |
37 | @Test
38 | fun `create when class has two user defined class as parameters should work fine`() {
39 | // When
40 | val createdStub = StubGenerator.create(WithMultipleCustomClasses::class)
41 |
42 | // Then
43 | val expected = WithMultipleCustomClasses(CustomClass(0), CustomClass(0))
44 | assertThat(createdStub).isEqualTo(expected)
45 | }
46 |
47 | @Test(expected = IllegalStateException::class)
48 | fun `create when class has a circular dependency should throw exception`() {
49 | // When
50 | val createdStub = StubGenerator.create(Circular::class)
51 |
52 | // Then
53 | // Should throw exception
54 | }
55 |
56 | @Test
57 | fun `create when class has common collections should return default values`() {
58 | // When
59 | val createdStub = StubGenerator.create(OnlyCommonCollections::class)
60 |
61 | // Then
62 | val expected = OnlyCommonCollections(
63 | emptyList(),
64 | mutableListOf(),
65 | emptySet(),
66 | mutableSetOf(),
67 | emptyMap(),
68 | mutableMapOf()
69 | )
70 | assertThat(createdStub).isEqualTo(expected)
71 | }
72 |
73 | @Test
74 | fun `create when argument is marked as nullable should set it as null`() {
75 | // When
76 | val createdStub = StubGenerator.create(WithNullable::class)
77 |
78 | // Then
79 | val expected = WithNullable(null)
80 | assertThat(createdStub).isEqualTo(expected)
81 | }
82 |
83 | @Test(expected = IllegalStateException::class)
84 | fun `create when class has an interface as parameter should throw exception`() {
85 | // When
86 | StubGenerator.create(WithInterface::class)
87 |
88 | // Then
89 | // Should throw exception
90 | }
91 |
92 | @Test
93 | fun `createStub should work just as create`() {
94 | // When
95 | val createdStub = createStub()
96 |
97 | // Then
98 | val expected = OnlyPrimitive(0.0, 0F, 0L, 0, 0.toShort(), 0.toByte(), false, 'a', "String")
99 | assertThat(createdStub).isEqualTo(expected)
100 | }
101 |
102 | }
103 |
104 | interface NoPrimaryConstructor
105 |
106 | data class OnlyPrimitive(
107 | val double: Double,
108 | val float: Float,
109 | val long: Long,
110 | val int: Int,
111 | val short: Short,
112 | val byte: Byte,
113 | val boolean: Boolean,
114 | val char: Char,
115 | val string: String
116 | )
117 |
118 | data class WithCustomClass(
119 | val customClass: CustomClass
120 | )
121 |
122 | data class WithMultipleCustomClasses(
123 | val customClass: CustomClass,
124 | val secondCustomClass: CustomClass
125 | )
126 |
127 | data class CustomClass(val i: Int)
128 |
129 | data class Circular(val next: Circular)
130 |
131 | data class OnlyCommonCollections(
132 | val intList: List,
133 | val mutIntList: MutableList,
134 | val stringSet: Set,
135 | val mutStringSet: MutableSet,
136 | val doubleMap: Map,
137 | val mutDoubleMap: MutableMap
138 | )
139 |
140 | data class WithNullable(
141 | val nullable: Int?
142 | )
143 |
144 | data class WithInterface(
145 | val noPrimaryConstructor: NoPrimaryConstructor
146 | )
--------------------------------------------------------------------------------