Skip to content

Commit 3418af3

Browse files
committed
Add unit test for libjsonnet C API. Move Bazel build targets into BUILD files in respective directories.
Issue #39
1 parent 6fc7945 commit 3418af3

File tree

9 files changed

+110
-101
lines changed

9 files changed

+110
-101
lines changed

BUILD

Lines changed: 0 additions & 98 deletions
This file was deleted.

cmd/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_binary(
4+
name = "jsonnet",
5+
srcs = ["jsonnet.cpp"],
6+
deps = ["//core:libjsonnet"],
7+
)

core/BUILD

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_library(
4+
name = "jsonnet-common",
5+
srcs = [
6+
"desugarer.cpp",
7+
"formatter.cpp",
8+
"libjsonnet.cpp",
9+
"lexer.cpp",
10+
"parser.cpp",
11+
"static_analysis.cpp",
12+
"string_utils.cpp",
13+
"vm.cpp",
14+
"//stdlib:std.jsonnet.h",
15+
],
16+
hdrs = [
17+
"ast.h",
18+
"desugarer.h",
19+
"formatter.h",
20+
"lexer.h",
21+
"parser.h",
22+
"state.h",
23+
"static_analysis.h",
24+
"static_error.h",
25+
"string_utils.h",
26+
"unicode.h",
27+
"vm.h",
28+
],
29+
deps = [
30+
"//include:libjsonnet",
31+
],
32+
linkopts = ["-lm"],
33+
includes = ["."],
34+
)
35+
36+
cc_library(
37+
name = "libjsonnet",
38+
srcs = ["libjsonnet.cpp"],
39+
deps = [
40+
":jsonnet-common",
41+
"//include:libjsonnet",
42+
],
43+
)
44+
45+
cc_test(
46+
name = "libjsonnet_test",
47+
srcs = ["libjsonnet_test.cc"],
48+
deps = [
49+
":jsonnet-common",
50+
"//external:gtest_main",
51+
],
52+
)

core/libjsonnet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct JsonnetVm {
6363

6464
FmtOpts fmtOpts;
6565
bool fmtDebugDesugaring;
66-
66+
6767
JsonnetVm(void)
6868
: gcGrowthTrigger(2.0), maxStack(500), gcMinObjects(1000), maxTrace(20),
6969
importCallback(default_import_callback), importCallbackContext(this), stringOutput(false),

core/libjsonnet_test.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
extern "C" {
16+
#include "libjsonnet.h"
17+
}
18+
19+
#include "gtest/gtest.h"
20+
21+
TEST(JsonnetTest, TestEvaluateSnippet) {
22+
const char* snippet = "std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)";
23+
struct JsonnetVm* vm = jsonnet_make();
24+
ASSERT_FALSE(vm == nullptr);
25+
int error = 0;
26+
char* output = jsonnet_evaluate_snippet(vm, "snippet", snippet, &error);
27+
EXPECT_EQ(0, error);
28+
jsonnet_realloc(vm, output, 0);
29+
jsonnet_destroy(vm);
30+
}

cpp/BUILD

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ cc_library(
44
name = "libjsonnet++",
55
srcs = ["libjsonnet++.cc"],
66
deps = [
7-
"//:libjsonnet",
7+
"//core:libjsonnet",
88
"//include:libjsonnet++",
99
],
10-
includes = ["include"],
1110
)
1211

1312
cc_test(

include/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package(default_visibility = ["//visibility:public"])
33
cc_library(
44
name = "libjsonnet",
55
hdrs = ["libjsonnet.h"],
6+
includes = ["."],
67
)
78

89
cc_library(
910
name = "libjsonnet++",
1011
hdrs = ["libjsonnet++.h"],
12+
includes = ["."],
1113
)

include/libjsonnet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
#ifndef LIB_JSONNET_H
1818
#define LIB_JSONNET_H
1919

20+
#include <stddef.h>
21+
2022
/** \file This file is a library interface for evaluating Jsonnet. It is written in C++ but exposes
2123
* a C interface for easier wrapping by other languages. See \see jsonnet_lib_test.c for an example
2224
* of using the library.

stdlib/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
filegroup(
4+
name = "std",
5+
srcs = ["std.jsonnet"],
6+
)
7+
8+
genrule(
9+
name = "gen-std-jsonnet-h",
10+
srcs = ["std.jsonnet"],
11+
outs = ["std.jsonnet.h"],
12+
cmd = "((od -v -Anone -t u1 $< | tr \" \" \"\n\" | grep -v \"^$$\" " +
13+
"| tr \"\n\" \",\" ) && echo \"0\") > $@; " +
14+
"echo >> $@",
15+
)

0 commit comments

Comments
 (0)