Example #1
0
        public void OkDoesNotEqualDifferentOk()
        {
            var x = IntResult.Ok(22);
            var y = IntResult.Ok(1);

            Assert.AreNotEqual(x, y);
        }
Example #2
0
        public void EqualsIsCommunitive()
        {
            var x = IntResult.Ok(22);
            var y = IntResult.Ok(22);

            Assert.AreEqual(x.Equals(y), y.Equals(x));
        }
Example #3
0
        public void ComprehensionsWork()
        {
            var result =
                from x in IntResult.Ok(2)
                select x + 1;

            Assert.AreEqual(IntResult.Ok(3), result);

            var error =
                from x in IntResult.Err("boom")
                select x + 1;

            Assert.AreEqual(IntResult.Err("boom"), error);

            var error2 =
                from x in IntResult.Ok(3)
                from y in IntResult.Err("boom")
                select x + y;

            Assert.AreEqual(IntResult.Err("boom"), error2);

            var result2 =
                from x in IntResult.Ok(3)
                from y in IntResult.Ok(4)
                select x + y;

            Assert.AreEqual(result2, IntResult.Ok(7));
        }
Example #4
0
        public void EqualsAnotherOk()
        {
            var x = IntResult.Ok(22);
            var y = IntResult.Ok(22);

            Assert.AreEqual(x, y);
        }
Example #5
0
        public void SatisfiesMonadRightIdentityLaw()
        {
            var m = IntResult.Ok(2);

            // m >>= return === m
            Assert.AreEqual(m.Bind(IntResult.Ok), m);
        }
Example #6
0
        public void OkDoesNotEqualError()
        {
            var x = IntResult.Ok(1);
            var y = IntResult.Err("bar");

            Assert.AreNotEqual(x, y);
        }
Example #7
0
        public void MapErrPassesThroughOks()
        {
            var actual =
                IntResult.Ok(2)
                .MapErr(e => new SomeError(e));

            Assert.AreEqual(Result <int, SomeError> .Ok(2), actual);
        }
Example #8
0
        public void SatisfiesFunctorIdentityLaw()
        {
            int identity(int a) => a;

            var result = IntResult.Ok(23);

            Assert.AreEqual(result, result.Map(identity));
        }
Example #9
0
        public void BindMapsFromOneResultTypeToAnother()
        {
            var result = IntResult.Ok(21);
            var actual =
                result
                .Bind(r => DoubleResult.Ok(r + 1.0));

            Assert.AreEqual(22.0, actual.Unwrap());
        }
Example #10
0
        public void SatisfiesMonadLeftidentityLaw()
        {
            IntResult add1(int a) => IntResult.Ok(a + 1);

            var a = 1;

            // return a >>= f === f a
            Assert.AreEqual(IntResult.Ok(a).Bind(add1), add1(a));
        }
Example #11
0
        public void IsAFunctor()
        {
            var a = IntResult.Ok(2);
            var b = IntResult.Ok(3);

            var actual = a.Map((a) => a + 3);

            Assert.AreEqual(IntResult.Ok(5), actual);
        }
Example #12
0
        public IntResult LoginStudent(string username, string password)
        {
            var encrypt_key     = configuration.GetEncryptPassword();
            var encodedPassword = FMCW.Seguridad.Encriptador.Encriptar(password, encrypt_key);
            var user            = context.Student.FirstOrDefault(s => s.Username == username && s.Password == encodedPassword);

            return(user != null?
                   IntResult.Ok(user.IdStudent) :
                       IntResult.Error(new Exception("Wrong user or password")));
        }
Example #13
0
        public void BindRailroadsErrors()
        {
            var result = IntResult.Ok(21);
            var actual =
                result
                .Bind(r => IntResult.Err("boom"))
                .Bind(r => IntResult.Ok(r + 1));

            Assert.AreEqual("boom", actual.Error());
        }
Example #14
0
        public void EqualsIsAssociative()
        {
            var x = IntResult.Ok(22);
            var y = IntResult.Ok(22);
            var z = IntResult.Ok(22);

            var left = x.Equals(y) && y.Equals(z);

            Assert.AreEqual(left, x.Equals(z));
        }
Example #15
0
        public void SatisfiesFunctorCompositionLaw()
        {
            int add1(int a) => a;
            int mult2(int a) => a;

            var result = IntResult.Ok(3);

            Assert.AreEqual(
                result.Map(x => add1(mult2(x))),
                result.Map(add1).Map(mult2)
                );
        }
Example #16
0
        public IntResult ValidateToken(string token)
        {
            try
            {
                string issuer   = _jwtConfiguration.Issuer;
                string audience = _jwtConfiguration.Audience;

                var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtConfiguration.SecretKey));

                var tokenValidationParamters = new TokenValidationParameters
                {
                    ValidateAudience = true,
                    ValidateIssuer   = true,
                    ValidateActor    = false,
                    ValidateLifetime = false,
                    IssuerSigningKey = secretKey
                };

                var           tokenHandler = new JwtSecurityTokenHandler();
                SecurityToken securityToken;
                var           principal = tokenHandler.ValidateToken(token, tokenValidationParamters, out securityToken);

                JwtSecurityToken jwtSecurityToken = securityToken as JwtSecurityToken;

                if (jwtSecurityToken == null || !jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(IntResult.Error("Invalid token"));
                }

                var userId = int.Parse(principal.FindFirst("IdUsuario")?.Value ?? "-1");

                if (userId == -1)
                {
                    return(IntResult.Error("Missing claim"));
                }

                if (jwtSecurityToken.ValidTo < DateTime.UtcNow)
                {
                    return(IntResult.Error("Expired token"));
                }
                else
                {
                    return(IntResult.Ok(userId));
                }
            }
            catch (Exception ex)
            {
                return(IntResult.Error(ex));
            }
        }
Example #17
0
        public void MapOrElseMapsOks()
        {
            var expected =
                Result <int, SomeError> .Ok(4);

            var actual =
                IntResult.Ok(2)
                .MapOrElse(
                    x => x * 2,
                    e => new SomeError(e)
                    );

            Assert.AreEqual(expected, actual);
        }
Example #18
0
        public void SatisfiesMonadAssociativityLaw()
        {
            // (m >>= f) >>= g === m >>= (\x -> f x >>= g)
            var m = IntResult.Ok(1);

            IntResult f(int a) => IntResult.Ok(a + 1);
            IntResult g(int a) => IntResult.Ok(a * 2);

            var left =
                m.Bind(f).Bind(g);
            Func <int, IntResult> right =
                (x) => f(x).Bind(g);

            Assert.AreEqual(left, right(1));
        }
Example #19
0
        public IntResult Add(TranslateDTO wordDto, int userId)
        {
            // check if word exists
            var word = context.Word.Where(w => w.Meaning == wordDto.OriginalMeaning && w.IdLanguage == wordDto.IdLanguage)
                       .Include(w => w.Translation)
                       .FirstOrDefault();

            if (word == null)
            {
                word = new Word
                {
                    IdLanguage  = wordDto.IdLanguage,
                    Meaning     = wordDto.OriginalMeaning,
                    IdStudent   = userId,
                    SearchValue = Helpers.GetSearchValue(wordDto.OriginalMeaning)
                };
                context.Word.Add(word);
            }
            // check if translation already exists
            else if (word.Translation.Any(t => t.Translate == wordDto.TranslateMeaning))
            {
                return(new IntResult
                {
                    ResultOperation = ResultOperation.RegisterAlreadyAdd,
                    Success = true
                });
            }

            var translation = new Translation
            {
                IdWordNavigation = word,
                Translate        = wordDto.TranslateMeaning,
                SearchValue      = Helpers.GetSearchValue(wordDto.TranslateMeaning),
                Score            = 0
            };

            context.Translation.Add(translation);

            context.SaveChanges();
            return(IntResult.Ok(word.IdWord));
        }
Example #20
0
        public void EqualsItself()
        {
            var result = IntResult.Ok(22);

            Assert.AreEqual(result, result);
        }
Example #21
0
 public void DoesNotEqualNull()
 {
     Assert.AreNotEqual(IntResult.Ok(1), null);
 }