Skip to content

Commit

Permalink
add default value check and write json test
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Jul 31, 2024
1 parent a685b72 commit 8b4cb72
Show file tree
Hide file tree
Showing 26 changed files with 2,196 additions and 1,027 deletions.
36 changes: 0 additions & 36 deletions internal/tlcodegen/test/codegen_test/casetests/json_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package casetests
import (
"encoding/json"
"fmt"
"github.com/vkcom/tl/internal/utils"
"math/rand"
"os"

Expand Down Expand Up @@ -213,38 +212,3 @@ func TestGeneralCases(t *testing.T) {
})
}
}

func TestJson(t *testing.T) {
const PathToJsonData = "../data/test-objects-json.json"
data, readErr := os.ReadFile(PathToJsonData)

if readErr != nil {
t.Fatalf("testing data is not provided")
return
}

tests := allTests{map[string]mappingTestSamples{}}
err := json.Unmarshal(data, &tests)

if err != nil {
t.Fatalf("can't unmarshall test data")
return
}

fmt.Println("{ \"Tests\": {")
for testName, testValues := range tests.Tests {
if testValues.UseBytes {
continue
}
fmt.Printf("\"%s\": {\"TestingType\": \"%s\", \"Succesess\":[", testName, testValues.TestingType)
for _, testValue := range testValues.Successes {
testObject := factory.CreateObjectFromName(testValues.TestingType)

_ = testObject.ReadJSON(true, &basictl.JsonLexer{Data: []byte(testValue.GoldenInput)})
bs, _ := testObject.WriteGeneral(nil)
fmt.Printf("{\"Bytes\": \"%s\"},\n", utils.SprintHexDump(bs))
}
fmt.Println("]},")
}
fmt.Println("}}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include <fstream>
#include <bitset>
#include "../dependencies/json.hpp"
#include "../utils/hex.h"

#include "../../../gen/cases_cpp/a_tlgen_helpers_code.hpp"
#include "../../../gen/cases_cpp/__meta/headers.hpp"
#include "../../../gen/cases_cpp/__factory/headers.hpp"

// for convenience
using json = nlohmann::json;

int main() {
tl2::factory::set_all_factories();

std::ifstream f("../data/test-objects-json-and-bytes.json");
json data = json::parse(f);

auto tests = data["Tests"];
for (auto& [test_name, test_data]: tests.items()) {
std::cout << "Run [" << test_name << "]:" << std::endl;
for (auto& test_data_input: test_data["Successes"]) {
std::cout << "\tTestData [" << test_data_input.at("DataAsBytes") << "]: ";

auto test_object = tl2::meta::get_item_by_name(test_data.at("TestingType")).create_object();
auto bytes_input = hex::parse_hex_to_bytes(test_data_input.at("DataAsBytes"));
std::string expected_json_output = test_data_input.at("DataAsJson");

basictl::tl_istream_string input{bytes_input};
std::stringstream output;

bool read_result = test_object->read(input);
bool write_result = test_object->write_json(output);

bool test_result = write_result && read_result;
if (test_result) {
test_result = output.str() == expected_json_output;
}
if (test_result) {
std::cout << "SUCCESS" << std::endl;
} else {
std::cout << "FAILED" << std::endl;
std::cout << "\t\tWrite result : " << write_result << std::endl;
std::cout << "\t\tExpected json output: " << expected_json_output << std::endl;
std::cout << "\t\tActual json result : " << output.str() << std::endl;
return 1;
}
test_object->write_json(std::cout);
std::cout << std::endl;
}
}

std::cout << std::endl;
std::cout << "All tests are passed!" << std::endl;

return 0;
}
Loading

0 comments on commit 8b4cb72

Please sign in to comment.