| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <string.h> |
| 20 |
#include <math.h> |
| 21 |
|
| 22 |
#include <libjsonnet.h> |
| 23 |
|
| 24 |
typedef struct JsonnetJsonValue JJV; |
| 25 |
|
| 26 |
static JJV *native_concat(void *ctx, const JJV * const *argv, int *succ) |
| 27 |
{ |
| 28 |
struct JsonnetVm *vm = (struct JsonnetVm *)ctx; |
| 29 |
const char *a = jsonnet_json_extract_string(vm, argv[0]); |
| 30 |
const char *b = jsonnet_json_extract_string(vm, argv[1]); |
| 31 |
if (a == NULL || b == NULL) { |
| 32 |
*succ = 0; |
| 33 |
return jsonnet_json_make_string(vm, "Bad params."); |
| 34 |
} |
| 35 |
char *str = malloc(strlen(a) + strlen(b) + 1); |
| 36 |
sprintf(str, "%s%s", a, b); |
| 37 |
JJV *r = jsonnet_json_make_string(vm, str); |
| 38 |
free(str); |
| 39 |
*succ = 1; |
| 40 |
return r; |
| 41 |
} |
| 42 |
|
| 43 |
static JJV *native_square(void *ctx, const JJV * const *argv, int *succ) |
| 44 |
{ |
| 45 |
struct JsonnetVm *vm = (struct JsonnetVm *)ctx; |
| 46 |
double a; |
| 47 |
if (!jsonnet_json_extract_number(vm, argv[0], &a)) { |
| 48 |
*succ = 0; |
| 49 |
return jsonnet_json_make_string(vm, "Bad param 'a'."); |
| 50 |
} |
| 51 |
*succ = 1; |
| 52 |
return jsonnet_json_make_number(vm, a * a); |
| 53 |
} |
| 54 |
|
| 55 |
static JJV *native_build(void *ctx, const JJV * const *argv, int *succ) |
| 56 |
{ |
| 57 |
struct JsonnetVm *vm = (struct JsonnetVm *)ctx; |
| 58 |
(void) argv; |
| 59 |
JJV *obj_top = jsonnet_json_make_object(vm); |
| 60 |
JJV *arr_top = jsonnet_json_make_array(vm); |
| 61 |
JJV *arr1 = jsonnet_json_make_array(vm); |
| 62 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_string(vm, "Test 1.1")); |
| 63 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_string(vm, "Test 1.2")); |
| 64 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_string(vm, "Test 1.3")); |
| 65 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_bool(vm, 1)); |
| 66 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_number(vm, 42)); |
| 67 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_null(vm)); |
| 68 |
jsonnet_json_array_append(vm, arr1, jsonnet_json_make_object(vm)); |
| 69 |
jsonnet_json_array_append(vm, arr_top, arr1); |
| 70 |
JJV *arr2 = jsonnet_json_make_array(vm); |
| 71 |
jsonnet_json_array_append(vm, arr2, jsonnet_json_make_string(vm, "Test 2.1")); |
| 72 |
jsonnet_json_array_append(vm, arr2, jsonnet_json_make_string(vm, "Test 2.2")); |
| 73 |
jsonnet_json_array_append(vm, arr2, jsonnet_json_make_string(vm, "Test 2.3")); |
| 74 |
jsonnet_json_array_append(vm, arr2, jsonnet_json_make_bool(vm, 0)); |
| 75 |
jsonnet_json_array_append(vm, arr2, jsonnet_json_make_number(vm, -42)); |
| 76 |
jsonnet_json_array_append(vm, arr2, jsonnet_json_make_null(vm)); |
| 77 |
JJV *little_obj = jsonnet_json_make_object(vm); |
| 78 |
jsonnet_json_object_append(vm, little_obj, "f", jsonnet_json_make_string(vm, "foo")); |
| 79 |
jsonnet_json_object_append(vm, little_obj, "g", jsonnet_json_make_string(vm, "bar")); |
| 80 |
jsonnet_json_array_append(vm, arr2, little_obj); |
| 81 |
jsonnet_json_array_append(vm, arr_top, arr2); |
| 82 |
jsonnet_json_object_append(vm, obj_top, "field", arr_top); |
| 83 |
*succ = 1; |
| 84 |
return obj_top; |
| 85 |
} |
| 86 |
|
| 87 |
#define BUILD_OUTPUT \ |
| 88 |
"{\"field\":[[\"Test 1.1\",\"Test 1.2\",\"Test 1.3\",true,42,null,{}],"\ |
| 89 |
"[\"Test 2.1\",\"Test 2.2\",\"Test 2.3\",false,-42,null,{\"f\":\"foo\",\"g\":\"bar\"}]]}" |
| 90 |
|
| 91 |
static int eval_and_check(const char *test_name, const char *input, const char *expect, int expect_error) |
| 92 |
{ |
| 93 |
struct JsonnetVm *vm = jsonnet_make(); |
| 94 |
|
| 95 |
static const char *PARAMS_0[] = {NULL}; |
| 96 |
static const char *PARAMS_1[] = {"a", NULL}; |
| 97 |
static const char *PARAMS_2[] = {"a", "b", NULL}; |
| 98 |
jsonnet_native_callback(vm, "build", native_build, vm, PARAMS_0); |
| 99 |
jsonnet_native_callback(vm, "square", native_square, vm, PARAMS_1); |
| 100 |
jsonnet_native_callback(vm, "concat", native_concat, vm, PARAMS_2); |
| 101 |
|
| 102 |
int error = 0; |
| 103 |
char *output = jsonnet_evaluate_snippet(vm, "snippet", input, &error); |
| 104 |
if (expect && strcmp(output, expect)) { |
| 105 |
error = 1; |
| 106 |
fprintf(stderr, "FAIL: %s produced wrong output. Want:\n%s\n----- Got -----\n%s\n-----\n", test_name, expect, output); |
| 107 |
} else { |
| 108 |
if (error && expect_error) { |
| 109 |
error = 0; |
| 110 |
} else if (error) { |
| 111 |
fprintf(stderr, "FAIL: %s failed evaluation: %s\n", test_name, output); |
| 112 |
} else if (expect_error) { |
| 113 |
fprintf(stderr, "FAIL: %s evaluated unexpectedly. output was:\n%s\n", test_name, output); |
| 114 |
} |
| 115 |
} |
| 116 |
jsonnet_realloc(vm, output, 0); |
| 117 |
jsonnet_destroy(vm); |
| 118 |
if (!error) { |
| 119 |
fprintf(stderr, "PASS: %s\n", test_name); |
| 120 |
} |
| 121 |
return !error; |
| 122 |
} |
| 123 |
|
| 124 |
static int run_tests() |
| 125 |
{ |
| 126 |
int fail_count = 0; |
| 127 |
|
| 128 |
fail_count += !eval_and_check("build", |
| 129 |
"std.assertEqual(std.native('build')()," BUILD_OUTPUT ") && true", |
| 130 |
"true\n", 0); |
| 131 |
|
| 132 |
fail_count += !eval_and_check("square_noarg", |
| 133 |
"std.native('square')()", |
| 134 |
"RUNTIME ERROR: function parameter a not bound in call.\n\tsnippet:1:1-23\t\n", 1); |
| 135 |
|
| 136 |
fail_count += !eval_and_check("square_badarg", |
| 137 |
"std.native('square')('prune')", |
| 138 |
"RUNTIME ERROR: Bad param 'a'.\n\tsnippet:1:1-30\t\n", 1); |
| 139 |
|
| 140 |
fail_count += !eval_and_check("square_badarg2", |
| 141 |
"std.native('square')({})", |
| 142 |
"RUNTIME ERROR: native extensions can only take primitives.\n\tsnippet:1:1-25\t\n", 1); |
| 143 |
|
| 144 |
fail_count += !eval_and_check("square", |
| 145 |
"std.native('square')(42.5)", |
| 146 |
"1806.25\n", 0); |
| 147 |
|
| 148 |
fail_count += !eval_and_check("concat_noarg0", |
| 149 |
"std.native('concat')()", |
| 150 |
"RUNTIME ERROR: function parameter a not bound in call.\n\tsnippet:1:1-23\t\n", 1); |
| 151 |
|
| 152 |
fail_count += !eval_and_check("concat_noarg1", |
| 153 |
"std.native('concat')('hello')", |
| 154 |
"RUNTIME ERROR: function parameter b not bound in call.\n\tsnippet:1:1-30\t\n", 1); |
| 155 |
|
| 156 |
fail_count += !eval_and_check("concat_badarg1", |
| 157 |
"std.native('concat')(false, 'hello')", |
| 158 |
"RUNTIME ERROR: Bad params.\n\tsnippet:1:1-37\t\n", 1); |
| 159 |
|
| 160 |
fail_count += !eval_and_check("concat_badarg2", |
| 161 |
"std.native('concat')('hello', true)", |
| 162 |
"RUNTIME ERROR: Bad params.\n\tsnippet:1:1-36\t\n", 1); |
| 163 |
|
| 164 |
fail_count += !eval_and_check("concat_badarg3", |
| 165 |
"std.native('concat')(99, 100)", |
| 166 |
"RUNTIME ERROR: Bad params.\n\tsnippet:1:1-30\t\n", 1); |
| 167 |
|
| 168 |
fail_count += !eval_and_check("concat", |
| 169 |
"std.native('concat')('hello', 'world')", |
| 170 |
"\"helloworld\"\n", 0); |
| 171 |
|
| 172 |
return !fail_count; |
| 173 |
} |
| 174 |
|
| 175 |
int main(int argc, const char **argv) |
| 176 |
{ |
| 177 |
(void)argv; /* not unused */ |
| 178 |
if (argc > 1) { |
| 179 |
fprintf(stderr, "libjsonnet_native_callbacks_test does not support any command line arguments"); |
| 180 |
return EXIT_FAILURE; |
| 181 |
} |
| 182 |
const int ok = run_tests(); |
| 183 |
return (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 184 |
} |