From: Stefan Beller <sbeller@google.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: "git@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: [PATCH 3/3] test-parse-options: --expect=<string> option to simplify tests
Date: Thu, 5 May 2016 22:51:50 -0700 [thread overview]
Message-ID: <CAGZ79kZ59K5BoSVsbt4YM-Try9Q1CVdFeBW8GE5E1dJpSBWzVA@mail.gmail.com> (raw)
In-Reply-To: <xmqqeg9f7v1l.fsf@gitster.mtv.corp.google.com>
On Thu, May 5, 2016 at 7:57 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Stefan Beller <sbeller@google.com> writes:
>
>> instead of filtering afterwards, i.e. each strbuf_add is guarded by
>> an
>>
>> if (is_interesting_output(...))
>> strbuf_add(...)
>
> That's a good approach.
>
> The implementation gets a bit trickier than the previous one, but it
> would look like this. Discard 2/3 and 3/3 and replace them with
> this one.
>
> The external interface on the input side is no different, but on the
> output side, this version has "expected '%s', got '%s'" error, in
> the same spirit as the output from "test_cmp", added in.
>
> Instead of checking the entire output line-by-line for each expected
> output (in case you did not notice, you can give --expect='quiet: 3'
> --expect='abbrev: 7' and both must match), this one will check each
> output line against each expected pattern. We wouldn't have too
> many entries in the variable dump and we wouldn't be taking too many
> --expect options, so the matching performance would not matter,
> though.
>
>
> t/t0040-parse-options.sh | 1 +
> test-parse-options.c | 88 ++++++++++++++++++++++++++++++++++++++++--------
> 2 files changed, 75 insertions(+), 14 deletions(-)
>
> diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
> index dbaee55..d678fbf 100755
> --- a/t/t0040-parse-options.sh
> +++ b/t/t0040-parse-options.sh
> @@ -45,6 +45,7 @@ Standard options
> -v, --verbose be verbose
> -n, --dry-run dry run
> -q, --quiet be quiet
> + --expect <string> expected output in the variable dump
>
> EOF
>
> diff --git a/test-parse-options.c b/test-parse-options.c
> index b5f4e90..e3f25df 100644
> --- a/test-parse-options.c
> +++ b/test-parse-options.c
> @@ -39,6 +39,61 @@ static int number_callback(const struct option *opt, const char *arg, int unset)
> return 0;
> }
>
> +static int collect_expect(const struct option *opt, const char *arg, int unset)
> +{
> + struct string_list *expect;
> + struct string_list_item *item;
> + struct strbuf label = STRBUF_INIT;
> + const char *colon;
> +
> + if (!arg || unset)
> + die("malformed --expect option");
> +
> + expect = (struct string_list *)opt->value;
> + colon = strchr(arg, ':');
> + if (!colon)
> + die("malformed --expect option, lacking a colon");
> + strbuf_add(&label, arg, colon - arg);
> + item = string_list_insert(expect, strbuf_detach(&label, NULL));
> + if (item->util)
> + die("malformed --expect option, duplicate %s", label.buf);
> + item->util = (void *)arg;
> + return 0;
> +}
> +
> +__attribute__((format (printf,3,4)))
> +static void show(struct string_list *expect, int *status, const char *fmt, ...)
> +{
> + struct string_list_item *item;
> + struct strbuf buf = STRBUF_INIT;
> + va_list args;
> +
> + va_start(args, fmt);
> + strbuf_vaddf(&buf, fmt, args);
> + va_end(args);
> +
> + if (!expect->nr)
> + printf("%s\n", buf.buf);
> + else {
> + char *colon = strchr(buf.buf, ':');
> + if (!colon)
> + die("malformed output format, output lacking colon: %s", fmt);
> + *colon = '\0';
> + item = string_list_lookup(expect, buf.buf);
> + *colon = ':';
I have been staring at this for a good couple of minutes and wondered if this
low level string manipulation is really the best way to do it.
(It feels very C idiomatic, not using a lot of Gits own data
structures. I would have
expected some sort of skip_prefix just with partial regular expression or a
string_list_split_in_place for the splitting. But this "set and reset *colon"
seems to be optimal here)
> + if (!item)
> + ; /* not among entries being checked */
> + else {
> + if (strcmp((const char *)item->util, buf.buf)) {
> + printf("expected '%s', got '%s'\n",
> + (char *)item->util, buf.buf);
> + *status = 1;
> + }
> + }
> + }
> + strbuf_reset(&buf);
strbuf_release ?
> +}
> +
> int main(int argc, char **argv)
> {
> const char *prefix = "prefix/";
> @@ -46,6 +101,7 @@ int main(int argc, char **argv)
> "test-parse-options <options>",
> NULL
> };
> + struct string_list expect = STRING_LIST_INIT_NODUP;
> struct option options[] = {
> OPT_BOOL(0, "yes", &boolean, "get a boolean"),
> OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
> @@ -86,34 +142,38 @@ int main(int argc, char **argv)
> OPT__VERBOSE(&verbose, "be verbose"),
> OPT__DRY_RUN(&dry_run, "dry run"),
> OPT__QUIET(&quiet, "be quiet"),
> + OPT_CALLBACK(0, "expect", &expect, "string",
> + "expected output in the variable dump",
> + collect_expect),
> OPT_END(),
> };
> int i;
> + int ret = 0;
>
> argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
>
> if (length_cb.called) {
> const char *arg = length_cb.arg;
> int unset = length_cb.unset;
> - printf("Callback: \"%s\", %d\n",
> - (arg ? arg : "not set"), unset);
> + show(&expect, &ret, "Callback: \"%s\", %d",
> + (arg ? arg : "not set"), unset);
> }
> - printf("boolean: %d\n", boolean);
> - printf("integer: %d\n", integer);
> - printf("magnitude: %lu\n", magnitude);
> - printf("timestamp: %lu\n", timestamp);
> - printf("string: %s\n", string ? string : "(not set)");
> - printf("abbrev: %d\n", abbrev);
> - printf("verbose: %d\n", verbose);
> - printf("quiet: %d\n", quiet);
> - printf("dry run: %s\n", dry_run ? "yes" : "no");
> - printf("file: %s\n", file ? file : "(not set)");
> + show(&expect, &ret, "boolean: %d", boolean);
> + show(&expect, &ret, "integer: %d", integer);
> + show(&expect, &ret, "magnitude: %lu", magnitude);
> + show(&expect, &ret, "timestamp: %lu", timestamp);
> + show(&expect, &ret, "string: %s", string ? string : "(not set)");
> + show(&expect, &ret, "abbrev: %d", abbrev);
> + show(&expect, &ret, "verbose: %d", verbose);
> + show(&expect, &ret, "quiet: %d", quiet);
> + show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
> + show(&expect, &ret, "file: %s", file ? file : "(not set)");
>
> for (i = 0; i < list.nr; i++)
> - printf("list: %s\n", list.items[i].string);
> + show(&expect, &ret, "list: %s", list.items[i].string);
>
> for (i = 0; i < argc; i++)
> - printf("arg %02d: %s\n", i, argv[i]);
> + show(&expect, &ret, "arg %02d: %s", i, argv[i]);
>
> return 0;
return ret; ? Otherwise `ret` is unused.
> }
next prev parent reply other threads:[~2016-05-06 5:51 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-30 20:03 [PATCH v15 1/7] t0040-test-parse-options.sh: fix style issues Pranit Bauva
2016-04-30 20:03 ` [PATCH v15 2/7] test-parse-options: print quiet as integer Pranit Bauva
2016-04-30 20:03 ` [PATCH v15 3/7] t0040-parse-options: improve test coverage Pranit Bauva
2016-05-04 8:36 ` Eric Sunshine
2016-05-05 4:46 ` Pranit Bauva
2016-04-30 20:03 ` [PATCH v15 4/7] parse-options.c: make OPTION_COUNTUP respect "unspecified" values Pranit Bauva
2016-04-30 20:03 ` [PATCH v15 5/7] t7507-commit-verbose: improve test coverage by testing number of diffs Pranit Bauva
2016-04-30 20:03 ` [PATCH v15 6/7] commit: add a commit.verbose config variable Pranit Bauva
2016-04-30 20:03 ` [PATCH v15 7/7] t/t7507: tests for broken behavior of status Pranit Bauva
2016-05-02 23:07 ` Junio C Hamano
2016-05-03 3:39 ` Pranit Bauva
2016-05-03 5:12 ` Eric Sunshine
2016-05-03 6:42 ` Pranit Bauva
2016-05-03 6:49 ` Eric Sunshine
2016-05-03 9:18 ` Pranit Bauva
2016-05-03 16:17 ` Eric Sunshine
2016-05-03 16:18 ` Pranit Bauva
2016-05-03 15:47 ` Junio C Hamano
2016-05-05 9:49 ` [PATCH v16 0/7] config commit verbose Pranit Bauva
2016-05-05 9:49 ` [PATCH v16 1/7] t0040-test-parse-options.sh: fix style issues Pranit Bauva
2016-05-05 9:49 ` [PATCH v16 2/7] test-parse-options: print quiet as integer Pranit Bauva
2016-05-05 9:49 ` [PATCH v16 3/7] t0040-parse-options: improve test coverage Pranit Bauva
2016-05-05 9:49 ` [PATCH v16 4/7] t/t7507: " Pranit Bauva
2016-05-05 9:50 ` [PATCH v16 5/7] parse-options.c: make OPTION_COUNTUP respect "unspecified" values Pranit Bauva
2016-05-05 9:50 ` [PATCH v16 6/7] t7507-commit-verbose: improve test coverage by testing number of diffs Pranit Bauva
2016-05-05 9:50 ` [PATCH v16 7/7] commit: add a commit.verbose config variable Pranit Bauva
2016-05-05 19:14 ` Junio C Hamano
2016-05-06 5:05 ` Pranit Bauva
2016-05-06 6:40 ` Pranit Bauva
2016-05-06 5:07 ` Eric Sunshine
2016-05-05 19:21 ` [PATCH v16 0/7] config commit verbose Junio C Hamano
2016-05-05 21:50 ` [PATCH 0/3] test-parse-options update Junio C Hamano
2016-05-05 21:50 ` [PATCH 1/3] test-parse-options: fix output when callback option fails Junio C Hamano
2016-05-05 21:50 ` [PATCH 2/3] test-parse-options: hold output in a strbuf Junio C Hamano
2016-05-05 21:50 ` [PATCH 3/3] test-parse-options: --expect=<string> option to simplify tests Junio C Hamano
2016-05-06 0:41 ` Stefan Beller
2016-05-06 1:27 ` Eric Sunshine
2016-05-06 2:57 ` Junio C Hamano
2016-05-06 5:51 ` Stefan Beller [this message]
2016-05-06 7:18 ` Junio C Hamano
2016-05-06 17:34 ` Junio C Hamano
2016-05-06 18:00 ` [PATCH] t0040: remove unused test helpers Junio C Hamano
2016-05-06 5:30 ` [PATCH v16 0/7] config commit verbose Eric Sunshine
2016-05-06 14:20 ` SZEDER Gábor
2016-05-06 15:33 ` Junio C Hamano
2016-05-07 5:32 ` Jeff King
2016-05-07 19:28 ` Ævar Arnfjörð Bjarmason
2016-05-08 18:48 ` Junio C Hamano
2016-05-09 14:28 ` Jeff King
2016-05-09 16:01 ` Junio C Hamano
[not found] ` <CACBZZX5ssO2EiuxR7wotGowMaPhtioaJVSDpQDUwUkv1rLJJWw@mail.gmail.com>
2016-05-06 16:16 ` Pranit Bauva
2016-05-06 19:47 ` Ævar Arnfjörð Bjarmason
2016-05-06 20:51 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAGZ79kZ59K5BoSVsbt4YM-Try9Q1CVdFeBW8GE5E1dJpSBWzVA@mail.gmail.com \
--to=sbeller@google.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).