public void TranslateFromAndToQuery()
        {
            using (var client = new ImapClient()) {
                var translator = new ImapQueryTranslator();
                var expression = client.Messages.Where(x => x.From.Contains("Foo") && x.To.Contains("Bar")).Expression;

                const string expected = "SEARCH FROM \"Foo\" TO \"Bar\"";

                var result = translator.Translate(expression);
                Assert.IsFalse(result.IsUid);
                Assert.AreEqual(expected, result.SearchCommand);
            }
        }
        public void TranslateHasHeaderField()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => x.Headers.Any(y => y.Name.Contains("Foo") && y.Value.Contains("Bar"))).Expression;
            }

            const string expected = "SEARCH HEADER \"Foo\" \"Bar\"";

            var result = target.Translate(expression);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchUnseen()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => !x.Flags.HasFlag(MessageFlags.Seen)).Expression;
            }

            const string expected = "SEARCH NOT SEEN";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchUnseenTextKeyword()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => !x.Flags.HasFlag(MessageFlags.Seen)
                                                        && x.Text.Contains("Peter File")
                                                        && x.Keywords.Contains("Cheers")
                                                        && !x.Keywords.Contains("Cheerio")).Expression;
            }

            const string expected = "SEARCH NOT SEEN TEXT \"Peter File\" KEYWORD Cheers NOT KEYWORD Cheerio";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchIsOldAndNotIsNew()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => x.IsNew && !x.IsOld).Expression;
            }

            const string expected = "SEARCH (RECENT UNSEEN) NOT NOT RECENT";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchSeenOperator()
        {
            try {
                using (var client = new ImapClient()) {
                    var target = new ImapQueryTranslator();
                    var expression = client.Messages.Where(x => x.Flags == MessageFlags.Seen).Expression;

                    target.Translate(expression);
                }
                Assert.Fail();
            }
            catch (NotSupportedException) {
                Assert.IsTrue(true);
            }
        }
        public void TranslateTestSearchDateSinceSeenOrAnswered()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                var date = DateTime.Parse("1-Feb-1994");
                target = new ImapQueryTranslator();

                expression = client.Messages.Where(x => x.Date < date && (x.Flags.HasFlag(MessageFlags.Seen) || x.Flags.HasFlag(MessageFlags.Answered))).Expression;

                // we need to evaluate the field date into a constant.
                expression = Evaluator.PartialEval(expression);
            }

            const string expected = "SEARCH SENTBEFORE 1-Feb-1994 OR SEEN ANSWERED";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateSingleUidQuery()
        {
            using (var client = new ImapClient()) {
                var translator = new ImapQueryTranslator();
                var expression = client.Messages.Where(x => x.Uid == 5).Expression;

                const string expected = "UID SEARCH UID 5";

                var result = translator.Translate(expression);
                Assert.IsTrue(result.IsUid);
                Assert.AreEqual(expected, result.SearchCommand);
            }
        }
        public void TranslateSingleSequenceNumberQuery()
        {
            using (var client = new ImapClient()) {
                var translator = new ImapQueryTranslator();
                var expression = client.Messages.Where(x => x.SequenceNumber == 5).Expression;

                const string expected = "SEARCH 5";

                var result = translator.Translate(expression);
                Assert.IsFalse(result.IsUid);
                Assert.AreEqual(expected, result.SearchCommand);
            }
        }
        public void TranslateSeperatedUidQueriesOpenEnd()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => x.Uid >= 1).Expression;
            }

            const string expected = "UID SEARCH UID 1:*";

            var resuult = target.Translate(expression);
            Assert.AreEqual(expected, resuult.SearchCommand);
        }
 public void TranslateNegativeOperatorSeenFlags()
 {
     using (var client = new ImapClient()) {
         var translator = new ImapQueryTranslator();
         var expression = client.Messages.Where(x => x.Flags == MessageFlags.Seen).Expression;
         SearchTranslationResult result = null;
         try {
             result = translator.Translate(expression);
             Assert.Fail("This call must throw a NotSupportedException.");
         }
         catch (NotSupportedException) {
             Assert.IsTrue(true);
             if (result != null) {
                 Assert.IsFalse(result.IsUid);
             }
         }
     }
 }