diff --git a/tests/IceRpc.Ice.Generator.Base.Tests/ConstTests.cs b/tests/IceRpc.Ice.Generator.Base.Tests/ConstTests.cs new file mode 100644 index 000000000..254d5cbf0 --- /dev/null +++ b/tests/IceRpc.Ice.Generator.Base.Tests/ConstTests.cs @@ -0,0 +1,55 @@ +// Copyright (c) ZeroC, Inc. + +using NUnit.Framework; + +namespace IceRpc.Ice.Generator.Base.Tests; + +public class ConstTests +{ + [TestCase(ConstBool.Value, true)] + [TestCase(ConstInt.Value, 3)] + [TestCase(ConstLong.Value, 4L)] + [TestCase(ConstFloat.Value, 5.1F)] + [TestCase(ConstDouble.Value, 6.2D)] + public void Const_has_the_expected_value(object value, object expected) => + Assert.That(value, Is.EqualTo(expected)); + + [Test] + public void Byte_const_has_the_expected_value() => + Assert.That(ConstByte.Value, Is.EqualTo((byte)254)); + + [Test] + public void Short_const_has_the_expected_value() => + Assert.That(ConstShort.Value, Is.EqualTo((short)16000)); + + [Test] + public void String_const_preserves_escape_sequences() + { + Assert.That(ConstString.Value, Does.StartWith("foo")); + Assert.That(ConstString.Value, Does.Contain("\\")); // literal backslash + Assert.That(ConstString.Value, Does.Contain("\"")); // literal double-quote + Assert.That(ConstString.Value, Does.Contain("\n")); // newline + Assert.That(ConstString.Value, Does.Contain("\t")); // tab + Assert.That(ConstString.Value, Does.Contain("\a")); // bell (from \007 and \x07) + } + + [TestCase(ConstColor1.Value, Color.Red)] + [TestCase(ConstColor2.Value, Color.Green)] + [TestCase(ConstColor3.Value, Color.Blue)] + public void Enum_const_has_the_expected_value(Color value, Color expected) => + Assert.That(value, Is.EqualTo(expected)); + + [TestCase(ConstNestedColor1.Value, Nested.Color.Red)] + [TestCase(ConstNestedColor2.Value, Nested.Color.Green)] + [TestCase(ConstNestedColor3.Value, Nested.Color.Blue)] + public void Nested_enum_const_has_the_expected_value(Nested.Color value, Nested.Color expected) => + Assert.That(value, Is.EqualTo(expected)); + + [Test] + public void Alias_const_has_the_same_value_as_referenced_const() + { + Assert.That(ConstFloatAlias.Value, Is.EqualTo((double)ConstFloat.Value)); + Assert.That(ConstColor2Alias.Value, Is.EqualTo(ConstColor2.Value)); + Assert.That(ConstNestedColor2Alias.Value, Is.EqualTo(ConstNestedColor2.Value)); + } +} diff --git a/tests/IceRpc.Ice.Generator.Base.Tests/ConstTests.ice b/tests/IceRpc.Ice.Generator.Base.Tests/ConstTests.ice new file mode 100644 index 000000000..2b8aa6169 --- /dev/null +++ b/tests/IceRpc.Ice.Generator.Base.Tests/ConstTests.ice @@ -0,0 +1,42 @@ +// Copyright (c) ZeroC, Inc. + +#pragma once + +module IceRpc::Ice::Generator::Base::Tests +{ + enum Color { red, green, blue } + + module Nested + { + enum Color { red, green, blue } + } + + /// My bool constant. + const bool ConstBool = true; + + const byte ConstByte = 254; + const short ConstShort = 16000; + const int ConstInt = 3; + const long ConstLong = 4; + const float ConstFloat = 5.1; + const double ConstDouble = 6.2; + const double ConstFloatAlias = ConstFloat; + + const string ConstString = "foo \\ \"bar\n \r\n\t\v\f\a\b\? \007 \x07"; + + const Color ConstColor1 = ::IceRpc::Ice::Generator::Base::Tests::Color::red; + const Color ConstColor2 = Tests::green; + const Color ConstColor3 = blue; + const Color ConstColor2Alias = ConstColor2; + const Nested::Color ConstNestedColor1 = Tests::Nested::Color::red; + const Nested::Color ConstNestedColor2 = Tests::Nested::green; + const Nested::Color ConstNestedColor3 = blue; + const Nested::Color ConstNestedColor2Alias = ConstNestedColor2; + + const int ConstZeroI = 0; + const long ConstZeroL = 0; + const float ConstZeroF = 0; + const float ConstZeroDotF = 0.0; + const double ConstZeroD = 0; + const double ConstZeroDotD = 0.0; +} diff --git a/tests/IceRpc.Ice.Generator.Base.Tests/DefaultValueTests.cs b/tests/IceRpc.Ice.Generator.Base.Tests/DefaultValueTests.cs new file mode 100644 index 000000000..96b4c6751 --- /dev/null +++ b/tests/IceRpc.Ice.Generator.Base.Tests/DefaultValueTests.cs @@ -0,0 +1,153 @@ +// Copyright (c) ZeroC, Inc. + +using NUnit.Framework; + +namespace IceRpc.Ice.Generator.Base.Tests; + +public class DefaultValueTests +{ + [Test] + public void Struct_field_has_expected_default_value() + { + var p = new Point(); + Assert.That(p.X, Is.EqualTo(0)); + Assert.That(p.Y, Is.EqualTo(42)); + } + + [Test] + public void Struct_fields_have_expected_default_values() + { + var s = new StructWithDefaultValues { NoDefault = "hello" }; + + Assert.That(s.BoolFalse, Is.False); + Assert.That(s.BoolTrue, Is.True); + Assert.That(s.B, Is.EqualTo((byte)254)); + Assert.That(s.S, Is.EqualTo((short)16000)); + Assert.That(s.I, Is.EqualTo(3)); + Assert.That(s.L, Is.EqualTo(4L)); + Assert.That(s.F, Is.EqualTo(5.1F)); + Assert.That(s.D, Is.EqualTo(6.2D)); + Assert.That(s.Str, Does.StartWith("foo")); + Assert.That(s.Str, Does.Contain("\\")); + Assert.That(s.Str, Does.Contain("\n")); + Assert.That(s.C1, Is.EqualTo(Color.Red)); + Assert.That(s.C2, Is.EqualTo(Color.Green)); + Assert.That(s.C3, Is.EqualTo(Color.Blue)); + Assert.That(s.Nc1, Is.EqualTo(Nested.Color.Red)); + Assert.That(s.Nc2, Is.EqualTo(Nested.Color.Green)); + Assert.That(s.Nc3, Is.EqualTo(Nested.Color.Blue)); + Assert.That(s.NoDefault, Is.EqualTo("hello")); + Assert.That(s.ZeroI, Is.EqualTo(0)); + Assert.That(s.ZeroL, Is.EqualTo(0L)); + Assert.That(s.ZeroF, Is.EqualTo(0F)); + Assert.That(s.ZeroDotF, Is.EqualTo(0F)); + Assert.That(s.ZeroD, Is.EqualTo(0D)); + Assert.That(s.ZeroDotD, Is.EqualTo(0D)); + } + + [Test] + public void Class_fields_have_expected_default_values() + { + var c = new BaseWithDefaultValues { NoDefault = "hello" }; + + Assert.That(c.BoolFalse, Is.False); + Assert.That(c.BoolTrue, Is.True); + Assert.That(c.B, Is.EqualTo((byte)1)); + Assert.That(c.S, Is.EqualTo((short)2)); + Assert.That(c.I, Is.EqualTo(3)); + Assert.That(c.L, Is.EqualTo(4L)); + Assert.That(c.F, Is.EqualTo(5.1F)); + Assert.That(c.D, Is.EqualTo(6.2D)); + Assert.That(c.Str, Does.StartWith("foo")); + Assert.That(c.Str, Does.Contain("\\")); + Assert.That(c.Str, Does.Contain("\n")); + Assert.That(c.NoDefault, Is.EqualTo("hello")); + Assert.That(c.ZeroI, Is.EqualTo(0)); + Assert.That(c.ZeroL, Is.EqualTo(0L)); + Assert.That(c.ZeroF, Is.EqualTo(0F)); + Assert.That(c.ZeroDotF, Is.EqualTo(0F)); + Assert.That(c.ZeroD, Is.EqualTo(0D)); + Assert.That(c.ZeroDotD, Is.EqualTo(0D)); + + // St1 has no default value in Ice, so it's default(StructWithDefaultValues) not new StructWithDefaultValues(). + Assert.That(c.St1, Is.EqualTo(default(StructWithDefaultValues))); + Assert.That(c.St1, Is.Not.EqualTo(new StructWithDefaultValues { NoDefault = "" })); + } + + [Test] + public void Derived_class_fields_have_expected_default_values() + { + var d = new DerivedWithDefaultValues { NoDefault = "hello" }; + + // Base class fields + Assert.That(d.BoolTrue, Is.True); + Assert.That(d.I, Is.EqualTo(3)); + + // Derived class enum fields + Assert.That(d.C1, Is.EqualTo(Color.Red)); + Assert.That(d.C2, Is.EqualTo(Color.Green)); + Assert.That(d.C3, Is.EqualTo(Color.Blue)); + Assert.That(d.Nc1, Is.EqualTo(Nested.Color.Red)); + Assert.That(d.Nc2, Is.EqualTo(Nested.Color.Green)); + Assert.That(d.Nc3, Is.EqualTo(Nested.Color.Blue)); + } + + [Test] + public void Exception_fields_have_expected_default_values() + { + var e = new BaseExceptionWithDefaultValues { NoDefault = "hello" }; + + Assert.That(e.BoolFalse, Is.False); + Assert.That(e.BoolTrue, Is.True); + Assert.That(e.B, Is.EqualTo((byte)1)); + Assert.That(e.S, Is.EqualTo((short)2)); + Assert.That(e.I, Is.EqualTo(3)); + Assert.That(e.L, Is.EqualTo(4L)); + Assert.That(e.F, Is.EqualTo(5.1F)); + Assert.That(e.D, Is.EqualTo(6.2D)); + Assert.That(e.Str, Does.StartWith("foo")); + Assert.That(e.Str, Does.Contain("\\")); + Assert.That(e.Str, Does.Contain("\n")); + Assert.That(e.NoDefault, Is.EqualTo("hello")); + } + + [Test] + public void Derived_exception_fields_default_to_const_values() + { + var e = new DerivedExceptionWithDefaultValues { NoDefault = "hello" }; + + Assert.That(e.C1, Is.EqualTo(ConstColor1.Value)); + Assert.That(e.C2, Is.EqualTo(ConstColor2.Value)); + Assert.That(e.C3, Is.EqualTo(ConstColor3.Value)); + Assert.That(e.Nc1, Is.EqualTo(ConstNestedColor1.Value)); + Assert.That(e.Nc2, Is.EqualTo(ConstNestedColor2.Value)); + Assert.That(e.Nc3, Is.EqualTo(ConstNestedColor3.Value)); + } + + [Test] + public void Struct_fields_default_to_const_values() + { + var s = new StructWithConstDefaultValues(); + + Assert.That(s.BoolTrue, Is.EqualTo(ConstBool.Value)); + Assert.That(s.B, Is.EqualTo(ConstByte.Value)); + Assert.That(s.S, Is.EqualTo(ConstShort.Value)); + Assert.That(s.I, Is.EqualTo(ConstInt.Value)); + Assert.That(s.L, Is.EqualTo(ConstLong.Value)); + Assert.That(s.F, Is.EqualTo(ConstFloat.Value)); + Assert.That(s.D, Is.EqualTo(ConstDouble.Value)); + Assert.That(s.Str, Is.EqualTo(ConstString.Value)); + Assert.That(s.C1, Is.EqualTo(ConstColor1.Value)); + Assert.That(s.C2, Is.EqualTo(ConstColor2.Value)); + Assert.That(s.C3, Is.EqualTo(ConstColor3.Value)); + Assert.That(s.Nc1, Is.EqualTo(ConstNestedColor1.Value)); + Assert.That(s.Nc2, Is.EqualTo(ConstNestedColor2.Value)); + Assert.That(s.Nc3, Is.EqualTo(ConstNestedColor3.Value)); + Assert.That(s.ZeroI, Is.EqualTo(ConstZeroI.Value)); + Assert.That(s.ZeroL, Is.EqualTo(ConstZeroL.Value)); + Assert.That(s.ZeroF, Is.EqualTo(ConstZeroF.Value)); + Assert.That(s.ZeroDotF, Is.EqualTo(ConstZeroDotF.Value)); + Assert.That(s.ZeroD, Is.EqualTo(ConstZeroD.Value)); + Assert.That(s.ZeroDotD, Is.EqualTo(ConstZeroDotD.Value)); + } +} diff --git a/tests/IceRpc.Ice.Generator.Base.Tests/DefaultValueTests.ice b/tests/IceRpc.Ice.Generator.Base.Tests/DefaultValueTests.ice new file mode 100644 index 000000000..f3d8637dc --- /dev/null +++ b/tests/IceRpc.Ice.Generator.Base.Tests/DefaultValueTests.ice @@ -0,0 +1,121 @@ +// Copyright (c) ZeroC, Inc. + +#include "ConstTests.ice" + +module IceRpc::Ice::Generator::Base::Tests +{ + struct Point { int x; int y = 42; } + + struct StructWithDefaultValues + { + bool boolFalse = false; + bool boolTrue = true; + byte b = 254; + short s = 16000; + int i = 3; + long l = 4; + float f = 5.1; + double d = 6.2; + string str = "foo \\ \"bar\n \r\n\t\v\f\a\b\? \007 \x07"; + Color c1 = ::IceRpc::Ice::Generator::Base::Tests::Color::red; + Color c2 = Tests::green; + Color c3 = blue; + Nested::Color nc1 = Tests::Nested::Color::red; + Nested::Color nc2 = Nested::green; + Nested::Color nc3 = blue; + string noDefault; + int zeroI = 0; + long zeroL = 0; + float zeroF = 0; + float zeroDotF = 0.0; + double zeroD = 0; + double zeroDotD = 0.0; + } + + struct StructWithConstDefaultValues + { + bool boolTrue = ConstBool; + byte b = ConstByte; + short s = ConstShort; + int i = ConstInt; + long l = ConstLong; + float f = ConstFloat; + double d = ConstDouble; + string str = ConstString; + Color c1 = ConstColor1; + Color c2 = ConstColor2; + Color c3 = ConstColor3; + Nested::Color nc1 = ConstNestedColor1; + Nested::Color nc2 = ConstNestedColor2; + Nested::Color nc3 = ConstNestedColor3; + int zeroI = ConstZeroI; + long zeroL = ConstZeroL; + float zeroF = ConstZeroF; + float zeroDotF = ConstZeroDotF; + double zeroD = ConstZeroD; + double zeroDotD = ConstZeroDotD; + } + + class BaseWithDefaultValues + { + bool boolFalse = false; + bool boolTrue = true; + byte b = 1; + short s = 2; + int i = 3; + long l = 4; + float f = 5.1; + double d = 6.2; + string str = "foo \\ \"bar\n \r\n\t\v\f\a\b\? \007 \x07"; + string noDefault; + int zeroI = 0; + long zeroL = 0; + float zeroF = 0; + float zeroDotF = 0.0; + double zeroD = 0; + double zeroDotD = 0.0; + StructWithDefaultValues st1; + } + + class DerivedWithDefaultValues extends BaseWithDefaultValues + { + Color c1 = ::IceRpc::Ice::Generator::Base::Tests::Color::red; + Color c2 = Tests::green; + Color c3 = blue; + + Nested::Color nc1 = Tests::Nested::Color::red; + Nested::Color nc2 = Nested::green; + Nested::Color nc3 = blue; + } + + exception BaseExceptionWithDefaultValues + { + bool boolFalse = false; + bool boolTrue = true; + byte b = 1; + short s = 2; + int i = 3; + long l = 4; + float f = 5.1; + double d = 6.2; + string str = "foo \\ \"bar\n \r\n\t\v\f\a\b\? \007 \x07"; + string noDefault; + int zeroI = 0; + long zeroL = 0; + float zeroF = 0; + float zeroDotF = 0.0; + double zeroD = 0; + double zeroDotD = 0.0; + } + + exception DerivedExceptionWithDefaultValues extends BaseExceptionWithDefaultValues + { + Color c1 = ConstColor1; + Color c2 = ConstColor2; + Color c3 = ConstColor3; + + Nested::Color nc1 = ConstNestedColor1; + Nested::Color nc2 = ConstNestedColor2; + Nested::Color nc3 = ConstNestedColor3; + } +}