diff --git a/Code/Epic.Linq/Expressions/Relational/Scalar.cs b/Code/Epic.Linq/Expressions/Relational/Scalar.cs index e0aed867..eba2161c 100644 --- a/Code/Epic.Linq/Expressions/Relational/Scalar.cs +++ b/Code/Epic.Linq/Expressions/Relational/Scalar.cs @@ -42,7 +42,7 @@ public abstract class Scalar: VisitableBase, IEquatable /// /// The scalar . /// - public Scalar (ScalarType type) + internal Scalar (ScalarType type) { this._type = type; } @@ -55,6 +55,13 @@ public Scalar (ScalarType type) /// public ScalarType Type { get { return this._type; } } + public abstract override int GetHashCode (); + + public override bool Equals (object obj) + { + return Equals (obj as Scalar); + } + #region IEquatable[Scalar] implementation /// /// Determines whether the specified is equal to the current . diff --git a/Code/UnitTests/Epic.Linq.UnitTests/Epic.Linq.UnitTests.csproj b/Code/UnitTests/Epic.Linq.UnitTests/Epic.Linq.UnitTests.csproj index 9b0ba530..c9585ea0 100644 --- a/Code/UnitTests/Epic.Linq.UnitTests/Epic.Linq.UnitTests.csproj +++ b/Code/UnitTests/Epic.Linq.UnitTests/Epic.Linq.UnitTests.csproj @@ -9,7 +9,6 @@ Library Epic.Linq Epic.Linq.UnitTests - v3.5 true @@ -102,5 +101,6 @@ + diff --git a/Code/UnitTests/Epic.Linq.UnitTests/Expressions/Relational/ScalarFunctionQA.cs b/Code/UnitTests/Epic.Linq.UnitTests/Expressions/Relational/ScalarFunctionQA.cs index a1eb0034..4455d1e6 100644 --- a/Code/UnitTests/Epic.Linq.UnitTests/Expressions/Relational/ScalarFunctionQA.cs +++ b/Code/UnitTests/Epic.Linq.UnitTests/Expressions/Relational/ScalarFunctionQA.cs @@ -82,14 +82,59 @@ public void FunctionEquals_toOtherScalarType_isFalse(string name) { // arrange: ScalarFunction func1 = new FakeScalarFunction(name); - Scalar scalar = GenerateStrictMock(ScalarType.Constant); - + Scalar scalar = new FakeScalar(ScalarType.Constant); // act: - bool result = func1.Equals(scalar); - + bool result1 = func1.Equals(scalar); // assert: // here the FakeScalarFunction.Equals is called, so the result depends on the implementation - Assert.IsFalse(result); + Assert.IsFalse(result1); + } + + [TestCase("test")] + public void FunctionEquals_FromObjectReferenceToEqualInstance_isTrue(string name) + { + // arrange: + ScalarFunction func1 = new FakeScalarFunction(name); + ScalarFunction func2 = new FakeScalarFunction(name); + Scalar scalar = func2; + object obj = func2; + + // act: + bool result1 = func1.Equals (func2); + bool result2 = func1.Equals (scalar); + bool result3 = func1.Equals (obj); + bool result4 = scalar.Equals(func1); + bool result5 = obj.Equals (func1); + + // assert: + Assert.IsTrue (result1); + Assert.IsTrue (result2); + Assert.IsTrue (result3); + Assert.IsTrue (result4); + Assert.IsTrue (result5); + + + } + + [TestCase("test")] + public void FunctionEquals_withOtherReference_isFalse(string name) + { + // arrange: + ScalarFunction func1 = new FakeScalarFunction(name); + Scalar scalar = new FakeScalar(ScalarType.Function); + object obj = scalar; + + // act: + bool result1 = func1.Equals(scalar); + bool result2 = func1.Equals(obj); + bool result3 = obj.Equals (func1); + bool result4 = scalar.Equals(func1); + + // assert: + Assert.IsFalse (result1); + Assert.IsFalse (result2); + Assert.IsFalse (result3); + Assert.IsFalse (result4); } } diff --git a/Code/UnitTests/Epic.Linq.UnitTests/Fakes/FakeScalar.cs b/Code/UnitTests/Epic.Linq.UnitTests/Fakes/FakeScalar.cs new file mode 100644 index 00000000..83abd58d --- /dev/null +++ b/Code/UnitTests/Epic.Linq.UnitTests/Fakes/FakeScalar.cs @@ -0,0 +1,61 @@ +// +// FakeScalar.cs +// +// Author: +// Marco <${AuthorEmail}> +// +// Copyright (c) 2010-2011 Giacomo Tesio +// +// This file is part of Epic.NET. +// +// Epic.NET is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Epic.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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +using System; +using Epic.Linq.Expressions.Relational; + +namespace Epic.Linq.Fakes +{ + [Serializable] + public sealed class FakeScalar: Scalar + { + private static int count = 0; + private readonly int id; + public FakeScalar (ScalarType type): base(type) + { + id = count++; + } + + public override int GetHashCode() + { + return id; + } + + public override bool Equals(Scalar other) + { + return Equals (other as FakeScalar); + } + + public bool Equals(FakeScalar other) + { + if (null == other) return false; + return this.id == other.id; + } + + public override TResult Accept (Epic.Linq.Expressions.IVisitor visitor, Epic.Linq.Expressions.IVisitContext context) + { + throw new NotImplementedException(); + } + } +} +