Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. /////////////////////////////1 request validator
  2. function solve(obj) {
  3. const validMethods = ["GET", "POST", "DELETE", "CONNECT"];
  4. const uriRegex = /^([A-Za-z0-9.]+)$/gm; //проверка на uri...
  5. const validVersions = ["HTTP/0.9", "HTTP/1.0", "HTTP/1.1", "HTTP/2.0"];
  6. const messageRegex = /^[\w+\d+]([^\<\>\\\&\'\"]+)$/gm;
  7.  
  8.  
  9. let validMethod = obj.method && validMethods.includes(obj.method);
  10. let validUri = obj.uri && (obj.uri.match(uriRegex) || obj.uri === '*');
  11. let validVersion = obj.version && (validVersions.includes(obj.version));
  12. let validMessage = obj.message !== undefined ? (obj.message === "" || (obj.message.match(messageRegex))) :false;
  13.  
  14. if (!validMethod) { // проверка дали съществуват пропъртита...
  15. throw new Error("Invalid request header: Invalid Method");
  16. }
  17.  
  18. if (!validUri) {
  19. throw new Error("Invalid request header: Invalid URI");
  20. }
  21.  
  22. if (!validVersion) {
  23. throw new Error("Invalid request header: Invalid Version");
  24. }
  25.  
  26. if (!validMessage) {
  27. throw new Error("Invalid request header: Invalid Message");
  28. }
  29.  
  30. return obj;
  31. }
  32. //////////////////////////////// 2 even or odd
  33. describe('isOddOrEven', function () {
  34. it('with a number parameter, should return undefined', function () {
  35. expect(isOddOrEven(13)).to.equal(undefined, "Function did not return the correct result!");
  36. });
  37. it('with a object parameter, should return undefined', function () {
  38. isOddOrEven({name: 'pesho'}).should.equal(undefined, "Function did not return the correct result!");
  39. });
  40. it('with an even length string should return "even"', function () {
  41. assert.equal(isOddOrEven("roar"), "even", "Function did not return correct result!");
  42. });
  43. it('with an odd length string, should return "odd"', function () {
  44. expect(isOddOrEven("pesho")).to.equal("odd", "Function did not return correct result!");
  45. });
  46. it('with multiple consecutive checks, should return correct values', function () {
  47. expect(isOddOrEven("cat")).to.equal('odd', "Function did not return correct result!");
  48. expect(isOddOrEven("alabala")).to.equal("odd", "Function did not return correct result!");
  49. expect(isOddOrEven("is it even")).to.equal("even", "Function did not return correct result!");
  50. });
  51. });
  52. //////////////////////////////// 03 char lookuo
  53. describe("lookupChar", function () {
  54. it('with a non-string first parameter should return undefined', function () {
  55. expect(lookupChar(13, 0)).to.equal(undefined, "The function did not return correct result");
  56. });
  57. it('with a non-string second parameter should return undefined', function () {
  58. expect(lookupChar("pesho", "gosho")).to.equal(undefined, "The function did not return correct result");
  59. });
  60. it('with a floating point number second parameter should return undefined', function () {
  61. expect(lookupChar("pesho", 3.12)).to.equal(undefined, "The function did not return correct result");
  62. });
  63. it('with an incorrect index value should return incorrect index', function () {
  64. expect(lookupChar("gosho", 13)).to.equal("Incorrect index", "The function did not return correct result");
  65. });
  66. it('with a negative index value should return incorrect index', function () {
  67. expect(lookupChar("stamat", -1)).to.equal("Incorrect index", "The function did not return correct result");
  68. });
  69. it('with an index value equal to string length, should return incorrect index', function () {
  70. expect(lookupChar("pesho", 5)).to.equal("Incorrect index", "The function did not return correct result");
  71. });
  72. it('with correct parameters, should return correct value', function () {
  73. expect(lookupChar("pesho", 0)).to.equal("p", "The function did not return correct result");
  74. });
  75. it('with correct parameters should return correct value', function () {
  76. expect(lookupChar("stamat", 3)).to.equal("m", "The function did not return correct result");
  77. });
  78. });
  79. ////////////////////////////////04. Math Enforcer
  80. describe("mathEnforcer", function () {
  81. describe('addFive', function () {
  82. it("should return undefined for non-number parameter",function () {
  83. expect(mathEnforcer.addFive("5")).to.be.equal(undefined);
  84. });
  85. it("should return correct result for positive integer parameter", function () {
  86. expect(mathEnforcer.addFive(10)).to.be.equal(15);
  87. });
  88. it("should return correct result for negative integer parameter", function () {
  89. expect(mathEnforcer.addFive(-5)).to.be.equal(0);
  90. });
  91. it("should return correct result for floating point parameter", function () {
  92. expect(mathEnforcer.addFive(3.14)).to.be.closeTo(8.14, 0.01);
  93. });
  94. });
  95.  
  96. describe('subtractTen', function () {
  97. it("should return undefined for non-number parameter",function () {
  98. expect(mathEnforcer.subtractTen("5")).to.be.equal(undefined);
  99. });
  100. it("should return correct result for positive integer parameter", function () {
  101. expect(mathEnforcer.subtractTen(10)).to.be.equal(0);
  102. });
  103. it("should return correct result for negative integer parameter", function () {
  104. expect(mathEnforcer.subtractTen(-5)).to.be.equal(-15);
  105. });
  106. it("should return correct result for floating point parameter", function () {
  107. expect(mathEnforcer.subtractTen(3.14)).to.be.closeTo(-6.86, 0.01);
  108. });
  109. });
  110.  
  111. describe('sum', function () {
  112. it("should return undefined for non-number first parameter", function () {
  113. expect(mathEnforcer.sum("5", 5)).to.be.equal(undefined);
  114. });
  115. it("should return undefined for non-number second parameter", function () {
  116. expect(mathEnforcer.sum(5, "5")).to.be.equal(undefined);
  117. });
  118. it("should return correct result for integer parameters", function () {
  119. expect(mathEnforcer.sum(5, -3)).to.be.equal(2);
  120. });
  121. it("should return correct result for floating point parameters", function () {
  122. expect(mathEnforcer.sum(2.7, 3.4)).to.be.closeTo(6.1, 0.01);
  123. })
  124. })
  125. });
  126. ////////////////////////////////06. Payment Package
  127. describe("PaymentPackage", function(){
  128. it("constructor with 2 params", function() {
  129. let actual = new PaymentPackage("str", 5);
  130.  
  131. assert.equal("str", actual.name);
  132. assert.equal(5, actual.value);
  133. assert.equal(20, actual.VAT);
  134. assert.equal(true, actual.active)
  135. });
  136.  
  137. it("constructor with valid params", function() {
  138. var actual = new PaymentPackage("str", 5);
  139. assert.equal(actual.value, 5);
  140. assert.equal(actual.name, "str");
  141. assert.equal(actual.VAT, 20);
  142. assert.equal(actual.active, true);
  143. });
  144.  
  145. it("constructor with 1 params", function() {
  146. assert.throws(() => new PaymentPackage("str"), Error, 'Value must be a non-negative number');
  147. });
  148.  
  149. it("constructor with 1 params", function() {
  150. assert.throws(() => new PaymentPackage(5), Error, 'Name must be a non-empty string');
  151. });
  152.  
  153. it("constructor with no params", function() {
  154. assert.throws(() => new PaymentPackage(), Error, 'Name must be a non-empty string');
  155. });
  156.  
  157. it("name get/set", function() {
  158. let actual = new PaymentPackage("str", 5);
  159. assert.equal("str", actual.name);
  160.  
  161. actual.name = "new";
  162. assert.equal("new", actual.name);
  163. });
  164.  
  165. it("non-string name get/set", function() {
  166. let actual = new PaymentPackage("str", 5);
  167. assert.equal("str", actual.name);
  168.  
  169. assert.throws(() => actual.name = true, Error, 'Name must be a non-empty string');
  170. });
  171.  
  172. it("empty name get/set", function() {
  173. let actual = new PaymentPackage("str", 5);
  174. assert.equal("str", actual.name);
  175.  
  176. assert.throws(() => actual.name = "", Error, 'Name must be a non-empty string');
  177. });
  178.  
  179. it("value get/set", function() {
  180. let actual = new PaymentPackage("str", 5);
  181. assert.equal(5, actual.value);
  182.  
  183. actual.value = 10;
  184. assert.equal(10, actual.value);
  185. });
  186.  
  187. it("negative value get/set", function() {
  188. let actual = new PaymentPackage("str", 5);
  189. assert.equal("str", actual.name);
  190.  
  191. assert.throws(() => actual.value = -5, Error, 'Value must be a non-negative number');
  192. });
  193.  
  194. it("non-number value get/set", function() {
  195. let actual = new PaymentPackage("str", 5);
  196. assert.equal("str", actual.name);
  197.  
  198. assert.throws(() => actual.value = "aa", Error, 'Value must be a non-negative number');
  199. });
  200.  
  201. it("zero value get/set", function() {
  202. let actual = new PaymentPackage("str", 5);
  203. actual.value = 0;
  204.  
  205. assert.equal(actual.value, 0);
  206. });
  207.  
  208. it("non-number VAL get/set", function() {
  209. let actual = new PaymentPackage("str", 5);
  210.  
  211. assert.throws(() => actual.VAT = "a", Error, 'VAT must be a non-negative number');
  212. });
  213.  
  214. it("negative VAL get/set", function() {
  215. let actual = new PaymentPackage("str", 5);
  216.  
  217. assert.throws(() => actual.VAT = -5, Error, 'VAT must be a non-negative number');
  218. });
  219.  
  220. it("zero VAL get/set", function() {
  221. let actual = new PaymentPackage("str", 5);
  222. actual.VAT = 0;
  223.  
  224. assert.equal(actual.VAT, 0);
  225. });
  226.  
  227. it("boolean active get/set", function() {
  228. let actual = new PaymentPackage("str", 5);
  229. actual.active = false;
  230. assert.equal(actual.active, false);
  231. });
  232.  
  233. it("non-boolean active get/set", function() {
  234. let actual = new PaymentPackage("str", 5);
  235.  
  236. assert.throws(() => actual.active = 5, Error, 'Active status must be a boolean');
  237. });
  238.  
  239. it("toString() with active", function() {
  240. let actual = new PaymentPackage("str", 5);
  241.  
  242. assert.equal(actual.toString(), "Package: str\n- Value (excl. VAT): 5\n- Value (VAT 20%): 6");
  243. });
  244.  
  245. it("toString() with inactive", function() {
  246. let actual = new PaymentPackage("str", 5);
  247. actual.active = false;
  248.  
  249. assert.equal(actual.toString(), "Package: str (inactive)\n- Value (excl. VAT): 5\n- Value (VAT 20%): 6");
  250. });
  251. });
  252. ////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement