Tests: Add tests:

* Control
* Button
* HiddenField
* HtmlForm
* HyperLink
* Label
* LiteralControl
* Panel
* TextBox
* CTextBox
This commit is contained in:
2023-05-31 02:22:50 +02:00
parent 96601f62e5
commit 7ce3a00218
13 changed files with 647 additions and 4 deletions

View File

@@ -0,0 +1,69 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class ButtonTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
Button button = new();
page.Controls.Add(button);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<input type=""submit"" class=""button"" value=""""></input>", result);
}
[Fact]
public void MustRenderCorrectly__WithOnClientClick()
{
FakeWebContext fakeWebContext = new();
Page page = new();
Button button = new()
{
OnClientClick = "alert(1)",
};
page.Controls.Add(button);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<input type=""submit"" class=""button"" value="""" onclick=""alert(1)""></input>", result);
}
[Fact]
public void MustRenderCorrectly__ClickWithCommandArgument()
{
string commandArgument = "Test";
FakeWebContext fakeWebContext = new(requestMethod: "POST");
Page page = new();
Button button = new()
{
CommandArgument = commandArgument,
};
string? result = null;
button.Click += (o, _) =>
{
result = (o as Button)?.CommandArgument;
};
page.Controls.Add(button);
fakeWebContext.RequestForm.Add(button.ClientID, "Clicked");
page.ProcessRequest(fakeWebContext);
Assert.Equal(commandArgument, result);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
}
}

View File

@@ -0,0 +1,154 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class CTextBoxTests
{
#region MustRenderCorrectly
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
CTextBox cTextBox = new();
page.Controls.Add(cTextBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="text" id="ctl00_ctl00" name="ctl00_ctl00" class="textBox" onchange="ElementRemoveClass(this, &#39;textBoxInvalid&#39;);"></input>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__WithMostProperties()
{
FakeWebContext fakeWebContext = new();
Page page = new();
CTextBox cTextBox = new()
{
CssClassExtra = "extraClass",
PlaceHolder = "Placeholder",
MarkedInvalid = true,
TextMode = TextBoxMode.Normal,
AllowEmpty = true,
KeepSize = true,
Text = "Test",
};
page.Controls.Add(cTextBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="text" id="ctl00_ctl00" name="ctl00_ctl00" class="textBox extraClass textBoxInvalid" onchange="ElementRemoveClass(this, &#39;textBoxInvalid&#39;);" placeholder="Placeholder" value="Test"></input>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__WithNextFocusOnEnter()
{
FakeWebContext fakeWebContext = new();
Page page = new();
CTextBox cTextBox = new();
page.Controls.Add(cTextBox);
CTextBox cTextBox2 = new();
page.Controls.Add(cTextBox2);
cTextBox.NextFocusOnEnter = cTextBox2;
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="text" id="ctl00_ctl00" name="ctl00_ctl00" class="textBox" onchange="ElementRemoveClass(this, &#39;textBoxInvalid&#39;);" onkeydown="if(event.keyCode==13){document.getElementById(&#39;ctl01&#39;).focus(); return false;}"></input><input type="text" id="ctl01_ctl00" name="ctl01_ctl00" class="textBox" onchange="ElementRemoveClass(this, &#39;textBoxInvalid&#39;);"></input>
""",
actual: result);
}
#endregion MustRenderCorrectly
#region IsEmpty
[Fact]
public void IsEmpty__Empty__True()
{
CTextBox cTextBox = new() { Text = string.Empty, };
bool result = cTextBox.IsEmpty();
Assert.True(result);
}
[Fact]
public void IsEmpty__Text__False()
{
CTextBox cTextBox = new() { Text = "Text", };
bool result = cTextBox.IsEmpty();
Assert.False(result);
}
#endregion IsEmpty
#region IsValid
[Fact]
public void IsEmpty__EmptyAllowEmptyFalse__False()
{
CTextBox cTextBox = new() { Text = string.Empty, AllowEmpty = false, };
bool result = cTextBox.IsValid();
Assert.False(result);
}
[Fact]
public void IsEmpty__TextAllowEmptyFalse__True()
{
CTextBox cTextBox = new() { Text = "Text", AllowEmpty = false, };
bool result = cTextBox.IsValid();
Assert.True(result);
}
[Fact]
public void IsEmpty__EmptyAllowEmptyTrue__True()
{
CTextBox cTextBox = new() { Text = string.Empty, AllowEmpty = true, };
bool result = cTextBox.IsValid();
Assert.True(result);
}
[Fact]
public void IsEmpty__TextAllowEmptyTrue__True()
{
CTextBox cTextBox = new() { Text = "Text", AllowEmpty = true, };
bool result = cTextBox.IsValid();
Assert.True(result);
}
#endregion IsEmpty
}

View File

@@ -0,0 +1,25 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class ControlTests
{
[Fact]
public void MustRenderCorrectly__Empty()
{
FakeWebContext fakeWebContext = new();
Page page = new();
Control control = new();
page.Controls.Add(control);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("", result);
}
}

View File

@@ -0,0 +1,63 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class HiddenFieldTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HiddenField hiddenField = new();
page.Controls.Add(hiddenField);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<input type=""hidden"" id=""ctl00"" name=""ctl00""></input>", result);
}
[Fact]
public void MustRenderCorrectly__WithValue()
{
string value = "Test";
FakeWebContext fakeWebContext = new();
Page page = new();
HiddenField hiddenField = new() { Value = value };
page.Controls.Add(hiddenField);
page.ProcessRequest(fakeWebContext);
Assert.Equal(value, hiddenField.Value);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<input type=""hidden"" id=""ctl00"" name=""ctl00"" value=""Test""></input>", result);
}
[Fact]
public void MustRenderCorrectly__WithChangedValue()
{
string value = "Test";
string changedValue = "Changed";
FakeWebContext fakeWebContext = new(requestMethod: "POST");
Page page = new();
HiddenField hiddenField = new() { Value = value };
page.Controls.Add(hiddenField);
fakeWebContext.RequestForm.Add(hiddenField.ClientID, changedValue);
page.ProcessRequest(fakeWebContext);
Assert.Equal(changedValue, hiddenField.Value);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<input type=""hidden"" id=""ctl00"" name=""ctl00"" value=""Changed""></input>", result);
}
}

View File

@@ -0,0 +1,42 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class HtmlFormTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HtmlForm htmlForm = new();
page.Controls.Add(htmlForm);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<form method=""post"" action=""Page""></form>", result);
}
[Fact]
public void MustRenderCorrectly__WithQueryParameters()
{
FakeWebContext fakeWebContext = new();
fakeWebContext.RequestQuery.Add("test", "value");
Page page = new();
HtmlForm htmlForm = new();
page.Controls.Add(htmlForm);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<form method=""post"" action=""Page?&amp;test=value""></form>", result);
}
}

View File

@@ -24,7 +24,7 @@ public class HtmlHeadTests
}
[Fact]
public void MustRenderCorrectlyWithTitle()
public void MustRenderCorrectly__WithTitle()
{
FakeWebContext fakeWebContext = new();
Page page = new();
@@ -41,7 +41,7 @@ public class HtmlHeadTests
}
[Fact]
public void MustRenderCorrectlyWithMeta()
public void MustRenderCorrectly__WithMeta()
{
FakeWebContext fakeWebContext = new();
Page page = new();

View File

@@ -0,0 +1,45 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class HyperLinkTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HyperLink hyperLink = new();
page.Controls.Add(hyperLink);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<a ></a>", result);
}
[Fact]
public void MustRenderCorrectly__WithTextAndUrl()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HyperLink hyperLink = new()
{
NavigateUrl = "http://example.com",
Text = "Example.com"
};
page.Controls.Add(hyperLink);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"<a href=""http://example.com"">Example.com</a>", result);
}
}

View File

@@ -0,0 +1,25 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class LabelTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
Label label = new();
page.Controls.Add(label);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<span ></span>", result);
}
}

View File

@@ -0,0 +1,42 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class LiteralControlTests
{
[Fact]
public void MustRenderCorrectly__Empty()
{
FakeWebContext fakeWebContext = new();
Page page = new();
LiteralControl literalControl = new();
page.Controls.Add(literalControl);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(@"", result);
}
[Fact]
public void MustRenderCorrectly__AnyContent()
{
string anyContent = "AnyContent<script>alert(1)</script>";
FakeWebContext fakeWebContext = new();
Page page = new();
LiteralControl literalControl = new(anyContent);
page.Controls.Add(literalControl);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(anyContent, result);
}
}

View File

@@ -0,0 +1,25 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class PanelTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
Panel panel = new();
page.Controls.Add(panel);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<div ></div>", result);
}
}

View File

@@ -0,0 +1,154 @@
using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages;
using VAR.WebFormsCore.Tests.Fakes;
using Xunit;
namespace VAR.WebFormsCore.Tests.Controls;
public class TextBoxTests
{
[Fact]
public void MustRenderCorrectly__Normal()
{
FakeWebContext fakeWebContext = new();
Page page = new();
TextBox textBox = new();
page.Controls.Add(textBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="text" id="ctl00" name="ctl00"></input>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__NormalWithText()
{
FakeWebContext fakeWebContext = new();
Page page = new();
TextBox textBox = new() { Text = "Text", };
page.Controls.Add(textBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="text" id="ctl00" name="ctl00" value="Text"></input>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__Password()
{
FakeWebContext fakeWebContext = new();
Page page = new();
TextBox textBox = new() {TextMode = TextBoxMode.Password, };
page.Controls.Add(textBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="password" id="ctl00" name="ctl00"></input>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__PasswordWithText()
{
FakeWebContext fakeWebContext = new();
Page page = new();
TextBox textBox = new() { TextMode = TextBoxMode.Password, Text = "Password", };
page.Controls.Add(textBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="password" id="ctl00" name="ctl00" value="Password"></input>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__MultiLine()
{
FakeWebContext fakeWebContext = new();
Page page = new();
TextBox textBox = new() {TextMode = TextBoxMode.MultiLine, };
page.Controls.Add(textBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<textarea id="ctl00" name="ctl00"></textarea>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__MultiLineWithText()
{
FakeWebContext fakeWebContext = new();
Page page = new();
TextBox textBox = new() { TextMode = TextBoxMode.MultiLine, Text = "Multi\nLine", };
page.Controls.Add(textBox);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<textarea id="ctl00" name="ctl00">Multi
Line</textarea>
""",
actual: result);
}
[Fact]
public void MustRenderCorrectly__WithChangedText()
{
string text = "Test";
string changedValue = "Changed";
FakeWebContext fakeWebContext = new(requestMethod: "POST");
Page page = new();
TextBox textBox = new() { Text = text };
page.Controls.Add(textBox);
fakeWebContext.RequestForm.Add(textBox.ClientID, changedValue);
page.ProcessRequest(fakeWebContext);
Assert.Equal(changedValue, textBox.Text);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<input type="text" id="ctl00" name="ctl00" value="Changed"></input>
""",
actual: result);
}
}