我的開源兒子 amop [amop 見聞錄]

April 14, 2008 – 12:07 pm

amop 是我所寫的一個C++ Mock Object1 Framework,它是開源2的,也是我第一個開源的程式。

在我開發這個程式之前,我也是使用其他人所寫的Mock Object Framework,但使用起來,發覺有一點麻煩:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Interface
{
  virtual ~Interface(){};
  virtual void Foo() = 0;
};
 
class BaseMock : class Interface, MockObject
{
  virtual void Foo() 
  {
     // Check this function is called
  }
}
 
...
BaseMock mockObject;
...
// Some way to call the Interface::Foo
mockObject.Foo();
mockObject.Verify();

這種方法的麻煩,在於每一個你要測試的介面3,你也要繼承出一個BaseMock,而且,當你改變了那個介面:

1
2
3
4
5
6
class Interface
{
  virtual ~Interface(){};
  virtual void Foo() = 0;
  virtual void Bar() = 0;
};

你就得為每個BaseMock類別加上新的Bar 實作4

所以,我在想,若果能不需要那BaseMock就太好了。但想了很久也想不出方法,所以就不了了之了…
但這個問題在腦中不斷的出現,最後,想到了一個正常被禁止的方法5:

直接改動C++ 的virtual table

為什麼這個方法是被禁止?因為virtual table 的 ABI 6,對每個編譯器也會有不同的實作方式,所以肯定是不能跨編譯器7的。但實在是想不到其他方法後,我就開始試寫出來。

除了ABI 之外, 最大的問題是有大量template meta-programming,我差不多用了我一生所學,有關C++ template 的技巧也用上了。8,所以花了很長的時間才完成。完成後,就給了它一個名字:

Automatic Mock Object For C plus plus

AMOP,我的開源兒子,就在google code 出世了。

還以為一個不能跨編譯器的Library,不會太多人使用9,但它的下載數字,卻在不斷增加。在討論頁中的討論,也已經很長了:)

最開心的是,人們的反應:

“Hi very nice framework!!! This can be the leading mock framework for c++. ”
“Very, very good framework! We are using in ourcompany to make our Unit Tests easier and more robust. Congratulations. Very nice work !”
“Nice framework!”
“Hi, really like what I’m seeing. Great idea to use the ABI. Obvious and genius at the same time. ”

我編寫程式也很多年了,但從來沒有給別人說過,我寫的程式是 “very nice framework”。很多時候,在商業上,其他公司會要求給出一些他們的應用程式的好評,但多數也不是真心話,但Amop 這些評語,是在沒有利益關係下,在互聯網上的不知名的人給出的,他們用不著對我說假話。

我想,這就是為什麼,在沒有利益的情況下,有那麼多人會去寫開源軟件了。所以,希望你看到我這些說話後,也來寫寫開源軟件,不為利益,為快樂而寫程式 :)

  1. 有空會談多些 Mock Object 的運用
  2. Open Source
  3. Interface
  4. Implementation
  5. 對,就是所謂的邪道:P
  6. Application Binary Interface
  7. Cross Compiler
  8. 連Preprocessor Meta-programming也用上了…:P
  9. 但我也port 了它到gcc..
  1. 5 Responses to “我的開源兒子 amop [amop 見聞錄]”

  2. 我都要異口同聲說 “好框架!”

    希望有一日我都可以做這麼高質素的開源項目。

    P.S. 第一段 code 第3行應該係 virtual destructor吧。

    By Milo on Apr 14, 2008

  3. 事實上,也不是這麼好..相對於其他的語言的Mock Object Framework,還有很多進步的空間。

    而且,現在的方法還不是最好,由於有大量template,所以若大量用上,Compile 上來還有一點慢。不知Milo 兄知否有什麼書裡有談template meta programming 的optimization呢??

    P.S 謝謝,已更正

    By rdescartes on Apr 14, 2008

  4. 很感兴趣。虽然现在VC6已经是古董了,但很多人还在用,amop中用了很多的template,这使得移植到VC6相当困难。请问是否有什么办法解决此问题?谢谢!另外,template能否避免使用?这样既可以加快编译速度,也解决了很多问题。

    By sinojelly on Sep 9, 2008

  5. Good for people to know.

    By Cashlin on Nov 23, 2008

  6. i remember when u start this amop blog
    i am besides you always

    although i dont know wht u are doing
    i even dont understand things u mention inside
    now i know more about your world
    by asking you to tel me little story about everything about the programming world
    thanks for teaching me much
    i even want to take course to know more……

    more ppl are now using it now <—knowing from u
    that kind of saticfiction is more then words can describe
    rite ?

    proud of u sometimes even i didnt mmention
    love u always

    By little kat on Dec 15, 2008

Post a Comment