diff --git a/oss-internship-2020/jsonnet/README.md b/oss-internship-2020/jsonnet/README.md index b2ab3b0..2c1679e 100644 --- a/oss-internship-2020/jsonnet/README.md +++ b/oss-internship-2020/jsonnet/README.md @@ -38,6 +38,14 @@ cd examples absolute/path/to/the/output_directory ``` -All three tools support evaluating one input file (possibly relying on multiple other files, e.x. by jsonnet `import` command; the files must be held in the same directory as input file) into one or more output files. Example jsonnet codes to evaluate in a one-in-one-out manner can be found [here](https://github.com/google/jsonnet/tree/master/examples). Example code producing multiple output files or YAML stream files can be found in the `examples/jsonnet_codes` directory, in files called `multiple_files_example.jsonnet` and `yaml_stream_example.jsonnet`, respectively. +All three tools support evaluating one input file (possibly relying on multiple other files, e.x. by jsonnet `import` command; the files must be held in the same directory as input file) into one or more output files. Example jsonnet codes to evaluate in a one-in-one-out manner can be found [here](https://github.com/google/jsonnet/tree/master/examples). Example code producing multiple output files or YAML stream files can be found in the `examples/jsonnet_codes` directory (along with some other examples copied with minimal changes from the library files), in files called `multiple_files_example.jsonnet` and `yaml_stream_example.jsonnet`, respectively. In the `examples/jsonnet_codes_expected_output` directory one can found outputs the mentioned above files' evaluation should produce. The formatter reads one input file and produces one output file as a result. Example code for this tool can also be found in `examples/jsonnet_codes` directory, in a file called `formatter_example.jsonnet`. + +## Testing + +A few tests prepared with a use of [Google Test](https://github.com/google/googletest) framework can be found in the `tests/` directory. To run them type: +``` +cd tests +./tests +``` diff --git a/oss-internship-2020/jsonnet/examples/CMakeLists.txt b/oss-internship-2020/jsonnet/examples/CMakeLists.txt index 55bc891..1145fd9 100644 --- a/oss-internship-2020/jsonnet/examples/CMakeLists.txt +++ b/oss-internship-2020/jsonnet/examples/CMakeLists.txt @@ -24,6 +24,7 @@ add_custom_command( include_directories( ${PROJECT_SOURCE_DIR} + ${PROJECT_SOURCE_DIR}/headers ${PROJECT_BINARY_DIR}/gen_files ) diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet new file mode 100644 index 0000000..7b26dd2 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet @@ -0,0 +1,30 @@ +/* +Copyright 2015 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. +*/ + +local martinis = import 'martinis.libsonnet'; + +{ + 'Vodka Martini': martinis['Vodka Martini'], + Manhattan: { + ingredients: [ + { kind: 'Rye', qty: 2.5 }, + { kind: 'Sweet Red Vermouth', qty: 1 }, + { kind: 'Angostura', qty: 'dash' }, + ], + garnish: importstr 'garnish.txt', + served: 'Straight Up', + }, +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_base_sandbox.h b/oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_base_sandbox.h rename to oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.h b/oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h similarity index 100% rename from oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.h rename to oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h diff --git a/oss-internship-2020/jsonnet/tests/jsonnet_tests.h b/oss-internship-2020/jsonnet/headers/jsonnet_tests.h similarity index 94% rename from oss-internship-2020/jsonnet/tests/jsonnet_tests.h rename to oss-internship-2020/jsonnet/headers/jsonnet_tests.h index f42cfab..2b356c9 100644 --- a/oss-internship-2020/jsonnet/tests/jsonnet_tests.h +++ b/oss-internship-2020/jsonnet/headers/jsonnet_tests.h @@ -32,7 +32,7 @@ class JsonnetTestHelper { void TestTearDown(); void Read_input(char* filename); - void Evaluate_jsonnet_code(char* filename, Evaluation type); + void Evaluate_jsonnet_code(char* filename, Evaluation type, bool expected_correct); void Write_output(char* filename_or_directory, Evaluation type); std::string Read_output(char* filename); diff --git a/oss-internship-2020/jsonnet/tests/CMakeLists.txt b/oss-internship-2020/jsonnet/tests/CMakeLists.txt index 58a36fe..7703a5a 100644 --- a/oss-internship-2020/jsonnet/tests/CMakeLists.txt +++ b/oss-internship-2020/jsonnet/tests/CMakeLists.txt @@ -20,22 +20,16 @@ file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_output) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests/tests_expected_output) add_custom_target(test_preparation ALL - COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/* ${PROJECT_BINARY_DIR}/tests/tests_input - #COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/yaml_stream_example.jsonnet ${PROJECT_BINARY_DIR}/tests/tests_input - #COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/arith.jsonnet ${PROJECT_BINARY_DIR}/tests/tests_input - #COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/negroni.jsonnet ${PROJECT_BINARY_DIR}/tests/tests_input - #COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/utils.libsonnet ${PROJECT_BINARY_DIR}/tests/tests_input - COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output - #COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/arith.jsonnet.golden ${PROJECT_BINARY_DIR}/tests/tests_expected_output - #COMMAND cp ${PROJECT_SOURCE_DIR}/jsonnet/examples/negroni.jsonnet.golden ${PROJECT_BINARY_DIR}/tests/tests_expected_output + COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/* ${PROJECT_BINARY_DIR}/tests/tests_input + COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output ) include_directories( - ${PROJECT_SOURCE_DIR}/examples + ${PROJECT_SOURCE_DIR}/headers ) add_executable(tests - jsonnet_tests.h + ${PROJECT_SOURCE_DIR}/headers/jsonnet_tests.h jsonnet_tests.cc jsonnet_tests_utils.cc ) diff --git a/oss-internship-2020/jsonnet/tests/jsonnet_tests.cc b/oss-internship-2020/jsonnet/tests/jsonnet_tests.cc index 6e6dc20..45f8ac5 100644 --- a/oss-internship-2020/jsonnet/tests/jsonnet_tests.cc +++ b/oss-internship-2020/jsonnet/tests/jsonnet_tests.cc @@ -20,11 +20,13 @@ class JsonnetTest : public JsonnetTestHelper, public testing::Test { void TearDown() override { TestTearDown(); } }; +// Basic test TEST_F(JsonnetTest, SetUp_TearDown) { ASSERT_FALSE(if_jsonnet_vm_was_used); ASSERT_FALSE(if_input_was_read); } +// One file evaluation to one file TEST_F(JsonnetTest, One_file_no_dependencies) { char input_file[] = "arith.jsonnet"; char output_file[] = "arith_output"; @@ -32,7 +34,7 @@ TEST_F(JsonnetTest, One_file_no_dependencies) { char output_to_expect[] = "tests_expected_output/arith.golden"; Read_input(input_file); - Evaluate_jsonnet_code(input_file, BASE); + Evaluate_jsonnet_code(input_file, BASE, true); Write_output(output_file, BASE); std::string produced_output = Read_output(output_to_read); @@ -41,6 +43,7 @@ TEST_F(JsonnetTest, One_file_no_dependencies) { ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); } +// One file evaluating to one file, dependent on some other files TEST_F(JsonnetTest, One_file_some_dependencies) { char input_file[] = "negroni.jsonnet"; char output_file[] = "negroni_output"; @@ -48,7 +51,7 @@ TEST_F(JsonnetTest, One_file_some_dependencies) { char output_to_expect[] = "tests_expected_output/negroni.golden"; Read_input(input_file); - Evaluate_jsonnet_code(input_file, BASE); + Evaluate_jsonnet_code(input_file, BASE, true); Write_output(output_file, BASE); std::string produced_output = Read_output(output_to_read); @@ -57,6 +60,7 @@ TEST_F(JsonnetTest, One_file_some_dependencies) { ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); } +// One file evaluating to two files TEST_F(JsonnetTest, Multiple_files) { char input_file[] = "multiple_files_example.jsonnet"; char output_file[] = ""; @@ -66,7 +70,7 @@ TEST_F(JsonnetTest, Multiple_files) { char output_to_expect_2[] = "tests_expected_output/second_file.json"; Read_input(input_file); - Evaluate_jsonnet_code(input_file, MULTIPLE_FILES); + Evaluate_jsonnet_code(input_file, MULTIPLE_FILES, true); Write_output(output_file, MULTIPLE_FILES); std::string produced_output_1 = Read_output(output_to_read_1); @@ -77,3 +81,28 @@ TEST_F(JsonnetTest, Multiple_files) { ASSERT_STREQ(produced_output_1.c_str(), expected_output_1.c_str()); ASSERT_STREQ(produced_output_2.c_str(), expected_output_2.c_str()); } + +// One file evaluating to yaml stream format +TEST_F(JsonnetTest, Yaml_stream) { + char input_file[] = "yaml_stream_example.jsonnet"; + char output_file[] = "yaml_stream_example.yaml"; + char output_to_read[] = "tests_output/yaml_stream_example.yaml"; + char output_to_expect[] = "tests_expected_output/yaml_stream_example.yaml"; + + Read_input(input_file); + Evaluate_jsonnet_code(input_file, YAML_STREAM, true); + Write_output(output_file, YAML_STREAM); + + std::string produced_output = Read_output(output_to_read); + std::string expected_output = Read_output(output_to_expect); + + ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); +} + +// One file depended on some other files not accessible by the sandbox +TEST_F(JsonnetTest, Bad_evaluation) { + char input_file[] = "imports.jsonnet"; + + Read_input(input_file); + Evaluate_jsonnet_code(input_file, BASE, false); +} \ No newline at end of file diff --git a/oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc b/oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc index 6e074f9..d7f2bda 100644 --- a/oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc +++ b/oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc @@ -65,11 +65,13 @@ void JsonnetTestHelper::Read_input(char* filename) { api->c_read_input(0, in_file_var.PtrBefore())); input = std::make_unique(input_ptr); + if_input_was_read = true; + return; } // Evaluate jsonnet code. -void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type) { +void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type, bool expected_correct) { sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str()); sapi::v::Int error; char* output_ptr; @@ -100,9 +102,16 @@ void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type) { } } - ASSERT_THAT(error.GetValue(), testing::Eq(0)); + if (expected_correct) { + ASSERT_THAT(error.GetValue(), testing::Eq(0)); + } else { + ASSERT_THAT(error.GetValue(), testing::Eq(1)); + } + output = std::make_unique(output_ptr); + if_jsonnet_vm_was_used = true; + return; } @@ -147,6 +156,7 @@ void JsonnetTestHelper::Write_output(char* filename_or_directory, return; } +// Reading the output written to a file by library function / expected output std::string JsonnetTestHelper::Read_output(char* filename) { std::ifstream input_stream(filename); std::string contents((std::istreambuf_iterator(input_stream)), std::istreambuf_iterator());