Skip to content

Commit

Permalink
Still working on the file system and the file system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baudin999 committed Dec 27, 2019
1 parent 9d7b6fb commit f9e5386
Show file tree
Hide file tree
Showing 31 changed files with 756 additions and 257 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Project;
using System;
using System.IO;
using System.Threading.Tasks;
using Project;
using Xunit.Abstractions;

namespace ApplicationTests
Expand All @@ -10,7 +9,7 @@ public class BaseFileWatcherTest : IDisposable
{
protected readonly ITestOutputHelper output;
protected readonly string dir;
protected readonly FileProject project;
protected readonly IProject project;

public BaseFileWatcherTest(ITestOutputHelper _output, string _dir)
{
Expand All @@ -22,7 +21,7 @@ public BaseFileWatcherTest(ITestOutputHelper _output, string _dir)
}
Directory.CreateDirectory(dir);
output = _output;
project = new FileProject(dir);
project = ProjectContext.Init(dir);
project.Watch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task CreateModule()
{
try
{
var module = await project.CreateModule("Test");
var module = await project.CreateModule("Test", "Code");
Assert.True(File.Exists(path("Test.car")));
Assert.NotNull(module);
Assert.Equal("Test", module.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task DeleteModule()
{
try
{
var module = await project.CreateModule("Test");
var module = await project.CreateModule("Test", "");
var filePath = module.FilePath.Clone().ToString();
var outPath = module.OutPath.Clone().ToString();

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -15,7 +15,7 @@ public async Task MoveModule()
{
try
{
var module = await project.CreateModule("Test");
var module = await project.CreateModule("Test", "");
Assert.True(File.Exists(path("Test.car")));
Assert.NotNull(module);
Assert.Equal("Test", module.Name);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task CreateModule()
{
try
{
await project.CreateModule("Test");
await project.CreateModule("Test", "");
Assert.True(File.Exists(path("Test.car")));
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task AddStudentModule()
{
Console.WriteLine("STARTED: AddStudentModule");
await Init();
var studentModule = await project.CreateModule("Student");
var studentModule = await project.CreateModule("Student", "");
var schoolModule = project.FindModule("School");

Assert.NotNull(studentModule);
Expand Down
41 changes: 41 additions & 0 deletions ApplicationTests/InMemoryTests/BasicInMemoryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Project;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ApplicationTests.InMemoryTests
{
public class BasicInMemoryTest
{
[Fact]
public async Task CreateInMemoryProject()

{
IProject project = ProjectContext.InitInMemory();
IModule module = await project.CreateModule("Test", "");
Assert.NotNull(module);
IModule test = project.FindModule("Test");
Assert.NotNull(test);
}

[Fact]
public async Task CreateALotOfModules()

{
IProject project = ProjectContext.InitInMemory();
IModule test = await project.CreateModule("Test", "");
IModule other = await project.CreateModule("Other", "");
IModule something = await project.CreateModule("Something", "");
IModule foo = await project.CreateModule("Foo", "");
IModule bar = await project.CreateModule("Bar", "");

var instance = Project.FileSystems.MemorySystem.Instance;

Assert.NotNull(test);


}
}
}
81 changes: 81 additions & 0 deletions ApplicationTests/MappingTest/SimpleMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Compiler;
using Compiler.AST;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace ApplicationTests.MappingTest
{
public class SimpleMapping
{
[Fact]
public void NameMapper()
{
var code = @"
type Person =
FirstName: Name;
alias Name = String;
";

var g = new ASTGenerator(code);
var nameMapper = new NameMapper();
var names = nameMapper.Map(new NameVisitor(g));
var result = nameMapper.Process();

Assert.NotNull(result);
Assert.Equal(@"Person
FirstName
Name", result);
}
}

public class NameMapper : IMapper<string>
{
public IEnumerable<string> Result { get; private set; }

public IEnumerable<string> Map<U>(VisitorBase<U> visitor) where U : IEnumerable<string>
{
this.Result = visitor.Start().SelectMany(i => i);
return this.Result;
}

public string Process()
{

return String.Join(Environment.NewLine, Result);
}
}

public class NameVisitor : VisitorDefault<IEnumerable<string>>
{
public NameVisitor(ASTGenerator generator) : base(generator) { }

public override IEnumerable<string> VisitASTTypeField(ASTTypeField astTypeField)
{
yield return astTypeField.Name;
}

public override IEnumerable<string> VisitASTType(ASTType astType)
{
yield return astType.Name;
foreach (var field in astType.Fields)
{
yield return Visit(field).First();
}
}

public override IEnumerable<string> VisitASTAlias(ASTAlias astAlias)
{
yield return astAlias.Name;
}
}

public interface IMapper<T>
{
IEnumerable<T> Result { get; }
IEnumerable<T> Map<U>(VisitorBase<U> visitor) where U: IEnumerable<T>;
string Process();
}
}
46 changes: 46 additions & 0 deletions CLI/AssetHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace CLI
{
internal static class AssetHelpers
{
internal static string ReadAsset(string name)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "CLI.Assets." + name;

using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (!(stream is null))
{
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();
reader.Close();
reader.Dispose();
return result;
}
}
else
{
return "";
}
}
}

internal static void WriteAsset(string path, string content)
{
File.WriteAllText(path, content);
}

internal static void ReadAndWriteAsset(string assetName, string outPath)
{
var outName = System.IO.Path.GetFullPath(assetName, outPath);
AssetHelpers.WriteAsset(outName, AssetHelpers.ReadAsset(assetName));
}
}
}
1 change: 1 addition & 0 deletions CLI/Commands/Commands.Watch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void CreateWatchCommand(CommandLineApplication app)
true => fileOption.Value()
};
ProjectContext.Init(directory);
//ProjectContext.InitInMemory();
var project = ProjectContext.Instance;
if (project != null)
{
Expand Down
2 changes: 1 addition & 1 deletion CLI/Controllers/JsonDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CLI.Controllers
{
public class JsonDataController : ControllerBase
{
private Module? Module;
private IModule? Module;

[HttpGet("/api/data/{module}/{type}")]
public IActionResult GetData(string module, string type, [FromQuery]bool list)
Expand Down
2 changes: 1 addition & 1 deletion CLI/Controllers/ModuleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IActionResult> CreateModuleAsync(string name)
var project = ProjectContext.Instance;
if (project != null)
{
Module? module = await project.CreateModule(name, null);
IModule? module = await project.CreateModule(name, null);
if (module is null) return BadRequest($"Failed to created module {name}.");

return Ok(new List<Descriptor> {
Expand Down
46 changes: 5 additions & 41 deletions CLI/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Task Start(string rootPath)
{
var portNumber = ProjectContext.Instance?.CarConfig?.PortNumber ?? "5000";
RootPath = rootPath;
Task.Run(async () =>
Task.Run(async () =>
{
await Task.Delay(1500);
WebServer.OpenBrowser($"http://localhost:{portNumber}/index.html");
Expand Down Expand Up @@ -53,7 +53,7 @@ public static Task Start(string rootPath)
});
}

public static void OpenBrowser(string url)
private static void OpenBrowser(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -76,47 +76,11 @@ public static void OpenBrowser(string url)

private static void CreateAssets(string outPath)
{
Helpers.ReadAndWriteAsset("mermaid.min.js", outPath);
Helpers.ReadAndWriteAsset("mermaid.min.js.map", outPath);
AssetHelpers.ReadAndWriteAsset("mermaid.min.js", outPath);
AssetHelpers.ReadAndWriteAsset("mermaid.min.js.map", outPath);
}

private static class Helpers
{
public static string ReadAsset(string name)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "CLI.Assets." + name;

using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (!(stream is null))
{
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();
reader.Close();
reader.Dispose();
return result;
}
}
else
{
return "";
}
}
}

public static void WriteAsset(string path, string content)
{
File.WriteAllText(path, content);
}

public static void ReadAndWriteAsset(string assetName, string outPath)
{
var outName = System.IO.Path.GetFullPath(assetName, outPath);
Helpers.WriteAsset(outName, Helpers.ReadAsset(assetName));
}
}

}


Expand Down
Loading

0 comments on commit f9e5386

Please sign in to comment.