How do you know if loop or double loop is used in a function when unit testing?

paul.kim1901

I am new to unit testing, and would like to check if any line of code to contain double loop specifically.

I am currently studying at bootcamp and they somehow check if my solution contains specific loop (like while loop) or if I used nested loop as below.

Test Code
function () {
    expect(DOUBLE_LOOP_EXP.test(funcBody)).to.be.equal(true);
  } 

To clarify, below is my function that's been tested through above criterion;

function makePermutations(str) {
  let result = '';
  for (let i = 0; i < str.length; i++) {
    for (let j = 0; j < str.length; j++) {
      result += str[i] + str[j] + ',';
    }
  }
  return result.slice(0, result.length - 1);
}
module.exports = makePermutations;

Unfortunately, there's no way to know whats inside that DOUBLE_LOOP_EXP.test. Please advise.

Estus Flask

A regexp can be something like:

/for\s*\([^;]+;[^;]+;[^;]+\)\s*{?\s*for\s*\([^;]+;[^;]+;[^;]+\)/

It's incorrect to use regexps for this because they are unaware of language syntax and can result in false positive or negative, e.g. match comments or get stuck on them.

This requires to parse JavaScript syntax. E.g. with this library that allows to query AST generated by popular Esprima parser (a playground):

const ast = esprima.parse(makePermutations.toString());
const selector = esquery.parse('ForStatement ForStatement');
const nestedFor = esquery.match(ast, selector);

expect(nestedFor.length).toBe(1);

And considering that the question applies to testing, there's never a need to so this in unit tests - unless you test a compiler that generated this code.

There may be a need to assert implementation details, but not to this degree. From unit test perspective, it doesn't matter if there are for loops. If the function doesn't involve other units and doesn't do side effects, the test should be performed by specifying expected input and output:

expect(makePermutations('abc')).toBe('aa,ab,ac,ba,bb,bc,ca,cb,cc');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you know which components to import when unit testing?

Do you know how to loop this?

How do you await a Task when unit testing?

How do I know when the last asynchronous function has returned results in the "for key in ob" loop?

how do you mock an xml for unit testing?

How do you pass a value back to generator function when using for of loop

How to change values in OnTap Function when used in a for loop in Flutter

Unit testing: How much should you cover (when whitebox testing)?

How do you derive a loop invariant from a function?

How do you prompt a user and have conditional validation in a loop for a function?

How do you vectorise a loop?

How do you refer to the main "this" when inside a loop?

How do you get to the value inside of a python function that returns nothing for unit testing?

How do you stub private functions when unit testing a revealing module

Unit Testing With/Without hardward in the loop

How does a pytorch dataset object know whether it has hit the end when used in a for loop?

How to do a specific double "for" loop to do a calcul

How to know when a for loop with NSURLSessionDataTasks is complete

Do you know a way to change the loop for something more productive?

How do you do a for loop/for each in EJS?

How do you know whether to automate testing or use manual?

in a while loop how do you hold data so that when the loop ends it will output all data inputed and not just reset on the next while loop

How to do unit testing

Avoiding a double 'for' loop in function

How to do the equivalent of a double for loop append for SQL

What is a parallel for loop, and how/when should it be used?

How do you know if your github golang repo is being used?

How to know if no more values are present in the array as you iterate the array in a loop

How to know you're at the last pass of a for loop through object?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive