caderek / aocrunner

Advent of Code runner
ISC License
163 stars 23 forks source link

Template string multiline does not work on test input #6

Closed philippmossier closed 2 years ago

philippmossier commented 2 years ago

Hi,

let me say thank you for this package first. I'm really enjoying it. I found one issue with test input when using template literals. It looks like it adds some wierd whitespace or uses something als instead of \n when using multiline strings. Had this issue on day03 part1.

part1: {
    tests: [
      {
       // works:
        input: `00100\n11110\n10110\n10111\n10101\n01111\n00111\n11100\n10000\n11001\n00010\n01010`,

        // does not work: (multiline string)
        input: `00100
        11110
        10110
        10111
        10101
        01111
        00111
        11100
        10000
        11001
        00010
        01010`,
        expected: 198,
      },
    ],
    solution: part1,
  },
GustavPersson commented 2 years ago

Hi!

The test input worked for me on that day. It seems you have a lot of spaces or tabs in your test data, is that there in your actual code as well? Because that will be part of the string literal and add "weird whitespace". Try removing all spaces before the lines after the first.

caderek commented 2 years ago

It has to do with how trimTestInput option works - it will trim empty lines at the beginning and the end of the input and will remove the indents only if the indents are the same, so you can nicely format the input, while not messing deliberate differences in indentation. In your example, the first line of the test input is not indented as far as JS template string is concerned.

In the JS template strings all white spaces are a part of the string, so without the trimTestInput option the input would have to look like this:

run({
  part1: {
    tests: [
      {
        input: `00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010`,
        expected: 150,
      },
    ],
    solution: part1,
  },
  trimTestInputs: false,
})

/*
Produces input:

00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
*/

With trimTestInput: true it can look like this:

run({
  part1: {
    tests: [
      {
        input: `
          00100
          11110
          10110
          10111
          10101
          01111
          00111
          11100
          10000
          11001
          00010
          01010
        `,
        expected: 150,
      },
    ],
    solution: part1,
  },
  trimTestInputs: true,
})

/*
Produces input:

00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
*/

But not like this:

run({
  part1: {
    tests: [
      {
        input: `00100
        11110
        10110
        10111
        10101
        01111
        00111
        11100
        10000
        11001
        00010
        01010
        `,
        expected: 150,
      },
    ],
    solution: part1,
  },
  part2: {
    tests: [
      // { input: ``, expected: "" },
    ],
    solution: part2,
  },
  trimTestInputs: true,
})

/*
Produces input:

00100
        11110
        10110
        10111
        10101
        01111
        00111
        11100
        10000
        11001
        00010
        01010
*/
caderek commented 2 years ago

I added a relevant section to the README: https://github.com/caderek/aocrunner#trimtestinputs

Closing, let me know if something is not clear.

PS: There is a new handy feature, so I recommend you to update the runner ;)