Link Taxonomy Terms to Views in Drupal

07-Aug-09

Imagine a task (actually, quite common), if you have:

  • a nodes (articles, ads, whatever) taxonomy in Drupal,
  • a taxonomy-based url path rewrites, like /monkeys/primates/homosapiens
  • and want to show a block/page View with articles only from current path term (and, maybe, its subterms).

It might be a hard time finding out the current term. One could try having two nested views, passing a current term as a parameter to nested view, like Dustin Currie did.

Though, Views has several pre-defined solutions. Just go to /admin/build/views/ and enable this one: “Default Node view: taxonomy_term”, (clone it to play safe), voila!

You got a View for the current term.

Diff a Micorosoft Office documents inder SVN?

13-Jul-09

In case you, like me, need to compare version of Office documents (under Windows), just know that TortoiseSVN got a pretty set of scripts for that.
It works out of box!
YES!
You can compare Office documents just like plaintext files!.
Just tried it and it worked. If you were afraid of trying, like me – don’t be.

Grails: mocking domain objects in unit tests

13-Jul-09

We’re trying Grails, Rails-like web application framework for Java.
It’s fine, just that Groovy debugger support is, er, imperfect, even in the best Gruoovy IDE – IDEA.

And, if you want to unit test, you won’t have fancy domain class methods addTo* – like Customer.addToOrders().
They’re generated by Grails on startup.
In order to have addTo*(), inherit from GrailsUnitTestCase and call mockDomain(Customer) in setUp().

Oh, if you get "NullPointerException: Cannot invoke method containsKey() on null object", add super.setUp() to yout testcase’s setUp().

Having proper save() is more tricky. Implementation from mockDomain() works to some extent: it won’t save connected objects.
So, in order to get save() working, you have to do something like this:
More…

“Project Dependencies” of Visual Studio 2008 broken in MSBuild

20-Nov-08

Just dealt with another Visual Studio 2008 “feature”.

You can specify all the necessary “Project Dependencies” in Visual Studio, but will get “CSC : error CS0006: Metadata file FooBar.dll could not be found“. Even if your csproj files have correct references to other solution projects, msbuild will fail.
Maybe it appears only if project output path is outside of project directory.

It appears that Visual Studio keeps the dependencies in two ways, only one of which is read by MSBuild. I see that because I still can specify dependencies in GUI, copy solution to other machine and build it with VS in correct order.

Not with MSBuild.

The data needed by MSBuild is a “ProjectSection(ProjectDependencies) = postProject” section of SLN file. Like this:

More…

Algorithmic quiz: check a 3-braces expression

23-Oct-08

I just won a bet for $10 by solving a quiz:

Check the correctness of a braces, brackets and parentheses sequence. The solution should be of linear complexity (to say more, it’s 1-pass).

For instance, these expressions are correct: “()”, “()()[]“, “([][][]{})”, “([])()[]“, and these are not: “][“, “())(“, “(()”.

Ten dollars

It’s basically solved in 15 lines, all others being auxiliary. Can you reproduce the algorithm?

The solution follows.
More…

IListSource.ContainsListCollection explained

09-Oct-08

Whenever you implement own datasource for .NET GUI binding, you’lll have the choice – whether to implement IList or IListSource.

IListSource is a simplistic interface with two members: IList<T> GetList() and bool ContainsListCollection;

MSDN help about IListSource.ContainsListCollection states it’s “indicating whether the collection is a collection of IList objects”.
MSDN is not true here.

If IListSource.ContainsListCollection is false, GetList() just returns your IList.

If IListSource.ContainsListCollection is true, GetList() is expected to return ITypedList, which needs to provide a collection of PropertyDescriptor-s for every field of your collection.
Its purpose is to provide field names (including by-name field access) in runtime.

Quoted below is a code piece from a Microsoft newsgroup that resolves both cases to a data list (field values list, in second case).

And yes, if you’re planning to mutate the UI-bound collection, use IBindingList<> instead of IList<>.
More…

Unit tests quickstart

24-Jun-08

A friend asked me for it, so probably someone might need a jumpstart in unit tests.

Here’s a short list of short (though very deep, if not best) intros.

For deeper dive, take a “xUnit patterns” book (this one is longer).

For Kent Beck‘s book on TDD, shame on me – I never read it. Though after “XP explained” I don’t strive for reading his books.

// If you’re interested in XP and “XP explained” criticism, take a look at “Extreme Programming Considered Harmful for Reliable Software Development” by Gerald Keefer.

On unit tests in Visual Studio 2008 vs NUnit

02-Jun-08

My not-so-humble evaluation of VS tests distinctions from NUnit is (in points on -10 to 10 scale).

  • (-4) To run tests, VS launches an entire process which takes no less then 3 seconds to start. If you’re in “F5 hit breakpoint – Shift-F5″ loop, that’s disgusting. NUnit tests can be run in-process by Resharper or TestDriver.NET;
  • (-2) GUI that shows where test failed is ugly. You don’t get to failed line on double-click, you first get to test log page. Status and call stack are necessary, but I’d like not to trash my document tabs, I already have enough of them open. You can’t make that kind of windows docked/floating;
  • (-1) You got a “public TestContext TestContext;” sticking in your code. You’ll need it only when it comes to testing on data, which doesn’t always happen, even in a data-driven application;
  • (-1) It’s a “not invented here” technology, while NUnit was around for years;

Edit: In a moment of madness, I mixed together coverage profiling and unit testing tools. May God and readers forgive my aberration.

Edit2: If you need a half-page kickstart in unit testing techniques, the next post can help.

Please meet: Lua

15-May-08

Lua recently became very popular. I encountered Lua scripting in several applications including popular games, I believe it’s used in even more then Wikipedia says.

It’s extremely simple:

  • 6 data types (that’s counting “nul” and “function”),
  • no C++-style OOP out-of-box (but you can program one, he-he),
  • 400K of pure-C code

but is loaded with in-fashion features like

The only metaprogramming tutorial (with a ready code to implement virtual methods) is “Programming in Lua” book. Still, I don’t see a code to implement ad-hoc polymorphism… Maybe it’s a reason for another post :)

Redistributed book removed: it violated copyright.

Another C++ quiz, simpler

14-May-08

Did you ever think, what happens if you take a member pointer to a virtual function of a class, and then call it on another instance of derived class (that overrides that member)?

What if you do the same for nonvirtual function?

Here’s a test:

#include <iostream>

class A
{
  public:
  virtual void f() { std::cout << "A::f()\n"; };
  void g() { std::cout << "A::g()\n"; };
};

class B : public A
{
  public:
  virtual void f() { std::cout << "B::f()\n"; };
  void g() { std::cout << "B::g()\n"; };
};

typedef void (A::*VirtualFunctionPointer) ();

int main()
{
  A a;
  B b;
  B* pb = &b;

  VirtualFunctionPointer p = &A::f;
  (pb->*p) ();

  p = &A::g;
  (pb->*p) ();
  return 0;
}

Answer is in comments.

C++ quiz from the past

14-May-08

One of my friends once encountered a C++ compilation issue.
A program is given:

#include <iostream>
#include <sstream>
using namespace std;

#define SFORMAT(e) ((dynamic_cast<const ostringstream&>(ostringstream() << e)).str())

int main(int argc, char* argv[])
{
cout << SFORMAT("2 x " << " 2 = " << 2*2);
return 0;
}

No one asks you what it will print, unless you’re a god.

Question is: why is the first const char* printed as a pointer, instead of what we need?
Two years ago, I couldn’t tell.
Some guru from Apple answered this in a newsgroup.

Here, take another, simpler C++ quiz about member pointers.

MSBuild don’ts

15-Apr-08

A couple of notes how NOT to use MSBuild.

  1. Don’t generate files with AssemblyInfo task. It screws comments, screws InternalsVisibleTo attributes, and whatever else it doesn’t account for.
    Instead
    : Use
    <FileUpdate Files="@(AssemblyInfoFiles)"
    Regex="AssemblyVersion\(".*?"\)\]"
    ReplacementText="AssemblyVersion("$(Major).$(Minor).$(Build).$(Revision)")]"
    />
    <FileUpdate Files="@(AssemblyInfoFiles)"
    Regex="AssemblyFileVersion\(".*?"\)\]"
    ReplacementText="AssemblyFileVersion("$(Major).$(Minor).$(Build).$(Revision)")]"
    />
  2. Don’t account for TfsXXXX community tasks – it won’t run on VS 2008 with Team Explorer 9.0.
    Instead:  Use <Exec Command=”tf …”>.
    And if you have domain authentication in TFS and don’t run integration under domain user (which happens), you even can’t run tf in cmd-line mode: stupid authentication dialog will pop up.
  3. Do not use local files to store version number. You will lose them when moving build to another host, so use only source control-dependent files.
    Might seem evident, but strange how many people miss this.
    So you’ll need to
    <Attrib Files="version.txt" Normal="true" />
  4. Per Scott Colestock’s post, “since TfsVersion interrogates the build server’s workspace, and BuildNumberOverrideTarget comes early in the process… you might find that you don’t get the changeset number you expect”.
  5. Don’t try to operate on XML node collections with tasks other then Xslt.
    For instance, XmlRead returns a Nodeset only as a string of “;”-joined
    Node.Value-s.
    XmlQuery supports Values property, though I wasn’t able to operate it’s results.
    Instead you can use ReadLinesFromFile (sadly, this will trim the lines).
    My personal choice for not-too-complex XML is… you can guess… yes, FileUpdate.
  6. Don’t use FtpUpload. It won’t create folders for you.
    Just <Exec> a ftp…
    By the way, Windows’ ftp.exe client is unable to upload in passive mode. You can download passively, if you issue “LITERAL PASV“, but it won’t help the client.