I've read about unit testing and heard a lot of hullabaloo by others touting its usefulness, and would like to see it in action. As such, I've selected this basic class from a simple application that I created. I have no idea how testing would help me, and am hoping one of you will be able to help me see the benefit of it by pointing out what parts of this code can be tested, and what those tests might look like. So, how would I write unit tests for the following code?
public class Hole : INotifyPropertyChanged
{
#region Field Definitions
private double _AbsX;
private double _AbsY;
private double _CanvasX { get; set; }
private double _CanvasY { get; set; }
private bool _Visible;
private double _HoleDia = 20;
private HoleTypes _HoleType;
private int _HoleNumber;
private double _StrokeThickness = 1;
private Brush _StrokeColor = new SolidColorBrush(Colors.Black);
private HolePattern _ParentPattern;
#endregion
public enum HoleTypes { Drilled, Tapped, CounterBored, CounterSunk };
public Ellipse HoleEntity = new Ellipse();
public Ellipse HoleDecorator = new Ellipse();
public TextBlock HoleLabel = new TextBlock();
private static DoubleCollection HiddenLinePattern =
new DoubleCollection(new double[] { 5, 5 });
public int HoleNumber
{
get
{
return _HoleNumber;
}
set
{
_HoleNumber = value;
HoleLabel.Text = value.ToString();
NotifyPropertyChanged("HoleNumber");
}
}
public double HoleLabelX { get; set; }
public double HoleLabelY { get; set; }
public string AbsXDisplay { get; set; }
public string AbsYDisplay { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
//public event MouseEventHandler MouseActivity;
// Constructor
public Hole()
{
//_HoleDia = 20.0;
_Visible = true;
//this.ParentPattern = WhoIsTheParent;
HoleEntity.Tag = this;
HoleEntity.Width = _HoleDia;
HoleEntity.Height = _HoleDia;
HoleDecorator.Tag = this;
HoleDecorator.Width = 0;
HoleDecorator.Height = 0;
//HoleLabel.Text = x.ToString();
HoleLabel.TextAlignment = TextAlignment.Center;
HoleLabel.Foreground = new SolidColorBrush(Colors.White);
HoleLabel.FontSize = 12;
this.StrokeThickness = _StrokeThickness;
this.StrokeColor = _StrokeColor;
//HoleEntity.Stroke = Brushes.Black;
//HoleDecorator.Stroke = HoleEntity.Stroke;
//HoleDecorator.StrokeThickness = HoleEntity.StrokeThickness;
//HiddenLinePattern=DoubleCollection(new double[]{5, 5});
}
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(info));
}
}
#region Properties
public HolePattern ParentPattern
{
get
{
return _ParentPattern;
}
set
{
_ParentPattern = value;
}
}
public bool Visible
{
get { return _Visible; }
set
{
_Visible = value;
HoleEntity.Visibility = value ?
Visibility.Visible :
Visibility.Collapsed;
HoleDecorator.Visibility = HoleEntity.Visibility;
SetCoordDisplayValues();
NotifyPropertyChanged("Visible");
}
}
public double AbsX
{
get { return _AbsX; }
set
{
_AbsX = value;
SetCoordDisplayValues();
NotifyPropertyChanged("AbsX");
}
}
public double AbsY
{
get { return _AbsY; }
set
{
_AbsY = value;
SetCoordDisplayValues();
NotifyPropertyChanged("AbsY");
}
}
private void SetCoordDisplayValues()
{
AbsXDisplay = HoleEntity.Visibility ==
Visibility.Visible ? String.Format("{0:f4}", _AbsX) : "";
AbsYDisplay = HoleEntity.Visibility ==
Visibility.Visible ? String.Format("{0:f4}", _AbsY) : "";
NotifyPropertyChanged("AbsXDisplay");
NotifyPropertyChanged("AbsYDisplay");
}
public double CanvasX
{
get { return _CanvasX; }
set
{
if (value == _CanvasX) { return; }
_CanvasX = value;
UpdateEntities();
NotifyPropertyChanged("CanvasX");
}
}
public double CanvasY
{
get { return _CanvasY; }
set
{
if (value == _CanvasY) { return; }
_CanvasY = value;
UpdateEntities();
NotifyPropertyChanged("CanvasY");
}
}
public HoleTypes HoleType
{
get { return _HoleType; }
set
{
if (value != _HoleType)
{
_HoleType = value;
UpdateHoleType();
NotifyPropertyChanged("HoleType");
}
}
}
public double HoleDia
{
get { return _HoleDia; }
set
{
if (value != _HoleDia)
{
_HoleDia = value;
HoleEntity.Width = value;
HoleEntity.Height = value;
UpdateHoleType();
NotifyPropertyChanged("HoleDia");
}
}
}
public double StrokeThickness
{
get { return _StrokeThickness; }
//Setting this StrokeThickness will also set Decorator
set
{
_StrokeThickness = value;
this.HoleEntity.StrokeThickness = value;
this.HoleDecorator.StrokeThickness = value;
NotifyPropertyChanged("StrokeThickness");
}
}
public Brush StrokeColor
{
get { return _StrokeColor; }
//Setting this StrokeThickness will also set Decorator
set
{
_StrokeColor = value;
this.HoleEntity.Stroke = value;
this.HoleDecorator.Stroke = value;
NotifyPropertyChanged("StrokeColor");
}
}
#endregion
#region Methods
private void UpdateEntities()
{
//-- Update Margins for graph positioning
HoleEntity.Margin = new Thickness
(CanvasX - HoleDia / 2, CanvasY - HoleDia / 2, 0, 0);
HoleDecorator.Margin = new Thickness
(CanvasX - HoleDecorator.Width / 2,
CanvasY - HoleDecorator.Width / 2, 0, 0);
HoleLabel.Margin = new Thickness
((CanvasX * 1.0) - HoleLabel.FontSize * .3,
(CanvasY * 1.0) - HoleLabel.FontSize * .6, 0, 0);
}
private void UpdateHoleType()
{
switch (this.HoleType)
{
case HoleTypes.Drilled: //Drilled only
HoleDecorator.Visibility = Visibility.Collapsed;
break;
case HoleTypes.Tapped: // Drilled & Tapped
HoleDecorator.Visibility = (this.Visible == true) ?
Visibility.Visible : Visibility.Collapsed;
HoleDecorator.Width = HoleEntity.Width * 1.2;
HoleDecorator.Height = HoleDecorator.Width;
HoleDecorator.StrokeDashArray =
LinePatterns.HiddenLinePattern(1);
break;
case HoleTypes.CounterBored: // Drilled & CounterBored
HoleDecorator.Visibility = (this.Visible == true) ?
Visibility.Visible : Visibility.Collapsed;
HoleDecorator.Width = HoleEntity.Width * 1.5;
HoleDecorator.Height = HoleDecorator.Width;
HoleDecorator.StrokeDashArray = null;
break;
case HoleTypes.CounterSunk: // Drilled & CounterSunk
HoleDecorator.Visibility = (this.Visible == true) ?
Visibility.Visible : Visibility.Collapsed;
HoleDecorator.Width = HoleEntity.Width * 1.8;
HoleDecorator.Height = HoleDecorator.Width;
HoleDecorator.StrokeDashArray = null;
break;
}
UpdateEntities();
}
#endregion
}
The test consists of completing texts in which alternate words have missing letters. To complete the words, you need to add the same number of letters as those given or one more. Example: I us__ to li__ in t____ city, b__ I dec___ moving he__ would b__ a lit___ more rela_____.
Unit Test Example:
Usually this is done with a testing framework such as NUnit.
(Kind of funny cause you will notice that the ParentPattern property doesn't fire the event.)
You can not properly test this code unless the specification is also given. "Testing" generally means making sure software works as designed.
EDIT: This is really not a "cop out" answer. I've worked as a tester before and I can tell you that almost all of the test cases I wrote were derived straight from the software spec.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With