Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Dec 13, 2024
1 parent 1006f2c commit 7592c0f
Show file tree
Hide file tree
Showing 17 changed files with 582 additions and 213 deletions.
12 changes: 2 additions & 10 deletions include/tao/pegtl/contrib/input_with_depth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace TAO_PEGTL_NAMESPACE
--m_depth;
}

depth_guard& operator=( depth_guard&& ) = delete;
depth_guard& operator=( const depth_guard& ) = delete;
void operator=( depth_guard&& ) = delete;
void operator=( const depth_guard& ) = delete;

[[nodiscard]] std::size_t current_depth() const noexcept
{
Expand All @@ -54,14 +54,6 @@ namespace TAO_PEGTL_NAMESPACE
public:
using Input::Input;

void restart() noexcept
{
if constexpr( internal::has_restart< Input > ) {
Input::restart();
}
m_depth = 0;
}

[[nodiscard]] internal::depth_guard make_depth_guard() noexcept
{
return internal::depth_guard( m_depth ); // NOLINT(google-readability-casting)
Expand Down
2 changes: 1 addition & 1 deletion include/tao/pegtl/contrib/input_with_offset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace TAO_PEGTL_NAMESPACE
return m_offset;
}

void direct_position() = delete;
void direct_position() const = delete;

protected:
offset_position_t m_offset;
Expand Down
6 changes: 3 additions & 3 deletions include/tao/pegtl/debug/analyze_traits_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace TAO_PEGTL_NAMESPACE
: analyze_opt_traits<>
{};

template< typename Name, std::size_t Count, typename Reference >
struct analyze_traits< Name, internal::consume< Count, Reference > >
template< typename Name, std::size_t Count >
struct analyze_traits< Name, internal::consume< Count > >
: std::conditional_t< ( Count > 0 ), analyze_any_traits<>, analyze_opt_traits<> >
{};

Expand Down Expand Up @@ -150,7 +150,7 @@ namespace TAO_PEGTL_NAMESPACE

template< typename Name, typename Head, typename... Rules >
struct analyze_traits< Name, internal::rematch< Head, Rules... > >
: analyze_traits< Name, typename internal::sor< Head, internal::sor< internal::seq< Rules, internal::consume< 1, void > >... > >::rule_t > // TODO: Correct (enough)?
: analyze_traits< Name, typename internal::sor< Head, internal::sor< internal::seq< Rules, internal::consume< 1 > >... > >::rule_t > // TODO: Correct (enough)?
{};

template< typename Name, std::size_t Cnt, typename... Rules >
Expand Down
32 changes: 6 additions & 26 deletions include/tao/pegtl/internal/consume.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2022 Dr. Colin Hirsch and Daniel Frey
// Copyright (c) 2017-2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

Expand All @@ -13,32 +13,12 @@
#include "../type_list.hpp"

#include "enable_control.hpp"
#include "math_utility.hpp"
#include "success.hpp"

namespace TAO_PEGTL_NAMESPACE::internal
{
template< std::size_t Count, typename Reference, typename = void >
struct consume
{
using rule_t = consume;
using subs_t = empty_list;

template< typename ParseInput >
[[nodiscard]] static bool match( ParseInput& in ) noexcept
{
static_assert( is_divisible( sizeof( Reference ), sizeof( *in.current() ) ) );

if( in.size( Count ) >= Count ) {
in.template consume< eol_unknown_tag >( Count );
return true;
}
return false;
}
};

template< std::size_t Count >
struct consume< Count, void, std::enable_if_t< ( Count > 0 ) > >
struct consume
{
using rule_t = consume;
using subs_t = empty_list;
Expand All @@ -54,13 +34,13 @@ namespace TAO_PEGTL_NAMESPACE::internal
}
};

template< typename Reference >
struct consume< 0, Reference, void >
template<>
struct consume< 0 >
: success
{};

template< std::size_t Count, typename Reference >
inline constexpr bool enable_control< consume< Count, Reference > > = false;
template< std::size_t Count >
inline constexpr bool enable_control< consume< Count > > = false;

} // namespace TAO_PEGTL_NAMESPACE::internal

Expand Down
2 changes: 1 addition & 1 deletion include/tao/pegtl/internal/input_with_funcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
[[nodiscard]] T peek_as( const std::size_t offset = 0 ) const noexcept
{
static_assert( sizeof( data_t ) == sizeof( T ) );
return static_cast< T >( *this->current( offset ) );
return static_cast< T >( peek( offset ) );
}

[[nodiscard]] char peek_char( const std::size_t offset = 0 ) const noexcept
Expand Down
2 changes: 1 addition & 1 deletion include/tao/pegtl/rules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace TAO_PEGTL_NAMESPACE
struct bof : internal::bof {};
struct bol : internal::bol {};
template< typename Left, typename Right > struct combine : internal::combine_traits< typename Left::rule_t, typename Right::rule_t >::rule_t {};
template< std::size_t Count > struct consume : internal::consume< Count, void > {};
template< std::size_t Count > struct consume : internal::consume< Count > {};
template< template< typename... > class Control, typename... Rules > struct control : internal::control< Control, Rules... > {};
template< typename... Rules > struct disable : internal::disable< Rules... > {};
template< typename... Rules > struct enable : internal::enable< Rules... > {};
Expand Down
6 changes: 3 additions & 3 deletions src/example/pegtl/coverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ namespace coverage
report( pair.first, pair.second );
}
}
const double fraction = double( m_total_lines - m_problem_lines ) / double( m_total_lines );
std::cout << "Summary for lines -- total " << m_total_lines << " problems " << m_problem_lines << " coverage " << fraction << std::endl;
std::cout << "Summary for files -- total " << m_total_files << " problems " << m_problem_files << std::endl;
const double fraction = 100.0 * double( m_total_lines - m_problem_lines ) / double( m_total_lines );
std::cout << "Summary for lines -- total " << m_total_lines << " uncovered " << m_problem_lines << " coverage " << fraction << '%' << std::endl;
std::cout << "Summary for files -- total " << m_total_files << " problematic " << m_problem_files << std::endl;
}

void report( const std::string& name, const per_file_data& data )
Expand Down
2 changes: 2 additions & 0 deletions src/test/pegtl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(test_sources
buffer_rule_is_buffer.cpp
buffer_rule_require.cpp
buffer_static_buffer.cpp
buffer_text_buffer_input.cpp
contrib_alphabet.cpp
contrib_check_count.cpp
contrib_check_depth.cpp
Expand All @@ -56,6 +57,7 @@ set(test_sources
contrib_integer.cpp
contrib_integer_convert.cpp
contrib_integer_match.cpp
contrib_integer_maximum.cpp
contrib_iri.cpp
contrib_json.cpp
contrib_limit_count.cpp
Expand Down
1 change: 1 addition & 0 deletions src/test/pegtl/buffer_buffer_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace TAO_PEGTL_NAMESPACE
#if defined( __cpp_exceptions )
TAO_PEGTL_TEST_THROWS( bc.require( 101 ) );
TAO_PEGTL_TEST_THROWS( (void)bc.end( 100, 101 ) );
TAO_PEGTL_TEST_THROWS( (void)bc.size( 100, 101 ) );
#endif

TAO_PEGTL_TEST_ASSERT( !bc.empty() );
Expand Down
48 changes: 48 additions & 0 deletions src/test/pegtl/buffer_text_buffer_input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#include "test.hpp"

#include <tao/pegtl/buffer.hpp>

namespace TAO_PEGTL_NAMESPACE
{
template< typename ParseInput >
void main_test( ParseInput& in )
{
in.require( 10 );
const auto m = in.rewind_position();
TAO_PEGTL_TEST_ASSERT( in.previous( m ) == in.current() );
in.consume< any >( 6 );
TAO_PEGTL_TEST_ASSERT( in.previous( m ) + 6 == in.current() );
in.rewind_to_position( m );
TAO_PEGTL_TEST_ASSERT( in.previous( m ) == in.current() );
TAO_PEGTL_TEST_ASSERT( &in.current_position() == &in.direct_position() );
TAO_PEGTL_TEST_ASSERT( in.direct_count() == in.current_position().count );
TAO_PEGTL_TEST_ASSERT( in.direct_line() == in.current_position().line );
TAO_PEGTL_TEST_ASSERT( in.direct_column() == in.current_position().column );
in.consume< alpha >( 5 );
const auto n = in.rewind_position();
in.consume< eol >( 1 );
TAO_PEGTL_TEST_ASSERT( in.direct_count() == 6 );
TAO_PEGTL_TEST_ASSERT( in.direct_line() == 2 );
TAO_PEGTL_TEST_ASSERT( in.direct_column() == 1 );
const auto p = in.previous_position( n );
TAO_PEGTL_TEST_ASSERT( p.count == 5 );
TAO_PEGTL_TEST_ASSERT( p.line == 1 );
TAO_PEGTL_TEST_ASSERT( p.column == 6 );
}

void unit_test()
{
dynamic_text_cstring_input<> i1( 50, 10, "input\ndata" );
main_test( i1 );
dynamic_text_cstring_input< tao_buffer_eol, std::string > i2( "source", 50, 10, "input\ndata" );
main_test( i2 );
TAO_PEGTL_TEST_ASSERT( i2.direct_source() == "source" );
}

} // namespace TAO_PEGTL_NAMESPACE

#include "main.hpp"
33 changes: 33 additions & 0 deletions src/test/pegtl/contrib_input_with_depth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#include <string_view>

#include "test.hpp"
#include "test_utility.hpp"

#include <tao/pegtl/contrib/input_with_depth.hpp>

namespace TAO_PEGTL_NAMESPACE
{
void unit_test()
{
using input_t = input_with_depth< text_view_input< scan::lf > >;
const std::string_view data = "test";
input_t in( data );
TAO_PEGTL_TEST_ASSERT( in.current() == data.data() );
TAO_PEGTL_TEST_ASSERT( in.current_depth() == 0 );
{
const auto dg = in.make_depth_guard();
TAO_PEGTL_TEST_ASSERT( in.current_depth() == 1 );
in.consume< alpha >( 2 );
TAO_PEGTL_TEST_ASSERT( in.current() == data.data() + 2 );
}
TAO_PEGTL_TEST_ASSERT( in.current() == data.data() + 2 );
TAO_PEGTL_TEST_ASSERT( in.current_depth() == 0 );
}

} // namespace TAO_PEGTL_NAMESPACE

#include "main.hpp"
2 changes: 2 additions & 0 deletions src/test/pegtl/contrib_input_with_offset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace TAO_PEGTL_NAMESPACE
TAO_PEGTL_TEST_ASSERT( b );
TAO_PEGTL_TEST_ASSERT( in.previous_position( r ).count == 46 );
TAO_PEGTL_TEST_ASSERT( in.current_position().count == 49 );
TAO_PEGTL_TEST_ASSERT( in.direct_offset() == count_position( 42 ) );
}

void test_count_offset_source()
Expand Down Expand Up @@ -71,6 +72,7 @@ namespace TAO_PEGTL_NAMESPACE
TAO_PEGTL_TEST_ASSERT( in.current_position().count == 107 );
TAO_PEGTL_TEST_ASSERT( in.current_position().line == 201 );
TAO_PEGTL_TEST_ASSERT( in.current_position().column == 4 );
TAO_PEGTL_TEST_ASSERT( in.direct_offset() == text_position( 200, 300, 100 ) );
}

void test_text_offset_source()
Expand Down
Loading

0 comments on commit 7592c0f

Please sign in to comment.