| 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 "libjsonnet++.h" |
| 18 |
|
| 19 |
#include <cstdlib> |
| 20 |
#include <filesystem> |
| 21 |
#include <fstream> |
| 22 |
#include <streambuf> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "gtest/gtest.h" |
| 26 |
|
| 27 |
namespace jsonnet { |
| 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 |
std::string readFile(const std::string& path) |
| 51 |
{ |
| 52 |
std::ifstream in(path); |
| 53 |
if (!in.good()){ |
| 54 |
ADD_FAILURE() << "Could not open: " << path; |
| 55 |
return ""; |
| 56 |
} |
| 57 |
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()); |
| 58 |
} |
| 59 |
|
| 60 |
TEST_F(TestInSourceDir, TestEvaluateSnippet) |
| 61 |
{ |
| 62 |
const std::string input = readFile("cpp/testdata/example.jsonnet"); |
| 63 |
const std::string expected = readFile("cpp/testdata/example_golden.json"); |
| 64 |
|
| 65 |
Jsonnet jsonnet; |
| 66 |
ASSERT_TRUE(jsonnet.init()); |
| 67 |
std::string output; |
| 68 |
EXPECT_TRUE(jsonnet.evaluateSnippet("snippet", input, &output)); |
| 69 |
EXPECT_EQ(expected, output); |
| 70 |
EXPECT_EQ("", jsonnet.lastError()); |
| 71 |
} |
| 72 |
|
| 73 |
TEST_F(TestInSourceDir, TestEvaluateInvalidSnippet) |
| 74 |
{ |
| 75 |
const std::string input = readFile("cpp/testdata/invalid.jsonnet"); |
| 76 |
const std::string error = readFile("cpp/testdata/invalid.out"); |
| 77 |
|
| 78 |
Jsonnet jsonnet; |
| 79 |
ASSERT_TRUE(jsonnet.init()); |
| 80 |
std::string output; |
| 81 |
EXPECT_FALSE(jsonnet.evaluateSnippet("cpp/testdata/invalid.jsonnet", input, &output)); |
| 82 |
EXPECT_EQ("", output); |
| 83 |
EXPECT_EQ(error, jsonnet.lastError()); |
| 84 |
} |
| 85 |
|
| 86 |
TEST_F(TestInSourceDir, TestEvaluateFile) |
| 87 |
{ |
| 88 |
const std::string expected = readFile("cpp/testdata/example_golden.json"); |
| 89 |
|
| 90 |
Jsonnet jsonnet; |
| 91 |
ASSERT_TRUE(jsonnet.init()); |
| 92 |
std::string output; |
| 93 |
EXPECT_TRUE(jsonnet.evaluateFile("cpp/testdata/example.jsonnet", &output)); |
| 94 |
EXPECT_EQ(expected, output); |
| 95 |
EXPECT_EQ("", jsonnet.lastError()); |
| 96 |
} |
| 97 |
|
| 98 |
TEST_F(TestInSourceDir, TestEvaluateInvalidFile) |
| 99 |
{ |
| 100 |
const std::string expected = readFile("cpp/testdata/invalid.out"); |
| 101 |
|
| 102 |
Jsonnet jsonnet; |
| 103 |
ASSERT_TRUE(jsonnet.init()); |
| 104 |
std::string output; |
| 105 |
EXPECT_FALSE(jsonnet.evaluateFile("cpp/testdata/invalid.jsonnet", &output)); |
| 106 |
EXPECT_EQ("", output); |
| 107 |
EXPECT_EQ(expected, jsonnet.lastError()); |
| 108 |
} |
| 109 |
|
| 110 |
TEST_F(TestInSourceDir, TestAddImportPath) |
| 111 |
{ |
| 112 |
const std::string expected = readFile("cpp/testdata/importing_golden.json"); |
| 113 |
|
| 114 |
Jsonnet jsonnet; |
| 115 |
ASSERT_TRUE(jsonnet.init()); |
| 116 |
jsonnet.addImportPath("cpp/testdata"); |
| 117 |
std::string output; |
| 118 |
EXPECT_TRUE(jsonnet.evaluateFile("cpp/testdata/importing.jsonnet", &output)); |
| 119 |
EXPECT_EQ(expected, output); |
| 120 |
EXPECT_EQ("", jsonnet.lastError()); |
| 121 |
} |
| 122 |
|
| 123 |
} // namespace jsonnet |