require "vnd.dovecot.testsuite";
require "relational";
require "foreverypart";
require "mime";
require "variables";
require "include";

test_set "message" text:
From: Hendrik <hendrik@example.com>
To: Harrie <harrie@example.com>
Date: Sat, 11 Oct 2010 00:31:44 +0200
Subject: Harrie is een prutser
Content-Type: multipart/mixed; boundary=AA
X-Test: AA

This is a multi-part message in MIME format.
--AA
Content-Type: multipart/mixed; boundary=BB
X-Test: BB

This is a multi-part message in MIME format.
--BB
Content-Type: text/plain; charset="us-ascii"
X-Test: CC

Hello

--BB
Content-Type: text/plain; charset="us-ascii"
X-Test: DD

Hello again

--BB--
This is the end of MIME multipart.

--AA
Content-Type: text/plain; charset="us-ascii"
X-Test: EE

And again

--AA--
This is the end of  MIME multipart.
.
;

test "Single loop" {
	set "a" "a";
	foreverypart {
		set :length "la" "${a}";

		if string "${a}" "a" {
			if not header :mime "X-Test" "AA" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aa" {
			if not header :mime "X-Test" "BB" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaa" {
			if not header :mime "X-Test" "CC" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaaa" {
			if not header :mime "X-Test" "DD" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaaaa" {
			if not header :mime "X-Test" "EE" {
				test_fail "wrong header extracted (${la})";
			}
		}
		set "a" "a${a}";
	}
}

test "Double loop" {
	set "a" "a";
	foreverypart {
		set :length "la" "${a}";

		if string "${a}" "a" {
			if not header :mime "X-Test" "AA" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaaaaa" {
			if not header :mime "X-Test" "BB" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaaaaaaaa" {
			if not header :mime "X-Test" "CC" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaaaaaaaaa" {
			if not header :mime "X-Test" "DD" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${a}" "aaaaaaaaaaa" {
			if not header :mime "X-Test" "EE" {
				test_fail "wrong header extracted (${la})";
			}
		}

		set "a" "a${a}";

		foreverypart {
			set :length "la" "${a}";

			if string "${a}" "aa" {
				if not header :mime "X-Test" "BB" {
					test_fail "wrong header extracted (${la})";
				}
			} elsif string "${a}" "aaa" {
				if not header :mime "X-Test" "CC" {
					test_fail "wrong header extracted (${la})";
				}
			} elsif string "${a}" "aaaa" {
				if not header :mime "X-Test" "DD" {
					test_fail "wrong header extracted (${la})";
				}
			} elsif string "${a}" "aaaaa" {
				if not header :mime "X-Test" "EE" {
					test_fail "wrong header extracted (${la})";
				}
			} elsif string "${a}" "aaaaaaa" {
				if not header :mime "X-Test" "CC" {
					test_fail "wrong header extracted (${la})";
				}
			} elsif string "${a}" "aaaaaaaa" {
				if not header :mime "X-Test" "DD" {
					test_fail "wrong header extracted (${la})";
				}
			}
			set "a" "a${a}";
		}
	}
}

test "Double loop - include" {
	global "in";
	global "error";
	set "in" "a";
	foreverypart {
		set :length "la" "${in}";

		if string "${in}" "in" {
			if not header :mime "X-Test" "AA" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${in}" "aaaaaa" {
			if not header :mime "X-Test" "BB" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${in}" "aaaaaaaaa" {
			if not header :mime "X-Test" "CC" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${in}" "aaaaaaaaaa" {
			if not header :mime "X-Test" "DD" {
				test_fail "wrong header extracted (${la})";
			}
		} elsif string "${in}" "aaaaaaaaaaa" {
			if not header :mime "X-Test" "EE" {
				test_fail "wrong header extracted (${la})";
			}
		}

		set "in" "a${in}";

		include "include-foreverypart";

		if not string "${error}" "" {
			test_fail "INCLUDED: ${error}";
		}
	}
}

