Skip to content

Commit a8f109f

Browse files
Use TestData files for duration deserialization test
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
1 parent 4a84e15 commit a8f109f

4 files changed

Lines changed: 117 additions & 11 deletions

File tree

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/DeserializationTests.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,9 @@ public void TestDeserializationOfDurationMillisecondsIntegerWireType(string wire
136136
var mrwProvider = new ModelProvider(inputModel).SerializationProviders.FirstOrDefault();
137137
Assert.IsNotNull(mrwProvider);
138138

139-
var deserializationMethod = mrwProvider!.Methods.Where(m => m.Signature.Name.StartsWith("Deserialize")).FirstOrDefault();
140-
Assert.IsNotNull(deserializationMethod);
141-
142-
var methodBody = deserializationMethod!.BodyStatements!.ToDisplayString();
143-
144-
Assert.IsTrue(
145-
methodBody.Contains("global::System.TimeSpan.FromMilliseconds(prop.Value.GetInt32())"),
146-
$"Duration milliseconds with integer wire type should deserialize via GetInt32. Actual:\n{methodBody}");
147-
Assert.IsFalse(
148-
methodBody.Contains("prop.Value.GetDouble()"),
149-
$"Duration milliseconds with integer wire type should not call GetDouble. Actual:\n{methodBody}");
139+
var writer = new TypeProviderWriter(mrwProvider!);
140+
var file = writer.Write();
141+
Assert.AreEqual(Helpers.GetExpectedFromFile(parameters: wireKindName), file.Content);
150142
}
151143
}
152144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <auto-generated/>
2+
3+
#nullable disable
4+
5+
using System;
6+
using System.ClientModel.Primitives;
7+
using System.Collections.Generic;
8+
using System.Text.Json;
9+
using Sample;
10+
11+
namespace Sample.Models
12+
{
13+
public partial class TestModel : global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.TestModel>
14+
{
15+
internal static global::Sample.Models.TestModel DeserializeTestModel(global::System.Text.Json.JsonElement element, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
16+
{
17+
if ((element.ValueKind == global::System.Text.Json.JsonValueKind.Null))
18+
{
19+
return null;
20+
}
21+
global::System.TimeSpan audioEndMs = default;
22+
global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> additionalBinaryDataProperties = new global::Sample.ChangeTrackingDictionary<string, global::System.BinaryData>();
23+
foreach (var prop in element.EnumerateObject())
24+
{
25+
if (prop.NameEquals("audio_end_ms"u8))
26+
{
27+
audioEndMs = global::System.TimeSpan.FromMilliseconds(prop.Value.GetInt32());
28+
continue;
29+
}
30+
if ((options.Format != "W"))
31+
{
32+
additionalBinaryDataProperties.Add(prop.Name, global::System.BinaryData.FromString(prop.Value.GetRawText()));
33+
}
34+
}
35+
return new global::Sample.Models.TestModel(audioEndMs, additionalBinaryDataProperties);
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <auto-generated/>
2+
3+
#nullable disable
4+
5+
using System;
6+
using System.ClientModel.Primitives;
7+
using System.Collections.Generic;
8+
using System.Text.Json;
9+
using Sample;
10+
11+
namespace Sample.Models
12+
{
13+
public partial class TestModel : global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.TestModel>
14+
{
15+
internal static global::Sample.Models.TestModel DeserializeTestModel(global::System.Text.Json.JsonElement element, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
16+
{
17+
if ((element.ValueKind == global::System.Text.Json.JsonValueKind.Null))
18+
{
19+
return null;
20+
}
21+
global::System.TimeSpan audioEndMs = default;
22+
global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> additionalBinaryDataProperties = new global::Sample.ChangeTrackingDictionary<string, global::System.BinaryData>();
23+
foreach (var prop in element.EnumerateObject())
24+
{
25+
if (prop.NameEquals("audio_end_ms"u8))
26+
{
27+
audioEndMs = global::System.TimeSpan.FromMilliseconds(prop.Value.GetInt32());
28+
continue;
29+
}
30+
if ((options.Format != "W"))
31+
{
32+
additionalBinaryDataProperties.Add(prop.Name, global::System.BinaryData.FromString(prop.Value.GetRawText()));
33+
}
34+
}
35+
return new global::Sample.Models.TestModel(audioEndMs, additionalBinaryDataProperties);
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <auto-generated/>
2+
3+
#nullable disable
4+
5+
using System;
6+
using System.ClientModel.Primitives;
7+
using System.Collections.Generic;
8+
using System.Text.Json;
9+
using Sample;
10+
11+
namespace Sample.Models
12+
{
13+
public partial class TestModel : global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.TestModel>
14+
{
15+
internal static global::Sample.Models.TestModel DeserializeTestModel(global::System.Text.Json.JsonElement element, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
16+
{
17+
if ((element.ValueKind == global::System.Text.Json.JsonValueKind.Null))
18+
{
19+
return null;
20+
}
21+
global::System.TimeSpan audioEndMs = default;
22+
global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> additionalBinaryDataProperties = new global::Sample.ChangeTrackingDictionary<string, global::System.BinaryData>();
23+
foreach (var prop in element.EnumerateObject())
24+
{
25+
if (prop.NameEquals("audio_end_ms"u8))
26+
{
27+
audioEndMs = global::System.TimeSpan.FromMilliseconds(prop.Value.GetInt32());
28+
continue;
29+
}
30+
if ((options.Format != "W"))
31+
{
32+
additionalBinaryDataProperties.Add(prop.Name, global::System.BinaryData.FromString(prop.Value.GetRawText()));
33+
}
34+
}
35+
return new global::Sample.Models.TestModel(audioEndMs, additionalBinaryDataProperties);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)