| 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 |
extern "C" { |
| 18 |
#include "libjsonnet.h" |
| 19 |
} |
| 20 |
|
| 21 |
#include <cstdlib> |
| 22 |
#include <filesystem> |
| 23 |
|
| 24 |
#include "gtest/gtest.h" |
| 25 |
|
| 26 |
namespace jsonnet::internal { |
| 27 |
namespace { |
| 28 |
|
| 29 |
class TestInSourceDir : public testing::Test { |
| 30 |
protected: |
| 31 |
static void SetUpTestSuite() { |
| 32 |
original_cwd_ = std::filesystem::current_path(); |
| 33 |
const char* source_base_env = std::getenv("JSONNET_SOURCE_BASE"); |
| 34 |
if (source_base_env) { |
| 35 |
std::filesystem::path source_base{source_base_env}; |
| 36 |
std::filesystem::current_path(source_base); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static void TearDownTestSuite() { |
| 41 |
std::filesystem::current_path(original_cwd_); |
| 42 |
} |
| 43 |
|
| 44 |
private: |
| 45 |
static std::filesystem::path original_cwd_; |
| 46 |
}; |
| 47 |
|
| 48 |
std::filesystem::path TestInSourceDir::original_cwd_; |
| 49 |
|
| 50 |
TEST_F(TestInSourceDir, TestEvaluateSnippet) |
| 51 |
{ |
| 52 |
const char* snippet = "std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)"; |
| 53 |
struct JsonnetVm* vm = jsonnet_make(); |
| 54 |
ASSERT_FALSE(vm == nullptr); |
| 55 |
int error = 0; |
| 56 |
char* output = jsonnet_evaluate_snippet(vm, "snippet", snippet, &error); |
| 57 |
EXPECT_EQ(0, error); |
| 58 |
jsonnet_realloc(vm, output, 0); |
| 59 |
jsonnet_destroy(vm); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_F(TestInSourceDir, TestEvaluateFile) |
| 63 |
{ |
| 64 |
struct JsonnetVm *vm = jsonnet_make(); |
| 65 |
ASSERT_FALSE(vm == nullptr); |
| 66 |
int error = 0; |
| 67 |
char* output = jsonnet_evaluate_file(vm, "test_suite/object.jsonnet", &error); |
| 68 |
EXPECT_EQ(0, error); |
| 69 |
EXPECT_STREQ("true\n", output); |
| 70 |
jsonnet_realloc(vm, output, 0); |
| 71 |
jsonnet_destroy(vm); |
| 72 |
} |
| 73 |
|
| 74 |
} // namespace |
| 75 |
} // namespace jsonnet::internal |