Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
80f89b4e
Commit
80f89b4e
authored
Aug 01, 2015
by
Jan Tattermusch
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #669 from jskeet/simpify-json-tests
Simpify json tests
parents
82aa6bbd
6cf5f660
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
60 deletions
+71
-60
JsonFormatterTest.cs
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
+71
-60
No files found.
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
View file @
80f89b4e
...
...
@@ -31,16 +31,16 @@
#endregion
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Google.Protobuf.TestProtos
;
using
NUnit.Framework
;
using
UnitTest.Issues.TestProtos
;
namespace
Google.Protobuf
{
/// <summary>
/// Tests for the JSON formatter. Note that in these tests, double quotes are replaced with apostrophes
/// for the sake of readability (embedding \" everywhere is painful). See the AssertJson method for details.
/// </summary>
public
class
JsonFormatterTest
{
[
Test
]
...
...
@@ -48,16 +48,16 @@ namespace Google.Protobuf
{
var
formatter
=
new
JsonFormatter
(
new
JsonFormatter
.
Settings
(
formatDefaultValues
:
false
));
Assert
.
AreEqual
(
"{ }"
,
formatter
.
Format
(
new
ForeignMessage
()));
Assert
.
AreEqual
(
"{ }"
,
formatter
.
Format
(
new
TestAllTypes
()));
Assert
.
AreEqual
(
"{ }"
,
formatter
.
Format
(
new
TestMap
()));
Assert
Json
(
"{ }"
,
formatter
.
Format
(
new
ForeignMessage
()));
Assert
Json
(
"{ }"
,
formatter
.
Format
(
new
TestAllTypes
()));
Assert
Json
(
"{ }"
,
formatter
.
Format
(
new
TestMap
()));
}
[
Test
]
public
void
DefaultValues_WhenIncluded
()
{
var
formatter
=
new
JsonFormatter
(
new
JsonFormatter
.
Settings
(
formatDefaultValues
:
true
));
Assert
.
AreEqual
(
"{ \"c\"
: 0 }"
,
formatter
.
Format
(
new
ForeignMessage
()));
Assert
Json
(
"{ 'c'
: 0 }"
,
formatter
.
Format
(
new
ForeignMessage
()));
}
[
Test
]
...
...
@@ -90,45 +90,45 @@ namespace Google.Protobuf
};
var
actualText
=
JsonFormatter
.
Default
.
Format
(
message
);
// Fields in
declaration order, which matches numeric order.
// Fields in
numeric order
var
expectedText
=
"{ "
+
"
\"singleInt32\"
: 100, "
+
"
\"singleInt64\": \"3210987654321\"
, "
+
"
\"singleUint32\"
: 4294967295, "
+
"
\"singleUint64\": \"18446744073709551615\"
, "
+
"
\"singleSint32\"
: -456, "
+
"
\"singleSint64\": \"-12345678901235\"
, "
+
"
\"singleFixed32\"
: 23, "
+
"
\"singleFixed64\": \"1234567890123\"
, "
+
"
\"singleSfixed32\"
: -123, "
+
"
\"singleSfixed64\": \"-12345678901234\"
, "
+
"
\"singleFloat\"
: 12.25, "
+
"
\"singleDouble\"
: 23.5, "
+
"
\"singleBool\"
: true, "
+
"
\"singleString\": \"test\\twith\\ttabs\"
, "
+
"
\"singleBytes\": \"AQIDBA==\"
, "
+
"
\"singleNestedMessage\": { \"bb\"
: 35 }, "
+
"
\"singleForeignMessage\": { \"c\"
: 10 }, "
+
"
\"singleImportMessage\": { \"d\"
: 20 }, "
+
"
\"singleNestedEnum\": \"FOO\"
, "
+
"
\"singleForeignEnum\": \"FOREIGN_BAR\"
, "
+
"
\"singleImportEnum\": \"IMPORT_BAZ\"
, "
+
"
\"singlePublicImportMessage\": { \"e\"
: 54 }"
+
"
'singleInt32'
: 100, "
+
"
'singleInt64': '3210987654321'
, "
+
"
'singleUint32'
: 4294967295, "
+
"
'singleUint64': '18446744073709551615'
, "
+
"
'singleSint32'
: -456, "
+
"
'singleSint64': '-12345678901235'
, "
+
"
'singleFixed32'
: 23, "
+
"
'singleFixed64': '1234567890123'
, "
+
"
'singleSfixed32'
: -123, "
+
"
'singleSfixed64': '-12345678901234'
, "
+
"
'singleFloat'
: 12.25, "
+
"
'singleDouble'
: 23.5, "
+
"
'singleBool'
: true, "
+
"
'singleString': 'test\\twith\\ttabs'
, "
+
"
'singleBytes': 'AQIDBA=='
, "
+
"
'singleNestedMessage': { 'bb'
: 35 }, "
+
"
'singleForeignMessage': { 'c'
: 10 }, "
+
"
'singleImportMessage': { 'd'
: 20 }, "
+
"
'singleNestedEnum': 'FOO'
, "
+
"
'singleForeignEnum': 'FOREIGN_BAR'
, "
+
"
'singleImportEnum': 'IMPORT_BAZ'
, "
+
"
'singlePublicImportMessage': { 'e'
: 54 }"
+
" }"
;
Assert
.
AreEqual
(
expectedText
,
actualText
);
Assert
Json
(
expectedText
,
actualText
);
}
[
Test
]
public
void
RepeatedField
()
{
Assert
.
AreEqual
(
"{ \"repeatedInt32\"
: [ 1, 2, 3, 4, 5 ] }"
,
Assert
Json
(
"{ 'repeatedInt32'
: [ 1, 2, 3, 4, 5 ] }"
,
JsonFormatter
.
Default
.
Format
(
new
TestAllTypes
{
RepeatedInt32
=
{
1
,
2
,
3
,
4
,
5
}
}));
}
[
Test
]
public
void
MapField_StringString
()
{
Assert
.
AreEqual
(
"{ \"mapStringString\": { \"with spaces\": \"bar\", \"a\": \"b\"
} }"
,
Assert
Json
(
"{ 'mapStringString': { 'with spaces': 'bar', 'a': 'b'
} }"
,
JsonFormatter
.
Default
.
Format
(
new
TestMap
{
MapStringString
=
{
{
"with spaces"
,
"bar"
},
{
"a"
,
"b"
}
}
}));
}
...
...
@@ -136,7 +136,7 @@ namespace Google.Protobuf
public
void
MapField_Int32Int32
()
{
// The keys are quoted, but the values aren't.
Assert
.
AreEqual
(
"{ \"mapInt32Int32\": { \"0\": 1, \"2\"
: 3 } }"
,
Assert
Json
(
"{ 'mapInt32Int32': { '0': 1, '2'
: 3 } }"
,
JsonFormatter
.
Default
.
Format
(
new
TestMap
{
MapInt32Int32
=
{
{
0
,
1
},
{
2
,
3
}
}
}));
}
...
...
@@ -144,34 +144,34 @@ namespace Google.Protobuf
public
void
MapField_BoolBool
()
{
// The keys are quoted, but the values aren't.
Assert
.
AreEqual
(
"{ \"mapBoolBool\": { \"false\": true, \"true\"
: false } }"
,
Assert
Json
(
"{ 'mapBoolBool': { 'false': true, 'true'
: false } }"
,
JsonFormatter
.
Default
.
Format
(
new
TestMap
{
MapBoolBool
=
{
{
false
,
true
},
{
true
,
false
}
}
}));
}
[
TestCase
(
1.0
,
"1"
)]
[
TestCase
(
double
.
NaN
,
"
\"NaN\"
"
)]
[
TestCase
(
double
.
PositiveInfinity
,
"
\"Infinity\"
"
)]
[
TestCase
(
double
.
NegativeInfinity
,
"
\"-Infinity\"
"
)]
[
TestCase
(
double
.
NaN
,
"
'NaN'
"
)]
[
TestCase
(
double
.
PositiveInfinity
,
"
'Infinity'
"
)]
[
TestCase
(
double
.
NegativeInfinity
,
"
'-Infinity'
"
)]
public
void
DoubleRepresentations
(
double
value
,
string
expectedValueText
)
{
var
message
=
new
TestAllTypes
{
SingleDouble
=
value
};
string
actualText
=
JsonFormatter
.
Default
.
Format
(
message
);
string
expectedText
=
"{
\"singleDouble\"
: "
+
expectedValueText
+
" }"
;
Assert
.
AreEqual
(
expectedText
,
actualText
);
string
expectedText
=
"{
'singleDouble'
: "
+
expectedValueText
+
" }"
;
Assert
Json
(
expectedText
,
actualText
);
}
[
Test
]
public
void
UnknownEnumValueOmitted_SingleField
()
{
var
message
=
new
TestAllTypes
{
SingleForeignEnum
=
(
ForeignEnum
)
100
};
Assert
.
AreEqual
(
"{ }"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
public
void
UnknownEnumValueOmitted_RepeatedField
()
{
var
message
=
new
TestAllTypes
{
RepeatedForeignEnum
=
{
ForeignEnum
.
FOREIGN_BAZ
,
(
ForeignEnum
)
100
,
ForeignEnum
.
FOREIGN_FOO
}
};
Assert
.
AreEqual
(
"{ \"repeatedForeignEnum\": [ \"FOREIGN_BAZ\", \"FOREIGN_FOO\"
] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO'
] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
...
...
@@ -179,7 +179,7 @@ namespace Google.Protobuf
{
// This matches the C++ behaviour.
var
message
=
new
TestMap
{
MapInt32Enum
=
{
{
1
,
MapEnum
.
MAP_ENUM_FOO
},
{
2
,
(
MapEnum
)
100
},
{
3
,
MapEnum
.
MAP_ENUM_BAR
}
}
};
Assert
.
AreEqual
(
"{ \"mapInt32Enum\": { \"1\": \"MAP_ENUM_FOO\", \"3\": \"MAP_ENUM_BAR\"
} }"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '3': 'MAP_ENUM_BAR'
} }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
...
...
@@ -188,14 +188,14 @@ namespace Google.Protobuf
// *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write...
// but this is what happens at the moment, and it doesn't seem too awful.
var
message
=
new
TestAllTypes
{
RepeatedForeignEnum
=
{
(
ForeignEnum
)
200
,
(
ForeignEnum
)
100
}
};
Assert
.
AreEqual
(
"{ \"repeatedForeignEnum\"
: [ ] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ 'repeatedForeignEnum'
: [ ] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
public
void
NullValueForMessage
()
{
var
message
=
new
TestMap
{
MapInt32ForeignMessage
=
{
{
10
,
null
}
}
};
Assert
.
AreEqual
(
"{ \"mapInt32ForeignMessage\": { \"10\"
: null } }"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ 'mapInt32ForeignMessage': { '10'
: null } }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
...
...
@@ -205,14 +205,14 @@ namespace Google.Protobuf
public
void
SimpleNonAscii
(
string
text
,
string
encoded
)
{
var
message
=
new
TestAllTypes
{
SingleString
=
text
};
Assert
.
AreEqual
(
"{ \"singleString\": \""
+
encoded
+
"\"
}"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ 'singleString': '"
+
encoded
+
"'
}"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
public
void
SurrogatePairEscaping
()
{
var
message
=
new
TestAllTypes
{
SingleString
=
"a\uD801\uDC01b"
};
Assert
.
AreEqual
(
"{ \"singleString\": \"a\\ud801\\udc01b\"
}"
,
JsonFormatter
.
Default
.
Format
(
message
));
Assert
Json
(
"{ 'singleString': 'a\\ud801\\udc01b'
}"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
...
...
@@ -241,8 +241,8 @@ namespace Google.Protobuf
[
Test
]
[
TestCase
(
null
,
"{ }"
)]
[
TestCase
(
"x"
,
"{
\"fooString\": \"x\"
}"
)]
[
TestCase
(
""
,
"{
\"fooString\": \"\"
}"
)]
[
TestCase
(
"x"
,
"{
'fooString': 'x'
}"
)]
[
TestCase
(
""
,
"{
'fooString': ''
}"
)]
[
TestCase
(
null
,
"{ }"
)]
public
void
Oneof
(
string
fooStringValue
,
string
expectedJson
)
{
...
...
@@ -254,9 +254,9 @@ namespace Google.Protobuf
// We should get the same result both with and without "format default values".
var
formatter
=
new
JsonFormatter
(
new
JsonFormatter
.
Settings
(
false
));
Assert
.
AreEqual
(
expectedJson
,
formatter
.
Format
(
message
));
Assert
Json
(
expectedJson
,
formatter
.
Format
(
message
));
formatter
=
new
JsonFormatter
(
new
JsonFormatter
.
Settings
(
true
));
Assert
.
AreEqual
(
expectedJson
,
formatter
.
Format
(
message
));
Assert
Json
(
expectedJson
,
formatter
.
Format
(
message
));
}
[
Test
]
...
...
@@ -271,8 +271,8 @@ namespace Google.Protobuf
BytesField
=
ByteString
.
FromBase64
(
"ABCD"
),
StringField
=
""
};
var
expectedJson
=
"{
\"int64Field\": \"10\", \"int32Field\": 0, \"stringField\": \"\", \"bytesField\": \"ABCD\"
}"
;
Assert
.
AreEqual
(
expectedJson
,
JsonFormatter
.
Default
.
Format
(
message
));
var
expectedJson
=
"{
'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD'
}"
;
Assert
Json
(
expectedJson
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
...
...
@@ -291,11 +291,11 @@ namespace Google.Protobuf
{
var
formatter
=
new
JsonFormatter
(
new
JsonFormatter
.
Settings
(
false
));
var
message
=
new
TestJsonFieldOrdering
{
PlainString
=
"p1"
,
PlainInt32
=
2
};
Assert
.
AreEqual
(
"{ \"plainString\": \"p1\", \"plainInt32\"
: 2 }"
,
formatter
.
Format
(
message
));
Assert
Json
(
"{ 'plainString': 'p1', 'plainInt32'
: 2 }"
,
formatter
.
Format
(
message
));
message
=
new
TestJsonFieldOrdering
{
O1Int32
=
5
,
O2String
=
"o2"
,
PlainInt32
=
10
,
PlainString
=
"plain"
};
Assert
.
AreEqual
(
"{ \"plainString\": \"plain\", \"o2String\": \"o2\", \"plainInt32\": 10, \"o1Int32\"
: 5 }"
,
formatter
.
Format
(
message
));
Assert
Json
(
"{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32'
: 5 }"
,
formatter
.
Format
(
message
));
message
=
new
TestJsonFieldOrdering
{
O1String
=
""
,
O2Int32
=
0
,
PlainInt32
=
10
,
PlainString
=
"plain"
};
Assert
.
AreEqual
(
"{ \"plainString\": \"plain\", \"o1String\": \"\", \"plainInt32\": 10, \"o2Int32\"
: 0 }"
,
formatter
.
Format
(
message
));
Assert
Json
(
"{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32'
: 0 }"
,
formatter
.
Format
(
message
));
}
[
Test
]
...
...
@@ -303,11 +303,22 @@ namespace Google.Protobuf
{
var
formatter
=
new
JsonFormatter
(
new
JsonFormatter
.
Settings
(
true
));
var
message
=
new
TestJsonFieldOrdering
();
Assert
.
AreEqual
(
"{ \"plainString\": \"\", \"plainInt32\"
: 0 }"
,
formatter
.
Format
(
message
));
Assert
Json
(
"{ 'plainString': '', 'plainInt32'
: 0 }"
,
formatter
.
Format
(
message
));
message
=
new
TestJsonFieldOrdering
{
O1Int32
=
5
,
O2String
=
"o2"
,
PlainInt32
=
10
,
PlainString
=
"plain"
};
Assert
.
AreEqual
(
"{ \"plainString\": \"plain\", \"o2String\": \"o2\", \"plainInt32\": 10, \"o1Int32\"
: 5 }"
,
formatter
.
Format
(
message
));
Assert
Json
(
"{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32'
: 5 }"
,
formatter
.
Format
(
message
));
message
=
new
TestJsonFieldOrdering
{
O1String
=
""
,
O2Int32
=
0
,
PlainInt32
=
10
,
PlainString
=
"plain"
};
Assert
.
AreEqual
(
"{ \"plainString\": \"plain\", \"o1String\": \"\", \"plainInt32\": 10, \"o2Int32\": 0 }"
,
formatter
.
Format
(
message
));
AssertJson
(
"{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }"
,
formatter
.
Format
(
message
));
}
/// <summary>
/// Checks that the actual JSON is the same as the expected JSON - but after replacing
/// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
/// to read.
/// </summary>
private
static
void
AssertJson
(
string
expectedJsonWithApostrophes
,
string
actualJson
)
{
var
expectedJson
=
expectedJsonWithApostrophes
.
Replace
(
"'"
,
"\""
);
Assert
.
AreEqual
(
expectedJson
,
actualJson
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment