Tests: Add tests to HtmlGenericControl, HtmlBody and HtmlHead.

This commit is contained in:
2023-05-30 03:05:42 +02:00
parent 61fce0efb7
commit 96601f62e5
7 changed files with 144 additions and 84 deletions

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 HtmlBodyTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HtmlBody htmlBody = new();
page.Controls.Add(htmlBody);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<body ></body>", 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 HtmlGenericControlTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HtmlGenericControl htmlGenericControl = new("test");
page.Controls.Add(htmlGenericControl);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<test ></test>", result);
}
}

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 HtmlHeadTests
{
[Fact]
public void MustRenderCorrectly()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HtmlHead htmlHead = new();
page.Controls.Add(htmlHead);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<head ></head>", result);
}
[Fact]
public void MustRenderCorrectlyWithTitle()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HtmlHead htmlHead = new();
htmlHead.Title = "Test";
page.Controls.Add(htmlHead);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal("<head ><title>Test</title></head>", result);
}
[Fact]
public void MustRenderCorrectlyWithMeta()
{
FakeWebContext fakeWebContext = new();
Page page = new();
HtmlHead htmlHead = new();
page.Controls.Add(htmlHead);
HtmlMeta htmlMeta = new()
{
Name = "TestMeta",
Content = "TestMetaContent",
HttpEquiv = "TestMetaHttpEquiv"
};
htmlHead.Controls.Add(htmlMeta);
page.ProcessRequest(fakeWebContext);
Assert.Equal(200, fakeWebContext.ResponseStatusCode);
Assert.Equal("text/html", fakeWebContext.ResponseContentType);
string result = fakeWebContext.FakeWritePackages.ToString("");
Assert.Equal(
expected: """
<head ><meta name="TestMeta" content="TestMetaContent" http-equiv="TestMetaHttpEquiv" /></head>
""",
actual: result);
}
}

View File

@@ -7,13 +7,7 @@ public class Button : Control, IReceivePostbackEvent
{
public Button() { CssClass = "button"; }
private string _text = string.Empty;
public string Text
{
get => _text;
set => _text = value;
}
public string Text { get; set; } = string.Empty;
public string OnClientClick { get; set; } = string.Empty;
@@ -25,7 +19,7 @@ public class Button : Control, IReceivePostbackEvent
{
textWriter.Write("<input type=\"submit\" ");
RenderAttributes(textWriter);
RenderAttribute(textWriter, "value", _text);
RenderAttribute(textWriter, "value", Text);
if (string.IsNullOrEmpty(OnClientClick) == false)
{
RenderAttribute(textWriter, "onclick", OnClientClick);

View File

@@ -14,57 +14,22 @@ public class CTextBox : Control, INamingContainer, IValidableControl
private HiddenField? _hidSize;
private const string CssClassBase = "textBox";
private string _cssClassExtra = "";
private bool _allowEmpty = true;
private string _placeHolder = string.Empty;
private bool _markedInvalid;
private Control? _nextFocusOnEnter;
private bool _keepSize;
#endregion Declarations
#region Properties
public string CssClassExtra
{
get => _cssClassExtra;
set => _cssClassExtra = value;
}
public string CssClassExtra { get; set; } = string.Empty;
public bool AllowEmpty
{
get => _allowEmpty;
set => _allowEmpty = value;
}
public bool AllowEmpty { get; set; } = true;
public string PlaceHolder
{
get => _placeHolder;
set => _placeHolder = value;
}
public string PlaceHolder { get; set; } = string.Empty;
public bool MarkedInvalid
{
get => _markedInvalid;
set => _markedInvalid = value;
}
public bool MarkedInvalid { get; set; }
public Control? NextFocusOnEnter
{
get => _nextFocusOnEnter;
set => _nextFocusOnEnter = value;
}
public Control? NextFocusOnEnter { get; set; }
public bool KeepSize
{
get => _keepSize;
set => _keepSize = value;
}
public bool KeepSize { get; set; }
public string Text
{
@@ -94,7 +59,7 @@ public class CTextBox : Control, INamingContainer, IValidableControl
if (TextMode == TextBoxMode.MultiLine)
{
if (_keepSize)
if (KeepSize)
{
_hidSize = new HiddenField();
Controls.Add(_hidSize);
@@ -103,7 +68,7 @@ public class CTextBox : Control, INamingContainer, IValidableControl
string strCfgName = $"{ClientID}_cfg";
Dictionary<string, object> cfg = new()
{
{"txtContent", _txtContent.ClientID}, {"hidSize", _hidSize?.ClientID ?? string.Empty}, {"keepSize", _keepSize},
{"txtContent", _txtContent.ClientID}, {"hidSize", _hidSize?.ClientID ?? string.Empty}, {"keepSize", KeepSize},
};
StringBuilder sbCfg = new StringBuilder();
sbCfg.AppendFormat("<script>\n");
@@ -118,28 +83,28 @@ public class CTextBox : Control, INamingContainer, IValidableControl
private void CTextBox_PreRender(object? sender, EventArgs e)
{
_txtContent.CssClass = CssClassBase;
if (string.IsNullOrEmpty(_cssClassExtra) == false)
if (string.IsNullOrEmpty(CssClassExtra) == false)
{
_txtContent.CssClass = $"{CssClassBase} {_cssClassExtra}";
_txtContent.CssClass = $"{CssClassBase} {CssClassExtra}";
}
if (Page?.IsPostBack == true && (_allowEmpty == false && IsEmpty()) || _markedInvalid)
if (Page?.IsPostBack == true && (AllowEmpty == false && IsEmpty()) || MarkedInvalid)
{
_txtContent.CssClass += " textBoxInvalid";
}
_txtContent.Attributes.Add("onchange", "ElementRemoveClass(this, 'textBoxInvalid');");
if (string.IsNullOrEmpty(_placeHolder) == false)
if (string.IsNullOrEmpty(PlaceHolder) == false)
{
_txtContent.Attributes.Add("placeholder", _placeHolder);
_txtContent.Attributes.Add("placeholder", PlaceHolder);
}
if (_nextFocusOnEnter != null)
if (NextFocusOnEnter != null)
{
_txtContent.Attributes.Add(
"onkeydown",
$"if(event.keyCode==13){{document.getElementById('{_nextFocusOnEnter.ClientID}').focus(); return false;}}"
$"if(event.keyCode==13){{document.getElementById('{NextFocusOnEnter.ClientID}').focus(); return false;}}"
);
}
}
@@ -150,7 +115,7 @@ public class CTextBox : Control, INamingContainer, IValidableControl
private bool IsEmpty() { return string.IsNullOrEmpty(_txtContent.Text); }
public bool IsValid() { return _allowEmpty || (string.IsNullOrEmpty(_txtContent.Text) == false); }
public bool IsValid() { return AllowEmpty || (string.IsNullOrEmpty(_txtContent.Text) == false); }
public int? GetClientsideHeight()
{

View File

@@ -4,13 +4,7 @@ namespace VAR.WebFormsCore.Controls;
public class HiddenField : Control
{
private string _value = string.Empty;
public string Value
{
get => _value;
set => _value = value;
}
public string Value { get; set; } = string.Empty;
protected override void Process()
{
@@ -24,7 +18,7 @@ public class HiddenField : Control
{
textWriter.Write("<input type=\"hidden\" ");
RenderAttributes(textWriter, forceId: true);
if (string.IsNullOrEmpty(Value) == false) { RenderAttribute(textWriter, "value", _value); }
if (string.IsNullOrEmpty(Value) == false) { RenderAttribute(textWriter, "value", Value); }
textWriter.Write(">");
textWriter.Write("</input>");

View File

@@ -7,21 +7,9 @@ public class Label : Control
{
#region Properties
private string _tagName = "span";
public string Tag { get; set; } = "span";
public string Tag
{
get => _tagName;
set => _tagName = value;
}
private string _text = string.Empty;
public string Text
{
get => _text;
set => _text = value;
}
public string Text { get; set; } = string.Empty;
#endregion Properties
@@ -29,15 +17,15 @@ public class Label : Control
protected override void Render(TextWriter textWriter)
{
textWriter.Write("<{0} ", _tagName);
textWriter.Write("<{0} ", Tag);
RenderAttributes(textWriter);
textWriter.Write(">");
textWriter.Write(ServerHelpers.HtmlEncode(_text));
textWriter.Write(ServerHelpers.HtmlEncode(Text));
base.Render(textWriter);
textWriter.Write("</{0}>", _tagName);
textWriter.Write("</{0}>", Tag);
}
#endregion Life cycle