From 539d9aab3510b92d02b9eff89bcba6c8c7878b2e Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Thu, 25 Jun 2020 14:50:34 -0500 Subject: [PATCH 1/2] [AnyMap] Improve error message for missing key in getMapWhere --- interfaces/cython/cantera/test/test_thermo.py | 16 ++++++++++++++ src/base/AnyMap.cpp | 6 ++++++ test/general/test_containers.cpp | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 56a80b691f5..a59618dcd92 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -17,6 +17,22 @@ def setUp(self): def test_source(self): self.assertEqual(self.phase.source, 'h2o2.xml') + def test_missing_phases_key(self): + yaml = ''' + species: + - name: H + composition: {H: 1} + thermo: + model: NASA7 + temperature-ranges: [200.0, 1000.0, 6000.0] + data: + - [2.5, 0.0, 0.0, 0.0, 0.0, 2.547366e+04, -0.44668285] + - [2.5, 0.0, 0.0, 0.0, 0.0, 2.547366e+04, -0.44668285] + note: L6/94 + ''' + with self.assertRaisesRegex(ct.CanteraError, "Key 'phases' not found"): + _ = ct.Solution(yaml=yaml) + def test_base_attributes(self): self.assertIsInstance(self.phase.name, str) self.assertIsInstance(self.phase.phase_of_matter, str) diff --git a/src/base/AnyMap.cpp b/src/base/AnyMap.cpp index b070038ed32..c7d3ca5a03c 100644 --- a/src/base/AnyMap.cpp +++ b/src/base/AnyMap.cpp @@ -656,6 +656,9 @@ const AnyMap& AnyValue::getMapWhere(const std::string& key, const std::string& v throw InputFileError("AnyValue::getMapWhere", *this, "Map does not contain a key where '{}' = '{}'", key, value); } + } else if (is()) { + throw InputFileError("AnyValue::getMapWhere", *this, + "Key '{}' not found", m_key); } else { throw InputFileError("AnyValue::getMapWhere", *this, "Element is not a mapping or list of mappings"); @@ -703,6 +706,9 @@ AnyMap& AnyValue::getMapWhere(const std::string& key, const std::string& value, child[key] = value; operator=(std::move(child)); return as(); + } else if (is()) { + throw InputFileError("AnyValue::getMapWhere", *this, + "Key '{}' not found", m_key); } else { throw InputFileError("AnyValue::getMapWhere", *this, "Element is not a mapping or list of mappings"); diff --git a/test/general/test_containers.cpp b/test/general/test_containers.cpp index 2105f46aeb8..5b47ab6b5fe 100644 --- a/test/general/test_containers.cpp +++ b/test/general/test_containers.cpp @@ -1,4 +1,5 @@ #include "gtest/gtest.h" +#include "gmock/gmock.h" #include "cantera/base/AnyMap.h" using namespace Cantera; @@ -322,6 +323,26 @@ TEST(AnyMap, loadYaml) EXPECT_DOUBLE_EQ(coeffs[1][2], -8.280690600E-07); } +TEST(AnyMap, missingKey) +{ + AnyMap root = AnyMap::fromYamlFile("ideal-gas.yaml"); + try { + root["spam"].getMapWhere("name", "unknown"); + } catch (std::exception& ex) { + EXPECT_THAT(ex.what(), ::testing::HasSubstr("Key 'spam' not found")); + } +} + +TEST(AnyMap, missingKeyAt) +{ + AnyMap root = AnyMap::fromYamlFile("ideal-gas.yaml"); + try { + root.at("spam").getMapWhere("name", "unknown"); + } catch (std::exception& ex) { + EXPECT_THAT(ex.what(), ::testing::HasSubstr("Key 'spam' not found")); + } +} + TEST(AnyMap, loadDeprecatedYaml) { // The deprecation warning in this file is turned into an From bd8ffd6b94bff804bf6d6afc70ed9162b7c7949a Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 26 Jun 2020 12:49:32 -0500 Subject: [PATCH 2/2] Do not track executables in test_problems --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitignore b/.gitignore index 098561c4be0..67f751f5349 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,14 @@ doc/ctdeploy_key *.exe.manifest build/ test/work/ +test_problems/ChemEquil_ionizedGas/ChemEquil_ionizedGas +test_problems/cathermo/testWaterPDSS/WaterPDSS +test_problems/cathermo/testWaterTP/WaterSSTP +test_problems/cxx_ex/cxx_ex +test_problems/diamondSurf/diamondSurf +test_problems/pureFluidTest/pureFluid +test_problems/rankine_democxx/rankine_democxx +test_problems/surfkin/surfkin interfaces/cython/cantera/_cantera.h include/cantera/base/config.h include/cantera/base/config.h.build