没有不值得去解决的问题,也没有不值得去学习的技术!

Building a Test Infrastructure: Writing Sample Code for Testing in Refactoring: Improving the Design of Existing Code (2nd Edition)

A piece of code to be tested. This code comes from a simple application that allows users to view and adjust production plans. Its (somewhat rough) interface looks like the image below

1. A piece of code to be tested. This code comes from a simple application that allows users to view and adjust production plans. Its (somewhat rough) interface looks like the image below. Figure 1

A piece of code to be tested. This code comes from a simple application that allows users to view and adjust production plans. Its (somewhat rough) interface looks like the image below
Figure 1

2. The final implementation of the example code is as follows. The entry productionPlan.html file code is as follows

Plaintext
    
    Building a Test System - Example Code to be Tested
    
    
    






3. The imported module productionPlan.js file code is as follows

JavaScript

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 {
    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;
    }

    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);
    }

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

    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);
    }
}

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;
    }
}

const asia = new Province(sampleProvinceData());
console.log(asia.shortfall);

3. The code const asia = new Province(sampleProvinceData()); references: 4.3 The First Test Example. Figure 2

The code const asia = new Province(sampleProvinceData()); references: 4.3 The First Test Example
Figure 2

4. Open http://localhost/refactoring/4/productionPlan.html in the browser and check the console output: 5, which meets expectations. Figure 3

Open http://localhost/refactoring/4/productionPlan.html in the browser and check the console output: 5, which meets expectations
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系