mirror of
https://github.com/arabine/open-story-teller.git
synced 2025-12-06 17:09:06 +01:00
75 lines
2.8 KiB
C++
75 lines
2.8 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
|
#include <catch2/matchers/catch_matchers_vector.hpp>
|
|
#include <catch2/matchers/catch_matchers_string.hpp>
|
|
#include "json_reader.h"
|
|
#include "json.hpp"
|
|
|
|
using namespace Catch::Matchers;
|
|
|
|
TEST_CASE("JsonReader::get - Scénarios de base", "[JsonReader]") {
|
|
json data = R"({
|
|
"nom": "Bob",
|
|
"age": 42,
|
|
"is_admin": true
|
|
})"_json;
|
|
|
|
// --- Scénario 1 : Succès (Clé présente, Type correct) ---
|
|
SECTION("Succès - Récupération d'un entier") {
|
|
int age = JsonReader::get<int>(data, "age", 0, false, false);
|
|
REQUIRE(age == 42);
|
|
}
|
|
|
|
SECTION("Succès - Récupération d'une chaîne") {
|
|
std::string nom = JsonReader::get<std::string>(data, "nom", "N/A", false, false);
|
|
REQUIRE(nom == "Bob");
|
|
}
|
|
|
|
// --- Scénario 2 : Clé Manquante (Retourne la valeur par défaut) ---
|
|
SECTION("Clé Manquante - Retourne la valeur par défaut") {
|
|
// Log désactivé pour ne pas polluer la sortie de test
|
|
double taux = JsonReader::get<double>(data, "taux_inexistant", 1.5, false, false);
|
|
REQUIRE(taux == 1.5);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("JsonReader::get - Gestion des Erreurs", "[JsonReader][Erreur]") {
|
|
json data_err = R"({
|
|
"taux_texte": "0.99",
|
|
"liste": [1, 2]
|
|
})"_json;
|
|
|
|
// --- Scénario 3 : Erreur de Type (Retourne la valeur par défaut) ---
|
|
SECTION("Erreur de Type - Retourne la valeur par défaut (Int vs String)") {
|
|
// Tente de lire "0.99" (string) comme un int.
|
|
int taux = JsonReader::get<int>(data_err, "taux_texte", -1, false, false);
|
|
REQUIRE(taux == -1);
|
|
}
|
|
|
|
// --- Scénario 4 : Clé Manquante (Lance une exception) ---
|
|
SECTION("Clé Manquante - Lance une runtime_error") {
|
|
REQUIRE_THROWS_AS(
|
|
JsonReader::get<std::string>(data_err, "cle_manquante_fatale", "N/A", false, true),
|
|
std::runtime_error
|
|
);
|
|
// Vérifie le message pour s'assurer qu'il mentionne la clé
|
|
REQUIRE_THROWS_WITH(
|
|
JsonReader::get<std::string>(data_err, "cle_manquante_fatale", "N/A", false, true),
|
|
ContainsSubstring("cle_manquante_fatale")
|
|
);
|
|
}
|
|
|
|
// --- Scénario 5 : Erreur de Type (Lance une exception) ---
|
|
SECTION("Erreur de Type - Lance une runtime_error") {
|
|
// Tente de lire une [array] comme un int
|
|
REQUIRE_THROWS_AS(
|
|
JsonReader::get<int>(data_err, "liste", 0, false, true),
|
|
std::runtime_error
|
|
);
|
|
// Vérifie le message pour s'assurer qu'il mentionne la clé
|
|
REQUIRE_THROWS_WITH(
|
|
JsonReader::get<int>(data_err, "liste", 0, false, true),
|
|
ContainsSubstring("liste") && ContainsSubstring("Erreur de type")
|
|
);
|
|
}
|
|
} |