Random programming things I'd want to remember

Wednesday, January 17, 2018

Visual Studio snippet for a TestMethod with a proper naming convention (naming convention was proposed by Roy Osherove)

Here it is, see this post for details on how to import it into your Visual Studio. Enjoy!

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Test method with a naming convention</Title>
      <Author>Roy Osherove, yevgeller</Author>
      <Description>Test method with the following naming convention: method under testing, condition, and expected results. The naming convention was taken from Roy Osherove's book "The Art of Unit Testing"</Description>
      <Shortcut>testmc</Shortcut>
    </Header>
    <Snippet>
      <Imports>
        <Import>
          <Namespace>
            Microsoft.VisualStudio.TestTools.UnitTesting;
          </Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>NameOfMethodUnderTest</ID>
          <ToolTip>Replace with the method that you want to test</ToolTip>
          <Default>YourMethod</Default>
        </Literal>
        <Literal>
          <ID>ConditionYouAreTesting</ID>
          <ToolTip>Condition that you want to test</ToolTip>
          <Default>IsNull</Default>
        </Literal>
        <Literal>
          <ID>ExpectedResult</ID>
          <ToolTip>The result that you expect</ToolTip>
          <Default>NullArgumentException</Default>
        </Literal>
      </Declarations>
        <Code Language="csharp">
          <![CDATA[          
          [TestMethod]
          public void $NameOfMethodUnderTest$_$ConditionYouAreTesting$_$ExpectedResult$()
          {

          }
          ]]>
        </Code>
      </Snippet>
  </CodeSnippet>
</CodeSnippets>



Thursday, January 11, 2018

Timer to count up in JavaScript

Here is my implementation, nothing original or extraordinary. Putting it here for future reference.
        
        var totalSeconds = 0;
        var timer;

        function startTimer() {
            timer = setInterval(function () {
                totalSeconds++;
                var t = document.getElementById("time2"); //or other place where to display data
                t.innerHTML = displayTimer(totalSeconds);
            }, 1000); 
        }

        function stopTimer() {
            clearInterval(timer);
        }

        function displayTimer(numberOfSeconds) {
            var result = "";

            var hours = Math.floor(numberOfSeconds / 3600);
            if (hours > 0) { result += hours + " h "; }

            var minutes = Math.floor((numberOfSeconds - (hours * 3600)) / 60);
            if (minutes > 0) { result += minutes + " m "; }

            result += (numberOfSeconds % 60 + " s");

            return result;
        }

Tuesday, January 9, 2018

To remember: declaring an inline array for splitting strings

This is how to declare an inline char array (C#) to split the string into multiple parts:

List result = inputLine.Split(new[] {'%'}, StringSplitOptions.RemoveEmptyEntries).ToList();