使用DSL进行TDD演练

作者: 而立不惑之年 | 来源:发表于2018-03-29 21:23 被阅读36次

前言

同事分享《C++沉思录》,提供一个练习题。直观感受很适合使用DSL&TDD解题,演练一番,过程如下:

题目

1、实现Picture类及其他类或函数,满足以下要求:

#include <iostream>
#include "Picture.h"
using namespace std;
int main(int argc, char const *argv[])
{
    const char* init[] = {"Paris", "in the", "Spring" };
    Picture p(init, 3);
    cout << p << endl;
    return 0;
}

以上代码的打印结果为:

Paris
in the
Spring

2、frame函数给图形加边框:

cout << frame(p) << endl;

输出:

+------+
|Paris |
|in the|
|Spring|
+------+

3、操作符|将两个字符图形横向连接:

cout << p | frame(p) << endl;

输出:

Paris +------+
in the|Paris |
Spring|in the|
      |Spring|
      +------+

4、操作符&将两个字符图形纵向连接:

cout << p & frame(p) << endl;

//输出:

Paris 
in the
Spring
+------+
|Paris |
|in the|
|Spring|
+------+

5、更复杂的例子:

cout << frame(frame(p) | (p & frame(p))) << endl;

输出:

+----------------+
|+------+Paris   |
||Paris |in the  |
||in the|Spring  |
||Spring|+------+|
|+------+|Paris ||
|        |in the||
|        |Spring||
|        +------+|
+----------------+

解题过程

0 分析

DSL是去年参加过丁辉教练的训练营掌握的技能,印象很深。结合测试用例直观感受,很适合使用DSL AST的思路来实战。行动起来------
语法抽象如下:

Pic pic(init,3);
Frame frame(pic);
And andrule(pic,frame);
Or orrule(frame,andrule);

直接用语法作为测试用例,小步快跑,TDD模式进行实现:

1 实现Pic

第一个测试用例test.cpp:

#include "gtest/gtest.h"
#include "rule.h"

TEST(Draw,Pic)
{
  const char* init[] = {"Paris", "in the", "Spring" };
  Pic pic(init,3);
  const char *result= "Paris \n"\
                      "in the\n"\
                      "Spring\n";

  EXPECT_STREQ(result,pic.exec().GetStr().c_str());
}

头文件定义rule.h,设计三个类:
Result为结果类;Rule为统一规则类,所有的规则都继承该类;Pic 为实现的图类。代码如下:

#ifndef RULE_H_
#define RULE_H_

#include <string>
#include <vector>
using namespace std;

struct Result
{
    Result(vector<string> &v,int l);
    string GetStr();
private:
    vector<string> vRes;
    int length;
};

struct Rule
{
  Result exec();

  vector<string> vRes;
  int length;
};

struct Pic:Rule
{
  Pic(const char* init[],int count);
};

实现文件rule.cpp,代码如下:

#include <algorithm>
#include <string.h>
#include "rule.h"

Result::Result(vector<string> &v,int l):vRes(v),length(l)
{
}

string Result::GetStr()
{
    string res;
    for(const auto &it:vRes)
    {
    res+=it;
    res+="\n";
    }
    return res;
}

Result Rule::exec()
{
  return Result(vRes,length);
}

Pic::Pic(const char* init[],int count)
{
  length=0;

  for (int i=0; i<count; i++)
  {
      int iLen=strlen(init[i]);
      if(iLen>length)
    length=iLen;
  }

  for (int i=0; i<count; i++)
  {
      string str=init[i];
      str.resize(length,' ');
      vRes.push_back(str);
  }
}

2 实现frame

测试用例

TEST(Draw,Frame_Pic)
{
  const char* init[] = {"Paris", "in the", "Spring" };
  const char *result="+------+\n"\
                     "|Paris |\n"\
                     "|in the|\n"\
                     "|Spring|\n"\
                     "+------+\n";

  Pic pic(init,3);
  Frame frame(pic);

  EXPECT_STREQ(result,frame.exec().GetStr().c_str());
}

接口设计

struct Frame:Rule
{
  Frame(Rule &rule);
};

实现

Frame::Frame(Rule &rule)
{
  length=rule.length+2;

  string shead;
  shead.resize(length,'-');
  shead[0]=shead[length-1]='+';

  vRes.push_back(shead);
  for(const auto &it:rule.vRes)
  {
      string str("|");
      str+=it;
      str+="|";
      vRes.push_back(str);
  }
  vRes.push_back(shead);
}

3 实现And

测试用例

TEST(Draw,Frame_And_Pic)
{
  const char* init[] = {"Paris", "in the", "Spring" };
  const char *result="Paris   \n"\
                     "in the  \n"\
                     "Spring  \n"\
                     "+------+\n"\
                     "|Paris |\n"\
                     "|in the|\n"\
                     "|Spring|\n"\
                     "+------+\n";
  Pic pic(init,3);
  Frame frame(pic);
  And andrule(pic,frame);

  EXPECT_STREQ(result,andrule.exec().GetStr().c_str());
}

接口设计

struct And:Rule
{
  And(Rule &left,Rule &right);
};

实现

And::And(Rule &left,Rule &right)
{
  length=max(left.length,right.length);

  for(const auto &it:left.vRes)
  {
      string str=it;
      str.resize(length,' ');
      vRes.push_back(str);
  }

  for(const auto &it:right.vRes)
  {
      string str=it;
      str.resize(length,' ');
      vRes.push_back(str);
  }
}

4 实现Or

测试用例

TEST(Draw,Frame_Or_Pic)
{
  const char* init[] = {"Paris", "in the", "Spring" };
  const char *result="Paris +------+\n"\
                     "in the|Paris |\n"\
                     "Spring|in the|\n"\
                     "      |Spring|\n"\
                     "      +------+\n";
 
 Pic pic(init,3);
  Frame frame(pic);
  Or orrule(pic,frame);

  EXPECT_STREQ(result,orrule.exec().GetStr().c_str());
}

接口设计

struct Or:Rule
{
  Or(Rule &left,Rule &right);
};

实现

Or::Or(Rule &left,Rule &right)
{
  length=left.length+right.length;
  size_t heigh=max(left.vRes.size(),right.vRes.size());

  string str;
  for(size_t i=0; i<heigh; i++)
  {
      str.clear();

      if(i<left.vRes.size())
        str=left.vRes[i];
      else
        str.resize(left.length,' ');

      if(i<right.vRes.size())
        str+=right.vRes[i];
       else
        str.resize(left.length+right.length,' ');

      vRes.push_back(str);
  }
}

5 接收测试

利用 单元测试验证frame(frame(p) | (p & frame(p)))。

TEST(Draw,result1)
{  const char* init[] = {"Paris", "in the", "Spring" };
  const char *result= "+----------------+\n"\
                      "|+------+Paris   |\n"\
                      "||Paris |in the  |\n"\
                      "||in the|Spring  |\n"\
                      "||Spring|+------+|\n"\
                      "|+------+|Paris ||\n"\
                      "|        |in the||\n"\
                      "|        |Spring||\n"\
                      "|        +------+|\n"\
                      "+----------------+\n";
  Pic pic(init,3);
  Frame frame(pic);
  And andrule(pic,frame);
  Or orrule(frame,andrule);
  Frame frame1(orrule);

  EXPECT_STREQ(result,frame1.exec().GetStr().c_str());
}

小结

这道题目可以用不同思路可以实现,每个人都会有不同选择。DSL AST是一种不错的选择。
利用题目演练自己掌握的技能,是一种自我修炼加强的方式,值得推荐。你也可以试试。


原创,转载引用请注明出处。谢谢!

相关文章

网友评论

本文标题:使用DSL进行TDD演练

本文链接:https://www.haomeiwen.com/subject/cjtucftx.html