Skip to content

Commit f420b1f

Browse files
committed
#334: port simple html tests
1 parent 8ec8c3c commit f420b1f

3 files changed

Lines changed: 150 additions & 0 deletions

File tree

tests/Plotly.NET.CSharp.Tests/Plotly.NET.CSharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5+
<LangVersion>11.0</LangVersion>
56
<IsPackable>false</IsPackable>
67
</PropertyGroup>
78

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DynamicObj;
7+
using Newtonsoft.Json;
8+
using System.Reflection;
9+
using System.IO;
10+
using Xunit;
11+
12+
namespace CSharp.Tests
13+
{
14+
internal class TestUtils
15+
{
16+
static string GetFullPlotlyJS()
17+
{
18+
Assembly assembly = Assembly.GetExecutingAssembly();
19+
using Stream str = assembly.GetManifestResourceStream("Plotly.NET.Tests.plotly-2.18.1.min.js");
20+
using StreamReader r = new StreamReader(str);
21+
return r.ReadToEnd();
22+
}
23+
24+
//A method that takes a Generic chart as input, transforms it with a delegate called 'htmlizer' into a string, for which it then should be tested wether another string is contained.
25+
internal static void SubstringIsInChart(Plotly.NET.GenericChart.GenericChart chart, Func<Plotly.NET.GenericChart.GenericChart, string> htmlizer, string expected)
26+
{
27+
string actual = htmlizer(chart);
28+
Assert.Contains(expected, actual);
29+
}
30+
31+
32+
internal static void ChartGeneratedContains(Plotly.NET.GenericChart.GenericChart chart, string expected)
33+
{
34+
SubstringIsInChart(chart, Plotly.NET.GenericChart.toChartHTML, expected);
35+
SubstringIsInChart(chart, Plotly.NET.GenericChart.toEmbeddedHTML, expected);
36+
}
37+
38+
//C# version of the following F# code:
39+
//let substringListIsInChart chart htmlizer substringList =
40+
// for substring in substringList do
41+
// substringIsInChart chart htmlizer substring
42+
internal static void SubstringListIsInChart(Plotly.NET.GenericChart.GenericChart chart, Func<Plotly.NET.GenericChart.GenericChart, string> htmlizer, string[] substringList)
43+
{
44+
foreach (string substring in substringList)
45+
{
46+
SubstringIsInChart(chart, htmlizer, substring);
47+
}
48+
}
49+
50+
//C# verison of the following F# code:
51+
//let chartGeneratedContainsList chart substringList =
52+
//for substring in substringList do
53+
// chartGeneratedContains chart substring
54+
internal static void ChartGeneratedContainsList(Plotly.NET.GenericChart.GenericChart chart, string[] substringList)
55+
{
56+
foreach (string substring in substringList)
57+
{
58+
ChartGeneratedContains(chart, substring);
59+
}
60+
}
61+
62+
}
63+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using Xunit.Sdk;
8+
using Plotly.NET.CSharp;
9+
10+
// avoid the namespace 'Plotly.NET', as we dont want to accidentally use F# type extensions instead of real C# bindings
11+
namespace CSharp.Tests.HtmlCodegen
12+
{
13+
public class SimpleTests
14+
{
15+
static Plotly.NET.GenericChart.GenericChart simpleChart =
16+
Chart.Point<double, double, string>(
17+
x: Enumerable.Range(0, 11).Select(x => Convert.ToDouble(x)).ToArray(),
18+
y: Enumerable.Range(0, 11).Select(x => Convert.ToDouble(x)).ToArray(),
19+
UseDefaults: false
20+
)
21+
.WithXAxisStyle<int, int, int>(TitleText: "xAxis")
22+
.WithYAxisStyle<int, int, int>(TitleText: "yAxis");
23+
24+
public class HtmlLayoutTests
25+
{
26+
[Fact]
27+
public void PlotlyJsScriptInGeneratedHTMLDocument()
28+
{
29+
TestUtils.SubstringIsInChart(
30+
simpleChart,
31+
Plotly.NET.GenericChart.toEmbeddedHTML,
32+
$"https://cdn.plot.ly/plotly-{Globals.PLOTLYJS_VERSION}.min"
33+
);
34+
}
35+
36+
[Fact]
37+
public void ExpectingData()
38+
{
39+
TestUtils.ChartGeneratedContains(
40+
simpleChart,
41+
"""var data = [{"type":"scatter","mode":"markers","x":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0],"y":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0],"marker":{},"line":{}}];"""
42+
);
43+
}
44+
45+
[Fact]
46+
public void ExpectingLayoutInfo()
47+
{
48+
TestUtils.ChartGeneratedContains(
49+
simpleChart,
50+
"""var layout = {"xaxis":{"title":{"text":"xAxis"}},"yaxis":{"title":{"text":"yAxis"}}};"""
51+
);
52+
}
53+
54+
[Fact]
55+
public void ExpectingHTMLTagsInEmbeddedPageOnly()
56+
{
57+
string[] tags = new string[] { "<html>", "</html>", "<head>", "</head>", "<body>", "</body>", "<script type=\"text/javascript\">", "</script>" };
58+
TestUtils.SubstringListIsInChart(
59+
simpleChart,
60+
Plotly.NET.GenericChart.toEmbeddedHTML,
61+
tags
62+
);
63+
}
64+
65+
[Fact]
66+
public void ExpectingSomeHTMLTagsInBothEmbeddedAndNotEmbedded()
67+
{
68+
string[] tags = new string[] { "<script type=\"text/javascript\">", "</script>" };
69+
TestUtils.ChartGeneratedContainsList(
70+
simpleChart,
71+
tags
72+
);
73+
}
74+
75+
[Fact]
76+
public void PassingArgsToTheFunction()
77+
{
78+
TestUtils.ChartGeneratedContains(
79+
simpleChart,
80+
"data, layout, config);"
81+
);
82+
}
83+
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)