Skip to content

Commit

Permalink
Fixed namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
anpetroc committed Dec 2, 2023
1 parent 691dfb2 commit dce741f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 80 deletions.
79 changes: 37 additions & 42 deletions src/PAModel/PAConvert/Yaml/YamlConverter.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,53 @@
using Microsoft.PowerPlatform.Formulas.Tools.Yaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Microsoft.PowerPlatform
namespace Microsoft.PowerPlatform.Formulas.Tools.Yaml;

/// <summary>
/// Convert a dictionary to and from yaml
/// </summary>
public static class YamlConverter
{
/// <summary>
/// Convert a dictionary to and from yaml
/// </summary>
public static class YamlConverter
public static Dictionary<string, string> Read(TextReader reader, string filenameHint = null)
{
public static Dictionary<string, string> Read(TextReader reader, string filenameHint = null)
{
var properties = new Dictionary<string, string>(StringComparer.Ordinal);
var properties = new Dictionary<string, string>(StringComparer.Ordinal);

var yaml = new YamlLexer(reader, filenameHint);
var yaml = new YamlLexer(reader, filenameHint);

while (true)
while (true)
{
var t = yaml.ReadNext();
if (t.Kind == YamlTokenKind.EndOfFile)
{
var t = yaml.ReadNext();
if (t.Kind == YamlTokenKind.EndOfFile)
{
break;
}

if (t.Kind != YamlTokenKind.Property)
{
// $$$ error
t = YamlToken.NewError(t.Span, "Only properties are supported in this context");

}
if (t.Kind == YamlTokenKind.Error)
{
// ToString will include source span and message.
throw new InvalidOperationException(t.ToString());
}

properties[t.Property] = t.Value;
break;
}
return properties;
}

public static void Write(TextWriter writer, IDictionary<string, string> properties)
{
var yaml = new YamlWriter(writer);
if (t.Kind != YamlTokenKind.Property)
{
// $$$ error
t = YamlToken.NewError(t.Span, "Only properties are supported in this context");

// Sort by keys to enforce canonical format.
foreach (var kv in properties.OrderBy(x => x.Key))
}
if (t.Kind == YamlTokenKind.Error)
{
yaml.WriteProperty(kv.Key, kv.Value);
// ToString will include source span and message.
throw new InvalidOperationException(t.ToString());
}
writer.Flush();

properties[t.Property] = t.Value;
}
return properties;
}

public static void Write(TextWriter writer, IDictionary<string, string> properties)
{
var yaml = new YamlWriter(writer);

// Sort by keys to enforce canonical format.
foreach (var kv in properties.OrderBy(x => x.Key))
{
yaml.WriteProperty(kv.Key, kv.Value);
}
writer.Flush();
}
}
68 changes: 33 additions & 35 deletions src/PAModelTests/PublicSurfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,49 @@

using Microsoft.PowerPlatform.Formulas.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PAModelTests
namespace PAModelTests;

[TestClass]
public class PublicSurfaceTests
{
[TestClass]
public class PublicSurfaceTests
[TestMethod]
public void Test()
{
[TestMethod]
public void Test()
{
var asm = typeof(CanvasDocument).Assembly;
var asm = typeof(CanvasDocument).Assembly;

var ns = "Microsoft.PowerPlatform.Formulas.Tools";
HashSet<string> allowed = new HashSet<string>()
{
$"{ns}.{nameof(CanvasDocument)}",
$"{ns}.{nameof(CanvasMerger)}",
$"{ns}.{nameof(ChecksumMaker)}",
$"{ns}.{nameof(ErrorContainer)}",
$"{ns}.{nameof(Error)}",
$"Microsoft.PowerPlatform.YamlConverter",
$"Microsoft.PowerPlatform.YamlPocoSerializer",
$"Microsoft.PowerPlatform.Formulas.Tools.Yaml.YamlWriter",
};

StringBuilder sb = new StringBuilder();
foreach (var type in asm.GetTypes().Where(t => t.IsPublic))
var ns = "Microsoft.PowerPlatform.Formulas.Tools";
var allowed = new HashSet<string>()
{
$"{ns}.{nameof(CanvasDocument)}",
$"{ns}.{nameof(CanvasMerger)}",
$"{ns}.{nameof(ChecksumMaker)}",
$"{ns}.{nameof(ErrorContainer)}",
$"{ns}.{nameof(Error)}",
$"{ns}.Yaml.YamlConverter",
$"{ns}.Yaml.YamlPocoSerializer",
$"{ns}.Yaml.YamlWriter",
};

var sb = new StringBuilder();
foreach (var type in asm.GetTypes().Where(t => t.IsPublic))
{
var name = type.FullName;
if (!allowed.Contains(name))
{
var name = type.FullName;
if (!allowed.Contains(name))
{
sb.Append(name);
sb.Append("; ");
}

allowed.Remove(name);
sb.Append(name);
sb.Append("; ");
}

Assert.AreEqual(0, sb.Length, $"Unexpected public types: {sb}");

// Types we expect to be in the assembly aren't there.
Assert.AreEqual(0, allowed.Count);
allowed.Remove(name);
}

Assert.AreEqual(0, sb.Length, $"Unexpected public types: {sb}");

// Types we expect to be in the assembly aren't there.
Assert.AreEqual(0, allowed.Count);
}
}

6 changes: 3 additions & 3 deletions src/PAModelTests/YamlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public void ReadDict()
{
var reader = new StringReader(expectedYaml);

var props = Microsoft.PowerPlatform.YamlConverter.Read(reader);
var props = YamlConverter.Read(reader);
Assert.AreEqual(props.Count, 2);
Assert.AreEqual(props["P1"], "123");
Assert.AreEqual(props["P2"], " \"hello\" & \"world\"");
Expand All @@ -475,7 +475,7 @@ public void ReadDictError()
sub1: 123"); // error, not supported objects

Assert.ThrowsException<InvalidOperationException>(
() => Microsoft.PowerPlatform.YamlConverter.Read(reader));
() => YamlConverter.Read(reader));
}

[TestMethod]
Expand All @@ -489,7 +489,7 @@ public void WriteDict()
{"P1", "123" }
};

Microsoft.PowerPlatform.YamlConverter.Write(writer, d);
YamlConverter.Write(writer, d);

// order should be alphabetical
Assert.AreEqual(expectedYaml, writer.ToString());
Expand Down

0 comments on commit dce741f

Please sign in to comment.