2007-08-31

Say 'IoC' - imply 'testing', say 'testing' - imply...

I'm curious, what if instead of standard {Before, After}[Class] use IoC like declarations for setting a test environment. It's not necessary to use IoC in basic project.. but if an architecture is created with taking in the account the possibility of automate testing then it's most likely that the whole test environment creation can be easy implemented by means of IoC container.

2007-08-29

What the fun??

I do not understand.

Why in NUnit and in new Junt instead of simplest agreements of tests naming attributes and annotations are used. All the same, it's very difficult to give more suitable names to SetUp, TearDown and TestSomething methods.

It's hard to agree with an opinion that it's more clear in such a way
[SetUp]
public void SetUp() {}


Class attribute instead of the inheritance from TestCase is great. Now the free place of ancestor a class being tested can get. This is for a case of protected stuff is desired to be tested.

Yes, if in a base class suddenly there are methods which names start with Test and which are not tests themselves then indeed needed things can be selected only with explicit attributes. But this is such a rare case...

P.S. Many other things related to testing I consider are better to implement with attributed.

2007-08-24

Not a day without a bug - ContextMenuStrip appears after second click

A hot flood happens on rsdn - Property in С# - is it bad or good.

Here is a bug to piggy bank of opponents of syntactic sugar:

If in ContextMenuStrip initially there are no elements and during handling event Opening add an element but do not set e.Cancel = false then on the first click menu will not appear.

And this is in spite of e.Cancel is set to false by default. Exactly this stupid row in the sample from Microsoft is tend to be ignored by many people.

P.S. Yesterday there was a bug also, but it was in Spring.Net 1.1 M2. The version number and open source allows to close one's eyes to it...

Update 29.08.2007 By hook or by crook: A small sample how to by means of a couple of strings of code and a small helper class to test properties behavior.

2007-08-21

Not a day without a bug

One more evident blunder. In DataGridView there is Russian text, I select everything, copy and paste to в Excel - instead of the text I see hieroglyphs.

The problem again is quite famous. It's amazing that a correct solution "out of the box" has not been googled at once, just common advices - "look here, look there".

Though indeed just coding of a text, putting into the clipboard should be indicated right.

private void dataGridView_FixCopyEncoding_OnKeyUp(object sender, KeyEventArgs e)
{

DataGridView dataGrid = sender as DataGridView;
if (dataGrid != null && (e.KeyCode == Keys.Insert
|| (e.Control && e.KeyCode == Keys.C)))
{
if (dataGrid.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
string content = dataGrid.GetClipboardContent().GetText();
Clipboard.SetText(content, TextDataFormat.UnicodeText);
}
}
}

If somebody wishes to make a little bit more correct of course it is better to inherit and redefine DataGridView.GetClipboardContent().


I was very surprised when saw that on RightCtrl-Insert press two messages come instead of one. The first is e.Control == false && e.KeyCode = LButton | ShiftKey and the second is e.Control == false && e.KeyCode = Insert.

2007-08-20

Right click on treeview

I've triggered a backflash and surprised. Either I do not understand something or it is indeed a banal blunder in Treeview from WinForms.

On right mouse click context menu appears but tree node has not become selected. I've found one more similar complaint

It seems to be generally accepted behavior - right click and the selection moved to a node under the mouse and the menu appeared. The most interesting thing is that in Visual Studio it works exactly so.

Well, dumb fix:
private void treeView_MouseClick(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Right)
{

TreeNode node = treeView.GetNodeAt(e.X, e.Y);

if (node != null)
{
treeView.SelectedNode = node;

}
}
}


Update: 24.08.2007 And it is much better to tie the bug fix to NodeMouseClick

2007-08-10

C#: Generic type conversion

I've come across a strange restriction of generic in C#. The only good thing is the most problems appear  during programming the popular language are solved by a second query to google.

Because of the error "Cannot convert type 'string' to 'T'" instead of the evident:

do so
public static T GetSetting<T>(string key)
{
   return (T)ConfigurationManager.AppSettings[key];
}

write like this:

public static T GetSetting<T>(string key) where T : IConvertible
{
   return (T)Convert.ChangeType(ConfigurationManager.AppSettings[key], typeof(T));
}





Update 13.08.2007: And in addition, in spite of autoboxing the problem with simple type arrays is remained. If we have variable of type Object which stores array of strings that one can easy get n value with explicit cast, everything has become more interesting with array of integers:

[Test]
public void testCastArray()
{
Object object_arr = new String[] { "a", "b", "c" };
Object[] object_arr_cust = (Object[])object_arr;
Assert.AreEqual(object_arr_cust[1], "b");

Object value_arr = new Int32[] { 1, 2, 3 };
Array value_arr_cust = (Array)value_arr;
Assert.AreEqual(value_arr_cust.GetValue(1), 2);
}

[Test]
[ExpectedException(typeof(InvalidCastException))]
public void testInvalidArrayCast()
{
Object value_arr = new Int32[] { 1, 2, 3 };
Object[] value_arr_cust = (Object[])value_arr;
Assert.AreEqual(value_arr_cust[1], 2);
}

2007-08-07

DateTimePicker and nullable value

Is it really that the simplest solution of bringing closer together DataBinding and nullable types is to change a behavior of a control itself?


class NullableDateTimePicker : DateTimePicker
{
[Bindable(true)]
public object DBValue
{
get
{
if (this.Checked)
return base.Value;
else
return System.DBNull.Value;
}
set
{
if (System.Convert.IsDBNull(value))
this.Checked=false;
else
this.Value = Convert.ToDateTime(value);
}
}
}

Though in the ideal case it should be solved at the databinding level instead of the custom control.

2007-08-02

The big gap between web and me

Wiki is a wonderful thing for the document writing... until you like to include a screen shot.

Two default scripts:

  • Linux Gnome: Print Screen -> Save -> Wiki Add Image -> Open File Button -> Locate it -> Ok -> Upload

  • Windows: Print Screen -> Open MSPaint -> Paste -> Save -> Wiki Add Image -> Open File Button -> Locate it -> Ok -> Upload

How much better to use a usual visual editor (not emacs).
  • Print Screen -> Paste
I'd like:
1) Either an external utility which on PrintScreen pressing saves a picture into temporary folder and places to the clipboard full path to it;

2)either Firefox plug-in, understanding that if clipboard contains a picture and I paste it to file upload field then the picture should be temporary stored and full path to the file should be inserted in the field.

An external utility breaking the standard behavior is not desirable, can't google Firefox plugin..
I'm curious if the are elisp api for working with pictures from the clipboard.