1 // Copyright (c) 2014 Tomoki Imai
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/spirit/include/support_line_pos_iterator.hpp>
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/assign.hpp>
9 #include <iostream>
10 #include <string>
11 #include <vector>
12
13 struct validation {
validationvalidation14 validation()
15 : line(), column(), current(), is_defined(false) {
16 }
17
validationvalidation18 validation(size_t line, size_t column, std::string current)
19 : line(line), column(column), current(current), is_defined(true) {
20 }
21
22 size_t line;
23 size_t column;
24 std::string current;
25 bool is_defined;
26 };
27
28 typedef std::vector<validation> validations;
29
test(std::string const & input,validations const & validations)30 void test(std::string const& input, validations const& validations) {
31 typedef boost::spirit::line_pos_iterator<std::string::const_iterator> pos_iterator_t;
32
33 pos_iterator_t const input_begin(input.begin());
34 pos_iterator_t const input_end(input.end());
35 pos_iterator_t position(input_begin);
36 validations::const_iterator expected = validations.begin();
37
38 for (; position != input_end && expected != validations.end(); ++position, ++expected) {
39 if (!expected->is_defined)
40 continue;
41
42 boost::iterator_range<pos_iterator_t> const range = get_current_line(input_begin, position, input_end);
43 std::string const current(range.begin(), range.end());
44
45 BOOST_TEST_EQ(expected->line, get_line(position));
46 BOOST_TEST_EQ(expected->column, get_column(input_begin, position));
47 BOOST_TEST_EQ(expected->current, current);
48 }
49
50 BOOST_TEST(position == input_end);
51 BOOST_TEST(expected == validations.end());
52 }
53
54 // LR and CR
55
testLRandCR(std::string const & line_break)56 void testLRandCR(std::string const& line_break) {
57 std::string const input = line_break + line_break;
58 validations const validations = boost::assign::list_of<validation>
59 (1,1,"")()
60 (2,1,"")();
61 test(input, validations);
62 }
63
testLRandCR_foo_bar_git(std::string const & line_break)64 void testLRandCR_foo_bar_git(std::string const& line_break) {
65 std::string const input = "foo" + line_break + "bar" + line_break + "git";
66 validations const validations = boost::assign::list_of<validation>
67 (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")()
68 (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")()
69 (3,1,"git")(3,2,"git")(3,3,"git");
70 test(input, validations);
71 }
72
testLRandCR_bar_git(std::string const & line_break)73 void testLRandCR_bar_git(std::string const& line_break) {
74 std::string const input = line_break + "bar" + line_break + "git";
75 validations const validations = boost::assign::list_of<validation>
76 (1,1,"")()
77 (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")()
78 (3,1,"git")(3,2,"git")(3,3,"git");
79 test(input, validations);
80 }
81
testLRandCR_foo_bar(std::string const & line_break)82 void testLRandCR_foo_bar(std::string const& line_break) {
83 std::string const input = "foo" + line_break + "bar" + line_break;
84 validations const validations = boost::assign::list_of<validation>
85 (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")()
86 (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")();
87 test(input, validations);
88 }
89
90 // LR or CR
91
testLRorCR(std::string const & line_break)92 void testLRorCR(std::string const& line_break) {
93 std::string const input = line_break + line_break;
94 validations const validations = boost::assign::list_of<validation>
95 (1,1,"")
96 (2,1,"");
97 test(input, validations);
98 }
99
testLRorCR_foo_bar_git(std::string const & line_break)100 void testLRorCR_foo_bar_git(std::string const& line_break) {
101 std::string const input = "foo" + line_break + "bar" + line_break + "git";
102 validations const validations = boost::assign::list_of<validation>
103 (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")
104 (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")
105 (3,1,"git")(3,2,"git")(3,3,"git");
106 test(input, validations);
107 }
108
testLRorCR_bar_git(std::string const & line_break)109 void testLRorCR_bar_git(std::string const& line_break) {
110 std::string const input = line_break + "bar" + line_break + "git";
111 validations const validations = boost::assign::list_of<validation>
112 (1,1,"")
113 (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")
114 (3,1,"git")(3,2,"git")(3,3,"git");
115 test(input, validations);
116 }
117
testLRorCR_foo_bar(std::string const & line_break)118 void testLRorCR_foo_bar(std::string const& line_break) {
119 std::string const input = "foo" + line_break + "bar" + line_break;
120 validations const validations = boost::assign::list_of<validation>
121 (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")
122 (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar");
123 test(input, validations);
124 }
125
main()126 int main()
127 {
128 testLRandCR("\r\n");
129 testLRandCR_foo_bar_git("\r\n");
130 testLRandCR_bar_git("\r\n");
131 testLRandCR_foo_bar("\r\n");
132
133 testLRandCR("\n\r");
134 testLRandCR_foo_bar_git("\n\r");
135 testLRandCR_bar_git("\n\r");
136 testLRandCR_foo_bar("\n\r");
137
138 testLRorCR("\r");
139 testLRorCR_foo_bar_git("\r");
140 testLRorCR_bar_git("\r");
141 testLRorCR_foo_bar("\r");
142
143 testLRorCR("\n");
144 testLRorCR_foo_bar_git("\n");
145 testLRorCR_bar_git("\n");
146 testLRorCR_foo_bar("\n");
147
148 return boost::report_errors();
149 }
150