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