Reputation: 3845
Let's say I've got the following code in a (part of a) method:
ToolTip tip = new ToolTip();
Control ctrl = new Control();
tip.SetToolTip(ctrl, String.Empty);
I want to assert in a test that in a particular situation that tip
is associated with ctrl
. The following call does not suffice:
Assert.AreEqual(String.Empty, tip.GetToolTip(ctrl));
This call succeeds whether or not the above method-body was executed. I'm looking for something along the lines of this:
ToolTip t = GetTooltipForControl(ctrl);
Assert.IsNotNull(t);
GetTooltipForControl
should return null when no tooltip is associated with the control, and should return the ToolTip instance when it is. Can this be done?
Reminder: I personally only require this logic for unit testing / regressions testing in order to fix a bug in a TDD fashion.
Upvotes: 2
Views: 1546
Reputation: 1934
Could not find any way how to check if tooltip has been set, if had been set empty. A way how to do it could be to create custom control and use it instead of standard control. Like:
public class CustomLabel : Label
{
public bool isTooltipSet
{
get;
set;
}
public void SetToolTip()
{
ToolTip tt = new ToolTip();
tt.SetToolTip(this, string.Empty);
isTooltipSet = true;
}
}
You could then check it with:
CustomLabel lbl = new CustomLabel();
lbl.SetToolTip();
Assert.AreEqual(true, lbl.isToolTipSet);
Why do you need do set and check empty tooltip? maybe there is better way to do it. Maybe you could set some text, but set the Active
property of tooltip to false
ToolTip tt = new ToolTip();
tt.Active = false;
Control cc = new Control();
tt.SetToolTip(cc, "text");
Assert.AreEqual("text", tt.GetToolTip(cc));
EDIT:
One more idea that is not so super hacky and needs less refactoring. You could make extension method for setting tooltip, in which you would store the tooltip text into Control.Tag
property.
public static class Extensions
{
public static void SetToolTip(this Control ctrl, ToolTip tt, string text)
{
tt.SetToolTip(ctrl, text);
ctrl.Tag = text;
}
}
And then assert:
Assert.AreEqual(string.Empty, (string)ctrl.Tag);
(string)ctrl.Tag
will be null when extension method SetToolTip
was not called, and sting.Empty
when it was called with string.Empty
parameter
Upvotes: 1
Reputation: 3816
string.IsNullOrWhiteSpace
should check that properly:
Assert.IsTrue(!string.IsNullOrWhiteSpace(tip.GetToolTip(ctrl));
Upvotes: 0