Skip to content

Commit

Permalink
Null condition after script optimizer job
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <[email protected]>
  • Loading branch information
tvallin committed Oct 10, 2024
1 parent b5fd830 commit 0f84bc9
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@ void testDeserialize() throws IOException {
System.out.println(JsonFactory.toPrettyString(archetypeJson));
assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY));
}

@Test
void testDeserializeCondition() throws IOException {
Path targetDir = targetDir(this.getClass());
Path jsonFile = targetDir.resolve("test-classes/deserializer/script1.json");
Path expected = targetDir.resolve("test-classes/expected/deserializer.json");
Script script = ScriptDeserializer.deserialize(Files.newInputStream(jsonFile));

JsonObject archetypeJson = ScriptSerializer.serialize(script);
System.out.println(JsonFactory.toPrettyString(archetypeJson));
assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ void testInlined4() throws IOException {
assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY));
}

@Test
void testInlined5() throws IOException {
Path targetDir = targetDir(this.getClass());
Path sourceDir = targetDir.resolve("test-classes/inlined5");
Path expected = targetDir.resolve("test-classes/expected/inlined5.json");
FileSystem fs = VirtualFileSystem.create(sourceDir);

JsonObject archetypeJson = ScriptSerializer.serialize(fs);
System.out.println(JsonFactory.toPrettyString(archetypeJson));
assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY));
}

@Test
void testEmptyMethodNotCompiled() throws IOException {
Path targetDir = targetDir(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"expressions": {
"1": [
{
"kind": "variable",
"value": "foo"
}
],
"2": [
{
"kind": "variable",
"value": "bar2"
}
],
"3": [
{
"kind": "variable",
"value": "bar3"
}
]
},
"methods": {
},
"children": [
{
"kind": "variables",
"children": [
{
"kind": "boolean",
"path": "foo",
"value": true
},
{
"kind": "boolean",
"path": "bar2",
"value": true
},
{
"kind": "boolean",
"path": "bar3",
"value": true
}
]
},
{
"kind": "step",
"name": "Step2",
"id": "1",
"if": "2",
"children": [
{
"kind": "inputs",
"children": [
{
"kind": "boolean",
"name": "Boolean2",
"id": "boolean2",
"default": false
}
]
}
]
},
{
"kind": "step",
"name": "Step3",
"id": "2",
"if": "3",
"children": [
{
"kind": "inputs",
"children": [
{
"kind": "boolean",
"name": "Boolean3",
"id": "boolean3",
"default": false
}
]
}
]
}
]
}
Empty file.
29 changes: 29 additions & 0 deletions archetype/engine-v2-json/src/test/resources/inlined5/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">

<variables>
<boolean path="foo">true</boolean>
<boolean path="bar2">true</boolean>
<boolean path="bar3">true</boolean>
</variables>
<exec src="script1.xml" if="${foo}"/>
</archetype-script>
25 changes: 25 additions & 0 deletions archetype/engine-v2-json/src/test/resources/inlined5/script1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">

<exec src="script2.xml" if="${bar2}"/>
<exec src="script3.xml" if="${bar3}"/>
</archetype-script>
27 changes: 27 additions & 0 deletions archetype/engine-v2-json/src/test/resources/inlined5/script2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">
<step name="Step2">
<inputs>
<boolean id="boolean2" name="Boolean2"/>
</inputs>
</step>
</archetype-script>
28 changes: 28 additions & 0 deletions archetype/engine-v2-json/src/test/resources/inlined5/script3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">

<step name="Step3">
<inputs>
<boolean id="boolean3" name="Boolean3"/>
</inputs>
</step>
</archetype-script>
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public Builder then(Node.Builder<? extends Node, ?> then) {
return this;
}

public boolean isThenNull() {
return then == null;
}

@Override
protected Condition doBuild() {
return new Condition(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public Node.VisitResult visitBlock(Block block, Script script) {
@Override
public Node.VisitResult postVisitBlock(Block block, Script arg) {
Node.Builder<?, ?> builder = stack.pop();
Node.Builder<?, ?> parentBuilder = stack.peek();
if (builder.children().isEmpty()) {
// skip certain blocks without children
switch (block.kind()) {
Expand All @@ -159,14 +160,20 @@ public Node.VisitResult postVisitBlock(Block block, Script arg) {
case VARIABLES:
case INVOKE:
case INVOKE_DIR:
Node.Builder<?, ?> parentBuilder = stack.peek();
if (parentBuilder != null) {
parentBuilder.nestedBuilders().remove(builder);
}
return Node.VisitResult.CONTINUE;
default:
}
}

if (builder instanceof Condition.Builder) {
if (((Condition.Builder) builder).isThenNull() && parentBuilder != null) {
parentBuilder.nestedBuilders().remove(builder);
return Node.VisitResult.CONTINUE;
}
}
builder.build();
return Node.VisitResult.CONTINUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,39 @@ public VisitResult visitAny(Input input, Void arg) {
assertThat(index[0], is(17));
}

@Test
void testInlined5() {
Script script = load("compiler/inlined5/main.xml");
Script compiledScript = ClientCompiler.compile(script, false);
int[] index = new int[]{0};
walk(new Node.Visitor<>() {
@Override
public VisitResult visitBlock(Block block, Void arg) {
return block.accept(new Block.Visitor<>() {

@Override
public VisitResult visitInput(Input input, Void arg) {
return input.accept(new Input.Visitor<>() {

@Override
public VisitResult visitBoolean(Input.Boolean input, Void arg) {
++index[0];
return VisitResult.CONTINUE;
}

@Override
public VisitResult visitAny(Input input, Void arg) {
fail(String.format("Unexpected input: %s, index=%d", block, index[0]));
return VisitResult.CONTINUE;
}
}, arg);
}
}, arg);
}
}, compiledScript);
//assertThat(index[0], is(17));
}

@Test
void testDeclarations() {
Script script = load("compiler/declarations/main.xml");
Expand Down
24 changes: 24 additions & 0 deletions archetype/engine-v2/src/test/resources/compiler/inlined5/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">

<exec src="script1.xml" if="${foo}"/>
</archetype-script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">

<exec src="script2.xml" if="${bar2}"/>
<exec src="script3.xml" if="${bar3}"/>
</archetype-script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 file:/archetype.xsd">

<inputs>
<boolean id="boolean2" name="Boolean2"/>
</inputs>
</archetype-script>
Loading

0 comments on commit 0f84bc9

Please sign in to comment.