Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Snake / Nibbles clone in C and Ncurses
Please improve
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Simple Particle Engine for a shooter game
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
Created a Telnet Parser with TDD. All tests pass, further refactoring?
<pre class='prettyprint' language='cs'>public class TelnetParser { public event EventHandler<TelnetCommandEventArgs> Command; protected void OnCommand(TelnetCommandEventArgs e) { if (Command != null) Command(this, e); } public byte[] Parse(byte[] bytes) { byte subOptionCode = 0; List<byte> subnegotiation = null; List<byte> nonCommandOutput = new List<byte>(); List<byte> output = nonCommandOutput; for (int i = 0; i < bytes.Length; i++) { if (bytes[i] == (byte)TelnetCommand.IAC) { i++; var command = (TelnetCommand)bytes[i]; switch (command) { case TelnetCommand.IAC: output.Add((byte)TelnetCommand.IAC); break; case TelnetCommand.WILL: case TelnetCommand.WONT: case TelnetCommand.DO: case TelnetCommand.DONT: i++; byte optionCode = bytes[i]; OnCommand(new TelnetCommandEventArgs(command, optionCode)); break; case TelnetCommand.SB: i++; subnegotiation = new List<byte>(); output = subnegotiation; subOptionCode = bytes[i]; break; case TelnetCommand.SE: OnCommand(new TelnetCommandEventArgs(command, subOptionCode, subnegotiation.ToArray())); output = nonCommandOutput; break; default: OnCommand(new TelnetCommandEventArgs(command)); break; } } else { output.Add(bytes[i]); } } return nonCommandOutput.ToArray(); } } public enum TelnetCommand : byte { SE = 240, NOP = 241, DM = 242, Break = 243, IP = 244, AO = 245, AYT = 246, EC = 247, EL = 248, GA = 249, SB = 250, WILL = 251, WONT = 252, DO = 253, DONT = 254, IAC = 255 } public class TelnetCommandEventArgs : EventArgs { public Byte[] Data { get; private set; } public byte OptionCode { get; private set; } public TelnetCommand TelnetCommand { get; private set; } public TelnetCommandEventArgs(TelnetCommand command, byte subOptionCode, byte[] data) { TelnetCommand = command; OptionCode = subOptionCode; Data = data; } public TelnetCommandEventArgs(TelnetCommand command, byte optionCode) : this(command, optionCode, new byte[0]) { } public TelnetCommandEventArgs(TelnetCommand command) : this(command, 0) { } } [TestFixture] public class TelnetParserTests { [Test] public void When_No_Commands_In_Data_Returns_Input_Data() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { 1, 2, 3, 4, 5 }; byte[] returnBytes = parser.Parse(bytes); Assert.AreElementsEqual(bytes, returnBytes, "Output bytes were wrong"); } [Test] public void When_Two_Command_Parameters_Output_Byte_Array_Has_One_Byte_Equal_To_The_Command_Byte_Value() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.IAC }; byte[] expected = new byte[] { (byte)TelnetCommand.IAC }; byte[] returnBytes = parser.Parse(bytes); Assert.AreElementsEqual(expected, returnBytes, "Output bytes are wrong"); } [Test] public void When_Two_Command_Parameters_In_Input_With_Non_Command_Characters_Command_Parameters_Are_Replaced_With_A_Single_In_Output() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { 1, 2, (byte)TelnetCommand.IAC, (byte)TelnetCommand.IAC, 3, 4 }; byte[] expected = new byte[] { 1, 2, (byte)TelnetCommand.IAC, 3, 4 }; byte[] returnBytes = parser.Parse(bytes); Assert.AreElementsEqual(expected, returnBytes); } [Test] public void When_Telnet_Command_Found_Fires_Event() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.NOP }; TelnetCommand foundCommand = (TelnetCommand)0; parser.Command += (object sender, TelnetCommandEventArgs eventArgs) => { foundCommand = eventArgs.TelnetCommand; }; parser.Parse(bytes); Assert.AreEqual(TelnetCommand.NOP, foundCommand, "Incorrect command set"); } [Test] public void When_Option_Command_Found_EventArgs_Contains_Option_Code() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.WILL, 1, (byte)TelnetCommand.IAC, (byte)TelnetCommand.WONT, 2, (byte)TelnetCommand.IAC, (byte)TelnetCommand.DO, 3, (byte)TelnetCommand.IAC, (byte)TelnetCommand.DONT, 4 }; byte[] expectedOptionCodes = new byte[] { 1, 2, 3, 4 }; List<byte> optionCodes = new List<byte>(); parser.Command += (sender, e) => { optionCodes.Add(e.OptionCode); }; parser.Parse(bytes); Assert.AreElementsEqual(expectedOptionCodes, optionCodes); } [Test] public void When_Option_Is_Start_Sub_Negotiation_EventArgs_Has_Gathered_Data_Until_End_Sub_Negotiation() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.SB, 1, 2, 3, 4, (byte)TelnetCommand.IAC, (byte)TelnetCommand.SE }; byte expectedOptionCode = 1; byte[] expectedData = new byte[] { 2, 3, 4 }; byte actualOptionCode = 1; byte[] actualData = null; parser.Command += (sender, e) => { actualData = e.Data; actualOptionCode = e.OptionCode; }; parser.Parse(bytes); Assert.AreEqual(expectedOptionCode, actualOptionCode); Assert.AreElementsEqual(expectedData, actualData, "Sub negotiation data was not parsed correctly"); } [Test] public void When_Subnegotiation_Ends_Non_Command_Data_Is_Still_Appended_To_Output() { TelnetParser parser = new TelnetParser(); byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.SB, 1, 2, 3, 4, (byte)TelnetCommand.IAC, (byte)TelnetCommand.SE, 5, 6, 7, 8 }; byte[] expectedBytes = new byte[] { 5, 6, 7, 8 }; byte[] returnedBytes = parser.Parse(bytes); Assert.AreElementsEqual(expectedBytes, returnedBytes); } }</pre> <a href="http://www.refactormycode.com/codes/1164-created-a-telnet-parser-with-tdd-all-tests-pass-further-refactoring" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>