require "vnd.dovecot.testsuite";
require "date";
require "variables";
require "relational";

test_set "message" text:
From: stephan@example.org
To: sirius@friep.example.com
Subject: Frop!
Date: Mon, 20 Jul 2009 21:44:43 +0300
Delivery-Date: Mon, 22 Jul 2009 23:30:14 +0300
Invalid-Date: Moo, 34 Juul 3060 25:30:42 +6600
Wanna date?
.
;

test "Defaults" {
	if not date :originalzone "date" "std11" "mon, 20 jul 2009 21:44:43 +0300" {
		test_fail "default comparator is not i;ascii-casemap";
	}

	if anyof ( date "date" "std11" "Mon", date "date" "std11" "*") {
		test_fail "default match type appears to be :contains or :matches";
	}
}

test "Count" {
	if not date :count "eq" "date" "date" "1" {
		test_fail "count of existing date header field is not 1";
	}

	if not date :count "eq" "resent-date" "date" "0" {
		test_fail "count of non-existent date header field is not 0";
	}
}

test "Invalid" {
	if date :matches "invalid-date" "std11" "*" {
		test_fail "matched invalid date: ${0}";
	}
}

test "Comparison" {
	if not date :originalzone :is "delivery-date" "date" "2009-07-22" {
		if date :originalzone :matches "delivery-date" "date" "*" { set "date" "${1}"; }
		test_fail "date is invalid: ${date}";
	}
	if not date :originalzone :value "ge" "delivery-date" "date" "2009-07-22" {
		test_fail "date comparison ge failed equal";
	}

	if not date :originalzone :value "ge" "delivery-date" "date" "2009-07-21" {
		test_fail "date comparison ge failed greater";
	}

	if anyof (not date :originalzone :value "ge" "delivery-date" "date" "2009-06-22",
		not date :originalzone :value "ge" "date" "date" "2006-07-22" ) {
		test_fail "date comparison ge failed much greater";
	}

	if not date :originalzone :value "le" "delivery-date" "date" "2009-07-22" {
		test_fail "date comparison le failed equal";
	}

	if not date :originalzone :value "le" "delivery-date" "date" "2009-07-23" {
		test_fail "date comparison le failed less";
	}

	if anyof (not date :originalzone :value "le" "delivery-date" "date" "2009-09-22",
		not date :originalzone :value "le" "date" "date" "2012-07-22" ) {
		test_fail "date comparison ge failed much less";
	}
}
