Skip to content

Commit

Permalink
Added implicit conversion for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
vfrz committed Aug 8, 2023
1 parent d8fbe9c commit cc20113
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Sources/DotNetGraph.Tests/Attributes/DotColorAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ public async Task CompileFromColor()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"#FF0000\"");
}

[TestMethod]
public void ImplicitConversionFromColor()
{
DotColorAttribute attribute = Color.Red;
attribute.Value.Should().Be("#FF0000");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotColorAttribute attribute = "#FF0000";
attribute.Value.Should().Be("#FF0000");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ public async Task CompileWithSpecifiedFormat()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("123.456");
}

[TestMethod]
public void ImplicitConversionFromDouble()
{
DotDoubleAttribute attribute = 123.456d;
attribute.Value.Should().Be(123.456d);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ public async Task CompileFromEnum()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"box\"");
}

[TestMethod]
public void ImplicitConversionFromDotEdgeArrowType()
{
DotEdgeArrowTypeAttribute attribute = DotEdgeArrowType.Box;
attribute.Value.Should().Be("box");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotEdgeArrowTypeAttribute attribute = "box";
attribute.Value.Should().Be("box");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task CompileFromString()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"custom\"");
}

[TestMethod]
public async Task CompileFromEnum()
{
Expand All @@ -36,4 +36,18 @@ public async Task CompileFromEnum()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"solid\"");
}

[TestMethod]
public void ImplicitConversionFromDotEdgeStyle()
{
DotEdgeStyleAttribute attribute = DotEdgeStyle.Solid;
attribute.Value.Should().Be("solid");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotEdgeStyleAttribute attribute = "solid";
attribute.Value.Should().Be("solid");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ public async Task CompileHtml()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("<<b>Hello, world!</b>>");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotLabelAttribute attribute = "Hello, world!";

attribute.Value.Should().Be("Hello, world!");
attribute.IsHtml.Should().Be(false);
}
}
14 changes: 14 additions & 0 deletions Sources/DotNetGraph.Tests/Attributes/DotNodeShapeAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ public async Task CompileFromEnum()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"terminator\"");
}

[TestMethod]
public void ImplicitConversionFromDotNodeShape()
{
DotNodeShapeAttribute attribute = DotNodeShape.Terminator;
attribute.Value.Should().Be("terminator");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotNodeShapeAttribute attribute = "terminator";
attribute.Value.Should().Be("terminator");
}
}
14 changes: 14 additions & 0 deletions Sources/DotNetGraph.Tests/Attributes/DotNodeStyleAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ public async Task CompileFromEnum()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"bold\"");
}

[TestMethod]
public void ImplicitConversionFromDotNodeStyle()
{
DotNodeStyleAttribute attribute = DotNodeStyle.Bold;
attribute.Value.Should().Be("bold");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotNodeStyleAttribute attribute = "bold";
attribute.Value.Should().Be("bold");
}
}
14 changes: 14 additions & 0 deletions Sources/DotNetGraph.Tests/Attributes/DotPointAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ public async Task CompileFromDotPoint()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("42,69,75!");
}

[TestMethod]
public void ImplicitConversionFromDotPoint()
{
DotPointAttribute attribute = new DotPoint(42, 69, 75, true);
attribute.Value.Should().Be("42,69,75!");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotPointAttribute attribute = "42,69,75!";
attribute.Value.Should().Be("42,69,75!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ public async Task CompileFromEnum()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"rounded\"");
}

[TestMethod]
public void ImplicitConversionFromDotSubgraphStyle()
{
DotSubgraphStyleAttribute attribute = DotSubgraphStyle.Rounded;
attribute.Value.Should().Be("rounded");
}

[TestMethod]
public void ImplicitConversionFromString()
{
DotSubgraphStyleAttribute attribute = "rounded";
attribute.Value.Should().Be("rounded");
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotColorAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotColorAttribute(Color value) => new DotColorAttribute(value);
public static implicit operator DotColorAttribute(string value) => new DotColorAttribute(value);
}
}
2 changes: 2 additions & 0 deletions Sources/DotNetGraph/Attributes/DotDoubleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync(Value.ToString(Format, NumberFormatInfo.InvariantInfo));
}

public static implicit operator DotDoubleAttribute(double value) => new DotDoubleAttribute(value);
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotEdgeArrowTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotEdgeArrowTypeAttribute(DotEdgeArrowType value) => new DotEdgeArrowTypeAttribute(value);
public static implicit operator DotEdgeArrowTypeAttribute(string value) => new DotEdgeArrowTypeAttribute(value);
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotEdgeStyleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotEdgeStyleAttribute(DotEdgeStyle value) => new DotEdgeStyleAttribute(value);
public static implicit operator DotEdgeStyleAttribute(string value) => new DotEdgeStyleAttribute(value);
}
}
4 changes: 3 additions & 1 deletion Sources/DotNetGraph/Attributes/DotLabelAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DotLabelAttribute : IDotAttribute
{
public string Value { get; set; }

public bool IsHtml { get; set; } = false;
public bool IsHtml { get; set; }

public DotLabelAttribute(string value, bool isHtml = false)
{
Expand All @@ -27,5 +27,7 @@ public async Task CompileAsync(CompilationContext context)
var value = context.Options.AutomaticEscapedCharactersFormat ? Value.FormatGraphvizEscapedCharacters() : Value;
await context.TextWriter.WriteAsync($"\"{value}\"");
}

public static implicit operator DotLabelAttribute(string value) => new DotLabelAttribute(value);
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotNodeShapeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotNodeShapeAttribute(DotNodeShape value) => new DotNodeShapeAttribute(value);
public static implicit operator DotNodeShapeAttribute(string value) => new DotNodeShapeAttribute(value);
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotNodeStyleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotNodeStyleAttribute(DotNodeStyle value) => new DotNodeStyleAttribute(value);
public static implicit operator DotNodeStyleAttribute(string value) => new DotNodeStyleAttribute(value);
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotPointAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync(Value);
}

public static implicit operator DotPointAttribute(DotPoint value) => new DotPointAttribute(value);
public static implicit operator DotPointAttribute(string value) => new DotPointAttribute(value);
}
}
3 changes: 3 additions & 0 deletions Sources/DotNetGraph/Attributes/DotSubgraphStyleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public async Task CompileAsync(CompilationContext context)
{
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotSubgraphStyleAttribute(DotSubgraphStyle value) => new DotSubgraphStyleAttribute(value);
public static implicit operator DotSubgraphStyleAttribute(string value) => new DotSubgraphStyleAttribute(value);
}
}
2 changes: 1 addition & 1 deletion Sources/DotNetGraph/Core/DotElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public bool RemoveAttribute(string name)
return Attributes.Remove(name);
}

public async Task CompileAttributesAsync(CompilationContext context)
protected async Task CompileAttributesAsync(CompilationContext context)
{
foreach (var attributePair in Attributes)
{
Expand Down

0 comments on commit cc20113

Please sign in to comment.