File: XmlElementExtensionsTests.cs

package info (click to toggle)
mujoco 2.2.2-3.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,796 kB
  • sloc: ansic: 28,947; cpp: 28,897; cs: 14,241; python: 10,465; xml: 5,104; sh: 93; makefile: 34
file content (193 lines) | stat: -rw-r--r-- 7,143 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2019 DeepMind Technologies Limited
//
// 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.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using NUnit.Framework;
using UnityEngine;

namespace Mujoco {

  [TestFixture]
  public class XmlElementExtensionsEditorTests {
    public enum TestEnum { ValueDefault, ValueA }

    private XmlElement _element;

    [SetUp]
    public void SetUp() {
      var doc = new XmlDocument();
      _element = doc.CreateElement("element");
    }

    [Test]
    public void ParsingMissingFloatAttribute() {
      Assert.That(_element.GetFloatAttribute("attrib", 7.0f), Is.EqualTo(7.0f));
    }

    [TestCase("5.1", 5.1f)]
    [TestCase("-4.83", -4.83f)]
    public void ParsingFloatAttribute(string input, float expected) {
      _element.SetAttribute("attrib", input);
      Assert.That(_element.GetFloatAttribute("attrib", 7.0f), Is.EqualTo(expected));
    }

    [TestCase("")]
    [TestCase("string value")]
    public void InvalidFloatValueTriggersAnException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetFloatAttribute("attrib", 7.0f); }, Throws.Exception);
    }

    [Test]
    public void ParsingMissingEnumAttribute() {
      Assert.That(_element.GetEnumAttribute<TestEnum>("attrib", TestEnum.ValueDefault),
                  Is.EqualTo(TestEnum.ValueDefault));
    }

    [TestCase("ValueA", TestEnum.ValueA)]
    [TestCase("valuea", TestEnum.ValueA)]
    public void ParsingEnumAttributeIsCaseInsensitive(string input, TestEnum expected) {
      _element.SetAttribute("attrib", input);
      Assert.That(_element.GetEnumAttribute<TestEnum>("attrib", TestEnum.ValueDefault, true),
                  Is.EqualTo(expected));
    }

    [TestCase("")]
    [TestCase("wrong value")]
    public void InvalidEnumValueTriggersAnException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetEnumAttribute<TestEnum>("attrib", TestEnum.ValueDefault); },
                  Throws.Exception);
    }

    [Test]
    public void ParsingMissingStringAttribute() {
      Assert.That(_element.GetStringAttribute("attrib", "missing"), Is.EqualTo("missing"));
    }

    [TestCase("value", "value")]
    [TestCase("", "")]
    public void ParsingStringAttributes(string input, string expected) {
      _element.SetAttribute("attrib", input);
      Assert.That(_element.GetStringAttribute("attrib", "missing"), Is.EqualTo(expected));
    }

    [Test]
    public void ParsingMissingVector3Attribute() {
      Assert.That(_element.GetVector3Attribute("attrib", Vector3.one), Is.EqualTo(Vector3.one));
    }

    public void ParsingVector3Attributes() {
      _element.SetAttribute("attrib", "-1 2 3.5");
      var expected = new Vector3(-1, 2, 3.5f);
      Assert.That(_element.GetVector3Attribute("attrib", Vector3.one), Is.EqualTo(expected));
    }

    [TestCase("1 2")]
    [TestCase("1")]
    [TestCase("")]
    public void Vector3WithInsufficientNumberOfComponentsTriggersAnException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetVector3Attribute("attrib", Vector3.one); }, Throws.Exception);
    }

    [TestCase("a 2 3")]
    [TestCase("1 b 3")]
    [TestCase("1 2 c")]
    public void InvalidVector3ComponentValueTriggersException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetVector3Attribute("attrib", Vector3.one); }, Throws.Exception);
    }

    public void ParsingVector2Attributes() {
      _element.SetAttribute("attrib", "-1 2.5");
      var expected = new Vector3(-1, 2.5f);
      Assert.That(_element.GetVector2Attribute("attrib", Vector2.one), Is.EqualTo(expected));
    }

    [TestCase("1")]
    [TestCase("")]
    public void Vector2WithInsufficientNumberOfComponentsTriggersAnException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetVector2Attribute("attrib", Vector2.one); }, Throws.Exception);
    }

    [TestCase("a 2")]
    [TestCase("1 b")]
    public void InvalidVector2ComponentValueTriggersException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetVector2Attribute("attrib", Vector2.one); }, Throws.Exception);
    }

    [Test]
    public void ParsingMissingQuaternionAttribute() {
      Assert.That(_element.GetQuaternionAttribute("attrib", Quaternion.identity),
                  Is.EqualTo(Quaternion.identity));
    }

    public void ParsingQuaternionAttributes() {
      _element.SetAttribute("attrib", "0.1 0.2 0.3 0.4");
      var expected = new Quaternion(0.1f, 0.2f, 0.3f, 0.4f);
      Assert.That(_element.GetQuaternionAttribute("attrib", Quaternion.identity),
                  Is.EqualTo(expected));
    }

    [TestCase("1 2 3")]
    [TestCase("1 2")]
    [TestCase("1")]
    [TestCase("")]
    public void QuaternionWithInsufficientNumberOfComponentsTriggersAnException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetQuaternionAttribute("attrib", Quaternion.identity); },
                  Throws.Exception);
    }

    [TestCase("a 2 3 4")]
    [TestCase("1 b 3 4")]
    [TestCase("1 2 c 4")]
    [TestCase("1 2 3 d")]
    public void InvalidQuaternionComponentValueTriggersException(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(() => { _element.GetQuaternionAttribute("attrib", Quaternion.identity); },
                  Throws.Exception);
    }

    [TestCase("1  2")]
    [TestCase("1  2  ")]
    [TestCase("  1 2")]
    public void FloatArraysCanContainArbitraryNumberOfWhitespaces(string input) {
      _element.SetAttribute("attrib", input);
      Assert.That(_element.GetFloatArrayAttribute("attrib", defaultValue: null),
                  Is.EqualTo(new float[] { 1, 2 }));
    }

    public void FloatArraysCanBeFilledUpWithDefaultValues() {
      _element.SetAttribute("attrib", "1 2");
      var defaultValue = new float[] { 5, 6, 7, 8 };
      Assert.That(_element.GetFloatArrayAttribute("attrib", defaultValue: defaultValue),
                  Is.EqualTo(new float[] { 1, 2, 7, 8 }));
    }

    public void FloatArrayDefaultFillingCanBeSwitchedOff() {
      _element.SetAttribute("attrib", "1 2");
      var defaultValue = new float[] { 5, 6, 7, 8 };
      var result = _element.GetFloatArrayAttribute("attrib", defaultValue: defaultValue,
                                                   fillMissingValues: false);
      Assert.That(result, Is.EqualTo(new float[] { 1, 2 }));
    }
  }
}