Skip to content

Commit b27ce1b

Browse files
committed
Merge pull request restsharp#211 from petejohanson/json_enum_test
Restore a JSON lists test that got truncated, and add an additional test...
2 parents 0dcad14 + 112e541 commit b27ce1b

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

RestSharp.Tests/JsonTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public void Can_Deserialize_4sq_Json_With_Root_Element_Specified()
4949
public void Can_Deserialize_Lists_of_Simple_Types()
5050
{
5151
var doc = File.ReadAllText(Path.Combine("SampleData", "jsonlists.txt"));
52+
var json = new JsonDeserializer ();
53+
54+
var output = json.Deserialize<JsonLists> (new RestResponse { Content = doc });
55+
56+
Assert.NotEmpty (output.Names);
57+
Assert.NotEmpty (output.Numbers);
5258
}
5359

5460
[Fact]
@@ -149,6 +155,23 @@ public void Can_Deserialize_Root_Json_Array_To_List()
149155
Assert.Equal(4, output.Count);
150156
}
151157

158+
[Fact]
159+
public void Can_Deserialize_Various_Enum_Values ()
160+
{
161+
var data = File.ReadAllText (Path.Combine ("SampleData", "jsonenums.txt"));
162+
var response = new RestResponse { Content = data };
163+
var json = new JsonDeserializer ();
164+
var output = json.Deserialize<JsonEnumsTestStructure>(response);
165+
166+
Assert.Equal (output.Upper, Disposition.Friendly);
167+
Assert.Equal (output.Lower, Disposition.Friendly);
168+
Assert.Equal (output.CamelCased, Disposition.SoSo);
169+
Assert.Equal (output.Underscores, Disposition.SoSo);
170+
Assert.Equal (output.LowerUnderscores, Disposition.SoSo);
171+
Assert.Equal (output.Dashes, Disposition.SoSo);
172+
Assert.Equal (output.LowerDashes, Disposition.SoSo);
173+
}
174+
152175
[Fact]
153176
public void Can_Deserialize_Guid_String_Fields()
154177
{

RestSharp.Tests/RestSharp.Tests.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
<Content Include="SampleData\datetimes.txt">
112112
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
113113
</Content>
114+
<Content Include="SampleData\jsonenums.txt">
115+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
116+
</Content>
114117
<Content Include="SampleData\person.json.txt">
115118
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
116119
</Content>

RestSharp.Tests/SampleClasses/misc.cs

+11
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,15 @@ public class DateTimeTestStructure
153153
public DateTimeOffset? NullableDateTimeOffsetWithNull { get; set; }
154154
public DateTimeOffset? NullableDateTimeOffsetWithValue { get; set; }
155155
}
156+
157+
public class JsonEnumsTestStructure
158+
{
159+
public Disposition Upper { get; set; }
160+
public Disposition Lower { get; set; }
161+
public Disposition CamelCased { get; set; }
162+
public Disposition Underscores { get; set; }
163+
public Disposition LowerUnderscores { get; set; }
164+
public Disposition Dashes { get; set; }
165+
public Disposition LowerDashes { get; set; }
166+
}
156167
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"upper": "FRIENDLY",
3+
"lower": "friendly",
4+
"camel_cased": "SoSo",
5+
"underscores": "So_So",
6+
"lower_underscores": "so_so",
7+
"dashes": "So-So",
8+
"lower_dashes": "so_so",
9+
}

RestSharp/Extensions/StringExtensions.cs

+7-14
Original file line numberDiff line numberDiff line change
@@ -315,32 +315,25 @@ public static IEnumerable<string> GetNameVariants(this string name, CultureInfo
315315
if (String.IsNullOrEmpty(name))
316316
yield break;
317317

318-
var actualName = name;
319-
yield return actualName;
318+
yield return name;
320319

321320
// try camel cased name
322-
actualName = name.ToCamelCase(culture);
323-
yield return actualName;
321+
yield return name.ToCamelCase(culture);
324322

325323
// try lower cased name
326-
actualName = name.ToLower(culture);
327-
yield return actualName;
324+
yield return name.ToLower(culture);
328325

329326
// try name with underscores
330-
actualName = name.AddUnderscores();
331-
yield return actualName;
327+
yield return name.AddUnderscores();
332328

333329
// try name with underscores with lower case
334-
actualName = name.AddUnderscores().ToLower(culture);
335-
yield return actualName;
330+
yield return name.AddUnderscores().ToLower(culture);
336331

337332
// try name with dashes
338-
actualName = name.AddDashes();
339-
yield return actualName;
333+
yield return name.AddDashes();
340334

341335
// try name with dashes with lower case
342-
actualName = name.AddDashes().ToLower(culture);
343-
yield return actualName;
336+
yield return name.AddDashes().ToLower(culture);
344337
}
345338
}
346339
}

0 commit comments

Comments
 (0)