Skip to content

Commit

Permalink
feat(QueryAttribute): log error if no elements are found
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Oct 27, 2022
1 parent 2a3a924 commit 58582cc
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 2 deletions.
33 changes: 32 additions & 1 deletion Assets/UIComponents.Tests/QueryAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using NSubstitute;
using NUnit.Framework;
using UIComponents.Testing;
using UnityEngine.TestTools;
Expand Down Expand Up @@ -37,11 +38,13 @@ private partial class ComponentWithQueryAttribute : UIComponent
}

private TestBed _testBed;
private ILogger _mockLogger;

[SetUp]
public void SetUp()
{
_testBed = TestBed.Create().Build();
_mockLogger = Substitute.For<ILogger>();
_testBed = TestBed.Create().WithSingleton(_mockLogger).Build();
}

[UnityTest]
Expand Down Expand Up @@ -115,5 +118,33 @@ public IEnumerator Should_Not_Populate_Invalid_Fields()
Assert.That(component.InvalidArray, Is.Null);
Assert.That(component.InvalidList, Is.Null);
}

private partial class ComponentWithMissingFields : UIComponent
{
[Query]
public Label label;

[Query]
public ComponentWithQueryAttribute[] components;

[Query]
public List<Button> buttons;
}

[UnityTest]
public IEnumerator Should_Log_Errors_If_Query_Yields_No_Results()
{
var component = _testBed.CreateComponent<ComponentWithMissingFields>();

yield return component.WaitForInitializationEnumerator();

Assert.That(component.label, Is.Null);
Assert.That(component.components.Length, Is.EqualTo(0));
Assert.That(component.buttons.Count, Is.EqualTo(0));

_mockLogger.Received().LogError("Query (label): No instances of UnityEngine.UIElements.Label found", component);
_mockLogger.Received().LogError("Query (components): No instances of Tests.QueryAttributeTests.ComponentWithQueryAttribute found", component);
_mockLogger.Received().LogError("Query (buttons): No instances of UnityEngine.UIElements.Button found", component);
}
}
}
5 changes: 4 additions & 1 deletion Assets/UIComponents.Tests/QueryClassAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using NSubstitute;
using NUnit.Framework;
using UIComponents.Testing;
using UnityEngine.TestTools;
Expand Down Expand Up @@ -55,12 +56,14 @@ private partial class QueryClassTestComponent : UIComponent
}

private TestBed _testBed;
private ILogger _mockLogger;
private QueryClassTestComponent _queryClassTestComponent;

[UnitySetUp]
public IEnumerator UnitySetUp()
{
_testBed = TestBed.Create().Build();
_mockLogger = Substitute.For<ILogger>();
_testBed = TestBed.Create().WithSingleton(_mockLogger).Build();
_queryClassTestComponent = _testBed.CreateComponent<QueryClassTestComponent>();
yield return _queryClassTestComponent.WaitForInitializationEnumerator();
}
Expand Down
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.dll
Binary file not shown.
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ protected override void UIC_PopulateQueryFields()
// element
var UIC_elementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("valid-usage", (string) null).ToList(UIC_elementList);
if (UIC_elementList.Count == 0)
Logger.LogError("Query (element): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementList.Count > 0)
element = UIC_elementList[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,48 @@ protected override void UIC_PopulateQueryFields()
// element
var UIC_elementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_elementList);
if (UIC_elementList.Count == 0)
Logger.LogError("Query (element): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementList.Count > 0)
element = UIC_elementList[0];

// elementWithUxmlNameInConstructor
var UIC_elementWithUxmlNameInConstructorList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", (string) null).ToList(UIC_elementWithUxmlNameInConstructorList);
if (UIC_elementWithUxmlNameInConstructorList.Count == 0)
Logger.LogError("Query (elementWithUxmlNameInConstructor): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementWithUxmlNameInConstructorList.Count > 0)
elementWithUxmlNameInConstructor = UIC_elementWithUxmlNameInConstructorList[0];

// elementWithUxmlNameAsNameArgument
var UIC_elementWithUxmlNameAsNameArgumentList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("second-uxml-name", (string) null).ToList(UIC_elementWithUxmlNameAsNameArgumentList);
if (UIC_elementWithUxmlNameAsNameArgumentList.Count == 0)
Logger.LogError("Query (elementWithUxmlNameAsNameArgument): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementWithUxmlNameAsNameArgumentList.Count > 0)
elementWithUxmlNameAsNameArgument = UIC_elementWithUxmlNameAsNameArgumentList[0];

// elementWithClassName
var UIC_elementWithClassNameList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, "class-name").ToList(UIC_elementWithClassNameList);
if (UIC_elementWithClassNameList.Count == 0)
Logger.LogError("Query (elementWithClassName): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementWithClassNameList.Count > 0)
elementWithClassName = UIC_elementWithClassNameList[0];

// elementWithNameAndClass
var UIC_elementWithNameAndClassList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("third-uxml-name", "second-class-name").ToList(UIC_elementWithNameAndClassList);
if (UIC_elementWithNameAndClassList.Count == 0)
Logger.LogError("Query (elementWithNameAndClass): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementWithNameAndClassList.Count > 0)
elementWithNameAndClass = UIC_elementWithNameAndClassList[0];

// elementProperty
var UIC_elementPropertyList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("fourth-uxml-name", "third-class-name").ToList(UIC_elementPropertyList);
if (UIC_elementPropertyList.Count == 0)
Logger.LogError("Query (elementProperty): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_elementPropertyList.Count > 0)
elementProperty = UIC_elementPropertyList[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ protected override void UIC_PopulateQueryFields()
// elements
var UIC_elementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", "class-name").ToList(UIC_elementsList);
if (UIC_elementsList.Count == 0)
Logger.LogError("Query (elements): No instances of UnityEngine.UIElements.VisualElement found", this);
elements = new UnityEngine.UIElements.VisualElement[UIC_elementsList.Count];
for (var i = 0; i < UIC_elementsList.Count; i++)
elements[i] = UIC_elementsList[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ protected override void UIC_PopulateQueryFields()
// element
var UIC_elementList = new List<Core.Elements.MyElement>();
this.Query<Core.Elements.MyElement>(null, (string) null).ToList(UIC_elementList);
if (UIC_elementList.Count == 0)
Logger.LogError("Query (element): No instances of Core.Elements.MyElement found", this);
if (UIC_elementList.Count > 0)
element = UIC_elementList[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ protected override void UIC_PopulateQueryFields()
// elements
var UIC_elementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", "class-name").ToList(UIC_elementsList);
if (UIC_elementsList.Count == 0)
Logger.LogError("Query (elements): No instances of UnityEngine.UIElements.VisualElement found", this);
elements = UIC_elementsList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ protected override void UIC_PopulateQueryFields()
var UIC_firstElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_firstElementList);
this.Query<UnityEngine.UIElements.VisualElement>(null, "test").ToList(UIC_firstElementList);
if (UIC_firstElementList.Count == 0)
Logger.LogError("Query (firstElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_firstElementList.Count > 0)
firstElement = UIC_firstElementList[0];

// manyElementsArray
var UIC_manyElementsArrayList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", "class").ToList(UIC_manyElementsArrayList);
this.Query<UnityEngine.UIElements.VisualElement>(null, "class-name").ToList(UIC_manyElementsArrayList);
if (UIC_manyElementsArrayList.Count == 0)
Logger.LogError("Query (manyElementsArray): No instances of UnityEngine.UIElements.VisualElement found", this);
manyElementsArray = new UnityEngine.UIElements.VisualElement[UIC_manyElementsArrayList.Count];
for (var i = 0; i < UIC_manyElementsArrayList.Count; i++)
manyElementsArray[i] = UIC_manyElementsArrayList[i];
Expand All @@ -33,6 +37,8 @@ protected override void UIC_PopulateQueryFields()
this.Query<UnityEngine.UIElements.VisualElement>("name", "class").ToList(UIC_manyElementsListList);
this.Query<UnityEngine.UIElements.VisualElement>("other-name", "other-class").ToList(UIC_manyElementsListList);
this.Query<UnityEngine.UIElements.VisualElement>("third-name", "third-class").ToList(UIC_manyElementsListList);
if (UIC_manyElementsListList.Count == 0)
Logger.LogError("Query (manyElementsList): No instances of UnityEngine.UIElements.VisualElement found", this);
manyElementsList = UIC_manyElementsListList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ protected override void UIC_PopulateQueryFields()
// field
var UIC_fieldList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_fieldList);
if (UIC_fieldList.Count == 0)
Logger.LogError("Query (field): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_fieldList.Count > 0)
field = UIC_fieldList[0];

// elements
var UIC_elementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", "class-name").ToList(UIC_elementsList);
if (UIC_elementsList.Count == 0)
Logger.LogError("Query (elements): No instances of UnityEngine.UIElements.VisualElement found", this);
elements = UIC_elementsList;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ protected override void UIC_PopulateQueryFields()
// field
var UIC_fieldList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_fieldList);
if (UIC_fieldList.Count == 0)
Logger.LogError("Query (field): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_fieldList.Count > 0)
field = UIC_fieldList[0];

// elements
var UIC_elementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", "class-name").ToList(UIC_elementsList);
if (UIC_elementsList.Count == 0)
Logger.LogError("Query (elements): No instances of UnityEngine.UIElements.VisualElement found", this);
elements = UIC_elementsList;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protected override void UIC_PopulateQueryFields()
// component
var UIC_componentList = new List<Parent.PTwo.PThree.PFour.FirstNestedComponent>();
this.Query<Parent.PTwo.PThree.PFour.FirstNestedComponent>(null, (string) null).ToList(UIC_componentList);
if (UIC_componentList.Count == 0)
Logger.LogError("Query (component): No instances of Parent.PTwo.PThree.PFour.FirstNestedComponent found", this);
if (UIC_componentList.Count > 0)
component = UIC_componentList[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ protected override void UIC_PopulateQueryFields()
// elements
var UIC_elementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_elementsList);
if (UIC_elementsList.Count == 0)
Logger.LogError("Query (elements): No instances of UnityEngine.UIElements.VisualElement found", this);
elements = UIC_elementsList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ protected override void UIC_PopulateQueryFields()
// baseElement
var UIC_baseElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_baseElementList);
if (UIC_baseElementList.Count == 0)
Logger.LogError("Query (baseElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_baseElementList.Count > 0)
baseElement = UIC_baseElementList[0];

// anotherBaseElement
var UIC_anotherBaseElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", (string) null).ToList(UIC_anotherBaseElementList);
if (UIC_anotherBaseElementList.Count == 0)
Logger.LogError("Query (anotherBaseElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_anotherBaseElementList.Count > 0)
anotherBaseElement = UIC_anotherBaseElementList[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,48 @@ protected override void UIC_PopulateQueryFields()
// subclassQueryComponent
var UIC_subclassQueryComponentList = new List<SubclassQueryComponent>();
this.Query<SubclassQueryComponent>(null, "asdf").ToList(UIC_subclassQueryComponentList);
if (UIC_subclassQueryComponentList.Count == 0)
Logger.LogError("Query (subclassQueryComponent): No instances of SubclassQueryComponent found", this);
if (UIC_subclassQueryComponentList.Count > 0)
subclassQueryComponent = UIC_subclassQueryComponentList[0];

// subclassElements
var UIC_subclassElementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("foo", "bar").ToList(UIC_subclassElementsList);
if (UIC_subclassElementsList.Count == 0)
Logger.LogError("Query (subclassElements): No instances of UnityEngine.UIElements.VisualElement found", this);
subclassElements = new UnityEngine.UIElements.VisualElement[UIC_subclassElementsList.Count];
for (var i = 0; i < UIC_subclassElementsList.Count; i++)
subclassElements[i] = UIC_subclassElementsList[i];

// subclassList
var UIC_subclassListList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_subclassListList);
if (UIC_subclassListList.Count == 0)
Logger.LogError("Query (subclassList): No instances of UnityEngine.UIElements.VisualElement found", this);
subclassList = UIC_subclassListList;

// baseQueryComponent
var UIC_baseQueryComponentList = new List<BaseQueryComponent>();
this.Query<BaseQueryComponent>("fourth-uxml-name", "third-class-name").ToList(UIC_baseQueryComponentList);
if (UIC_baseQueryComponentList.Count == 0)
Logger.LogError("Query (baseQueryComponent): No instances of BaseQueryComponent found", this);
if (UIC_baseQueryComponentList.Count > 0)
baseQueryComponent = UIC_baseQueryComponentList[0];

// baseElement
var UIC_baseElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_baseElementList);
if (UIC_baseElementList.Count == 0)
Logger.LogError("Query (baseElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_baseElementList.Count > 0)
baseElement = UIC_baseElementList[0];

// anotherBaseElement
var UIC_anotherBaseElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", (string) null).ToList(UIC_anotherBaseElementList);
if (UIC_anotherBaseElementList.Count == 0)
Logger.LogError("Query (anotherBaseElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_anotherBaseElementList.Count > 0)
anotherBaseElement = UIC_anotherBaseElementList[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,40 @@ protected override void UIC_PopulateQueryFields()
// subclassElements
var UIC_subclassElementsList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("foo", "bar").ToList(UIC_subclassElementsList);
if (UIC_subclassElementsList.Count == 0)
Logger.LogError("Query (subclassElements): No instances of UnityEngine.UIElements.VisualElement found", this);
subclassElements = new UnityEngine.UIElements.VisualElement[UIC_subclassElementsList.Count];
for (var i = 0; i < UIC_subclassElementsList.Count; i++)
subclassElements[i] = UIC_subclassElementsList[i];

// subclassList
var UIC_subclassListList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_subclassListList);
if (UIC_subclassListList.Count == 0)
Logger.LogError("Query (subclassList): No instances of UnityEngine.UIElements.VisualElement found", this);
subclassList = UIC_subclassListList;

// baseQueryComponent
var UIC_baseQueryComponentList = new List<BaseQueryComponent>();
this.Query<BaseQueryComponent>("fourth-uxml-name", "third-class-name").ToList(UIC_baseQueryComponentList);
if (UIC_baseQueryComponentList.Count == 0)
Logger.LogError("Query (baseQueryComponent): No instances of BaseQueryComponent found", this);
if (UIC_baseQueryComponentList.Count > 0)
baseQueryComponent = UIC_baseQueryComponentList[0];

// baseElement
var UIC_baseElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>(null, (string) null).ToList(UIC_baseElementList);
if (UIC_baseElementList.Count == 0)
Logger.LogError("Query (baseElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_baseElementList.Count > 0)
baseElement = UIC_baseElementList[0];

// anotherBaseElement
var UIC_anotherBaseElementList = new List<UnityEngine.UIElements.VisualElement>();
this.Query<UnityEngine.UIElements.VisualElement>("uxml-name", (string) null).ToList(UIC_anotherBaseElementList);
if (UIC_anotherBaseElementList.Count == 0)
Logger.LogError("Query (anotherBaseElement): No instances of UnityEngine.UIElements.VisualElement found", this);
if (UIC_anotherBaseElementList.Count > 0)
anotherBaseElement = UIC_anotherBaseElementList[0];
}
Expand Down
Loading

0 comments on commit 58582cc

Please sign in to comment.