Skip to main content

Posts

Unit testing code that uses environment variables and system properties with fakes

I did not exactly learn this today, but I am sharing it as I have found it extremely useful when unit testing code that depends on environment or system property settings. While I am using Java as an example, the general concepts apply any where. Problem : You have a piece of code you are unit testing that uses settings from env variables or system properties passed to the VM (System.getProperty), but you don't want the tests to be affected by the 'real' environment or system properties in the VM. So, your unit tests should not get different results or fail when the real environment changes. Solution : There are several. But the most straightforward is to use a mocking library to mock out the environment or fake it out, whatever your prefer. You can create a fake using a library like EasyMock, PowerMock etc. This I won't discuss in this post, since there are numerous articles for that. Or you can write a simple class that acts as a proxy, using the proxy pattern...

Pattern matching in Scala

One cool feature I  learned a few days ago in Scala was Pattern Matching. It lets you decompose into a virtual switch..case statement any input object or variable, to easily do different things in different cases Example funciton that returns true if the input is an empty List, otherwise false. This is not obviosuly a good way to implement this feature, but this post is just to demonstrate the pattern matching in action. def func(x: List[Int]): Boolean = {     x match {       case List() => true // if x matches this pattern return true       case List(x: Int) => false // not needed, but for demo of another pattern match       case _ => false // default case     }   }                                                   So this returns true  ...

Javascript in all its weirdness: Immediate functions

What does this line do? var x = ( function() {     return "name"; }()); This is called an immediate function in Javascript. It looks like x will become a function, but in fact, it is assigned the value "name", the return value of executing the anonymous function. Immediate functions are nice for one time work, such as setup, because (a) you won't have to create an object or function that lingers, as it is one time code, and (b) you protect the global scope from being written to, because all the variables in this functions are local. Here is how to pass arguments to immediate functions var x = ( function(arg1) {     return arg1 + "name"; }("my")); // returns "myname" You can assign immediate fuction restuls to a property of an object such as in var p = { name:   ( function() {                           return "ana...

Javascript in all its weirdness: What is 'this' ?

The this pointer, people's favorite in languages including Javascript was a great source of pain for me when I was developing a Javascript drop down custom component. I learned today that it was 'this' that caused me problems! Generally, 'this' refers to the global object. It also takes on different values based on your scope/context. -In a global constructor, global function and global code, this refers to the global object ( the first should not be the case in strict mode in ECMAScript5) -In a method of an object, 'this' refers to the object // this pointer name='what?'  // same as this.name var p = function () {      console.log(this.name);  // changes the global name, as no var name is defined locally      name = 'again?';      console.log(this.name); } p(); // the global this.name was modified by the function p // Why? because p is a global function, // and this refers to the global object in that functi...

NoSQL Databases - MongoDB first cut

A few days ago I tried MongoDB, my first encounter with what is called a NOSQL Database. No, it does not mean 'no' 'sql', rather Not only SQL, anyway I thought I would try mongodb, out of the many available there. It has both the document-oriented database design and ability to be accessed from Javascript, both features I really like. However, since I spend most time in Java, I thought I would start there. First, let me tell you you should try their web console before writing a line of code or downloading anything because it's a really good self-run tutorial, that lets you type real commands and get real output all from the browser! pretty awesome... Anyway, here is what I did to get started. 1. Download mongodb from http://www.mongodb.org/ 2. Create working dir for MongoDB to use as data directory 3. Open favorite IDE or editor, and start writing test, yes Test , not source, in the spirit of Test Driven Development. 4. In test package, create test such as: ...

Javascript in all its weirdness: Function hoisting (will spin your head!)

Unless you are a Javascript guru, or have read related stuff before, this will spin your head! To see this in action: Paste this into a "test.html" file and open in firefox, and open Firebug. Here, two functions a and b are defined, both using what are called 'function declaration syntax', much like a C/Java function. However, another function tries to redefine each of these locally. 'a' is redefined using a function declaration syntax, whereas b is redefined using a 'function expression' syntax (i.e., treating function like an expression and assigning it to a var called 'b'). Notice, how the entire defintion of 'a' is hoisted (pushed up to the top of the defining function), whereas only the variable name 'b' is hoisted, not its definition. See my other article on the basics of hoisting, if you are not familiar with it - "Javascript in all its weirdness - Hoisting" <script type="text/javascript"...

Javascript in all its weirdness - Hoisting

This piece of code did not do what I expected, can you tell? // define a var x = 1; function changex() { console.log(x); // I expect this to print 1 var x = 2; // define a local var x console.log(x); // I expect this to print 2 } changex(); What do I actually get? undefined 2 Why the heck is x undefined for the first print? it turns out javascript does what is called "hoisting" meaning it collects all local variable definitions in one place before executing any function code. therefore the function is effectively doing this var x; // undefined console.log(x); // prints undefined x=2; console.log(x); // prints 2 This really got me the first time I read this References: Javascript patterns, Stoyan Stefanov