Thursday, November 20, 2008

VSTS 2010 Local Microsoft Event

Last night I went to a local Microsoft Event demonstrating the cool new features of VSTS 2010 presented by Mark Mydland (Principal Group Manager of VSTS Test).

Mark demonstrated numerous new features of VSTS 2010 such as...

  • Allowing the tester to automatically capture data to eliminate no-repro bugs
  • Allowing the developer to immediately navigate to the problem code
  • Creating automating test cases with coded UI test
  • And much more...

In addition Mark demonstrated some products that have recently come out of Microsoft Research...

  • Pex
    "Right from the code editor, Pex finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage. Pex performs a systematic analysis, hunting for boundary conditions, exceptions and assertion failures, which you can debug right away."

    If you are new to Pex, you might want to check out the following resources...


  • CHESS
    "CHESS is an automated tool for finding errors in multithreaded software by systematic exploration of thread schedules. It finds errors, such as data-races, deadlocks, hangs, and data-corruption induced access violations, that are extremely hard to find with current testing tools. Once CHESS locates an error, it provides a fully repeatable execution of the program leading to the error, thus greatly aiding the debugging process."

    If you are new to CHESS, you might want to check out the following resources...

Feel free to download Visual Studio 2010 and .NET Framework 4.0 CTP and start playing with it today.

Note: If you were unable to make this talk, then there is a PDC video covering most, but not all of the content entitled Microsoft Visual Studio Team System: A Lap Around VSTS 2010 by Cameron Skinner (Product Unit Manager on the VSTS team). Here is a brief description of the video...

"In the spirit of an agile sprint, see how to use the next version of Visual Studio Team System to manage user stories and re-factor existing architecture. Learn how to diagnose real production problems, debug in-production virtual labs, capture test data to eliminate the no-repro bugs, transparently plan, monitor, and adapt software projects."

Thank you Compuware for sponsoring the event!

Wednesday, November 19, 2008

Tech Twitterings via jQuery & Twitter

Several weeks ago, I decided to start a weekly blog entry compiling a list of my technical tweets from Twitter. After the second week, I noticed that it was taking me much too long to compile the list, so I decided to create a tool to make it easier.

My current solution involves several Firefox add-ons, jQuery, and Twitter. Eventually, it would probably be much easier if I used the Twitter APIs directly, but for now I have found this solution to be a fun learning exercise :)

Steps to Create the Tech Twitterings Entry
  1. Search for my technical (#tech) tweets from Twitter
  2. Use the Firefox AutoPager add-on to automatically load the next several pages to capture all the tweets from the last week.
  3. Use the Firefox Greasemonkey add-on and run the Load jQuery Library script to load the jQuery library onto the Twitter search page
Load jQuery Library
// ==UserScript==
// @name           Load jQuery Library
// @namespace      http://webdevdotnet.blogspot.com
// @include        http://search.twitter.com/*
// ==/UserScript==

//Code taken from http://internetducttape.com/2008/05/08/greasemonkey-ninja-jquery/
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

function GM_wait() {
   if (typeof unsafeWindow.jQuery == 'undefined') { 
      window.setTimeout(GM_wait,100); 
   } else { 
      $ = unsafeWindow.jQuery; letsJQuery(); 
   }
}
GM_wait();
    
function letsJQuery() {
   unsafeWindow.console.log('The jQuery Library has been loaded'); 
} 
  1. Use the Firefox Firebug add-on and use it's console (F12) to run the jQuery Twitter Script
jQuery Twitter Script
$('li.result .msg .msgtxt .expand').remove();
var categories = new Array();
var categoryKeys = new Array();
$('li.result .msg .msgtxt').each(function(i,tweet) {
   var tweetText = $(tweet).text();
   var regularExpression = new RegExp('.*#tech.*(#\\w+).*', 'gi');
   var matches = regularExpression.exec(tweetText);
   if (matches != null) {
      var match = matches[1];
      if (categories[match] == null ) {
         categories[match] = new Array();
         categoryKeys.push(match);
      }
      categories[match].push(tweetText);
   }
});
for (var i = 0; i < categoryKeys.length; ++i) {
   var categoryKey = categoryKeys[i];
   console.log('<h3>' + categoryKey + '</h3><ul>');
   var category = categories[categoryKey];
   for (var j = 0; j < category.length; ++j) {
      console.log('<li>' + category[j] + '</li>');
   }
   console.log('</ul>');
}
  1. Copy & Paste the contents from the console into Blogger
  2. Surround the auto-generated HTML content in a div and prepend the JavaScript to convert Text to Hyperlinks.
Text to Hyperlink JavaScript
<script type='text/javascript'>  
$(document).ready(function() {
   $('#tt4').html(replaceUrlWithHtmlLinks($('#tt4').html()));
});

function replaceUrlWithHtmlLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href='$1'>$1</a>"); 
}
<div id="tt4">
<!-- Insert Autogenerated jQuery Twitter HTML here... -->
</div>
</script>

Wednesday, November 12, 2008

New F# SyntaxHighlighter Brush

Ever since my previous blog about Code Highlighters For Your Blog, I have heard several people interested in implementing the SyntaxHighlighter on their blog. If so, there is a good article by Guogang Hu about integrating it into your Blogger blog.

One of those individuals was my friend, Daniel Mohl (via twitter... @dmohl). However, Daniel had a need to support F#, which is not one of the out-of-the-box languages that is supported by the SyntaxHighlighter. I saw this as a fun side project to support this language for him.

F.Y.I. If you like F#, his blog is definitely one you should check out!

Here is an example F# program that is only a proof of concept that Dan put together for me to exercise the F# brush sufficiently... it doesn't actually work! :) I am just starting to learn the language, so please give me some slack!

If you find any areas that I missed while supporting the language, please let me know and I'll fix it. Feel free to download and use the F# SyntaxHighlighter Brush and provide any suggestions or comments.

Note: The JavaScript file has been minimized with JSMin, so it might be a little difficult to read.

I'd like to contribute the F# Brush to the Google Code project, but I am not yet a contributor on the project. If I am able to become a contributor, then I will post the code as part of the main project.

#light
namespace testforElijah
module testModuleForElijah
open System
let multiple x = x * x
val CalculateSeqExample: seq<int> -> int
type ICustomerDao = interface
    abstract GetById: int -> ICustomer
end
type CustomerDao = class
    new: unit -> CustomerDao
    interface ICustomerDao
end
if
then
elif
else
@"c:\test\test.xml"
'c'
"xyz"B
"tes"+"t"
[for x in list -> expr]
type 'a option = 
  | None
  | Some of 'a

//This is a test
let showSomething (name, something)
  match name with
    | Some  (some, thing) -> (* do something*)
    | None
 
type node = 
  { Name : string;
    Links : list list;}
and link = 
  | Dangling
  | Link of node 

Tuesday, November 04, 2008

Enum Parse Extension Methods

When I write code I don't like to depend on catching exceptions to control my programming logic, so as much as I can I try to check for nulls, empty, etc...

When parsing one type into another there are usually TryParse() methods available so that an exception does not occur. I've always been a little confused why there wasn't such a method off the Enum class. So, I decided to make my own.

However, after I started to extend the TryParse method off of the Enum, the answer started to become clear why that was not a feature :) I could extend the actual specific enum, but that didn't really help me any. I wanted a Generic solution for all enums.

So, instead of making an Enum.TryParse(), I decided to extend the string class to add string.ToEnum() and string.TryToEnum(). The string is usually the class that I want to parse into a specific Enum after all.

The following is the Extension methods that I created...

using System;

namespace Web.Helpers {
    public static class EnumHelper {
        public static bool TryToEnum<T>(this string obj, out T parsed) {
            bool isParsed = false;

            if (Enum.IsDefined(typeof(T), obj)) {
                parsed = (T)Enum.Parse(typeof(T), obj);
                isParsed = true;
            } else {
                parsed = (T)Enum.Parse(typeof(T), Enum.GetNames(typeof(T))[0]);
            }

            return isParsed;
        }
        
        public static T ToEnum<T>(this string obj) {
            return (T)Enum.Parse(typeof(T), obj);
        }
    }
}

I created a set of 4 MS Unit tests to exercise different scenarios that might exist. Let me know if you see any other tests that I should test.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Web.Helpers;

namespace WebTests.Generic {
    [TestClass]
    public class EnumTest {
        public enum Color { Red, Orange, Yellow, Green, Blue, Indigo, Violet };

        [TestMethod]
        public void TryToEnumWorksWithValidEnum() {
            Color parsedColor = Color.Blue;

            string realColor = "Yellow";
            bool canParse = realColor.TryToEnum<Color>(out parsedColor);
            Assert.AreEqual(true, canParse);          
        }

        [TestMethod]
        public void TryToEnumWorksWithInvalidEnum() {
            Color parsedColor = Color.Blue;

            string fakeColor = "Elijahish";
            bool canParse = fakeColor.TryToEnum<Color>(out parsedColor);
            Assert.AreEqual(false, canParse);
        }

        [TestMethod]
        public void ToEnumWorksWithValidEnum() {
            Color parsedColor = Color.Blue;

            string realColor = "Yellow";
            parsedColor = realColor.ToEnum<Color>();
            Assert.AreEqual(Color.Yellow, parsedColor);
        }

        [TestMethod, ExpectedException(typeof(ArgumentException), "Unable to parse enum")]
        public void ToEnumThrowsExceptionWithInalidEnum() {
            Color parsedColor = Color.Blue;

            string fakeColor = "Elijahish";
            parsedColor = fakeColor.ToEnum<Color>(); 
        }
    }
}

Have you found yourself doing some sort of the same thing? If so, how?