99import java .nio .charset .StandardCharsets ;
1010import java .nio .file .Files ;
1111import java .nio .file .Path ;
12+ import java .nio .file .Paths ;
13+ import java .util .stream .Stream ;
1214import java .util .*;
1315
1416import de .uka .ilkd .key .control .DefaultUserInterfaceControl ;
2527
2628import org .key_project .util .Streams ;
2729
28- import com .fasterxml .jackson .core .type .TypeReference ;
2930import com .fasterxml .jackson .databind .ObjectMapper ;
3031import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
32+ import org .junit .jupiter .api .TestFactory ;
33+ import org .junit .jupiter .api .DynamicTest ;
3134import org .assertj .core .api .Assertions ;
3235import org .junit .jupiter .api .Assumptions ;
33- import org .junit .jupiter .params .ParameterizedTest ;
34- import org .junit .jupiter .params .provider .Arguments ;
35- import org .junit .jupiter .params .provider .MethodSource ;
3636import org .slf4j .Logger ;
3737import org .slf4j .LoggerFactory ;
3838
4848 */
4949public class MasterHandlerTest {
5050 /**
51- * If this variable is set when running this test class, then those cases with expected result
52- * "weak_valid" will raise an exception unless they can be proved using the solver.
51+ * If this variable is set when running this test class, then those cases with
52+ * expected result
53+ * "weak_valid" will raise an exception unless they can be proved using the
54+ * solver.
5355 * <p>
54- * Otherwise, a "timeout" or "unknown" is accepted. This can be used to deal with test cases
56+ * Otherwise, a "timeout" or "unknown" is accepted. This can be used to deal
57+ * with test cases
5558 * that
5659 * should verify but do not yet do so.
5760 * <p>
@@ -65,61 +68,82 @@ public class MasterHandlerTest {
6568 && it .getName ().equals ("Z3 (Legacy Translation)" ))
6669 .findFirst ().orElse (null );
6770
68- public static List <Arguments > data ()
69- throws IOException , URISyntaxException , ProblemLoaderException {
70- try (var input = MasterHandlerTest .class .getResourceAsStream ("cases.yml" )) {
71- var om = new ObjectMapper (new YAMLFactory ());
72- TypeReference <HashMap <String , TestData >> typeRef = new TypeReference <>() {
73- };
74- Map <String , TestData > preData = om .readValue (input , typeRef );
75- var result = new ArrayList <Arguments >(preData .size ());
76- for (var entry : preData .entrySet ()) {
77- final var value = entry .getValue ();
78-
79- if (value .state == TestDataState .IGNORE ) {
80- LOGGER .info ("Test {} case has been marked ignore" , entry .getKey ());
81- continue ;
82- }
71+ private static List <LoadedTestData > data = null ;
72+
73+ public static List <LoadedTestData > data () throws IOException , URISyntaxException , ProblemLoaderException {
74+ if (data != null )
75+ return data ;
76+
77+ var om = new ObjectMapper (new YAMLFactory ());
78+ var path = Paths .get ("src/test/resources/de/uka/ilkd/key/smt/newsmt2/cases/" );
79+ try (var walker = Files .walk (path )) {
80+ List <Path > files = walker .filter (it -> it .getFileName ().toString ().endsWith (".yml" )).toList ();
81+ data = new ArrayList <>(files .size ());
82+
83+ for (Path pFile : files ) {
84+ try {
85+ var file = pFile .toFile ();
86+
87+ TestData value = om .readValue (file , TestData .class );
88+
89+ if (value .state == TestDataState .IGNORE ) {
90+ LOGGER .info ("Test {} case has been marked ignore" , file );
91+ continue ;
92+ }
93+
94+ System .err .println (pFile );
95+ final var testData = value .load (file .getName ().replace (".yml" , "" ));
96+ data .add (testData );
8397
84- final var testData = value .load (entry .getKey ());
85- result .add (Arguments .of (testData ));
98+ } catch (Exception e ) {
99+ e .printStackTrace ();
100+ throw e ;
101+ }
86102 }
87- return result ;
103+ System .out .println (data );
104+ return data ;
88105 }
89106 }
90107
108+ /**
109+ * Describes the state of a test instance.
110+ */
91111 public enum TestDataState {
92112 EMPTY , EXTENDED , IGNORE
93113 }
94114
115+ /**
116+ * Expected outcome of a test instance.
117+ */
95118 public enum Expectation {
96119 VALID , FAIL , IRRELEVANT
97120 }
98121
99122 /**
100- * This class contains the information about the test fixtures that is loaded via the YAML.
101- * @param contains a list of strings that are expected in the SMT translation
123+ * This class contains the information about the test fixtures that is loaded
124+ * via the YAML.
125+ *
126+ * @param contains a list of strings that are expected in the SMT translation
102127 * @param smtSettings required key/values in the smt settings.
103- * @param expected expected output of Z3
104- * @param state state of the test
105- * @param javaSrc path to necessary java sources
106- * @param keySrc contents of the key file to be loaded.
128+ * @param expected expected output of Z3
129+ * @param state state of the test
130+ * @param javaSrc path to necessary java sources
131+ * @param keySrc contents of the key file to be loaded.
107132 */
108133 public record TestData (List <String > contains ,
109- Properties smtSettings ,
110- Expectation expected ,
111- TestDataState state ,
112- String javaSrc ,
113- String keySrc
114- ) {
134+ Properties smtSettings ,
135+ Expectation expected ,
136+ TestDataState state ,
137+ String javaSrc ,
138+ String keySrc ) {
115139
116140 private LoadedTestData load (String name ) throws IOException , ProblemLoaderException {
117141 var keySrc = keySrc ();
118142 if (javaSrc != null && !javaSrc .isEmpty ()) {
119143 Path srcDir = Files .createTempDirectory ("SMT_key_" + name );
120144 Path tmpSrc = srcDir .resolve ("src.java" );
121145 Files .writeString (tmpSrc , javaSrc );
122- keySrc += "\n \\ javaSource \" %s\" ;\n " .formatted (srcDir );
146+ keySrc += "\n / javaSource \" %s\" ;\n " .formatted (srcDir );
123147 }
124148
125149 Path tmpKey = Files .createTempFile ("SMT_key_%s" .formatted (name ), ".key" );
@@ -140,6 +164,7 @@ private LoadedTestData load(String name) throws IOException, ProblemLoaderExcept
140164
141165 ModularSMTLib2Translator translator = new ModularSMTLib2Translator ();
142166 var translation = translator .translateProblem (sequent , env .getServices (), settings ).toString ();
167+ env .dispose ();
143168 return new LoadedTestData (name , this , translation );
144169 }
145170 }
@@ -151,8 +176,12 @@ public String toString() {
151176 }
152177 }
153178
154- @ ParameterizedTest
155- @ MethodSource ("data" )
179+ @ TestFactory
180+ Stream <DynamicTest > testTranslation () throws Exception {
181+ return data ().stream ()
182+ .map (it -> DynamicTest .dynamicTest (it .name , () -> testTranslation (it )));
183+ }
184+
156185 void testTranslation (LoadedTestData data ) throws Exception {
157186 if (DUMP_SMT ) {
158187 Path tmpSmt = Files .createTempFile ("SMT_key_%s" .formatted (data .name ), ".smt2" );
@@ -164,13 +193,16 @@ void testTranslation(LoadedTestData data) throws Exception {
164193 .containsIgnoringWhitespaces (data .data .contains ().toArray (new String [0 ]));
165194 }
166195
196+ @ TestFactory
197+ Stream <DynamicTest > testZ3 () throws Exception {
198+ return data ().stream ()
199+ .map (it -> DynamicTest .dynamicTest (it .name , () -> testZ3 (it )));
200+ }
167201
168- @ ParameterizedTest
169- @ MethodSource ("data" )
170202 void testZ3 (LoadedTestData data ) throws Exception {
171203 Assumptions .assumeTrue (Z3_SOLVER != null );
172204 Assumptions .assumeTrue (Z3_SOLVER .isInstalled (false ),
173- "Z3 is not installed, this testcase is ignored." );
205+ "Z3 is not installed, this testcase is ignored." );
174206
175207 var expectation = data .data .expected ;
176208 Assumptions .assumeTrue (expectation != null , "No Z3 expectation." );
@@ -187,9 +219,9 @@ void testZ3(LoadedTestData data) throws Exception {
187219
188220 try {
189221 String lookFor = switch (expectation ) {
190- case VALID -> "unsat" ;
191- case FAIL -> "(sat|timeout)" ;
192- case IRRELEVANT -> null ;
222+ case VALID -> "unsat" ;
223+ case FAIL -> "(sat|timeout)" ;
224+ case IRRELEVANT -> null ;
193225 };
194226
195227 if (lookFor != null ) {
@@ -205,7 +237,7 @@ void testZ3(LoadedTestData data) throws Exception {
205237
206238 if (!STRICT_TEST ) {
207239 assumeFalse (data .data .state == TestDataState .EXTENDED ,
208- "This is an extended test (will be run only in strict mode)" );
240+ "This is an extended test (will be run only in strict mode)" );
209241 }
210242
211243 if (lookFor != null ) {
0 commit comments