Blame tests/buf/quote.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
Packit Service 20376f
static void expect_quote_pass(const char *expected, const char *str)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_puts(&buf, str));
Packit Service 20376f
	cl_git_pass(git_buf_quote(&buf));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
Packit Service 20376f
	cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_buf_quote__quote_succeeds(void)
Packit Service 20376f
{
Packit Service 20376f
	expect_quote_pass("", "");
Packit Service 20376f
	expect_quote_pass("foo", "foo");
Packit Service 20376f
	expect_quote_pass("foo/bar/baz.c", "foo/bar/baz.c");
Packit Service 20376f
	expect_quote_pass("foo bar", "foo bar");
Packit Service 20376f
	expect_quote_pass("\"\\\"leading quote\"", "\"leading quote");
Packit Service 20376f
	expect_quote_pass("\"slash\\\\y\"", "slash\\y");
Packit Service 20376f
	expect_quote_pass("\"foo\\r\\nbar\"", "foo\r\nbar");
Packit Service 20376f
	expect_quote_pass("\"foo\\177bar\"", "foo\177bar");
Packit Service 20376f
	expect_quote_pass("\"foo\\001bar\"", "foo\001bar");
Packit Service 20376f
	expect_quote_pass("\"foo\\377bar\"", "foo\377bar");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void expect_unquote_pass(const char *expected, const char *quoted)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_puts(&buf, quoted));
Packit Service 20376f
	cl_git_pass(git_buf_unquote(&buf));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
Packit Service 20376f
	cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void expect_unquote_fail(const char *quoted)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_puts(&buf, quoted));
Packit Service 20376f
	cl_git_fail(git_buf_unquote(&buf));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_buf_quote__unquote_succeeds(void)
Packit Service 20376f
{
Packit Service 20376f
	expect_unquote_pass("", "\"\"");
Packit Service 20376f
	expect_unquote_pass(" ", "\" \"");
Packit Service 20376f
	expect_unquote_pass("foo", "\"foo\"");
Packit Service 20376f
	expect_unquote_pass("foo bar", "\"foo bar\"");
Packit Service 20376f
	expect_unquote_pass("foo\"bar", "\"foo\\\"bar\"");
Packit Service 20376f
	expect_unquote_pass("foo\\bar", "\"foo\\\\bar\"");
Packit Service 20376f
	expect_unquote_pass("foo\tbar", "\"foo\\tbar\"");
Packit Service 20376f
	expect_unquote_pass("\vfoo\tbar\n", "\"\\vfoo\\tbar\\n\"");
Packit Service 20376f
	expect_unquote_pass("foo\nbar", "\"foo\\012bar\"");
Packit Service 20376f
	expect_unquote_pass("foo\r\nbar", "\"foo\\015\\012bar\"");
Packit Service 20376f
	expect_unquote_pass("foo\r\nbar", "\"\\146\\157\\157\\015\\012\\142\\141\\162\"");
Packit Service 20376f
	expect_unquote_pass("newline: \n", "\"newline: \\012\"");
Packit Service 20376f
	expect_unquote_pass("0xff: \377", "\"0xff: \\377\"");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_buf_quote__unquote_fails(void)
Packit Service 20376f
{
Packit Service 20376f
	expect_unquote_fail("no quotes at all");
Packit Service 20376f
	expect_unquote_fail("\"no trailing quote");
Packit Service 20376f
	expect_unquote_fail("no leading quote\"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\z escape char\"");
Packit Service 20376f
	expect_unquote_fail("\"\\q invalid escape char\"");
Packit Service 20376f
	expect_unquote_fail("\"invalid escape char \\p\"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\1 escape char \"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\14 escape char \"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\280 escape char\"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\378 escape char\"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\380 escape char\"");
Packit Service 20376f
	expect_unquote_fail("\"invalid \\411 escape char\"");
Packit Service 20376f
	expect_unquote_fail("\"truncated escape char \\\"");
Packit Service 20376f
	expect_unquote_fail("\"truncated escape char \\0\"");
Packit Service 20376f
	expect_unquote_fail("\"truncated escape char \\01\"");
Packit Service 20376f
}