|
| 1 | +/* |
| 2 | + * Copyright 2013-2024 Real Logic Limited. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package uk.co.real_logic.sbe.generation.cpp; |
| 17 | + |
| 18 | +import org.agrona.generation.StringWriterOutputManager; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import uk.co.real_logic.sbe.SbeTool; |
| 21 | +import uk.co.real_logic.sbe.Tests; |
| 22 | +import uk.co.real_logic.sbe.ir.Ir; |
| 23 | +import uk.co.real_logic.sbe.xml.IrGenerator; |
| 24 | +import uk.co.real_logic.sbe.xml.MessageSchema; |
| 25 | +import uk.co.real_logic.sbe.xml.ParserOptions; |
| 26 | + |
| 27 | +import java.io.InputStream; |
| 28 | + |
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | +import static org.hamcrest.Matchers.containsString; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.fail; |
| 35 | + |
| 36 | +class CppEnumTest |
| 37 | +{ |
| 38 | + private StringWriterOutputManager outputManager; |
| 39 | + private CppGenerator generator; |
| 40 | + |
| 41 | + private void setupGenerator(final InputStream in) throws Exception |
| 42 | + { |
| 43 | + final ParserOptions options = ParserOptions.builder().stopOnError(true).build(); |
| 44 | + final MessageSchema schema = parse(in, options); |
| 45 | + final IrGenerator irg = new IrGenerator(); |
| 46 | + final Ir ir = irg.generate(schema); |
| 47 | + |
| 48 | + outputManager = new StringWriterOutputManager(); |
| 49 | + outputManager.setPackageName(ir.applicableNamespace()); |
| 50 | + generator = new CppGenerator(ir, false, outputManager); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("checkstyle:LineLength") |
| 54 | + @Test |
| 55 | + void shouldFailOnKeywordEnumValues() throws Exception |
| 56 | + { |
| 57 | + System.clearProperty(SbeTool.KEYWORD_APPEND_TOKEN); |
| 58 | + |
| 59 | + try (InputStream in = Tests.getLocalResource("issue1007.xml")) |
| 60 | + { |
| 61 | + setupGenerator(in); |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + generator.generate(); |
| 66 | + } |
| 67 | + catch (final IllegalStateException exception) |
| 68 | + { |
| 69 | + assertEquals( |
| 70 | + "Invalid property name='false' please correct the schema or consider setting system property: sbe.keyword.append.token", |
| 71 | + exception.getMessage()); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + fail("expected IllegalStateException"); |
| 76 | + final String source = outputManager.getSources().toString(); |
| 77 | + |
| 78 | + System.err.println(source); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void shouldAddSuffixToEnumValues() throws Exception |
| 84 | + { |
| 85 | + System.setProperty(SbeTool.KEYWORD_APPEND_TOKEN, "_"); |
| 86 | + |
| 87 | + try (InputStream in = Tests.getLocalResource("issue1007.xml")) |
| 88 | + { |
| 89 | + setupGenerator(in); |
| 90 | + |
| 91 | + generator.generate(); |
| 92 | + final String sources = outputManager.getSources().toString(); |
| 93 | + |
| 94 | + assertThat(sources, containsString("false_ = static")); |
| 95 | + assertThat(sources, containsString("true_ = static")); |
| 96 | + assertThat(sources, containsString("return false_;")); |
| 97 | + assertThat(sources, containsString("return true_;")); |
| 98 | + assertThat(sources, containsString("case false_:")); |
| 99 | + assertThat(sources, containsString("case true_:")); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments