diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2011-11-10 16:33:40 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2011-11-22 14:44:48 +0000 |
commit | 20359767c6a7b0e091f0132baac9974727991cc6 (patch) | |
tree | d5195948175af2ad8a32f6e7c7a47eedbfa1b353 | |
parent | 328f866c380f504ba93825c2e595096b39221dda (diff) | |
download | libguestfs-20359767c6a7b0e091f0132baac9974727991cc6.tar.gz libguestfs-20359767c6a7b0e091f0132baac9974727991cc6.tar.xz libguestfs-20359767c6a7b0e091f0132baac9974727991cc6.zip |
generator: Support testing the output of RHashtable functions.
You can use TestOutputHashtable to test the output of RHashtable
functions.
(cherry picked from commit 5f0b75f040b118c4394466438adc99f38144722d)
-rw-r--r-- | generator/generator_capitests.ml | 40 | ||||
-rw-r--r-- | generator/generator_types.ml | 6 | ||||
-rw-r--r-- | generator/generator_utils.ml | 1 |
3 files changed, 47 insertions, 0 deletions
diff --git a/generator/generator_capitests.ml b/generator/generator_capitests.ml index 7bfd87cd..fefc6e22 100644 --- a/generator/generator_capitests.ml +++ b/generator/generator_capitests.ml @@ -113,6 +113,24 @@ md5sum (const char *filename, char *result) result[32] = '\\0'; } +#if 0 /* <- Remove this if we add RHashtable tests in 1.14 branch. */ +/* Return the value for a key in a hashtable. + * Note: the return value is part of the hash and should not be freed. + */ +static const char * +get_key (char **hash, const char *key) +{ + size_t i; + + for (i = 0; hash[i] != NULL; i += 2) { + if (STREQ (hash[i], key)) + return hash[i+1]; + } + + return NULL; /* key not found */ +} +#endif + "; (* Generate a list of commands which are not tested anywhere. *) @@ -695,6 +713,28 @@ and generate_one_test_body name i test_name init test = in List.iter (generate_test_command_call test_name) seq; generate_test_command_call ~test test_name last + | TestOutputHashtable (seq, fields) -> + pr " /* TestOutputHashtable for %s (%d) */\n" name i; + pr " const char *key, *expected, *value;\n"; + let seq, last = get_seq_last seq in + let test () = + List.iter ( + fun (key, value) -> + pr " key = \"%s\";\n" (c_quote key); + pr " expected = \"%s\";\n" (c_quote value); + pr " value = get_key (r, key);\n"; + pr " if (value == NULL) {\n"; + pr " fprintf (stderr, \"%s: key \\\"%%s\\\" not found in hash: expecting \\\"%%s\\\"\\n\", key, expected);\n" test_name; + pr " return -1;\n"; + pr " }\n"; + pr " if (STRNEQ (value, expected)) {\n"; + pr " fprintf (stderr, \"%s: key \\\"%%s\\\": expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", key, expected, value);\n" test_name; + pr " return -1;\n"; + pr " }\n"; + ) fields + in + List.iter (generate_test_command_call test_name) seq; + generate_test_command_call ~test test_name last | TestLastFail seq -> pr " /* TestLastFail for %s (%d) */\n" name i; let seq, last = get_seq_last seq in diff --git a/generator/generator_types.ml b/generator/generator_types.ml index 9da7e45a..94592991 100644 --- a/generator/generator_types.ml +++ b/generator/generator_types.ml @@ -290,6 +290,12 @@ and test = *) | TestOutputDevice of seq * string + (* Run the command sequence and expect a hashtable. Check + * one of more fields in the hashtable against known good + * strings. + *) + | TestOutputHashtable of seq * (string * string) list + (* Run the command sequence and expect the final command (only) * to fail. *) diff --git a/generator/generator_utils.ml b/generator/generator_utils.ml index 4180c0d6..aa7fcba3 100644 --- a/generator/generator_utils.ml +++ b/generator/generator_utils.ml @@ -264,6 +264,7 @@ let seq_of_test = function | TestOutputStruct (s, _) | TestOutputFileMD5 (s, _) | TestOutputDevice (s, _) + | TestOutputHashtable (s, _) | TestLastFail s -> s let c_quote str = |