/* Tool for testing the CSS parser */
/* $Id: test.c,v 1.16 2003/01/30 03:36:36 fonseca Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

/* Placed here to get the right behaviour from utils */
#define DEBUG
#define LEAK_DEBUG

#include "util/error.h"
#include "util/string.h"
#include "util/memdebug.h"
#include "util/memory.h"
#include "viewer/common/messageline.h"

struct message_line_test {
	unsigned char *desc;
	unsigned char *format;
};

struct message_line_test message_line_tests[] = {
	{ "Test 1  - Plain string",	"A plain string" },
	{ "Test 2  - Only media type",	"Title [%c] and trimmed [%/c]" },
	{ "Test 3  - Only link number",	"Title [%n] and more" },
	{ "Test 4  - Width",		"Should be empty [%5c] should not [%10c]" },
	{ "Test 5  - Padding",		"Title (%05n) and more" },
	{ "Test 6a - Trimming",		"%20$t [%/c][%n]" },
	{ "Test 6b - Trimming",		"%20^t [%/c][%n]" },
	{ NULL, NULL },
};

int
main(int argc, char *argv[])
{
	unsigned char *format = NULL;
	struct message_line_test arg_tests[2];
	struct message_line_test *tests;
	int width = 40;
	int index = 1;

	while (index < argc && argv[index][0] == '-') {
		if (!strncasecmp(argv[index], "--width", 6)
		    || !strncasecmp(argv[index], "-w", 2)) {
			index++;
			width = atoi(argv[index++]);

		} else if (!strncasecmp(argv[index], "--string", 7)
			   || !strncasecmp(argv[index], "-s", 2)) {
			index++;
			format = argv[index++];
		}
	}

	if (format) {
		arg_tests[0].format = format;
		arg_tests[0].desc = "Parameter";
		arg_tests[1].format = NULL;
		arg_tests[1].desc = NULL;
		tests = arg_tests;
	} else {
		tests = message_line_tests;
	}

	index = 0;
	while (tests[index].format) {
		struct message_line_test test = tests[index++];
		struct message_line *line;

		printf("%-30s: \"%s\"\n", test.desc, test.format);
		line = init_message_line(test.format);
		if (line) {
			printf("%-30s  |%s|\n", "", get_message_line(line, width));
			free_message_line(line);
		}
		printf("\n");
	}

	return 0;
}
