Skip to content

Commit

Permalink
Merge pull request google#132 from davidzchen/libjsonnet-test
Browse files Browse the repository at this point in the history
Add unit test for libjsonnet C API.
  • Loading branch information
davidzchen committed Mar 21, 2016
2 parents 6fc7945 + 3418af3 commit 26ea096
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 101 deletions.
98 changes: 0 additions & 98 deletions BUILD

This file was deleted.

7 changes: 7 additions & 0 deletions cmd/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package(default_visibility = ["//visibility:public"])

cc_binary(
name = "jsonnet",
srcs = ["jsonnet.cpp"],
deps = ["//core:libjsonnet"],
)
52 changes: 52 additions & 0 deletions core/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "jsonnet-common",
srcs = [
"desugarer.cpp",
"formatter.cpp",
"libjsonnet.cpp",
"lexer.cpp",
"parser.cpp",
"static_analysis.cpp",
"string_utils.cpp",
"vm.cpp",
"//stdlib:std.jsonnet.h",
],
hdrs = [
"ast.h",
"desugarer.h",
"formatter.h",
"lexer.h",
"parser.h",
"state.h",
"static_analysis.h",
"static_error.h",
"string_utils.h",
"unicode.h",
"vm.h",
],
deps = [
"//include:libjsonnet",
],
linkopts = ["-lm"],
includes = ["."],
)

cc_library(
name = "libjsonnet",
srcs = ["libjsonnet.cpp"],
deps = [
":jsonnet-common",
"//include:libjsonnet",
],
)

cc_test(
name = "libjsonnet_test",
srcs = ["libjsonnet_test.cc"],
deps = [
":jsonnet-common",
"//external:gtest_main",
],
)
2 changes: 1 addition & 1 deletion core/libjsonnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct JsonnetVm {

FmtOpts fmtOpts;
bool fmtDebugDesugaring;

JsonnetVm(void)
: gcGrowthTrigger(2.0), maxStack(500), gcMinObjects(1000), maxTrace(20),
importCallback(default_import_callback), importCallbackContext(this), stringOutput(false),
Expand Down
30 changes: 30 additions & 0 deletions core/libjsonnet_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern "C" {
#include "libjsonnet.h"
}

#include "gtest/gtest.h"

TEST(JsonnetTest, TestEvaluateSnippet) {
const char* snippet = "std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)";
struct JsonnetVm* vm = jsonnet_make();
ASSERT_FALSE(vm == nullptr);
int error = 0;
char* output = jsonnet_evaluate_snippet(vm, "snippet", snippet, &error);
EXPECT_EQ(0, error);
jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);
}
3 changes: 1 addition & 2 deletions cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ cc_library(
name = "libjsonnet++",
srcs = ["libjsonnet++.cc"],
deps = [
"//:libjsonnet",
"//core:libjsonnet",
"//include:libjsonnet++",
],
includes = ["include"],
)

cc_test(
Expand Down
2 changes: 2 additions & 0 deletions include/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package(default_visibility = ["//visibility:public"])
cc_library(
name = "libjsonnet",
hdrs = ["libjsonnet.h"],
includes = ["."],
)

cc_library(
name = "libjsonnet++",
hdrs = ["libjsonnet++.h"],
includes = ["."],
)
2 changes: 2 additions & 0 deletions include/libjsonnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
#ifndef LIB_JSONNET_H
#define LIB_JSONNET_H

#include <stddef.h>

/** \file This file is a library interface for evaluating Jsonnet. It is written in C++ but exposes
* a C interface for easier wrapping by other languages. See \see jsonnet_lib_test.c for an example
* of using the library.
Expand Down
15 changes: 15 additions & 0 deletions stdlib/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "std",
srcs = ["std.jsonnet"],
)

genrule(
name = "gen-std-jsonnet-h",
srcs = ["std.jsonnet"],
outs = ["std.jsonnet.h"],
cmd = "((od -v -Anone -t u1 $< | tr \" \" \"\n\" | grep -v \"^$$\" " +
"| tr \"\n\" \",\" ) && echo \"0\") > $@; " +
"echo >> $@",
)

0 comments on commit 26ea096

Please sign in to comment.