From 76160d0824c2df8a3e137dcdb126b80a72f4774d Mon Sep 17 00:00:00 2001 From: Lipeng Jia Date: Wed, 12 Jan 2022 04:24:14 +0800 Subject: [PATCH] test the basic part --- __tests__/pieceOfCake/array_spec.js | 28 +++++++---- __tests__/pieceOfCake/boolean_spec.js | 34 +++++++++++-- __tests__/pieceOfCake/function_spec.js | 43 +++++++++-------- __tests__/pieceOfCake/numbers_spec.js | 5 +- __tests__/pieceOfCake/object_spec.js | 66 ++++++++++++++------------ __tests__/pieceOfCake/strings_spec.js | 27 +++++++---- __tests__/pieceOfCake/variable_spec.js | 6 +-- 7 files changed, 131 insertions(+), 78 deletions(-) diff --git a/__tests__/pieceOfCake/array_spec.js b/__tests__/pieceOfCake/array_spec.js index 0af9ff8..ed09bec 100644 --- a/__tests__/pieceOfCake/array_spec.js +++ b/__tests__/pieceOfCake/array_spec.js @@ -4,7 +4,7 @@ describe('for array', () => { // <--start // Please write down the correct result. You should write the result directly. - const expected = undefined; + const expected = 3; // --end-> expect(array[2]).toEqual(expected); @@ -15,7 +15,9 @@ describe('for array', () => { // <--start // Please write one line of code to push some elements in the array to pass the test - + // const newArray=[6,7,8]; + // array.push(...newArray); + array.push(6, 7, 8); // --end-> expect(array).toEqual([1, 2, 3, 4, 5, 6, 7, 8]); @@ -27,7 +29,7 @@ describe('for array', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [9,1,2,3,4,5,10]; // --end-> expect(newArray).toEqual(expected); @@ -38,8 +40,8 @@ describe('for array', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedRow = undefined; - const expectedColumn = undefined; + const expectedRow = 2; + const expectedColumn = 3; // --end-> expect(row).toEqual(expectedRow); @@ -52,7 +54,7 @@ describe('for array', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [2,4]; // --end-> expect(filtered).toEqual(expected); @@ -64,7 +66,13 @@ describe('for array', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [ + 'Unit 1 for element at index 0', + 'Unit 2 for element at index 1', + 'Unit 3 for element at index 2', + 'Unit 4 for element at index 3', + 'Unit 5 for element at index 4', + ]; // --end-> expect(mapped).toEqual(expected); @@ -74,9 +82,13 @@ describe('for array', () => { const numbers = [1, 2, 3, 4, 5]; const reduced = numbers.reduce((prev, current) => prev + current); + // const initialValue=100; + // const reducedWithInitialValue = numbers.reduce((prev, current) => prev + current,initialValue); + // console.log(reducedWithInitialValue); + // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 15; // --end-> expect(reduced).toEqual(expected); diff --git a/__tests__/pieceOfCake/boolean_spec.js b/__tests__/pieceOfCake/boolean_spec.js index 7521cc3..c93658e 100644 --- a/__tests__/pieceOfCake/boolean_spec.js +++ b/__tests__/pieceOfCake/boolean_spec.js @@ -1,16 +1,34 @@ describe('for boolean type', () => { it('should convert to same type then compare for equality operator', () => { + //https://github.com/mqyqingfeng/Blog/issues/41 + const objectLeft = { key: 'value' }; const objectRight = { key: 'value' }; const actual = [ // eslint-disable-next-line no-self-compare, eqeqeq, yoda - 1 == 1, '1' == 1, 1 == '1', 0 == false, 0 == null, objectLeft == objectRight, 0 == undefined, null == undefined, + 1 == 1, + '1' == 1, + 1 == '1', + 0 == false, + 0 == null, + objectLeft == objectRight, + 0 == undefined, + null == undefined, //??? ]; // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [ + true, + true, + true, + true, + false, + false, + false, + true, + ]; // --end-> expect(actual).toEqual(expected); @@ -22,12 +40,20 @@ describe('for boolean type', () => { const actual = [ // eslint-disable-next-line no-self-compare, eqeqeq, yoda - 3 === 3, 3 === '3', objectLeft === objectRight, null === undefined, + 3 === 3, + 3 === '3', + objectLeft === objectRight, + null === undefined, ]; // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [ + true, + false, + false, + false + ]; // --end-> expect(actual).toEqual(expected); diff --git a/__tests__/pieceOfCake/function_spec.js b/__tests__/pieceOfCake/function_spec.js index 943e3db..594bf3a 100644 --- a/__tests__/pieceOfCake/function_spec.js +++ b/__tests__/pieceOfCake/function_spec.js @@ -11,7 +11,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Hello World'; // --end-> expect(outerFunction()).toEqual(expected); @@ -26,7 +26,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Hello World'; // --end-> expect(greeting(sayHello, 'World')).toEqual(expected); @@ -37,7 +37,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 36; // --end-> expect(square(6, 'Hello', 4)).toEqual(expected); @@ -51,8 +51,8 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedForSingleArgument = undefined; - const expectedForTwoArguments = undefined; + const expectedForSingleArgument = -5; + const expectedForTwoArguments = 2; // --end-> expect(minus(5)).toEqual(expectedForSingleArgument); @@ -70,7 +70,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 16; // --end-> expect(power(4)).toEqual(expected); @@ -89,8 +89,8 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedReturnValue = undefined; - const expectedWord = undefined; + const expectedReturnValue = 'Changed'; + const expectedWord = 'Origin'; // --end-> expect(returnValue).toEqual(expectedReturnValue); @@ -110,10 +110,12 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedName = undefined; - const expectedReturnValueName = undefined; + const expectedName = 'Bob'; + const expectedReturnValueName = 'Bob'; // --end-> + // console.log(returnValue===person); + expect(person.name).toEqual(expectedName); expect(returnValue.name).toEqual(expectedReturnValueName); }); @@ -128,7 +130,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Hello'; // --end-> expect(actual).toEqual(expected); @@ -145,7 +147,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Changed'; // --end-> expect(guessIfIAmChanged).toEqual(expected); @@ -163,10 +165,9 @@ describe('for function', () => { return find(1, '1'); } - // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = '(((1 * 3) + 5) * 3)'; // --end-> expect(findSolution(24)).toEqual(expected); @@ -183,7 +184,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 6; // --end-> expect(sum(1, 2, 3)).toEqual(expected); @@ -200,7 +201,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 6; // --end-> const parameters = [1, 2, 3]; @@ -223,7 +224,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 25; // --end-> expect(actual).toEqual(expected); @@ -238,7 +239,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [0,3,6]; // --end-> expect(labels).toEqual(expected); @@ -253,7 +254,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = false; // --end-> expect(greaterThan10(3)).toEqual(expected); @@ -269,7 +270,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 11; // --end-> expect(actual).toEqual(expected); @@ -289,7 +290,7 @@ describe('for function', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [0,2,4]; // --end-> expect(logs).toEqual(expected); diff --git a/__tests__/pieceOfCake/numbers_spec.js b/__tests__/pieceOfCake/numbers_spec.js index 0594b8f..e6cfbbf 100644 --- a/__tests__/pieceOfCake/numbers_spec.js +++ b/__tests__/pieceOfCake/numbers_spec.js @@ -4,7 +4,8 @@ describe('for numbers', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 0.75; + //1? // --end-> expect(dividingResult).toEqual(expected); @@ -15,7 +16,7 @@ describe('for numbers', () => { // <--start // Please write an expression determine if `notNumber` is NaN. - const isNan = undefined; + const isNan = isNaN(notNumber); // --end-> expect(isNan).toBeTruthy(); diff --git a/__tests__/pieceOfCake/object_spec.js b/__tests__/pieceOfCake/object_spec.js index 2ec3e55..e6bee2d 100644 --- a/__tests__/pieceOfCake/object_spec.js +++ b/__tests__/pieceOfCake/object_spec.js @@ -4,7 +4,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedName = undefined; + const expectedName = 'Bob'; // --end-> expect(person.name).toEqual(expectedName); @@ -17,7 +17,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = {}; + const expected = undefined; // --end-> expect(person.whatTheHellIsThat).toEqual(expected); @@ -28,7 +28,7 @@ describe('for object', () => { // <--start // Please write a line of code to remove the `name` property. - + delete person.name; // --end-> expect(person.name).toBeUndefined(); @@ -41,7 +41,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = ['name','yearOfBirth']; // --end-> expect(Object.keys(person)).toEqual(expected); @@ -58,7 +58,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Hello, I am John'; // --end-> expect(person.greeting()).toEqual(expected); @@ -70,7 +70,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Bob'; // --end-> expect(name).toEqual(expected); @@ -82,7 +82,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = "{\"name\":\"Bob\",\"yearOfBirth\":2019}"; // --end-> expect(json).toEqual(expected); @@ -94,7 +94,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = {color:"Red",value:"#ff0000"}; // --end-> expect(color).toEqual(expected); @@ -108,7 +108,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'The white rabbit says Hello.'; // --end-> expect(rabbit.speak('Hello')).toEqual(expected); @@ -124,7 +124,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'The white rabbit says Hello.'; // --end-> expect(rabbit.speak('Hello')).toEqual(expected); @@ -139,7 +139,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = [0,2,3]; // --end-> expect(actual).toEqual(expected); @@ -150,7 +150,8 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = Object.getPrototypeOf({}); + //??? // --end-> expect(Object.getPrototypeOf(emptyObject)).toBe(expected); @@ -161,7 +162,8 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = null; + ///??? // --end-> expect(Object.getPrototypeOf(objectPrototype)).toEqual(expected); @@ -178,7 +180,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'The killer rabbit says SKREEEE.'; // --end-> expect(words).toEqual(expected); @@ -193,7 +195,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'The weird rabbit says ?_?.'; // --end-> expect(rabbit.speak('?_?')).toEqual(expected); @@ -202,6 +204,7 @@ describe('for object', () => { const rabbitFunctionPrototype = Rabbit.prototype; const prototypeOfRabbitFunction = Object.getPrototypeOf(Rabbit); const functionPrototype = Function.prototype; + //??? // <--start // Please write down the correct value. You should choose the correct value from: @@ -209,8 +212,8 @@ describe('for object', () => { // * rabbitFunctionPrototype // * prototypeOfRabbitFunction // * functionPrototype - const expectedPrototypeOfRabbitInstance = undefined; - const expectedPrototypeOfRabbitFunction = undefined; + const expectedPrototypeOfRabbitInstance = rabbitFunctionPrototype; + const expectedPrototypeOfRabbitFunction = functionPrototype; // --end-> expect(prototypeOfRabbitInstance).toBe(expectedPrototypeOfRabbitInstance); @@ -228,7 +231,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'The white rabbit says Hi.'; // --end-> expect(rabbit.speak('Hi')).toEqual(expected); @@ -241,7 +244,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedKillerRabbitTeeth = undefined; + const expectedKillerRabbitTeeth = 'small'; // --end-> expect(killerRabbit.teeth).toEqual(expectedKillerRabbitTeeth); @@ -250,8 +253,9 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedOverrideKillerRabbitTeeth = undefined; - const expectedRabbitPrototypeTeeth = undefined; + const expectedOverrideKillerRabbitTeeth = 'sharp'; + const expectedRabbitPrototypeTeeth = 'small'; + //??? // --end-> expect(killerRabbit.teeth).toEqual(expectedOverrideKillerRabbitTeeth); @@ -266,7 +270,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedKillerRabbitSpeak = undefined; + const expectedKillerRabbitSpeak = 'Hi'; // --end-> expect(killerRabbit.speak()).toEqual(expectedKillerRabbitSpeak); @@ -276,8 +280,8 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedOverrideKillerRabbitSpeak = undefined; - const expectedRabbitPrototypeSpeak = undefined; + const expectedOverrideKillerRabbitSpeak = '@_@'; + const expectedRabbitPrototypeSpeak = 'Hi'; // --end-> expect(killerRabbit.speak()).toEqual(expectedOverrideKillerRabbitSpeak); @@ -297,14 +301,14 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedFahrenheit = undefined; + const expectedFahrenheit = 71.6; // --end-> expect(new Temperature(22).fahrenheit).toEqual(expectedFahrenheit); // <--start // Please write down the correct value. You should write the final result directly. - const expectedCelsius = undefined; + const expectedCelsius = 30; // --end-> expect(Temperature.fromFahrenheit(86).celsius).toEqual(expectedCelsius); @@ -325,7 +329,7 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedSpeak = undefined; + const expectedSpeak = 'I am crazy white rabbit'; // --end-> expect(rabbit.speak()).toEqual(expectedSpeak); @@ -346,28 +350,28 @@ describe('for object', () => { // <--start // Please write down the correct value. You should write the final result directly. - const rabbitIsRabbit = undefined; + const rabbitIsRabbit = true; // --end-> expect(rabbit instanceof Rabbit).toEqual(rabbitIsRabbit); // <--start // Please write down the correct value. You should write the final result directly. - const rabbitIsCrazyRabbit = undefined; + const rabbitIsCrazyRabbit = true; // --end--> expect(rabbit instanceof CrazyRabbit).toEqual(rabbitIsCrazyRabbit); // <--start // Please write down the correct value. You should write the final result directly. - const rabbitIsObject = undefined; + const rabbitIsObject = true; // --end--> expect(rabbit instanceof Object).toEqual(rabbitIsObject); // <--start // Please write down the correct value. You should write the final result directly. - const rabbitIsNumber = undefined; + const rabbitIsNumber = false; // --end--> expect(rabbit instanceof Number).toEqual(rabbitIsNumber); diff --git a/__tests__/pieceOfCake/strings_spec.js b/__tests__/pieceOfCake/strings_spec.js index 467a7c2..5f55632 100644 --- a/__tests__/pieceOfCake/strings_spec.js +++ b/__tests__/pieceOfCake/strings_spec.js @@ -6,8 +6,8 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expectedCharWithinRange = {}; - const expectedCharOutOfRange = {}; + const expectedCharWithinRange = 'e'; + const expectedCharOutOfRange = undefined; // --end-> expect(characterWithinRange).toEqual(expectedCharWithinRange); @@ -20,7 +20,7 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'Hello World'; // --end-> expect(template).toEqual(expected); @@ -31,7 +31,9 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'nut'; + // includes start position + // not includes end position // --end-> expect(string.slice(4, 7)).toEqual(expected); @@ -42,7 +44,7 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 5; // --end-> expect(string.indexOf('ut')).toEqual(expected); @@ -53,7 +55,7 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'coconuts'; // --end-> expect(string.trim()).toEqual(expected); @@ -65,7 +67,7 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = ['what','a','beautiful', '','','','day']; // --end-> expect(splitted).toEqual(expected); @@ -76,18 +78,25 @@ describe('for strings', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'what->a->beautiful->day'; // --end-> expect(splitted.join('->')).toEqual(expected); }); it('should be aware to the codepoint larger than 16-bit', () => { + // https://juejin.cn/post/6945801630990204964 const emoji = '🐴👟'; + // 'a'.length 1 + // '嗨'.length 1 + // '𠮷'.length 2 + // '💩'.length 2 + // '🤦🏻‍♂️'.length 7 + // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 4; // --end-> expect(emoji.length).toEqual(expected); diff --git a/__tests__/pieceOfCake/variable_spec.js b/__tests__/pieceOfCake/variable_spec.js index 6eb94ef..dcbc7e5 100644 --- a/__tests__/pieceOfCake/variable_spec.js +++ b/__tests__/pieceOfCake/variable_spec.js @@ -9,7 +9,7 @@ describe('for variable', () => { // --end-> // eslint-disable-next-line block-scoped-var - expect(i).toEqual(expected); + expect(i).toEqual(6); }); it('should have block scope for let and const variable', () => { @@ -20,7 +20,7 @@ describe('for variable', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 1000; // --end-> // eslint-disable-next-line no-undef @@ -33,7 +33,7 @@ describe('for variable', () => { // <--start // Please write down the correct value. You should write the final result directly. - const expected = undefined; + const expected = 'new name'; // --end-> expect(constVariable.name).toEqual(expected);