Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Apr 4, 2024
1 parent 2f611f8 commit 2d2971b
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 12 deletions.
1 change: 0 additions & 1 deletion include/tao/config/annotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ namespace tao::config

void append_message_extension( std::ostream& o ) const
{
o << ' ';
to_stream( o, key );
o << ' ';
position.append_message_extension( o );
Expand Down
6 changes: 3 additions & 3 deletions include/tao/config/internal/system_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace tao::config::internal
std::unique_ptr< FILE, void ( * )( FILE* ) > file( ::popen( script.c_str(), "r" ), []( FILE* f ) { ::pclose( f ); } );

if( !file ) {
throw pegtl::parse_error( "TODO: Better error message for shell function error.", pos );
throw pegtl::parse_error( "TODO: Better error message for shell function popen error.", pos );
// throw pegtl::parse_error( format( __FILE__, __LINE__, "popen failed", { { "command", script }, { "errno", errno } } ), pos );
}
std::string result;
Expand All @@ -80,8 +80,8 @@ namespace tao::config::internal
}
errno = 0;

if( ( errno != 0 ) || ( ::pclose( file.release() ) != 0 ) ) {
throw pegtl::parse_error( "TODO: Better error message for shell function error.", pos );
if( ::pclose( file.release() ) != 0 ) {
throw pegtl::parse_error( "TODO: Better error message for shell function pclose error.", pos ); // LCOV_EXCL_LINE
}
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/test/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(testsources
parse_reference2.cpp
success.cpp
to_stream.cpp
value.cpp
)

# file(GLOB ...) is used to validate the above list of test_sources
Expand Down
21 changes: 14 additions & 7 deletions src/test/config/debug_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ namespace tao::config
{
void unit_test()
{
const std::string input = "a = 1 b { c = 42 d = [ null, true ] }";
internal::config_parser cp;
cp.parse( std::string_view( input ), __FUNCTION__ );
std::ostringstream oss;
internal::to_stream( oss, cp.st.root );
const std::string output = oss.str();
const std::string reference = "{position:\"(root):1:1\",object_data:{a:{remove:true,implicit:false,temporary:false,position:\"unit_test:1:1\",concat_list:[{type:\"unsigned\",data:1}]},b:{remove:false,implicit:false,temporary:false,position:\"unit_test:1:7\",concat_list:[{type:\"object\",data:{position:\"unit_test:1:9\",object_data:{c:{remove:true,implicit:false,temporary:false,position:\"unit_test:1:11\",concat_list:[{type:\"unsigned\",data:42}]},d:{remove:true,implicit:false,temporary:false,position:\"unit_test:1:18\",concat_list:[{type:\"array\",data:{position:\"unit_test:1:22\",function:\"\",array_data:[{remove:false,implicit:false,temporary:false,position:\"unit_test:1:24\",concat_list:[{type:\"null\"}]},{remove:false,implicit:false,temporary:false,position:\"unit_test:1:24\",concat_list:[{type:\"boolean\",data:true}]}]}}]}}}}]}}}";
TAO_CONFIG_TEST_ASSERT( output == reference );
cp.parse( std::filesystem::path( "tests/debug_traits.config" ) );
json::events::to_value consumer;
json::events::produce< internal::debug_traits >( consumer, cp.st.root );
const json::value config = std::move( consumer.value );
const json::value output = json::jaxn::from_file( std::filesystem::path( "tests/debug_traits.output" ) );
TAO_CONFIG_TEST_ASSERT( output == config );
{
std::ostringstream os1;
internal::to_stream( os1, cp.st.root );
TAO_CONFIG_TEST_ASSERT( json::jaxn::from_string( os1.str() ) == output );
}
std::ostringstream os2;
internal::to_stream( os2, cp.st.root, 3 );
TAO_CONFIG_TEST_ASSERT( json::jaxn::from_string( os2.str() ) == output );
}

} // namespace tao::config
Expand Down
2 changes: 1 addition & 1 deletion src/test/config/setenv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#ifndef TAO_CONFIG_SRC_TEST_CONFIG_SETENV_HPP
#define TAO_CONFIG_SRC_TEST_CONFIG_SETENV_HPP

#include <stdlib.h>
#include <stdexcept>
#include <stdlib.h>
#include <string>

namespace tao::config::internal
Expand Down
30 changes: 30 additions & 0 deletions src/test/config/value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/config/

#include <sstream>
#include <string>

#include "test.hpp"

#include <tao/config.hpp>

namespace tao::config
{
const std::string s = "invalid json type 'object' for conversion to std::string [(root):1:1]"; // TODO: Where does the second space come from?

void unit_test()
{
const auto v = from_string( "foo = 42", __FUNCTION__ );

try {
(void)v.as< std::string >();
++failed;
}
catch( const std::exception& e ) {
TAO_CONFIG_TEST_ASSERT( e.what() == s );
}
}

} // namespace tao::config

#include "main.hpp"
1 change: 1 addition & 0 deletions tests/debug_traits.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 1 b { c = 42 d = [ null, true ] e = (a.42.(b)) f = 'yo' g = -3 h = 3.14 i = (env "USER") j = $beef } c.* = 42
231 changes: 231 additions & 0 deletions tests/debug_traits.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
{
position: "(root):1:1",
object_data: {
a: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:1",
concat_list: [
{
type: "unsigned",
data: 1
}
]
},
b: {
remove: false,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:7",
concat_list: [
{
type: "object",
data: {
position: "tests/debug_traits.config:1:9",
object_data: {
c: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:11",
concat_list: [
{
type: "unsigned",
data: 42
}
]
},
d: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:18",
concat_list: [
{
type: "array",
data: {
position: "tests/debug_traits.config:1:22",
function: "",
array_data: [
{
remove: false,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:24",
concat_list: [
{
type: "null"
}
]
},
{
remove: false,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:24",
concat_list: [
{
type: "boolean",
data: true
}
]
}
]
}
}
]
},
e: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:37",
concat_list: [
{
type: "reference",
data: [
{
reference2_kind: "name",
reference2_data: {
index: 0,
value: "a"
},
position: "tests/debug_traits.config:1:42"
},
{
reference2_kind: "index",
reference2_data: {
index: 1,
value: 42
},
position: "tests/debug_traits.config:1:44"
},
{
reference2_kind: "reference",
reference2_data: {
index: 2,
value: [
{
reference2_kind: "name",
reference2_data: {
index: 0,
value: "b"
},
position: "tests/debug_traits.config:1:48"
}
]
},
position: "tests/debug_traits.config:1:47"
}
]
}
]
},
f: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:52",
concat_list: [
{
type: "string",
data: "yo"
}
]
},
g: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:61",
concat_list: [
{
type: "signed",
data: -3
}
]
},
h: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:68",
concat_list: [
{
type: "double",
data: 3.14
}
]
},
i: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:77",
concat_list: [
{
type: "array",
data: {
position: "tests/debug_traits.config:1:82",
function: "env",
array_data: [
{
remove: false,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:86",
concat_list: [
{
type: "string",
data: "USER"
}
]
}
]
}
}
]
},
j: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:94",
concat_list: [
{
type: "binary",
data: $BEEF
}
]
}
}
}
}
]
},
c: {
remove: false,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:106",
concat_list: [
{
type: "asterisk",
data: {
remove: true,
implicit: false,
temporary: false,
position: "tests/debug_traits.config:1:108",
concat_list: [
{
type: "unsigned",
data: 42
}
]
}
}
]
}
}
}

0 comments on commit 2d2971b

Please sign in to comment.