Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Add codegen tests for name clashes (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Balaji authored Jun 2, 2020
1 parent 1ce9ba6 commit a69a09e
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

- Fixed memory corruption in SpatialOS components with more than 16 fields. [#1378](https://github.com/spatialos/gdk-for-unity/pull/1378)

### Internal

- Added tests in code generator for name clashes. [#1380](https://github.com/spatialos/gdk-for-unity/pull/1380)
- The `test-project` no longer contains illegal schema.

## `0.3.6` - 2020-05-26

### Breaking Changes
Expand Down
27 changes: 0 additions & 27 deletions test-project/Assets/.schema/illegal.schema

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

<ItemGroup>
<None Remove="Tests\Model\SchemaBundleV1\Resources\exhaustive_bundle.json" />
<None Remove="Tests\Model\SchemaBundleV1\Resources\clash_command_in_component.json" />
<None Remove="Tests\Model\SchemaBundleV1\Resources\clash_event_in_component.json" />
<None Remove="Tests\Model\SchemaBundleV1\Resources\clash_enum_in_type.json" />
<None Remove="Tests\Model\SchemaBundleV1\Resources\clash_type_in_type.json" />
<EmbeddedResource Include="Tests\Model\SchemaBundleV1\Resources\exhaustive_bundle.json" />
<EmbeddedResource Include="Tests\Model\SchemaBundleV1\Resources\clash_command_in_component.json" />
<EmbeddedResource Include="Tests\Model\SchemaBundleV1\Resources\clash_event_in_component.json" />
<EmbeddedResource Include="Tests\Model\SchemaBundleV1\Resources\clash_enum_in_type.json" />
<EmbeddedResource Include="Tests\Model\SchemaBundleV1\Resources\clash_type_in_type.json" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Improbable.Gdk.CodeGeneration.FileHandling;
using Improbable.Gdk.CodeGeneration.Utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package improbable.gdk.test;

import "improbable/gdk/core/common.schema";

component IllegalComponentCommand
{
id=12499;
command improbable.gdk.core.Empty illegal_component_command(improbable.gdk.core.Empty);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package improbable.gdk.test;

type IllegalEnumField {
enum NestedEnum {
SUCCESS = 0;
FAILURE = 1;
}
NestedEnum nested_enum = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package improbable.gdk.test;

import "improbable/gdk/core/common.schema";

component IllegalComponentEvent
{
id=12599;
event improbable.gdk.core.Empty illegal_component_event;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package improbable.gdk.test;

type IllegalTypeField {
type NestedType {}
NestedType nested_type = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Improbable.Gdk.CodeGeneration.Model;
using Improbable.Gdk.CodeGeneration.Model.Details;
using NLog;
using NLog.Config;
using NLog.Fluent;
using NLog.Targets;
using NUnit.Framework;

namespace Improbable.Gdk.CodeGeneration.Tests.Model
{
[TestFixture]
public class NameClashingTests
{
private MemoryTarget memoryTarget;

[SetUp]
public void Setup()
{
memoryTarget = new MemoryTarget("MemoryTarget");

var configuration = new LoggingConfiguration();
configuration.AddTarget(memoryTarget);
configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Error, memoryTarget));

LogManager.Configuration = configuration;
}

[Test]
public void Clashing_command_in_component()
{
var bundleName = "clash_command_in_component";
var qualifiedName = "improbable.gdk.test.IllegalComponentCommand";

var store = GetDetailsFromBundle(bundleName);

Assert.IsTrue(store.Components.ContainsKey(qualifiedName));
Assert.AreEqual(0, store.Components[qualifiedName].CommandDetails.Count);

Assert.AreEqual(1, memoryTarget.Logs.Count);
}

[Test]
public void Clashing_event_in_component()
{
var bundleName = "clash_event_in_component";
var qualifiedName = "improbable.gdk.test.IllegalComponentEvent";

var store = GetDetailsFromBundle(bundleName);

Assert.IsTrue(store.Components.ContainsKey(qualifiedName));
Assert.AreEqual(0, store.Components[qualifiedName].EventDetails.Count);

Assert.AreEqual(1, memoryTarget.Logs.Count);
}

[Test]
public void Clashing_enum_in_type()
{
var bundleName = "clash_enum_in_type";
var qualifiedName = "improbable.gdk.test.IllegalEnumField";

var store = GetDetailsFromBundle(bundleName);

Assert.IsTrue(store.Types.ContainsKey(qualifiedName));
Assert.IsTrue(store.Enums.ContainsKey($"{qualifiedName}.NestedEnum"));
Assert.AreEqual(1, store.Types[qualifiedName].ChildEnums.Count);
Assert.AreEqual(0, store.Types[qualifiedName].FieldDetails.Count);

Assert.AreEqual(1, memoryTarget.Logs.Count);
}

[Test]
public void Clashing_type_in_type()
{
var bundleName = "clash_type_in_type";
var qualifiedName = "improbable.gdk.test.IllegalTypeField";

var store = GetDetailsFromBundle(bundleName);

Assert.IsTrue(store.Types.ContainsKey(qualifiedName));
Assert.IsTrue(store.Types.ContainsKey($"{qualifiedName}.NestedType"));
Assert.AreEqual(1, store.Types[qualifiedName].ChildTypes.Count);
Assert.AreEqual(0, store.Types[qualifiedName].FieldDetails.Count);

Assert.AreEqual(1, memoryTarget.Logs.Count);
}

private static DetailsStore GetDetailsFromBundle(string bundleName)
{
var bundleResourceName =
$"CodeGenerationLib.Tests.Model.SchemaBundleV1.Resources.{bundleName}.json";

var assembly = Assembly.GetExecutingAssembly();
var resource = assembly.GetManifestResourceStream(bundleResourceName);
var json = new StreamReader(resource).ReadToEnd();

return new DetailsStore(SchemaBundle.LoadBundle(json), new List<string>(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public static string GetBundleContents()
return new StreamReader(assembly.GetManifestResourceStream(BundleResourceName)).ReadToEnd();
}


private const string BundleResourceName =
"CodeGenerationLib.Tests.Model.SchemaBundleV1.Resources.exhaustive_bundle.json";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"schemaFiles": [
{
"canonicalPath": "clash_command_in_component.schema",
"package": {
"sourceReference": {
"line": 1,
"column": 1
},
"name": "improbable.gdk.test"
},
"imports": [
{
"sourceReference": {
"line": 3,
"column": 1
},
"path": "improbable/gdk/core/common.schema"
}
],
"enums": [],
"types": [],
"components": [
{
"sourceReference": {
"line": 5,
"column": 1
},
"annotations": [],
"qualifiedName": "improbable.gdk.test.IllegalComponentCommand",
"name": "IllegalComponentCommand",
"componentId": 12499,
"dataDefinition": "",
"fields": [],
"events": [],
"commands": [
{
"sourceReference": {
"line": 5,
"column": 1
},
"annotations": [],
"name": "illegal_component_command",
"requestType": "improbable.gdk.core.Empty",
"responseType": "improbable.gdk.core.Empty",
"commandIndex": 1
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"schemaFiles": [
{
"canonicalPath": "clash_enum_in_type.schema",
"package": {
"sourceReference": {
"line": 1,
"column": 1
},
"name": "improbable.gdk.test"
},
"imports": [],
"enums": [
{
"sourceReference": {
"line": 4,
"column": 5
},
"annotations": [],
"qualifiedName": "improbable.gdk.test.IllegalEnumField.NestedEnum",
"name": "NestedEnum",
"outerType": "improbable.gdk.test.IllegalEnumField",
"values": [
{
"sourceReference": {
"line": 5,
"column": 9
},
"annotations": [],
"name": "SUCCESS",
"value": 0
},
{
"sourceReference": {
"line": 6,
"column": 9
},
"annotations": [],
"name": "FAILURE",
"value": 1
}
]
}
],
"types": [
{
"sourceReference": {
"line": 3,
"column": 1
},
"annotations": [],
"qualifiedName": "improbable.gdk.test.IllegalEnumField",
"name": "IllegalEnumField",
"outerType": "",
"fields": [
{
"sourceReference": {
"line": 8,
"column": 5
},
"annotations": [],
"name": "nested_enum",
"fieldId": 1,
"transient": false,
"singularType": {
"type": {
"enum": "improbable.gdk.test.IllegalEnumField.NestedEnum"
}
}
}
]
}
],
"components": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"schemaFiles": [
{
"canonicalPath": "clash_event_in_component.schema",
"package": {
"sourceReference": {
"line": 1,
"column": 1
},
"name": "improbable.gdk.test"
},
"imports": [
{
"sourceReference": {
"line": 3,
"column": 1
},
"path": "improbable/gdk/core/common.schema"
}
],
"enums": [],
"types": [],
"components": [
{
"sourceReference": {
"line": 5,
"column": 1
},
"annotations": [],
"qualifiedName": "improbable.gdk.test.IllegalComponentEvent",
"name": "IllegalComponentEvent",
"componentId": 12599,
"dataDefinition": "",
"fields": [],
"events": [
{
"sourceReference": {
"line": 8,
"column": 5
},
"annotations": [],
"name": "illegal_component_event",
"type": "improbable.gdk.core.Empty",
"eventIndex": 1
}
],
"commands": []
}
]
}
]
}
Loading

0 comments on commit a69a09e

Please sign in to comment.