Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////1 request validator
- function solve(obj) {
- const validMethods = ["GET", "POST", "DELETE", "CONNECT"];
- const uriRegex = /^([A-Za-z0-9.]+)$/gm; //проверка на uri...
- const validVersions = ["HTTP/0.9", "HTTP/1.0", "HTTP/1.1", "HTTP/2.0"];
- const messageRegex = /^[\w+\d+]([^\<\>\\\&\'\"]+)$/gm;
- let validMethod = obj.method && validMethods.includes(obj.method);
- let validUri = obj.uri && (obj.uri.match(uriRegex) || obj.uri === '*');
- let validVersion = obj.version && (validVersions.includes(obj.version));
- let validMessage = obj.message !== undefined ? (obj.message === "" || (obj.message.match(messageRegex))) :false;
- if (!validMethod) { // проверка дали съществуват пропъртита...
- throw new Error("Invalid request header: Invalid Method");
- }
- if (!validUri) {
- throw new Error("Invalid request header: Invalid URI");
- }
- if (!validVersion) {
- throw new Error("Invalid request header: Invalid Version");
- }
- if (!validMessage) {
- throw new Error("Invalid request header: Invalid Message");
- }
- return obj;
- }
- //////////////////////////////// 2 even or odd
- describe('isOddOrEven', function () {
- it('with a number parameter, should return undefined', function () {
- expect(isOddOrEven(13)).to.equal(undefined, "Function did not return the correct result!");
- });
- it('with a object parameter, should return undefined', function () {
- isOddOrEven({name: 'pesho'}).should.equal(undefined, "Function did not return the correct result!");
- });
- it('with an even length string should return "even"', function () {
- assert.equal(isOddOrEven("roar"), "even", "Function did not return correct result!");
- });
- it('with an odd length string, should return "odd"', function () {
- expect(isOddOrEven("pesho")).to.equal("odd", "Function did not return correct result!");
- });
- it('with multiple consecutive checks, should return correct values', function () {
- expect(isOddOrEven("cat")).to.equal('odd', "Function did not return correct result!");
- expect(isOddOrEven("alabala")).to.equal("odd", "Function did not return correct result!");
- expect(isOddOrEven("is it even")).to.equal("even", "Function did not return correct result!");
- });
- });
- //////////////////////////////// 03 char lookuo
- describe("lookupChar", function () {
- it('with a non-string first parameter should return undefined', function () {
- expect(lookupChar(13, 0)).to.equal(undefined, "The function did not return correct result");
- });
- it('with a non-string second parameter should return undefined', function () {
- expect(lookupChar("pesho", "gosho")).to.equal(undefined, "The function did not return correct result");
- });
- it('with a floating point number second parameter should return undefined', function () {
- expect(lookupChar("pesho", 3.12)).to.equal(undefined, "The function did not return correct result");
- });
- it('with an incorrect index value should return incorrect index', function () {
- expect(lookupChar("gosho", 13)).to.equal("Incorrect index", "The function did not return correct result");
- });
- it('with a negative index value should return incorrect index', function () {
- expect(lookupChar("stamat", -1)).to.equal("Incorrect index", "The function did not return correct result");
- });
- it('with an index value equal to string length, should return incorrect index', function () {
- expect(lookupChar("pesho", 5)).to.equal("Incorrect index", "The function did not return correct result");
- });
- it('with correct parameters, should return correct value', function () {
- expect(lookupChar("pesho", 0)).to.equal("p", "The function did not return correct result");
- });
- it('with correct parameters should return correct value', function () {
- expect(lookupChar("stamat", 3)).to.equal("m", "The function did not return correct result");
- });
- });
- ////////////////////////////////04. Math Enforcer
- describe("mathEnforcer", function () {
- describe('addFive', function () {
- it("should return undefined for non-number parameter",function () {
- expect(mathEnforcer.addFive("5")).to.be.equal(undefined);
- });
- it("should return correct result for positive integer parameter", function () {
- expect(mathEnforcer.addFive(10)).to.be.equal(15);
- });
- it("should return correct result for negative integer parameter", function () {
- expect(mathEnforcer.addFive(-5)).to.be.equal(0);
- });
- it("should return correct result for floating point parameter", function () {
- expect(mathEnforcer.addFive(3.14)).to.be.closeTo(8.14, 0.01);
- });
- });
- describe('subtractTen', function () {
- it("should return undefined for non-number parameter",function () {
- expect(mathEnforcer.subtractTen("5")).to.be.equal(undefined);
- });
- it("should return correct result for positive integer parameter", function () {
- expect(mathEnforcer.subtractTen(10)).to.be.equal(0);
- });
- it("should return correct result for negative integer parameter", function () {
- expect(mathEnforcer.subtractTen(-5)).to.be.equal(-15);
- });
- it("should return correct result for floating point parameter", function () {
- expect(mathEnforcer.subtractTen(3.14)).to.be.closeTo(-6.86, 0.01);
- });
- });
- describe('sum', function () {
- it("should return undefined for non-number first parameter", function () {
- expect(mathEnforcer.sum("5", 5)).to.be.equal(undefined);
- });
- it("should return undefined for non-number second parameter", function () {
- expect(mathEnforcer.sum(5, "5")).to.be.equal(undefined);
- });
- it("should return correct result for integer parameters", function () {
- expect(mathEnforcer.sum(5, -3)).to.be.equal(2);
- });
- it("should return correct result for floating point parameters", function () {
- expect(mathEnforcer.sum(2.7, 3.4)).to.be.closeTo(6.1, 0.01);
- })
- })
- });
- ////////////////////////////////06. Payment Package
- describe("PaymentPackage", function(){
- it("constructor with 2 params", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal("str", actual.name);
- assert.equal(5, actual.value);
- assert.equal(20, actual.VAT);
- assert.equal(true, actual.active)
- });
- it("constructor with valid params", function() {
- var actual = new PaymentPackage("str", 5);
- assert.equal(actual.value, 5);
- assert.equal(actual.name, "str");
- assert.equal(actual.VAT, 20);
- assert.equal(actual.active, true);
- });
- it("constructor with 1 params", function() {
- assert.throws(() => new PaymentPackage("str"), Error, 'Value must be a non-negative number');
- });
- it("constructor with 1 params", function() {
- assert.throws(() => new PaymentPackage(5), Error, 'Name must be a non-empty string');
- });
- it("constructor with no params", function() {
- assert.throws(() => new PaymentPackage(), Error, 'Name must be a non-empty string');
- });
- it("name get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal("str", actual.name);
- actual.name = "new";
- assert.equal("new", actual.name);
- });
- it("non-string name get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal("str", actual.name);
- assert.throws(() => actual.name = true, Error, 'Name must be a non-empty string');
- });
- it("empty name get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal("str", actual.name);
- assert.throws(() => actual.name = "", Error, 'Name must be a non-empty string');
- });
- it("value get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal(5, actual.value);
- actual.value = 10;
- assert.equal(10, actual.value);
- });
- it("negative value get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal("str", actual.name);
- assert.throws(() => actual.value = -5, Error, 'Value must be a non-negative number');
- });
- it("non-number value get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal("str", actual.name);
- assert.throws(() => actual.value = "aa", Error, 'Value must be a non-negative number');
- });
- it("zero value get/set", function() {
- let actual = new PaymentPackage("str", 5);
- actual.value = 0;
- assert.equal(actual.value, 0);
- });
- it("non-number VAL get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.throws(() => actual.VAT = "a", Error, 'VAT must be a non-negative number');
- });
- it("negative VAL get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.throws(() => actual.VAT = -5, Error, 'VAT must be a non-negative number');
- });
- it("zero VAL get/set", function() {
- let actual = new PaymentPackage("str", 5);
- actual.VAT = 0;
- assert.equal(actual.VAT, 0);
- });
- it("boolean active get/set", function() {
- let actual = new PaymentPackage("str", 5);
- actual.active = false;
- assert.equal(actual.active, false);
- });
- it("non-boolean active get/set", function() {
- let actual = new PaymentPackage("str", 5);
- assert.throws(() => actual.active = 5, Error, 'Active status must be a boolean');
- });
- it("toString() with active", function() {
- let actual = new PaymentPackage("str", 5);
- assert.equal(actual.toString(), "Package: str\n- Value (excl. VAT): 5\n- Value (VAT 20%): 6");
- });
- it("toString() with inactive", function() {
- let actual = new PaymentPackage("str", 5);
- actual.active = false;
- assert.equal(actual.toString(), "Package: str (inactive)\n- Value (excl. VAT): 5\n- Value (VAT 20%): 6");
- });
- });
- ////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement