|
| 1 | +// JComponentTests.cs |
| 2 | +// EditMode unit tests for JComponent base class |
| 3 | + |
| 4 | +using NUnit.Framework; |
| 5 | +using UnityEngine.UIElements; |
| 6 | +using JEngine.UI.Editor.Components; |
| 7 | +using JEngine.UI.Editor.Components.Layout; |
| 8 | + |
| 9 | +namespace JEngine.UI.Tests.Editor.Components.Base |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class JComponentTests |
| 13 | + { |
| 14 | + // Using JStack as concrete implementation of JComponent |
| 15 | + private JStack _component; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void SetUp() |
| 19 | + { |
| 20 | + _component = new JStack(); |
| 21 | + } |
| 22 | + |
| 23 | + #region Constructor Tests |
| 24 | + |
| 25 | + [Test] |
| 26 | + public void Constructor_WithBaseClassName_AddsClass() |
| 27 | + { |
| 28 | + // JStack inherits from JComponent with "j-stack" as base class |
| 29 | + Assert.IsTrue(_component.ClassListContains("j-stack")); |
| 30 | + } |
| 31 | + |
| 32 | + #endregion |
| 33 | + |
| 34 | + #region WithClass Tests |
| 35 | + |
| 36 | + [Test] |
| 37 | + public void WithClass_AddsClassName() |
| 38 | + { |
| 39 | + _component.WithClass("custom-class"); |
| 40 | + Assert.IsTrue(_component.ClassListContains("custom-class")); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void WithClass_ReturnsComponentForChaining() |
| 45 | + { |
| 46 | + var result = _component.WithClass("test"); |
| 47 | + Assert.AreSame(_component, result); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public void WithClass_CanAddMultipleClasses() |
| 52 | + { |
| 53 | + _component.WithClass("class1"); |
| 54 | + _component.WithClass("class2"); |
| 55 | + |
| 56 | + Assert.IsTrue(_component.ClassListContains("class1")); |
| 57 | + Assert.IsTrue(_component.ClassListContains("class2")); |
| 58 | + } |
| 59 | + |
| 60 | + #endregion |
| 61 | + |
| 62 | + #region WithName Tests |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void WithName_SetsElementName() |
| 66 | + { |
| 67 | + _component.WithName("test-element"); |
| 68 | + Assert.AreEqual("test-element", _component.name); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void WithName_ReturnsComponentForChaining() |
| 73 | + { |
| 74 | + var result = _component.WithName("test"); |
| 75 | + Assert.AreSame(_component, result); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void WithName_CanOverwritePreviousName() |
| 80 | + { |
| 81 | + _component.WithName("first"); |
| 82 | + _component.WithName("second"); |
| 83 | + Assert.AreEqual("second", _component.name); |
| 84 | + } |
| 85 | + |
| 86 | + #endregion |
| 87 | + |
| 88 | + #region Add Tests |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void Add_SingleChild_AddsToComponent() |
| 92 | + { |
| 93 | + var child = new Label("test"); |
| 94 | + _component.Add(child); |
| 95 | + |
| 96 | + Assert.AreEqual(1, _component.childCount); |
| 97 | + Assert.AreSame(child, _component[0]); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void Add_MultipleChildren_AddsAllToComponent() |
| 102 | + { |
| 103 | + var child1 = new Label("test1"); |
| 104 | + var child2 = new Label("test2"); |
| 105 | + var child3 = new Label("test3"); |
| 106 | + |
| 107 | + _component.Add(child1, child2, child3); |
| 108 | + |
| 109 | + Assert.AreEqual(3, _component.childCount); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public void Add_NullChild_IsIgnored() |
| 114 | + { |
| 115 | + _component.Add((VisualElement)null); |
| 116 | + Assert.AreEqual(0, _component.childCount); |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + public void Add_MixedNullAndValid_AddsOnlyValidChildren() |
| 121 | + { |
| 122 | + var child1 = new Label("test1"); |
| 123 | + var child2 = new Label("test2"); |
| 124 | + |
| 125 | + _component.Add(child1, null, child2); |
| 126 | + |
| 127 | + Assert.AreEqual(2, _component.childCount); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void Add_ReturnsComponentForChaining() |
| 132 | + { |
| 133 | + var result = _component.Add(new Label()); |
| 134 | + Assert.AreSame(_component, result); |
| 135 | + } |
| 136 | + |
| 137 | + #endregion |
| 138 | + |
| 139 | + #region WithFlexGrow Tests |
| 140 | + |
| 141 | + [Test] |
| 142 | + public void WithFlexGrow_SetsFlexGrowValue() |
| 143 | + { |
| 144 | + _component.WithFlexGrow(2f); |
| 145 | + Assert.AreEqual(2f, _component.style.flexGrow.value); |
| 146 | + } |
| 147 | + |
| 148 | + [Test] |
| 149 | + public void WithFlexGrow_ReturnsComponentForChaining() |
| 150 | + { |
| 151 | + var result = _component.WithFlexGrow(1f); |
| 152 | + Assert.AreSame(_component, result); |
| 153 | + } |
| 154 | + |
| 155 | + [Test] |
| 156 | + public void WithFlexGrow_ZeroValue_SetsToZero() |
| 157 | + { |
| 158 | + _component.WithFlexGrow(0f); |
| 159 | + Assert.AreEqual(0f, _component.style.flexGrow.value); |
| 160 | + } |
| 161 | + |
| 162 | + #endregion |
| 163 | + |
| 164 | + #region WithFlexShrink Tests |
| 165 | + |
| 166 | + [Test] |
| 167 | + public void WithFlexShrink_SetsFlexShrinkValue() |
| 168 | + { |
| 169 | + _component.WithFlexShrink(2f); |
| 170 | + Assert.AreEqual(2f, _component.style.flexShrink.value); |
| 171 | + } |
| 172 | + |
| 173 | + [Test] |
| 174 | + public void WithFlexShrink_ReturnsComponentForChaining() |
| 175 | + { |
| 176 | + var result = _component.WithFlexShrink(1f); |
| 177 | + Assert.AreSame(_component, result); |
| 178 | + } |
| 179 | + |
| 180 | + #endregion |
| 181 | + |
| 182 | + #region WithMargin Tests |
| 183 | + |
| 184 | + [Test] |
| 185 | + public void WithMargin_SetsAllMargins() |
| 186 | + { |
| 187 | + _component.WithMargin(10f); |
| 188 | + |
| 189 | + Assert.AreEqual(10f, _component.style.marginTop.value.value); |
| 190 | + Assert.AreEqual(10f, _component.style.marginRight.value.value); |
| 191 | + Assert.AreEqual(10f, _component.style.marginBottom.value.value); |
| 192 | + Assert.AreEqual(10f, _component.style.marginLeft.value.value); |
| 193 | + } |
| 194 | + |
| 195 | + [Test] |
| 196 | + public void WithMargin_ReturnsComponentForChaining() |
| 197 | + { |
| 198 | + var result = _component.WithMargin(5f); |
| 199 | + Assert.AreSame(_component, result); |
| 200 | + } |
| 201 | + |
| 202 | + #endregion |
| 203 | + |
| 204 | + #region WithPadding Tests |
| 205 | + |
| 206 | + [Test] |
| 207 | + public void WithPadding_SetsAllPadding() |
| 208 | + { |
| 209 | + _component.WithPadding(10f); |
| 210 | + |
| 211 | + Assert.AreEqual(10f, _component.style.paddingTop.value.value); |
| 212 | + Assert.AreEqual(10f, _component.style.paddingRight.value.value); |
| 213 | + Assert.AreEqual(10f, _component.style.paddingBottom.value.value); |
| 214 | + Assert.AreEqual(10f, _component.style.paddingLeft.value.value); |
| 215 | + } |
| 216 | + |
| 217 | + [Test] |
| 218 | + public void WithPadding_ReturnsComponentForChaining() |
| 219 | + { |
| 220 | + var result = _component.WithPadding(5f); |
| 221 | + Assert.AreSame(_component, result); |
| 222 | + } |
| 223 | + |
| 224 | + #endregion |
| 225 | + |
| 226 | + #region WithVisibility Tests |
| 227 | + |
| 228 | + [Test] |
| 229 | + public void WithVisibility_True_SetsDisplayFlex() |
| 230 | + { |
| 231 | + _component.WithVisibility(true); |
| 232 | + Assert.AreEqual(DisplayStyle.Flex, _component.style.display.value); |
| 233 | + } |
| 234 | + |
| 235 | + [Test] |
| 236 | + public void WithVisibility_False_SetsDisplayNone() |
| 237 | + { |
| 238 | + _component.WithVisibility(false); |
| 239 | + Assert.AreEqual(DisplayStyle.None, _component.style.display.value); |
| 240 | + } |
| 241 | + |
| 242 | + [Test] |
| 243 | + public void WithVisibility_ReturnsComponentForChaining() |
| 244 | + { |
| 245 | + var result = _component.WithVisibility(true); |
| 246 | + Assert.AreSame(_component, result); |
| 247 | + } |
| 248 | + |
| 249 | + [Test] |
| 250 | + public void WithVisibility_CanToggle() |
| 251 | + { |
| 252 | + _component.WithVisibility(false); |
| 253 | + Assert.AreEqual(DisplayStyle.None, _component.style.display.value); |
| 254 | + |
| 255 | + _component.WithVisibility(true); |
| 256 | + Assert.AreEqual(DisplayStyle.Flex, _component.style.display.value); |
| 257 | + } |
| 258 | + |
| 259 | + #endregion |
| 260 | + |
| 261 | + #region Chaining Tests |
| 262 | + |
| 263 | + [Test] |
| 264 | + public void FluentApi_CanChainMultipleMethods() |
| 265 | + { |
| 266 | + _component |
| 267 | + .WithName("test") |
| 268 | + .WithClass("custom") |
| 269 | + .WithMargin(5f) |
| 270 | + .WithPadding(10f) |
| 271 | + .WithFlexGrow(1f) |
| 272 | + .WithVisibility(true); |
| 273 | + |
| 274 | + Assert.AreEqual("test", _component.name); |
| 275 | + Assert.IsTrue(_component.ClassListContains("custom")); |
| 276 | + Assert.AreEqual(5f, _component.style.marginTop.value.value); |
| 277 | + Assert.AreEqual(10f, _component.style.paddingTop.value.value); |
| 278 | + Assert.AreEqual(1f, _component.style.flexGrow.value); |
| 279 | + Assert.AreEqual(DisplayStyle.Flex, _component.style.display.value); |
| 280 | + } |
| 281 | + |
| 282 | + #endregion |
| 283 | + } |
| 284 | +} |
0 commit comments