2626import uk .co .real_logic .sbe .xml .ParserOptions ;
2727
2828import java .io .InputStream ;
29- import java .util .LinkedHashMap ;
30- import java .util .Map ;
3129
3230import static org .hamcrest .MatcherAssert .assertThat ;
3331import static org .hamcrest .Matchers .containsString ;
3432import static org .hamcrest .Matchers .not ;
33+ import static uk .co .real_logic .sbe .SbeTool .KEYWORD_APPEND_TOKEN ;
3534import static uk .co .real_logic .sbe .xml .XmlSchemaParser .parse ;
3635
3736/**
@@ -45,14 +44,7 @@ class CppEnumParseGeneratorTest
4544 // return the source of the "Model" enum.
4645 private String generateModelHeader (final String ... properties ) throws Exception
4746 {
48- final Map <String , String > toRestore = new LinkedHashMap <>();
49- // The GlobalKeywords message uses reserved words, so a keyword token is required.
50- setProperty (toRestore , "sbe.keyword.append.token" , "X" );
51- for (int i = 0 ; i < properties .length ; i += 2 )
52- {
53- setProperty (toRestore , properties [i ], properties [i + 1 ]);
54- }
55-
47+ System .setProperty (KEYWORD_APPEND_TOKEN , "X" );
5648 try (InputStream in = Tests .getLocalResource ("code-generation-schema.xml" ))
5749 {
5850 final ParserOptions options = ParserOptions .builder ().stopOnError (true ).build ();
@@ -67,26 +59,10 @@ private String generateModelHeader(final String... properties) throws Exception
6759 }
6860 finally
6961 {
70- for (final Map .Entry <String , String > e : toRestore .entrySet ())
71- {
72- if (null == e .getValue ())
73- {
74- System .clearProperty (e .getKey ());
75- }
76- else
77- {
78- System .setProperty (e .getKey (), e .getValue ());
79- }
80- }
62+ System .clearProperty (KEYWORD_APPEND_TOKEN );
8163 }
8264 }
8365
84- private static void setProperty (final Map <String , String > toRestore , final String key , final String value )
85- {
86- toRestore .putIfAbsent (key , System .getProperty (key ));
87- System .setProperty (key , value );
88- }
89-
9066 @ Test
9167 void doesNotEmitEnumParsingByDefault () throws Exception
9268 {
@@ -99,14 +75,22 @@ void doesNotEmitEnumParsingByDefault() throws Exception
9975 @ Test
10076 void emitsFromStringAndInputOperatorWhenEnabled () throws Exception
10177 {
102- final String model = generateModelHeader ("sbe.cpp.generate.enum.parse" , "true" );
78+ System .setProperty ("sbe.cpp.generate.enum.parse" , "true" );
79+ try
80+ {
81+ final String model = generateModelHeader ("sbe.cpp.generate.enum.parse" , "true" );
10382
104- assertThat (model , containsString ("static Model::Value fromString(const char *str)" ));
105- assertThat (model , containsString ("operator >> (" ));
106- // fromString() pulls in <unordered_map> for the inline reverse-lookup map
107- assertThat (model , containsString ("#include <unordered_map>" ));
108- // the reverse map covers the same names c_str()/operator<< emit
109- assertThat (model , containsString ("map[\" A\" ] = A;" ));
110- assertThat (model , containsString ("map[\" NULL_VALUE\" ] = NULL_VALUE;" ));
83+ assertThat (model , containsString ("static Model::Value fromString(const char *str)" ));
84+ assertThat (model , containsString ("operator >> (" ));
85+ // fromString() pulls in <unordered_map> for the inline reverse-lookup map
86+ assertThat (model , containsString ("#include <unordered_map>" ));
87+ // the reverse map covers the same names c_str()/operator<< emit
88+ assertThat (model , containsString ("map[\" A\" ] = A;" ));
89+ assertThat (model , containsString ("map[\" NULL_VALUE\" ] = NULL_VALUE;" ));
90+ }
91+ finally
92+ {
93+ System .clearProperty ("sbe.cpp.generate.enum.parse" );
94+ }
11195 }
11296}
0 commit comments