openvcdiff@gmail.com | 732fff2 | 2010-08-04 18:00:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 Google Inc. |
| 2 | // Author: James deBoer |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | // Unit tests for the class JSONCodeTableWriter, found in jsonwriter.h. |
| 17 | |
| 18 | #include <config.h> |
| 19 | #include "jsonwriter.h" |
| 20 | #include "testing.h" |
| 21 | #include "vcdiff_defs.h" |
| 22 | #include "google/output_string.h" |
| 23 | |
| 24 | namespace open_vcdiff { |
| 25 | namespace { |
| 26 | |
| 27 | class JSONWriterTest : public testing::Test { |
| 28 | protected: |
| 29 | typedef std::string string; |
| 30 | |
| 31 | JSONWriterTest() |
| 32 | : output_string_(&out_) { |
| 33 | EXPECT_TRUE(coder_.Init(0)); |
| 34 | coder_.WriteHeader(&output_string_, 0); |
| 35 | } |
| 36 | |
| 37 | virtual ~JSONWriterTest() { } |
| 38 | |
| 39 | string out_; |
| 40 | OutputString<string> output_string_; |
| 41 | JSONCodeTableWriter coder_; |
| 42 | }; |
| 43 | |
| 44 | TEST_F(JSONWriterTest, Null) { |
| 45 | EXPECT_EQ(0U, coder_.target_length()); |
| 46 | coder_.FinishEncoding(&output_string_); |
| 47 | EXPECT_EQ("", out_); |
| 48 | EXPECT_EQ(0U, coder_.target_length()); |
| 49 | } |
| 50 | |
| 51 | TEST_F(JSONWriterTest, Empty) { |
| 52 | EXPECT_EQ(0U, coder_.target_length()); |
| 53 | coder_.Output(&output_string_); |
| 54 | coder_.FinishEncoding(&output_string_); |
| 55 | EXPECT_EQ("[]", out_); |
| 56 | EXPECT_EQ(0U, coder_.target_length()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(JSONWriterTest, Add) { |
| 60 | coder_.Add("123", 3); |
| 61 | EXPECT_EQ(3U, coder_.target_length()); |
| 62 | coder_.Output(&output_string_); |
| 63 | EXPECT_EQ(0U, coder_.target_length()); |
| 64 | coder_.FinishEncoding(&output_string_); |
| 65 | EXPECT_EQ("[\"123\",]", out_); |
| 66 | } |
| 67 | |
| 68 | TEST_F(JSONWriterTest, Copy) { |
| 69 | coder_.Copy(3, 5); |
| 70 | EXPECT_EQ(5U, coder_.target_length()); |
| 71 | coder_.Output(&output_string_); |
| 72 | EXPECT_EQ(0U, coder_.target_length()); |
| 73 | coder_.FinishEncoding(&output_string_); |
| 74 | EXPECT_EQ("[3,5,]", out_); |
| 75 | } |
| 76 | |
| 77 | TEST_F(JSONWriterTest, Run) { |
| 78 | coder_.Run(3, 'a'); |
| 79 | EXPECT_EQ(3U, coder_.target_length()); |
| 80 | coder_.Output(&output_string_); |
| 81 | EXPECT_EQ(0U, coder_.target_length()); |
| 82 | coder_.FinishEncoding(&output_string_); |
| 83 | EXPECT_EQ("[\"aaa\",]", out_); |
| 84 | } |
| 85 | |
| 86 | TEST_F(JSONWriterTest, AddEscaped) { |
| 87 | coder_.Add("\n\b\r", 3); |
| 88 | EXPECT_EQ(3U, coder_.target_length()); |
| 89 | coder_.Output(&output_string_); |
| 90 | EXPECT_EQ(0U, coder_.target_length()); |
| 91 | coder_.FinishEncoding(&output_string_); |
| 92 | EXPECT_EQ("[\"\\n\\b\\r\",]", out_); |
| 93 | } |
| 94 | |
| 95 | TEST_F(JSONWriterTest, AddCopyAdd) { |
| 96 | coder_.Add("abc", 3); |
| 97 | coder_.Copy(3, 5); |
| 98 | coder_.Add("defghij", 7); |
| 99 | EXPECT_EQ(15U, coder_.target_length()); |
| 100 | coder_.Output(&output_string_); |
| 101 | EXPECT_EQ(0U, coder_.target_length()); |
| 102 | coder_.FinishEncoding(&output_string_); |
| 103 | EXPECT_EQ("[\"abc\",3,5,\"defghij\",]", out_); |
| 104 | } |
| 105 | |
| 106 | TEST_F(JSONWriterTest, AddOutputAddOutputToSameString) { |
| 107 | coder_.Add("abc", 3); |
| 108 | EXPECT_EQ(3U, coder_.target_length()); |
| 109 | coder_.Output(&output_string_); |
| 110 | EXPECT_EQ(0U, coder_.target_length()); |
| 111 | EXPECT_EQ("[\"abc\",", out_); |
| 112 | coder_.Add("def", 3); |
| 113 | EXPECT_EQ(3U, coder_.target_length()); |
| 114 | coder_.Output(&output_string_); |
| 115 | EXPECT_EQ(0U, coder_.target_length()); |
| 116 | coder_.FinishEncoding(&output_string_); |
| 117 | EXPECT_EQ("[\"abc\",\"def\",]", out_); |
| 118 | } |
| 119 | |
| 120 | TEST_F(JSONWriterTest, AddOutputAddOutputToDifferentString) { |
| 121 | coder_.Add("abc", 3); |
| 122 | EXPECT_EQ(3U, coder_.target_length()); |
| 123 | coder_.Output(&output_string_); |
| 124 | EXPECT_EQ(0U, coder_.target_length()); |
| 125 | coder_.FinishEncoding(&output_string_); |
| 126 | EXPECT_EQ("[\"abc\",]", out_); |
| 127 | string out2; |
| 128 | OutputString<string> output_string2(&out2); |
| 129 | coder_.Init(0); |
| 130 | coder_.Add("def", 3); |
| 131 | EXPECT_EQ(3U, coder_.target_length()); |
| 132 | coder_.Output(&output_string2); |
| 133 | EXPECT_EQ(0U, coder_.target_length()); |
| 134 | coder_.FinishEncoding(&output_string2); |
| 135 | EXPECT_EQ("[\"def\",]", out2); |
| 136 | } |
| 137 | |
| 138 | } // unnamed namespace |
| 139 | } // namespace open_vcdiff |