Sample Code to Test (โค้ดตัวอย่างที่จะนำมาเทสต์)

นี่คือ code ที่จะดูและ test กัน code นี้รองรับแอปพลิเคชันง่าย ๆ ที่ให้ผู้ใช้ดูและปรับเปลี่ยน production plan (แผนการผลิต) UI (แบบหยาบ ๆ) ของมันหน้าตาประมาณนี้:

A screenshot of an application to examine and manipulate a production plan.

แอปพลิเคชัน production plan มีชื่อว่า ‘Province: Asia’ โดยกรอกค่า 30 และ 20 ลงใน field demand และ price ตามลำดับ ถัดลงมาเป็น list ของ producer สามราย: ใน Byzantium กรอกค่า 10 และ 9 ลงใน field cost และ production ตามลำดับ และ revenue รวมแสดงเป็น 90 ใน Attalia กรอกค่า 12 และ 10 ลงใน field cost และ production ตามลำดับ และ revenue รวมแสดงเป็น 120 ใน Sinope กรอกค่า 10 และ 6 ลงใน field cost และ production ตามลำดับ และ revenue รวมแสดงเป็น 60 ที่ด้านล่าง shortfall แสดงค่าเป็น 5 และกำไร (profit) แสดงค่าเป็น 230

production plan จะมี demand และ price สำหรับแต่ละ province แต่ละ province จะมี producer ซึ่งแต่ละรายผลิตสินค้าได้จำนวนหนึ่งที่ราคาต่อหน่วยระดับหนึ่ง UI ยังแสดงว่า producer แต่ละรายจะได้ revenue เท่าไร ถ้าขายผลผลิตของตัวเองได้หมด ที่ด้านล่าง หน้าจอแสดง shortfall ของการผลิต (demand ลบด้วย total production) และกำไรของแผนนี้ UI เปิดให้ผู้ใช้ปรับ demand, price รวมถึง production และ cost ของ producer แต่ละราย เพื่อดูผลกระทบต่อ production shortfall และกำไร เมื่อใดก็ตามที่ผู้ใช้เปลี่ยนตัวเลขบนหน้าจอ ตัวเลขอื่น ๆ ทั้งหมดจะอัปเดตทันที

ผมแสดง user interface ไว้ตรงนี้เพื่อให้คุณพอเห็นภาพว่า software ถูกใช้งานอย่างไร แต่ผมจะโฟกัสเฉพาะส่วน business logic ของ software—คือ class ที่คำนวณกำไรกับ shortfall ไม่ใช่โค้ดที่สร้าง HTML หรือโค้ดที่ผูกการเปลี่ยนแปลงของ field เข้ากับ business logic เบื้องหลัง บทนี้เป็นเพียงการแนะนำโลกของ self-testing code ดังนั้นการเริ่มจากกรณีที่ง่ายที่สุด จึงสมเหตุสมผล—นั่นคือโค้ดที่ไม่เกี่ยวข้องกับ user interface, persistence หรือการติดต่อกับ external service แต่การแยกแบบนี้เป็นความคิดที่ดีในทุกกรณีอยู่แล้ว: เมื่อ business logic แบบนี้เริ่มซับซ้อน ผมจะแยกมันออกจากกลไกของ UI เพื่อให้ใช้เหตุผลกับมันและ test มันได้ง่ายขึ้น

business logic code ส่วนนี้ประกอบด้วยสอง class: class หนึ่งแทน producer รายเดียว ส่วน class อีกตัวแทน province ทั้งหมด constructor ของ province รับ JavaScript object—ซึ่งเราพอจินตนาการได้ว่ามันถูกส่งมาจากเอกสาร JSON

นี่คือ code ที่โหลด province จากข้อมูล JSON:

class Province…

constructor(doc) {
  this._name = doc.name;
  this._producers = [];
  this._totalProduction = 0;
  this._demand = doc.demand;
  this._price = doc.price;
  doc.producers.forEach(d => this.addProducer(new Producer(this, d)));
}
addProducer(arg) {
  this._producers.push(arg);
  this._totalProduction += arg.production;
}

function นี้สร้างข้อมูล JSON ที่เหมาะสมกับงาน ผมสามารถสร้าง province ตัวอย่างสำหรับการ test ได้โดยการสร้าง province object จากผลลัพธ์ของ function นี้

top level…

function sampleProvinceData() {
  return {
    name: "Asia",
    producers: [
      {name: "Byzantium", cost: 10, production: 9},
      {name: "Attalia",   cost: 12, production: 10},
      {name: "Sinope",    cost: 10, production: 6},
    ],
    demand: 30,
    price: 20
  };
}

class Province มี accessor สำหรับค่าข้อมูลต่าง ๆ เหล่านี้:

class Province…

get name()    {return this._name;}
get producers() {return this._producers.slice();}
get totalProduction()    {return this._totalProduction;}
set totalProduction(arg) {this._totalProduction = arg;}
get demand()    {return this._demand;}
set demand(arg) {this._demand = parseInt(arg);}
get price()    {return this._price;}
set price(arg) {this._price = parseInt(arg);}

setter พวกนี้จะถูกเรียกด้วย string จาก UI ซึ่งมีตัวเลขอยู่ในนั้น ดังนั้นผมจึงต้อง parse ตัวเลขก่อน เพื่อให้สามารถนำไปใช้ในการคำนวณได้อย่างน่าเชื่อถือ

class Producer ส่วนใหญ่เป็นแค่ตัวเก็บข้อมูล (data holder) ธรรมดา:

class Producer…

constructor(aProvince, data) {
  this._province = aProvince;
  this._cost = data.cost;
  this._name = data.name;
  this._production = data.production || 0;
}
get name() {return this._name;}
get cost()    {return this._cost;}
set cost(arg) {this._cost = parseInt(arg);}

get production() {return this._production;}
set production(amountStr) {
  const amount = parseInt(amountStr);
  const newProduction = Number.isNaN(amount) ? 0 : amount;
  this._province.totalProduction += newProduction - this._production;
  this._production = newProduction;
}

วิธีที่ set production อัปเดต derived data ใน province นั้นดูเกะกะ และทุกครั้งที่ผมเห็นอะไรแบบนี้ ผมก็อยาก refactor เพื่อกำจัดมันทิ้ง แต่ผมต้องเขียน test ให้ผ่านเสียก่อน แล้วถึงจะ refactor มันได้

การคำนวณ shortfall นั้นง่ายมาก

class Province…

get shortfall() {
  return this._demand - this.totalProduction;
}

ส่วนการคำนวณกำไรนั้นซับซ้อนขึ้นมาอีกหน่อย

class Province…

get profit() {
  return this.demandValue - this.demandCost;
}
get demandCost() {
  let remainingDemand = this.demand;
  let result = 0;
  this.producers
    .sort((a,b) => a.cost - b.cost)
    .forEach(p => {
      const contribution = Math.min(remainingDemand, p.production);
        remainingDemand -= contribution;
        result += contribution * p.cost;
    });
  return result;
}
get demandValue() {
  return this.satisfiedDemand * this.price;
}
get satisfiedDemand() {
  return Math.min(this._demand, this.totalProduction);
}