Skip to content

Commit

Permalink
Merge pull request #1 from sergey-shandar/testgenlib1
Browse files Browse the repository at this point in the history
TestGen Library.
  • Loading branch information
sergey-shandar authored Jan 20, 2017
2 parents f84dc86 + 95145a2 commit a6c7371
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>

<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>15c24e56-21aa-4dcb-ab6c-36bc4e93f24a</ProjectGuid>
Expand All @@ -13,9 +12,11 @@
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
55 changes: 55 additions & 0 deletions src/core/AutoRest.Core.TestGen.Tests/SampleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using AutoRest.Core.Utilities;
using AutoRest.Swagger;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Xunit;

namespace AutoRest.Core.TestGen.Tests
{
public sealed class SampleTest
{
[Fact]
public void SimpleSampleTest()
{
var fileSystem = new MemoryFileSystem();

fileSystem.WriteFile("swagger.json", JObject.FromObject(new { }).ToString());
var swagger = SwaggerParser.Load("swagger.json", fileSystem);

var sampleSource = new
{
title = "Title",
operationId = "Operation",
parameters = new
{
a = "3",
},
responses = new Dictionary<string, object>
{
{
"200",
new
{
headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json" }
}
}
}
}
};

var sampleSourceStr = JObject.FromObject(sampleSource).ToString();
var x = JsonConvert.DeserializeObject<Sample>(sampleSourceStr);

fileSystem.WriteFile("sample.json", sampleSourceStr);
var simple = Sample.Load("sample.json", fileSystem);

Assert.Equal(simple.Title, "Title");
Assert.Equal(simple.OperationId, "Operation");
Assert.Equal(simple.Parameters["a"], "3");
Assert.Equal(simple.Responses["200"].Headers["Content-Type"], "application/json");
}
}
}
4 changes: 4 additions & 0 deletions src/core/AutoRest.Core.TestGen.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"dependencies": {
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"AutoRest.Swagger": {
"target": "project",
"type": "build"
},
"AutoRest.Core.TestGen": {
"target": "project",
"type": "build"
Expand Down
12 changes: 12 additions & 0 deletions src/core/AutoRest.Core.TestGen/Response.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace AutoRest.Core.TestGen
{
public sealed class Response
{
public Dictionary<string, string> Headers { get; set; }

public JToken Body { get; set; }
}
}
23 changes: 20 additions & 3 deletions src/core/AutoRest.Core.TestGen/Sample.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
namespace AutoRest.Core.TestGen
using AutoRest.Core.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace AutoRest.Core.TestGen
{
public sealed class Sample
{
public string Title { get; }
public string Title { get; set; }

public string OperationId { get; set; }

public Dictionary<string, JToken> Parameters { get; set; }

public Dictionary<string, Response> Responses { get; set; }

public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
ContractResolver = new CamelCaseContractResolver()
};

public string OerationId { get; }
public static Sample Load(string fileName, IFileSystem fileSystem)
=> JsonConvert.DeserializeObject<Sample>(fileSystem.ReadFileAsText(fileName), Settings);
}
}
6 changes: 5 additions & 1 deletion src/core/AutoRest.Core.TestGen/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

"dependencies": {
"Newtonsoft.Json": "[9.0.1,10.0)",
"YamlDotNet.Signed": "3.8.0"
"YamlDotNet.Signed": "3.8.0",
"AutoRest.Swagger": {
"target": "project",
"type": "build"
}
}
}
25 changes: 6 additions & 19 deletions src/core/AutoRest.Core/Utilities/MemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,14 @@ public class MemoryFileSystem : IFileSystem, IDisposable
{
private const string FolderKey = "Folder";

private Dictionary<string, StringBuilder> _virtualStore =
public Dictionary<string, StringBuilder> VirtualStore { get; } =
new Dictionary<string, StringBuilder>();

public Dictionary<string, StringBuilder> VirtualStore
{
get { return _virtualStore; }
}

public bool IsCompletePath(string path)
=> Uri.IsWellFormedUriString(path, UriKind.Relative);

public string MakePathRooted(Uri rootPath, string relativePath)
{
return (new Uri(Path.Combine(rootPath.ToString(), relativePath).ToString(), UriKind.Relative)).ToString();
}
=> (new Uri(Path.Combine(rootPath.ToString(), relativePath).ToString(), UriKind.Relative)).ToString();

public Uri GetParentDir(string path)
{
Expand Down Expand Up @@ -98,9 +91,7 @@ public TextWriter GetTextWriter(string path)
}

public bool FileExists(string path)
{
return VirtualStore.ContainsKey(path);
}
=> VirtualStore.ContainsKey(path);

public void DeleteFile(string path)
{
Expand Down Expand Up @@ -222,15 +213,11 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_virtualStore?.Clear();
VirtualStore?.Clear();
}
}

public string CurrentDirectory
{
get
{
return "";
}
}
=> "";
}
}
1 change: 0 additions & 1 deletion src/modeler/AutoRest.Swagger/SwaggerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Globalization;
using System.Linq;
using AutoRest.Core;
using AutoRest.Core.Logging;
Expand Down

0 comments on commit a6c7371

Please sign in to comment.