$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r83117 - in trunk/tools/quickbook: src test/unit
From: dnljms_at_[hidden]
Date: 2013-02-24 06:21:50
Author: danieljames
Date: 2013-02-24 06:21:50 EST (Sun, 24 Feb 2013)
New Revision: 83117
URL: http://svn.boost.org/trac/boost/changeset/83117
Log:
Source map tests.
Added:
   trunk/tools/quickbook/test/unit/source_map_test.cpp   (contents, props changed)
Text files modified: 
   trunk/tools/quickbook/src/files.cpp        |     5 +++++                                   
   trunk/tools/quickbook/src/files.hpp        |     8 ++++++++                                
   trunk/tools/quickbook/test/unit/Jamfile.v2 |     1 +                                       
   3 files changed, 14 insertions(+), 0 deletions(-)
Modified: trunk/tools/quickbook/src/files.cpp
==============================================================================
--- trunk/tools/quickbook/src/files.cpp	(original)
+++ trunk/tools/quickbook/src/files.cpp	2013-02-24 06:21:50 EST (Sun, 24 Feb 2013)
@@ -135,6 +135,11 @@
         return pos->second;
     }
 
+    std::ostream& operator<<(std::ostream& out, file_position const& x)
+    {
+        return out << "line: " << x.line << ", column: " << x.column;
+    }
+
     file_position relative_position(
         boost::string_ref::const_iterator begin,
         boost::string_ref::const_iterator iterator)
Modified: trunk/tools/quickbook/src/files.hpp
==============================================================================
--- trunk/tools/quickbook/src/files.hpp	(original)
+++ trunk/tools/quickbook/src/files.hpp	2013-02-24 06:21:50 EST (Sun, 24 Feb 2013)
@@ -17,6 +17,7 @@
 #include <boost/utility/string_ref.hpp>
 #include <stdexcept>
 #include <cassert>
+#include <iosfwd>
 
 namespace quickbook {
 
@@ -32,6 +33,13 @@
 
         int line;
         int column;
+        
+        bool operator==(file_position const& other) const
+        {
+            return line == other.line && column == other.column;
+        }
+        
+        friend std::ostream& operator<<(std::ostream&, file_position const&);
     };
 
     struct file
Modified: trunk/tools/quickbook/test/unit/Jamfile.v2
==============================================================================
--- trunk/tools/quickbook/test/unit/Jamfile.v2	(original)
+++ trunk/tools/quickbook/test/unit/Jamfile.v2	2013-02-24 06:21:50 EST (Sun, 24 Feb 2013)
@@ -22,6 +22,7 @@
 
 run values_test.cpp ../../src/values.cpp ../../src/files.cpp ;
 run post_process_test.cpp ../../src/post_process.cpp ;
+run source_map_test.cpp ../../src/files.cpp ;
 
 # Copied from spirit
 run symbols_tests.cpp ;
Added: trunk/tools/quickbook/test/unit/source_map_test.cpp
==============================================================================
--- (empty file)
+++ trunk/tools/quickbook/test/unit/source_map_test.cpp	2013-02-24 06:21:50 EST (Sun, 24 Feb 2013)
@@ -0,0 +1,217 @@
+/*=============================================================================
+    Copyright (c) 2012 Daniel James
+
+    Use, modification and distribution is subject to the Boost Software
+    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+    http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+
+#include "fwd.hpp"
+#include "files.hpp"
+#include <boost/utility/string_ref.hpp>
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/range/algorithm/find.hpp>
+
+void simple_map_tests()
+{
+    boost::string_ref source("First Line\nSecond Line");
+    quickbook::file_ptr fake_file = new quickbook::file(
+        "(fake file)", source, 105u);
+
+    quickbook::string_iterator line1 = fake_file->source().begin();
+    quickbook::string_iterator line1_end = boost::find(fake_file->source(), '\n');
+    quickbook::string_iterator line2 = line1_end + 1;
+    quickbook::string_iterator line2_end = fake_file->source().end();
+
+    quickbook::mapped_file_builder builder;
+    
+    { // Empty test
+        builder.start(fake_file);
+        BOOST_TEST(builder.empty());
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST(f1->source().empty());
+    }
+    
+    { // Add full text
+        builder.start(fake_file);
+        builder.add(boost::string_ref(line1, line2_end - line1));
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(), source);
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 2),
+            quickbook::file_position(1,3));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line1_end - line1)),
+            quickbook::file_position(1,line1_end - line1 + 1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2 - line1)),
+            quickbook::file_position(2,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            fake_file->position_of(fake_file->source().end()));
+    }
+
+    { // Add first line
+        builder.start(fake_file);
+        builder.add(boost::string_ref(line1, line1_end - line1));
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(),
+            boost::string_ref(source.begin(), line1_end - line1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 2),
+            quickbook::file_position(1,3));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(1,line1_end - line1 + 1));
+    }
+
+    { // Add second line
+        builder.start(fake_file);
+        builder.add(boost::string_ref(line2, line2_end - line2));
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(), boost::string_ref("Second Line"));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(2,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 2),
+            quickbook::file_position(2,3));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(2,line2_end - line2 + 1));
+    }
+
+    { // Out of order
+        builder.start(fake_file);
+        builder.add(boost::string_ref(line2, line2_end - line2));
+        builder.add(boost::string_ref(line1_end, 1));
+        builder.add(boost::string_ref(line1, line1_end - line1));
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(),
+            boost::string_ref("Second Line\nFirst Line"));
+
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(2,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 2),
+            quickbook::file_position(2,3));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2_end - line2 - 1)),
+            quickbook::file_position(2,line2_end - line2));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2_end - line2)),
+            quickbook::file_position(1,(line1_end - line1 + 1)));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2_end - line2 + 1)),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(1,line1_end - line1 + 1));
+    }
+
+    { // Repeated text
+        builder.start(fake_file);
+        builder.add(boost::string_ref(line2, line2_end - line2));
+        builder.add(boost::string_ref(line1_end, 1));
+        builder.add(boost::string_ref(line2, line2_end - line2));
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(),
+            boost::string_ref("Second Line\nSecond Line"));
+
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(2,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 2),
+            quickbook::file_position(2,3));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2_end - line2 - 1)),
+            quickbook::file_position(2,line2_end - line2));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2_end - line2)),
+            quickbook::file_position(1,(line1_end - line1 + 1)));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + (line2_end - line2 + 1)),
+            quickbook::file_position(2,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(2,line2_end - line2 + 1));
+    }
+
+
+    { // Generated text
+        builder.start(fake_file);
+        builder.add("------\n", line1);
+        builder.add(boost::string_ref(line1, line1_end - line1));
+        builder.add("\n------\n", line1_end);
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(),
+            boost::string_ref("------\nFirst Line\n------\n"));
+        
+        quickbook::string_iterator newline = boost::find(f1->source(), '\n');
+
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 2),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(newline),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(newline + 1),
+            quickbook::file_position(1,1));
+        BOOST_TEST_EQ(f1->position_of(newline + 2),
+            quickbook::file_position(1,2));
+        BOOST_TEST_EQ(f1->position_of(newline + (line1_end - line1)),
+            quickbook::file_position(1,line1_end - line1));
+        BOOST_TEST_EQ(f1->position_of(newline + (line1_end - line1 + 1)),
+            quickbook::file_position(1,line1_end - line1 + 1));
+        BOOST_TEST_EQ(f1->position_of(newline + (line1_end - line1 + 2)),
+            quickbook::file_position(1,line1_end - line1 + 1));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(1,line1_end - line1 + 1));
+    }
+}
+
+void indented_map_tests()
+{
+    boost::string_ref source(
+        "   Code line1\n"
+        "   Code line2\n");
+    quickbook::file_ptr fake_file = new quickbook::file(
+        "(fake file)", source, 105u);
+
+    quickbook::mapped_file_builder builder;
+    
+    {
+        builder.start(fake_file);
+        builder.unindent_and_add(fake_file->source());
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(),
+            boost::string_ref("Code line1\nCode line2\n"));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(1,4));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 1),
+            quickbook::file_position(1,5));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 5),
+            quickbook::file_position(1,9));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 10),
+            quickbook::file_position(1,14));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 11),
+            quickbook::file_position(2,4));
+        // TODO: Shouldn't this be (3,1)? Does it matter?
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(3,4));
+    }
+
+    {
+        builder.start(fake_file);
+        builder.unindent_and_add(boost::string_ref(
+            fake_file->source().begin() + 3,
+            fake_file->source().end() - (fake_file->source().begin() + 3)));
+        quickbook::file_ptr f1 = builder.release();
+        BOOST_TEST_EQ(f1->source(),
+            boost::string_ref("Code line1\n   Code line2\n"));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+            quickbook::file_position(1,4));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 1),
+            quickbook::file_position(1,5));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 5),
+            quickbook::file_position(1,9));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 10),
+            quickbook::file_position(1,14));
+        BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 11),
+            quickbook::file_position(2,4));
+        BOOST_TEST_EQ(f1->position_of(f1->source().end()),
+            quickbook::file_position(3,4));
+    }
+}
+
+int main()
+{
+    simple_map_tests();
+    indented_map_tests();
+    return boost::report_errors();
+}