1 // Copyright 2021 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "json.h"
16 
17 #include "test.h"
18 
TEST(JSONTest, RegularAscii)19 TEST(JSONTest, RegularAscii) {
20   EXPECT_EQ(EncodeJSONString("foo bar"), "foo bar");
21 }
22 
TEST(JSONTest, EscapedChars)23 TEST(JSONTest, EscapedChars) {
24   EXPECT_EQ(EncodeJSONString("\"\\\b\f\n\r\t"),
25             "\\\""
26             "\\\\"
27             "\\b\\f\\n\\r\\t");
28 }
29 
30 // codepoints between 0 and 0x1f should be escaped
TEST(JSONTest, ControlChars)31 TEST(JSONTest, ControlChars) {
32   EXPECT_EQ(EncodeJSONString("\x01\x1f"), "\\u0001\\u001f");
33 }
34 
35 // Leave them alone as JSON accepts unicode literals
36 // out of control character range
TEST(JSONTest, UTF8)37 TEST(JSONTest, UTF8) {
38   const char* utf8str = "\xe4\xbd\xa0\xe5\xa5\xbd";
39   EXPECT_EQ(EncodeJSONString(utf8str), utf8str);
40 }
41