How can I break a function dependency in F#? - unit-testing

I'd like to unit test fun1 without calling fun2.
let fun2() =
// Some complex function with lots of dependencies.
1
let fun1() =
fun2() * 2
What the best way to break the dependency between the two functions?
I’ve tried a couple of different ways, but they just add clutter.
Pass fun2 into fun1
let fun1(fun2) =
fun2() * 2
Convert to a class and override
type FunClass() =
abstract member fun2 : unit -> int
default x.fun2() = 1
member x.fun1() =
x.fun2() * 2
type FunClassMock() =
override member x.fun2() = 1
Use stategy pattern
type Fun1Class(fun2Class) =
member x.fun1() =
fun2Class.fun2() * 2
Use a variable
let fun2Imp() =
1
let mutable fun2 = fun2Imp
let fun1() =
fun2() * 2
Is there a cleaner way?

It depends on your usage, but you could do something like this:
let fun2() =
// Some complex function with lots of dependencies.
1
let createFun1 fun2 =
fun () -> fun2() * 2
let fun1 = createFun1 fun2
This is also useful for unit testing since you can test fun1 by simply passing a simple function in for fun2.

It isn't very flexible, but a compiler directive would work.
let fun2() =
#if TESTING
1
#else
// Some complex function with lots of dependencies.
#endif
Defining fun2 in separate modules and opening the needed module is another option.
module Impl =
let fun2() =
// Some complex function with lots of dependencies.
module Testing =
let fun2() = 1

Any way you do it (that I know of, at least) is going to "add clutter". How about something like this?
let fun1() =
fun1_impl(fun2)
let fun1_impl(fun2) =
fun2() * 2
Then, in regular code, use fun1, and in your tests, use fun1_impl.

In all generality, the argument passing seems the cleanest.
The clutter effect might not be tackled technically, but semantically : it comes from the fact that it seems a bit arbitrary, from the lack of meaning attached to "function1".
Is there may be a higher, more coherent, level that might be more meaningful in your code ?

Related

xtext: efficient way for custom Scoping

I tried to custom Scoping like this:
In the file MyDslScopeProvider that extends AbstractMyDslScopeProvider,
I implemented the function with this signature:
override def IScope getScope(EObject context, EReference reference)
and I used cases like this
if (reference == SpectraPackage.Literals.SOMETHING__POINTER)
but I have function in my grammar that it's have parameters and we can declare inside local vars. I don't want that those local vars and the parameters of that function would be visible from the outside, I want them to be visible only inside the function so I did something like this:
if (contextDecl instanceof function) {
val fun= contextDecl as function
val allContentsCurrFile = EcoreUtil2.getAllContentsOfType(fun,Constant)
EObjectsInScope.addAll(fun.params)
EObjectsInScope.addAll(allContentsCurrFile)
return Scopes.scopeFor(EObjectsInScope)
}
else{
val removeEobjects = newArrayList()
EObjectsInScope.addAll(EcoreUtil2.getAllContentsOfType(root,EObject))
val funList= EcoreUtil2.getAllContentsOfType(root,function) as List<function>
for(function f: funList){
removeEobjects.addAll(f.varDeclList)
removeEobjects.addAll(f.params.params)
removeEobjects.addAll(EcoreUtil2.getAllContentsOfType(f,Constant))
}
EObjectsInScope.removeAll(removeEobjects)
return Scopes.scopeFor(EObjectsInScope)
This is very un-efficient to get all the EObjects and to remove the vars that I don't want to be visible from the outside (it's taking a lot of time).
There is a way to do this more efficient?
Thanks.
EDIT: You might also be interested in my answer to "Xtext: IResourceScopeCache for Avoid expensive scope calculation"
First of all, if you are talking about local variables, you probably don't want to allow using local variables before they are declared, e.g.
function foo() {
x = x + 1;
int x = 0;
}
So you are actually doing too much work by using getAllContentsOfType().
What exactly are you trying to achieve using your optimizations? Better performance for content assist inside a function? Better speed for large number of models with small functions bodies? Better speed for many large functions?
Keep in mind to avoid premature optimization - it is more important to keep your code maintainable, and optimize for speed only if it doesn't scale to the workloads you actually need to handle. Did you use a profiler to find Hotspots? Human intuition can be pretty wrong when it comes to performance bottlenecks.
Anyway, under the assumption that you need to improve speed of scoping but you don't have massive workloads, as a first shot, I'd suggest using a TreeIterator to traverse the function body collecting the local variables that should be visible, and, using the return value of EcoreUtil2.getAllContainers(context) as a guide when to use prune() and when to use next().
I.e.
import static extension org.eclipse.xtext.EcoreUtil2.*
// ...
val predecessors = new ArrayList<EObject>
val iterator = EcoreUtils.getAllContents(function, true)
// Could be optimized further
val descentGuide = context.allContainers.dropWhile[it != function].toList.reverseView.iterator
var current = iterator.next
var nextDescent = descentGuide.next
while(current != context) {
// collect list with local variables here
predecessors += current
if(current == nextDescent) {
// Reached another ancestor of context - will look for the following ancestor next
nextDescent = descentGuide.next
} else {
iterator.prune
}
current = iterator.next
}
// Reverse so innermost declarations shadow outer declarations
val localVariables = predecessors.filter(LocalVariableDeclaration).toList.reverseView
I didn't compile/test the code, but I hope the idea becomes clear.
The while loop should terminate in the end because at some point, context will be reached -- but to be more robust it might make sense to add && iterator.hasNext to the while loop.

Defining const "variable" inside if block

I have the following code:
Foo a;
if (some_fairly_long_condition) {
a = complicated_expression_to_make_foo_1();
} else {
a = complicated_expression_to_make_foo_2();
}
I have two issues with this:
a is a const and should be declared so
the "empty" constructor, Foo() is called for no reason (maybe this is optimised away?)
One way to fix it is by using the ternary operator:
const Foo a = some_fairly_long_condition?
complicated_expression_to_make_foo_1():
complicated_expression_to_make_foo_2();
Is this good practice? How do you go about it?
To answer the second part of your question:
I usually put the initialization code into a lambda:
const Foo a = [&]()->Foo{
if (some_fairly_long_condition) {
return complicated_expression_to_make_foo_1();
} else {
return complicated_expression_to_make_foo_2();
}
}();
In most cases you should even be able to omit the trailing return type, so you can write
const Foo a = [&](){ ...
As far as the first part is concerned:
I'd say that greatly depends on how complex your initialization code is. If all three parts are really complicated expressions (and not just a function call each) then the solution with the ternary operator becomes an unreadable mess, while the lambda method (or a separate named function for that matter) allows you to break up those parts into the respective sub expressions.
If the problem is to avoid ternaty operator and your goal is to define the constant a, this code is an option:
Foo aux;
if (some_fairly_long_condition) {
aux = complicated_expression_to_make_foo_1();
} else {
aux = complicated_expression_to_make_foo_2();
}
const Foo a(aux);
It is a good solution, without any new feature ---as lambdas--- and including the code inline, as you want.

(Unit) Test Driven Development

I am fairly new to TDD and not so seasoned at unit testing, hence the question.
I have this legacy function written in PHP
function foo(){
x = bar();
y = baz();
if (x > y)
return 'greater';
return 'lesser';
}
If x (value returned by bar()) is always greater than y (value returned by baz()), I will never be able to test for 'lesser' return statement.
What should I do to cover both the test cases and achieve 100% code coverage?
Redefining foo() as foo(x, y) for dependency injection hooks is not an option with legacy code.
I am assuming foo, bar and baz are all global functions. (If they are part of a class, you want to be using PHPUnit's mocking functionality).
I blogged before about how to use a pecl extension to replace a built-in function:
http://darrendev.blogspot.jp/2012/07/mock-socket-in-php.html
This article shows a very interesting alternative approach using namespaces:
http://marcelog.github.io/articles/php_mock_global_functions_for_unit_tests_with_phpunit.html
It appears you will need to wrap your legacy code in a file with a namespace declaration at the top. I don't know if that is a show-stopper for you or not.
Since bar() and baz() do not take input parameters, they are either returning a constant
(and you can immediately refactor foo() to { return 'greater' } ; or they depend on some
external variable(s). In that case, do something like
function testFooReturnsGreater() {
setEnvironmentSoBarIsGreaterThanBaz()
assert ("greater".equals(foo())
}
function testFooReturnsLesser() {
setEnvironmentSoBarIsLesserThanBaz()
assert("lesser".equals(foo())
}
Since you say bar() > baz() unless it's Christmas, the setEnvironmentxxx() fixtures would need to change the program's notion of the current date (hopefully something you can mock, and not the actual system clock).

Functor Structure extension and Multiple Ascription in SML

Is there any way in Standard ML to make a functor output a structure which has all of the functionality of the passed in structure, plus any new functionality.
In a similar way, is it possible to do multiple ascription? In the case of the above it would be immediately useful because you could ascribe the output of the functor to both the signature of the original structure and another signature which specifies the new functionality.
I understand the implications of doing such a thing, and why it might be a bad idea. Currently I've just been keeping a copy of the passed in structure within the functor output - but this means you have a long chain of "Foo.Bar.func" to access the base functionality.
Thanks
Say I wanted to make a signature for "TestUp". Is there any way to do this without duplicating the contents of the "TEST" into a new signature?
If I understand your question correctly then you are looking for the include keyword, which will include the definition of a previous signature into a new and thus extending the signature with the previous definitions.
signature TEST_EXT =
sig
include TEST
val beep1 : meep -> unit
end
functor TestUp_EXT(T : TEST) : TEST_EXT =
struct
open T
fun localFun s = beep (10, s)
val beep1 = localFun
end
structure Test2_EXT = TestUp_EXT (Test);
Test2_EXT.beep (5, "EXT: Hi");
Test2_EXT.beep1 "Hi";
print (Int.toString (Test2.rand ()) ^ "\n");
(* This will fail as the signature doesn't define this function,
however as seen the function can easily be used within the functor as
expected *)
(* Test2_EXT.localFun "Hi"; *)
You can use open to bring the contents of a structure into the current scope. If used inside another structure (or functor), it'll do what I believe it is you want.
An example can be seen here:
signature TEST =
sig
type meep;
val beep : int * meep -> unit;
end;
structure Test : TEST =
struct
type meep = string
fun beep (0, _) = ()
| beep (n, s) = (print (s^"\n"); beep (n-1, s));
end;
functor TestUp (T : TEST) =
struct
open T
fun rand () = 4
end;
structure Test2 = TestUp (Test);
Test.beep (5, "Hello");
Test2.beep (5, "Hi");
print (
Int.toString (Test2.rand ()) ^ "\n"
);

What is the easiest way to expose M-file subfunctions for unit testing?

I have been tinkering lately with fully integrating continuous testing into my Matlab development cycle and have run across a problem I don't know how to get around. As almost all users know, Matlab kindly hides sub-functions within an M-file from the view of any functions outside that M-file. A toy example can be seen below:
function [things] = myfunc(data)
[stuff] = mysubfunc(data)
things = mean(stuff);
end
I want to perform unit testing on subfunc itself. This is, AFAIK, impossible because I cannot call it from any external function.
I'm currently using Matlab xUnit by Steve Eddins and cannot get around this issue. The easy solution -- splitting subfunc out to its own M-file -- is not acceptable in practice because I will have numerous small functions I want to test and don't want to pollute my filesystem with a separate M-file for each one. What can I do to write and perform easy unit tests without making new files for each function I want to test?
What you need to do in general is get function handles to your subfunctions from within the primary function and pass them outside the function where you can unit test them. One way to do this is to modify your primary function such that, given a particular set of input arguments (i.e. no inputs, some flag value for an argument, etc.), it will return the function handles you need.
For example, you can add a few lines of code to the beginning of your function so that it returns all of the subfunction handles when no input is specified:
function things = myfunc(data)
if nargin == 0 % If data is not specified...
things = {#mysubfunc #myothersubfunc}; % Return a cell array of
% function handles
return % Return from the function
end
% The normal processing for myfunc...
stuff = mysubfunc(data);
things = mean(stuff);
end
function mysubfunc
% One subfunction
end
function myothersubfunc
% Another subfunction
end
Or, if you prefer specifying an input flag (to avoid any confusion associated with accidentally calling the function with no inputs as Jonas mentions in his comment), you could return the subfunction handles when the input argument data is a particular character string. For example, you could change the input checking logic in the above code to this:
if ischar(data) && strcmp(data, '-getSubHandles')
I have a pretty hacky way to do this. Not perfect but at least it's possible.
function [things] = myfunc(data)
global TESTING
if TESTING == 1
unittests()
else
[stuff] = mysubfunc(data);
things = mean(stuff);
end
end
function unittests()
%%Test one
tdata = 1;
assert(mysubfunc(tdata) == 3)
end
function [stuff] = mysubfunc(data)
stuff = data + 1;
end
Then at the prompt this will do the trick:
>> global TESTING; TESTING = 1; myfunc(1)
??? Error using ==> myfunc>unittests at 19
Assertion failed.
Error in ==> myfunc at 6
unittests()
>> TESTING = 0; myfunc(1)
ans =
2
>>
Have you used the new-style classes? You could turn that function in to a static method on a utility class. Then you could either turn the subfunctions in to other static methods, or turn the subfunctions in to local functions to the class, and give the class a static method that returns the handles to them.
classdef fooUtil
methods (Static)
function [things] = myfunc(data)
[stuff] = mysubfunc(data);
things = mean(stuff);
end
function out = getLocalFunctionHandlesForTesting()
onlyAllowThisInsideUnitTest();
out.mysubfunc = #mysubfunc;
out.sub2 = #sub2;
end
end
end
% Functions local to the class
function out = mysubfunc(x)
out = x .* 2; % example dummy logic
end
function sub2()
% ...
end
function onlyAllowThisInsideUnitTest()
%ONLYALLOWTHISINSIDEUNITTEST Make sure prod code does not depend on this encapsulation-breaking feature
isUnitTestRunning = true; % This should actually be some call to xUnit to find out if a test is active
assert(isUnitTestRunning, 'private function handles can only be grabbed for unit testing');
end
If you use the classdef style syntax, all these functions, and any other methods, can all go in a single fooUtil.m file; no filesystem clutter. Or, instead of exposing the private stuff, you could write the test code inside the class.
I think the unit testing purists will say you shouldn't be doing this at all, because you should be testing against the public interface of an object, and if you need to test the subparts they should be factored out to something else that presents them in its public interface. This argues in favor of making them all public static methods and testing directly against them, forgetting about exposing private functions with function handles.
classdef fooUtil
methods (Static)
function [things] = myfunc(data)
[stuff] = fooUtil.mysubfunc(data);
things = mean(stuff);
end
function out = mysubfunc(x)
out = x .* 2; % example dummy logic
end
function sub2()
% ...
end
end
end
I use a method that mirrors the way GUIDE use to generate its entry methods. Granted it's biased towards GUIs...
Foo.m
function varargout=foo(varargin)
if nargin > 1 && ischar(varargin{1}) && ~strncmp( varargin{1},'--',2)
if nargout > 0
varargout = feval( varargin{:} );
else
feval = ( varargout{:} );
else
init();
end
This allows you to do the following
% Calls bar in foo passing 10 and 1
foo('bar', 10, 1)