blob: f6ec3fa0caa014c3e1eb531c765d5b9ca1643941 [file] [log] [blame]
Bouke Versteegh69dcd1e2022-01-08 16:27:50 +01001#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc. All rights reserved.
Bouke Versteegh69dcd1e2022-01-08 16:27:50 +01005//
Joshua Haberman8c05a642023-09-08 17:13:18 -07006// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file or at
8// https://developers.google.com/open-source/licenses/bsd
Bouke Versteegh69dcd1e2022-01-08 16:27:50 +01009
10#endregion
11
12using Google.Protobuf.Reflection;
13using NUnit.Framework;
14
15// For WrapInQuotes
16
17namespace Google.Protobuf
18{
19 public class JsonFormatterSettingsTest
20 {
21 [Test]
22 public void WithIndentation()
23 {
24 var settings = JsonFormatter.Settings.Default.WithIndentation("\t");
25 Assert.AreEqual("\t", settings.Indentation);
26 }
27
28 [Test]
29 public void WithTypeRegistry()
30 {
31 var typeRegistry = TypeRegistry.Empty;
32 var settings = JsonFormatter.Settings.Default.WithTypeRegistry(typeRegistry);
33 Assert.AreEqual(typeRegistry, settings.TypeRegistry);
34 }
35
36 [Test]
37 public void WithFormatDefaultValues()
38 {
39 var settingsWith = JsonFormatter.Settings.Default.WithFormatDefaultValues(true);
40 Assert.AreEqual(true, settingsWith.FormatDefaultValues);
41
42 var settingsWithout = JsonFormatter.Settings.Default.WithFormatDefaultValues(false);
43 Assert.AreEqual(false, settingsWithout.FormatDefaultValues);
44 }
45
46 [Test]
47 public void WithFormatEnumsAsIntegers()
48 {
49 var settingsWith = JsonFormatter.Settings.Default.WithFormatEnumsAsIntegers(true);
50 Assert.AreEqual(true, settingsWith.FormatEnumsAsIntegers);
51
52 var settingsWithout = JsonFormatter.Settings.Default.WithFormatEnumsAsIntegers(false);
53 Assert.AreEqual(false, settingsWithout.FormatEnumsAsIntegers);
54 }
55
56 [Test]
57 public void WithMethodsPreserveExistingSettings()
58 {
59 var typeRegistry = TypeRegistry.Empty;
60 var baseSettings = JsonFormatter.Settings.Default
61 .WithIndentation("\t")
62 .WithFormatDefaultValues(true)
63 .WithFormatEnumsAsIntegers(true)
64 .WithTypeRegistry(typeRegistry)
65 .WithPreserveProtoFieldNames(true);
66
67 var settings1 = baseSettings.WithIndentation("\t");
68 var settings2 = baseSettings.WithFormatDefaultValues(true);
69 var settings3 = baseSettings.WithFormatEnumsAsIntegers(true);
70 var settings4 = baseSettings.WithTypeRegistry(typeRegistry);
71 var settings5 = baseSettings.WithPreserveProtoFieldNames(true);
72
73 AssertAreEqual(baseSettings, settings1);
74 AssertAreEqual(baseSettings, settings2);
75 AssertAreEqual(baseSettings, settings3);
76 AssertAreEqual(baseSettings, settings4);
77 AssertAreEqual(baseSettings, settings5);
78 }
79
80 private static void AssertAreEqual(JsonFormatter.Settings settings, JsonFormatter.Settings other)
81 {
82 Assert.AreEqual(settings.Indentation, other.Indentation);
83 Assert.AreEqual(settings.FormatDefaultValues, other.FormatDefaultValues);
84 Assert.AreEqual(settings.FormatEnumsAsIntegers, other.FormatEnumsAsIntegers);
85 Assert.AreEqual(settings.TypeRegistry, other.TypeRegistry);
86 }
87 }
88}