1 #include "tap.h"
2 #include "test.h"
3 #include "bson.h"
4 
5 #include <string.h>
6 
7 void
test_bson_cursor_get_javascript_w_scope(void)8 test_bson_cursor_get_javascript_w_scope (void)
9 {
10   bson *b, *scope = NULL, *valid;
11   bson_cursor *c;
12   const gchar *s = "deadbeef";
13 
14   ok (bson_cursor_get_javascript_w_scope (NULL, &s, &scope) == FALSE,
15       "bson_cursor_get_javascript_w_scope() with a NULL cursor fails");
16 
17   b = test_bson_generate_full ();
18   c = bson_cursor_new (b);
19 
20   ok (bson_cursor_get_javascript_w_scope (c, NULL, &scope) == FALSE,
21       "bson_cursor_get_javascript_w_scope() with a NULL js destination fails");
22   ok (bson_cursor_get_javascript_w_scope (c, &s, NULL) == FALSE,
23       "bson_cursor_get_javascript_w_scope() with a NULL scope destinatin fails");
24   ok (bson_cursor_get_javascript_w_scope (c, &s, &scope) == FALSE,
25       "bson_cursor_get_javascript_w_scope() at the initial position fails");
26   is (s, "deadbeef",
27       "destination remains unchanged after failed cursor operations");
28   bson_cursor_free (c);
29 
30   c = bson_find (b, "print");
31   ok (bson_cursor_get_javascript_w_scope (c, &s, &scope),
32       "bson_cursor_get_javascript_w_scope() works");
33   is (s, "alert (v);",
34       "bson_cursor_get_javascript_w_scope() returns the correct result");
35 
36   valid = bson_new ();
37   bson_append_string (valid, "v", "hello world", -1);
38   bson_finish (valid);
39 
40   cmp_ok (bson_size (scope), "==", bson_size (valid),
41           "The returned scope's length is correct");
42   ok (memcmp (bson_data (scope), bson_data (valid),
43               bson_size (scope)) == 0,
44       "The returned scope is correct");
45   bson_free (valid);
46 
47   bson_cursor_next (c);
48   ok (bson_cursor_get_javascript_w_scope (c, &s, &scope) == FALSE,
49       "bson_cursor_get_javascript_w_scope() should fail when the cursor "
50       "points to non-javascript data");
51 
52   bson_cursor_free (c);
53   bson_free (b);
54   bson_free (scope);
55 }
56 
57 RUN_TEST (10, bson_cursor_get_javascript_w_scope);
58