Advertisement
Guest User

06

a guest
Jul 4th, 2020
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class PaymentPackage {
  3.   constructor(name, value) {
  4.       this.name = name;
  5.       this.value = value;
  6.       this.VAT = 20;      // Default value        
  7.       this.active = true; // Default value
  8.   }
  9.  
  10.   get name() {
  11.       return this._name;
  12.   }
  13.  
  14.   set name(newValue) {
  15.       if (typeof newValue !== 'string') {
  16.           throw new Error('Name must be a non-empty string');
  17.       }
  18.       if (newValue.length === 0) {
  19.           throw new Error('Name must be a non-empty string');            
  20.       }
  21.       this._name = newValue;
  22.   }
  23.  
  24.   get value() {
  25.       return this._value;
  26.   }
  27.  
  28.   set value(newValue) {
  29.       if (typeof newValue !== 'number') {
  30.           throw new Error('Value must be a non-negative number');
  31.       }
  32.       if (newValue < 0) {
  33.           throw new Error('Value must be a non-negative number');            
  34.       }
  35.       this._value = newValue;
  36.   }
  37.  
  38.   get VAT() {
  39.       return this._VAT;
  40.   }
  41.  
  42.   set VAT(newValue) {
  43.       if (typeof newValue !== 'number') {
  44.           throw new Error('VAT must be a non-negative number');
  45.       }
  46.       if (newValue < 0) {
  47.           throw new Error('VAT must be a non-negative number');            
  48.       }
  49.       this._VAT = newValue;
  50.   }
  51.  
  52.   get active() {
  53.       return this._active;
  54.   }
  55.  
  56.   set active(newValue) {
  57.       if (typeof newValue !== 'boolean') {
  58.           throw new Error('Active status must be a boolean');
  59.       }
  60.       this._active = newValue;
  61.   }
  62.  
  63.   toString() {
  64.       const output = [
  65.           `Package: ${this.name}` + (this.active === false ? ' (inactive)' : ''),
  66.           `- Value (excl. VAT): ${this.value}`,
  67.           `- Value (VAT ${this.VAT}%): ${this.value * (1 + this.VAT / 100)}`
  68.       ];
  69.       return output.join('\n');
  70.   }
  71. };
  72.  
  73.  
  74.  
  75. // Judge link: https://judge.softuni.bg/Contests/Practice/Index/974#1
  76.  
  77. const assert = require("chai").assert;
  78. const expect = require("chai").expect;
  79.  
  80. // import * as PaymentPackage from "Payment Package func.mjs"    module extension must be " .mjs "
  81. //const PaymentPackage = require("./PaymentPackage");
  82.  
  83. describe("Tests for PaymentPackage class", function(){
  84.     let wrongVal = [NaN, {}, null, '', undefined];
  85.     describe("Tests for class name, if obj is instance of class and if all props are in place", function() {
  86.         let myObj = new PaymentPackage("Pesho", 5);
  87.         let className = myObj.constructor.toString().substring(0,20);        
  88.  
  89.         it("should have the same class name", function(){
  90.             assert.equal(className, "class PaymentPackage", "class Name is not the same");
  91.         });
  92.         it("should have all necessary props", function(){
  93.             expect(myObj).to.have.property("name", "Pesho");
  94.             expect(myObj).to.have.property("value", 5);
  95.             expect(myObj).to.have.property("VAT", 20);
  96.             expect(myObj).to.have.property("active", true);
  97.         });
  98.         it("should have toString method", function() {
  99.             expect(myObj).to.respondTo("toString");
  100.         });
  101.     });
  102.     describe("Tests for name prop setter & getter", function() {
  103.         let myObj = new PaymentPackage("Pesho", 5);
  104.  
  105.         it("should return the right name", function() {
  106.             assert.equal(myObj.name, "Pesho", "Name getter is incorrect");
  107.         });
  108.         it("should set the name correctly", function() {
  109.             myObj.name = "Gencho"
  110.             assert.equal(myObj.name, "Gencho", "name was not set correctly");
  111.         });
  112.         it("should throw error with incorrect values", function(){
  113.             wrongVal.forEach(val => {
  114.                 expect(() => myObj.name = val).to.throw("Name must be a non-empty string");
  115.             });
  116.         });
  117.     });
  118.     describe("Tests for value prop setter & getter", function() {
  119.         let myObj = new PaymentPackage("Pesho", 5);
  120.  
  121.         it("should get the value", function() {
  122.             assert.equal(myObj.value, 5, "Value getter is incorrect");
  123.         });
  124.         it("should set the value", function() {
  125.             myObj.value = 6
  126.             assert.equal(myObj.value, 6, "Value getter is incorrect");
  127.         });
  128.         it("should throw error with incorrect values", function() {
  129.             // apparently no one included assertion for NaN.
  130.             [...wrongVal, -1].slice(1).forEach(val => {
  131.                 expect(() => myObj.value = val).to.throw("Value must be a non-negative number");
  132.             });
  133.         });
  134.     });
  135.     describe("Tests for VAT prop setter & getter", function() {
  136.         let myObj = new PaymentPackage("Pesho", 5);
  137.  
  138.         it("should get the VAT", function() {
  139.             assert.equal(myObj.VAT, 20, "VAT getter is incorrect");
  140.         });
  141.         it("should set the VAT", function() {
  142.             myObj.VAT = 6
  143.             assert.equal(myObj.VAT, 6, "VAT setter is incorrect");
  144.         });
  145.         it("should throw error with incorrect values", function() {
  146.             // apparently no one included assertion for NaN.
  147.             [...wrongVal, -1].slice(1).forEach(val => {
  148.                 expect(() => myObj.VAT = val).to.throw("VAT must be a non-negative number");
  149.             });
  150.         });
  151.     });
  152.     describe("Tests for active status getter & setter", function() {
  153.         let myObj = new PaymentPackage("Pesho", 5);
  154.  
  155.         it("getter should work as expected", function(){
  156.             assert(myObj.active, true, "status getter should work as expected");
  157.         });
  158.         it("should work correct with booleans", function() {
  159.             expect(myObj.active = false, false, "setter should set false");
  160.         });
  161.         it("should throw error with incorrect values", function(){
  162.             wrongVal.forEach(val => {
  163.                 expect(() => myObj.active = val).to.throw("Active status must be a boolean");
  164.             });
  165.         });
  166.     });
  167.     describe("Tests for toString method", function () {
  168.         let myObj = new PaymentPackage("Pesho", 5);
  169.  
  170.         it("should work correctly with valid data", function() {
  171.             assert(myObj.toString(),"Package: Pesho\n- Value (excl. VAT): 5\n- Value (VAT 20%): 6",
  172.             "toString does not work correct with valid data");
  173.             myObj.active = false;
  174.             assert(myObj.toString(),"Package: Pesho inactive\n- Value (excl. VAT): 5\n- Value (VAT 20%): 6",
  175.             "toString does not work correct with valid data");
  176.         });
  177.     });
  178. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement