171 lines
6.6 KiB
C#

// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
using System.Runtime.CompilerServices;
using Mocha.Core.OmsImplementations.Mini;
namespace Mocha.Core.Tests;
[TestFixture]
public class BasicTests : OmsTestsBase
{
[Test]
public void PrerequisitesDefined()
{
InstanceHandle c_Class = Oms.GetInstance(KnownInstanceGuids.Classes.Class);
Assert.That(c_Class, Is.Not.EqualTo(InstanceHandle.Empty));
}
[Test]
public void CreateInstanceTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
Assert.That(irTestClass, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
Assert.That(irTestClassInstance, Is.Not.EqualTo(InstanceHandle.Empty));
}
[Test]
public void SetAttributeValueTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
Assert.That(irTestClass, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestAttribute = Oms.CreateInstanceOf(Oms.GetInstance(KnownInstanceGuids.Classes.TextAttribute), TEST_ATTR_GUID);
Assert.That(irTestAttribute, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AddAttribute(irTestClass, irTestAttribute);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass, TEST_CLASS_INST_GUID);
string testAttributeValue = Oms.GetAttributeValue<string>(irTestClassInstance, irTestAttribute);
Assert.That(testAttributeValue, Is.Null);
Oms.SetAttributeValue(irTestClassInstance, irTestAttribute, TEST_ATTR_VALUE);
testAttributeValue = Oms.GetAttributeValue<string>(irTestClassInstance, irTestAttribute);
Assert.That(testAttributeValue, Is.EqualTo(TEST_ATTR_VALUE));
}
[Test]
public void AssignRelationshipTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
Assert.That(irTestClass, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestClass2 = Oms.GetInstance(TEST_CLASS2_GUID);
Assert.That(irTestClass2, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestRelationship = Oms.GetInstance(TEST_REL_GUID);
Assert.That(irTestRelationship, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AssignRelationship(irTestClass, irTestRelationship, irTestClass2);
InstanceHandle irRelated = Oms.GetRelatedInstance(irTestClass, irTestRelationship);
Assert.That(irRelated, Is.EqualTo(irTestClass2));
}
[Test]
public void AssignStringToBooleanAttributeFails()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
Assert.That(irTestClass, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass, TEST_CLASS_INST_GUID);
Assert.That(irTestClassInstance, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestBooleanAttribute = Oms.GetInstance(TEST_ATTR_BOOL_GUID);
Assert.That(irTestBooleanAttribute, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AddAttribute(irTestClass, irTestBooleanAttribute);
Assert.That(delegate ()
{
Oms.SetAttributeValue(irTestClassInstance, irTestBooleanAttribute, false);
}, Throws.Nothing, "Assigning a System.Boolean value to a Boolean Attribute MUST NOT throw an exception.");
Assert.That(delegate ()
{
Oms.SetAttributeValue(irTestClassInstance, irTestBooleanAttribute, "The quick brown fox jumps over the lazy dog.");
}, Throws.ArgumentException, "Assigning something other than a System.Boolean value to a Boolean Attribute MUST throw an ArgumentException.");
}
[Test]
public void AssignObjectToTextAttributeFails()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
Assert.That(irTestClass, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass, TEST_CLASS_INST_GUID);
Assert.That(irTestClassInstance, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle irTestTextAttribute = Oms.GetInstance(TEST_ATTR_GUID);
Assert.That(irTestTextAttribute, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AddAttribute(irTestClass, irTestTextAttribute);
Assert.That(delegate ()
{
Oms.SetAttributeValue(irTestClassInstance, irTestTextAttribute, "Teh quick brown fox");
}, Throws.Nothing, "Assigning a System.String value to a Text Attribute MUST NOT throw an exception.");
Assert.That(delegate ()
{
Oms.SetAttributeValue(irTestClassInstance, irTestTextAttribute, irTestClass);
}, Throws.ArgumentException, "Assigning something other than a System.String value to a Text Attribute MUST throw an ArgumentException.");
}
[Test]
public void TextAttribute_GetInstance_returns_same_InstanceHandle_before_and_after_LoadLibrary()
{
// remove the Web reference just for this test
// Oms.RemoveLibraryReference(lhWeb);
InstanceHandle ihTextAttribute = Oms.GetInstance(KnownInstanceGuids.Classes.TextAttribute);
// load the other library, but do not reference it yet
LibraryHandle lhWeb = Oms.LoadLibrary(typeof(LibraryLoaderTests), "Mocha.Core.Tests.Resources.net.alcetech.Mocha.Web.mcl");
// add back the reference to the other library
// Oms.AddLibraryReference(lhWeb);
InstanceHandle ihTextAttribute2 = Oms.GetInstance(KnownInstanceGuids.Classes.TextAttribute);
Assert.That(ihTextAttribute2, Is.EqualTo(ihTextAttribute));
}
[Test]
public void Class_GetInstance_returns_same_InstanceHandle_before_and_after_AddLibraryReference()
{
// remove the Web reference just for this test
// Oms.RemoveLibraryReference(lhWeb);
InstanceHandle ihTextAttribute = Oms.GetInstance(KnownInstanceGuids.Classes.Class);
// load the other library, but do not reference it yet
LibraryHandle lhWeb = Oms.LoadLibrary(typeof(LibraryLoaderTests), "Mocha.Core.Tests.Resources.net.alcetech.Mocha.Web.mcl");
// add back the reference to the other library
Oms.AddLibraryReference(lhWeb);
InstanceHandle ihTextAttribute2 = Oms.GetInstance(KnownInstanceGuids.Classes.Class);
Assert.That(ihTextAttribute2, Is.EqualTo(ihTextAttribute));
}
}