public void CanConvertToString()
 {
     SemanticVersion v1 = new SemanticVersion(1, 2, 3, "alpha.bravo.1");
     SemanticVersion v2 = new SemanticVersion(1, 2, 3, "alpha.decker.3.6.mafia");
     SemanticVersion v3 = new SemanticVersion(1, 2, 3, "1.bravo.alpha");
     Assert.Equal("alpha.bravo.1", v1.PreRelease.ToString());
     Assert.NotEqual(v3.PreRelease.ToString(), v1.PreRelease.ToString());
     Assert.NotEqual(v3.PreRelease.GetHashCode(), v1.PreRelease.GetHashCode());
 }
 public void CanCompareLessThan()
 {
     SemanticVersion v100a = new SemanticVersion(1, 0, 0, "alpha");
     SemanticVersion v100a1 = new SemanticVersion(1, 0, 0, "alpha.1");
     SemanticVersion v2 = new SemanticVersion(1, 3, 4, "alpha.2.0");
     SemanticVersion v3 = new SemanticVersion(0, 0, 0, "beta.0");
     SemanticVersion v4 = new SemanticVersion(0, 0, 0, "beta.x.0");
     SemanticVersion v5 = new SemanticVersion(0, 0, 0, "beta");
     Assert.False(v100a1.PreRelease < v100a.PreRelease);
     Assert.True(v100a1.PreRelease < v2.PreRelease);
     Assert.True(v2.PreRelease < v3.PreRelease);
     Assert.True(v3.PreRelease < v4.PreRelease);
     Assert.True(v5.PreRelease < v4.PreRelease && v4.PreRelease > v3.PreRelease);
 }
        public void CanConstruct()
        {
            SemanticVersion v1 = new SemanticVersion(1, 2, 3, "alpha.1");
            Assert.NotNull(v1.PreRelease);
            Assert.Equal(v1.PreRelease[0], "alpha");
            Assert.Equal(v1.PreRelease[1], "1");

            SemanticVersion v2 = new SemanticVersion(1, 2, 3, "alpha.decker.3.6.mafia");
            Assert.NotNull(v2.PreRelease);
            Assert.Equal(v2.PreRelease[0], "alpha");
            Assert.Equal(v2.PreRelease[1], "decker");
            Assert.Equal(v2.PreRelease[2], "3");

        }
 public void CanCompareEqual()
 {
     SemanticVersion v1 = new SemanticVersion(1);
     SemanticVersion v2 = new SemanticVersion(1, 0);
     SemanticVersion v3 = new SemanticVersion(3, 4);
     SemanticVersion v4 = new SemanticVersion(3, 5);
     SemanticVersion v5 = new SemanticVersion(3, 5, 0);
     SemanticVersion v352 = new SemanticVersion(3, 5, 2);
     SemanticVersion v352a1 = new SemanticVersion(3, 5, 2, "alpha.1");
     SemanticVersion v352a1_ = new SemanticVersion(3, 5, 2, "alpha.1.0");
     Assert.Equal(v1, v2);
     Assert.NotEqual(v2, v3);
     Assert.NotEqual(v3, v4);
     Assert.Equal(v4, v5);
     Assert.NotEqual(v352, v352a1);
     Assert.NotEqual(v352a1_, v352a1);
 }
        public void CanConstruct()
        {
            SemanticVersion v1 = new SemanticVersion(3, 5, 0, "alpha.1");
            Assert.NotNull(v1);
            Assert.Equal(v1.Major, 3);
            Assert.Equal(v1.Minor, 5);
            Assert.Equal(v1.Patch, 0);
            Assert.Equal(v1.PreRelease.ToString(), "alpha.1");
            SemanticVersion v026a = new SemanticVersion("0.2.6-alpha");
            Assert.Equal(v026a.Major, 0);
            Assert.Equal(v026a.Minor, 2);
            Assert.Equal(v026a.Patch, 6);
            Assert.Equal(v026a.PreRelease.ToString(), "alpha.0");
            SemanticVersion v1001b3 = new SemanticVersion("10.0.1-beta.3");
            Assert.Equal(v1001b3.Major, 10);
            Assert.Equal(v1001b3.Minor, 0);
            Assert.Equal(v1001b3.Patch, 1);
            Assert.Equal(v1001b3.PreRelease.ToString(), "beta.3");

        }
        public void CanIncrement()
        {
            SemanticVersion v1 = new SemanticVersion(1, 0, 0, "alpha.1");
            SemanticVersion v2 = new SemanticVersion(1, 3, 4, "alpha.2.0");
            SemanticVersion v3 = new SemanticVersion(0, 0, 0, "beta");
            SemanticVersion v4 = new SemanticVersion(0, 0, 0, "beta.x.0");
            SemanticVersion bx860new = new SemanticVersion(0, 0, 0, "beta.x86.0.new");
            PreRelease v11 = ++(v1.PreRelease);
            PreRelease v21 = ++(v2.PreRelease);
            Assert.Equal(v11.ToString(), "alpha.2");
            Assert.Equal(v11[1], "2");
            Assert.Equal(v21[1], "3");
            Assert.Equal(v21.ToString(), "alpha.3");
            ++(v3.PreRelease);
            Assert.Equal(v3.PreRelease.Count, 2);
            Assert.Equal(v3.PreRelease[1], "1");
            ++(v4.PreRelease);
            Assert.Equal(v4.PreRelease[2], "1");
            Assert.Equal((++bx860new).PreRelease.ToString(), "beta.x86.0.new.1");

        }
 public void CanCompareLessThan()
 {
     SemanticVersion v1 = new SemanticVersion(1);
     SemanticVersion v13 = new SemanticVersion(1, 3);
     SemanticVersion v134 = new SemanticVersion(1, 3, 4);
     SemanticVersion v3 = new SemanticVersion(3);
     SemanticVersion v302 = new SemanticVersion(3, 0, 2);
     SemanticVersion v009 = new SemanticVersion(0,0,9);
     SemanticVersion v079 = new SemanticVersion(0, 7, 9);
     SemanticVersion v11 = new SemanticVersion(11);
     Assert.True(v009 < v1);
     Assert.True(v1 < v134);
     Assert.True(v134 < v3);
     Assert.True(v009 < v079);
     SemanticVersion v100a1 = new SemanticVersion(1, 0, 0, "alpha.1");
     SemanticVersion v100a4 = new SemanticVersion(1, 0, 0, "alpha.4");
     SemanticVersion v130b = new SemanticVersion(1, 3, 0, "beta");
     SemanticVersion v130bx = new SemanticVersion(1, 3, 0, "beta.x");
     Assert.True(v100a1 < v100a4); //Compare on pre-release
     Assert.False(v100a1 < v130b); //False, pre-release doesn't match
     Assert.True(v130b < v3); //Only one identifier has pre-release so do normal compare
 }
 public void CanIncrement()
 {
     SemanticVersion v1 = new SemanticVersion(1);
     SemanticVersion v11 = new SemanticVersion(1, 1);
     SemanticVersion v2 = new SemanticVersion(2);
     SemanticVersion v202 = new SemanticVersion(2, 0, 2);
     SemanticVersion v000a1 = new SemanticVersion(0, 0, 0, "alpha.1");
     SemanticVersion v000a2 = new SemanticVersion(0, 0, 0, "alpha.2");
     SemanticVersion v000a0 = new SemanticVersion(0, 0, 0, "alpha.0");
     SemanticVersion v090a1 = new SemanticVersion(0, 9, 0, "alpha.1");
     SemanticVersion v090b1 = new SemanticVersion(0, 9, 0, "beta.1");
     SemanticVersion v090b2 = new SemanticVersion(0, 9, 0, "beta.2");
     Assert.Equal((++v1).ToString(), "2");
     Assert.Equal((++v11).ToString(), "1.2");
     Assert.Equal((++v202).ToString(), "2.0.3");
     Assert.Equal((++v000a1).ToString(), "0.0.0.alpha.2");
     Assert.Equal((++v090b2).ToString(), "0.9.0.beta.3");
 }
 public void CanComparePrelease()
 {
     SemanticVersion v1 = new SemanticVersion(3, 5, 0, "alpha.1");
     SemanticVersion v2 = new SemanticVersion(3, 5, 0, "alpha.2");
     Assert.NotNull(v1);
 }
 public static bool Satisfies(SemanticVersion v, ComparatorSet s)
 {
     return InvokeBinaryExpression(GetBinaryExpression(v, s));
 }
        public static bool RangeIntersect(ExpressionType left_operator, SemanticVersion left, ExpressionType right_operator, SemanticVersion right)
        {
            if (left_operator != ExpressionType.LessThan && left_operator != ExpressionType.LessThanOrEqual &&
                    left_operator != ExpressionType.GreaterThan && left_operator != ExpressionType.GreaterThanOrEqual
                    && left_operator != ExpressionType.Equal)
                throw new ArgumentException("Unsupported left operator expression type " + left_operator.ToString() + ".");
            if (right_operator != ExpressionType.LessThan && right_operator != ExpressionType.LessThanOrEqual &&
                   right_operator != ExpressionType.GreaterThan && right_operator != ExpressionType.GreaterThanOrEqual
                   && right_operator != ExpressionType.Equal)
                throw new ArgumentException("Unsupported left operator expression type " + left_operator.ToString() + ".");

            if (left_operator == ExpressionType.Equal)
            {
                return InvokeBinaryExpression(GetBinaryExpression(right_operator, left, right));
            }
            else if (right_operator == ExpressionType.Equal)
            {
                return InvokeBinaryExpression(GetBinaryExpression(left_operator, right, left));
            }

            if ((left_operator == ExpressionType.LessThan || left_operator == ExpressionType.LessThanOrEqual)
                && (right_operator == ExpressionType.LessThan || right_operator == ExpressionType.LessThanOrEqual))
            {
                return true;
            }
            else if ((left_operator == ExpressionType.GreaterThan || left_operator == ExpressionType.GreaterThanOrEqual)
                && (right_operator == ExpressionType.GreaterThan || right_operator == ExpressionType.GreaterThanOrEqual))
            {
                return true;
            }

            else if ((left_operator == ExpressionType.LessThanOrEqual) && (right_operator == ExpressionType.GreaterThanOrEqual))
            {
                return right <= left;
            }

            else if ((left_operator == ExpressionType.GreaterThanOrEqual) && (right_operator == ExpressionType.LessThanOrEqual))
            {
                return right >= left;
            }


            else if ((left_operator == ExpressionType.LessThan || left_operator == ExpressionType.LessThanOrEqual)
                && (right_operator == ExpressionType.GreaterThan || right_operator == ExpressionType.GreaterThanOrEqual))
            {
                return right < left;
            }

            else
            {
                return right > left;
            }
        }
 public static BinaryExpression GetBinaryExpression(SemanticVersion left, ComparatorSet right)
 {
     if (right.Count() == 0)
     {
         return GetBinaryExpression(ExpressionType.Equal, left, left);
     }
     else
     {
         BinaryExpression c = null;
         foreach (Comparator r in right)
         {
             if (c == null)
             {
                 c = GetBinaryExpression(r.Operator, left, r.Version);
             }
             else
             {
                 c = Expression.AndAlso(c, GetBinaryExpression(r.Operator, left, r.Version));
             }
         }
         return c;
     }
 }
        public static BinaryExpression GetBinaryExpression(ExpressionType et, SemanticVersion left, SemanticVersion right)
        {
            if (ReferenceEquals(right, null)) throw new ArgumentNullException("Right operand cannot be null.");
            if (ReferenceEquals(left, null)) throw new ArgumentNullException("Left operand cannot be null.");
            ConstantExpression zero = Expression.Constant(0, typeof(int));
            ConstantExpression l = Expression.Constant(left, typeof(SemanticVersion));
            ConstantExpression r = Expression.Constant(right, typeof(SemanticVersion));
            ConstantExpression l_major = Expression.Constant(left.Major, typeof(int));
            ConstantExpression l_minor = left.Minor.HasValue ? Expression.Constant(left.Minor, typeof(int)) : Expression.Constant(0, typeof(int));
            ConstantExpression l_patch = left.Patch.HasValue ? Expression.Constant(left.Patch, typeof(int)) : Expression.Constant(0, typeof(int));
            ConstantExpression l_prerelease = Expression.Constant(left.PreRelease, typeof(PreRelease));
            ConstantExpression r_major = Expression.Constant(right.Major, typeof(int));
            ConstantExpression r_minor = right.Minor.HasValue ? Expression.Constant(right.Minor, typeof(int)) : zero;
            ConstantExpression r_patch = right.Patch.HasValue ? Expression.Constant(right.Patch, typeof(int)) : zero;
            ConstantExpression r_prerelease = Expression.Constant(right.PreRelease, typeof(PreRelease));
            BinaryExpression a = Expression.MakeBinary(et, l_major, r_major);
            BinaryExpression b = Expression.MakeBinary(et, l_minor, r_minor);
            BinaryExpression c = Expression.MakeBinary(et, l_patch, r_patch);
            BinaryExpression d = Expression.MakeBinary(et, l_prerelease, r_prerelease);
            if (et == ExpressionType.Equal || et == ExpressionType.NotEqual)
            {
                return Expression.AndAlso(Expression.AndAlso(a, Expression.AndAlso(b, c)), d);
            }
            else if (et == ExpressionType.LessThan || et == ExpressionType.GreaterThan)
            {
                if (!ReferenceEquals(right.PreRelease, null) && !ReferenceEquals(left.PreRelease, null))//Major + minor + patch + prerelease
                {
                    return Expression.AndAlso(Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major),
                        Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), Expression.MakeBinary(ExpressionType.Equal, l_patch, r_patch)), d);
                }
                else
                {
                    return Expression.OrElse(a,
                                Expression.OrElse(//  or major = major and b
                                    Expression.OrElse(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major), b),
                                        Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major),
                                        Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), c)),
                                    Expression.AndAlso(Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major),
                                    Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), Expression.MakeBinary(ExpressionType.Equal, l_patch, r_patch)), d))
                            );
                }
                /*
                if (!right.Minor.HasValue) //Major only
                {
                    return a;
                }
                else if (!right.Patch.HasValue) //Major + minor
                {
                    return Expression.OrElse(a,
                        Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major), b));
                }

                else if (ReferenceEquals(right.PreRelease, null) && ReferenceEquals(left.PreRelease, null))//Major + minor + patch only
                {
                    return Expression.OrElse(a,
                            Expression.OrElse(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major), b),
                                Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major), 
                                Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), c)
                            ));
                }

                else if (!ReferenceEquals(right.PreRelease, null) && !ReferenceEquals(left.PreRelease, null))//Major + minor + patch + prerelease
                {
                    return Expression.AndAlso(Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major),
                        Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), Expression.MakeBinary(ExpressionType.Equal, l_patch, r_patch)), d);
                }

                else //Major + minor + patch + 1 preonly
                {
                    return Expression.OrElse(a,
                            Expression.OrElse(//  or major = major and b
                                Expression.OrElse(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major), b),
                                    Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major), 
                                    Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), c)),         
                                Expression.AndAlso(Expression.AndAlso(Expression.AndAlso(Expression.MakeBinary(ExpressionType.Equal, l_major, r_major),
                                Expression.MakeBinary(ExpressionType.Equal, l_minor, r_minor)), Expression.MakeBinary(ExpressionType.Equal, l_patch, r_patch)), d))
                        );
                }
                //                left.Major > right.Major ||
                //                    (left.Major == right.Major) && (left.Minor > right.Minor) ||
                //                    (left.Major == right.Major) && (left.Minor == right.Minor) && (left.Patch > right.Patch);
                */
 
            }

            else if (et == ExpressionType.LessThanOrEqual)
            {
                return Expression.OrElse(GetBinaryExpression(ExpressionType.LessThan, left, right), GetBinaryExpression(ExpressionType.Equal, left, right));
            }

            else if (et == ExpressionType.GreaterThanOrEqual)
            {
                return Expression.OrElse(GetBinaryExpression(ExpressionType.GreaterThan, left, right), GetBinaryExpression(ExpressionType.Equal, left, right));
            }


            else if (et == ExpressionType.OnesComplement)
            {
                return Expression.AndAlso(Expression.MakeBinary(ExpressionType.GreaterThan, l, r),
                    Expression.MakeBinary(ExpressionType.LessThanOrEqual, l, Expression.MakeUnary(ExpressionType.OnesComplement, r, null)));
            }
                       
            else throw new ArgumentException("Unimplemented expression type: " + et.ToString() + ".");
        }
 public void CanDecrement()
 {
     SemanticVersion v1 = new SemanticVersion(1, 0, 0, "alpha.1");
     SemanticVersion v2 = new SemanticVersion(1, 3, 4, "alpha.2.0");
     SemanticVersion v3 = new SemanticVersion(0, 0, 0, "beta");
     SemanticVersion v4 = new SemanticVersion(0, 0, 0, "beta.x.0");
     PreRelease v11 = --(v1.PreRelease);
     PreRelease v21 = --(v2.PreRelease);
     Assert.Equal(v11.Count, 1);
     Assert.Equal(v11.ToString(), "alpha");
     Assert.Equal(v21.ToString(), "alpha.1");
     Assert.Equal((--v3).ToString(), "0.0.0");
     Assert.Equal((--v4).ToString(), "0.0.0.beta");            
 }
 public void CanCompareEqual()
 {
     SemanticVersion v1 = new SemanticVersion(1, 0, 0, "alpha2");
     SemanticVersion v2 = new SemanticVersion(3, 5, 0, "alpha2");
     Assert.Equal(v1.PreRelease, v2.PreRelease);
 }