<commit_before>package dotty.tools.dotc.printing

import dotty.tools.io.JFile
import org.junit.Test
import org.junit.Assert.fail

import scala.io.Source

/** Runs all tests contained in `compiler/test-resources/printing/`
  * To check test cases, you can use "cat" or "less -r" from bash*/
class SyntaxHighlightingTests {

  private def scripts(path: String): Array[JFile] = {
    val dir = new JFile(getClass.getResource(path).getPath)
    assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
    dir.listFiles
  }

  private def testFile(f: JFile): Unit = {
    val linesIt = Source.fromFile(f).getLines()
    val input = linesIt.takeWhile(_ != "result:").mkString("\n")
    val expectedOutput = linesIt.mkString("\n")
    val actualOutput = SyntaxHighlighting(input).mkString

    if (expectedOutput != actualOutput) {
      println("Expected output:")
      println(expectedOutput)
      println("Actual output:")
      println(actualOutput)

      fail(s"Error in file $f, expected output did not match actual")
    }
  }

  @Test def syntaxHighlight = scripts("/printing").foreach(testFile)
}<commit_msg>Added generator method for test files<commit_after>package dotty.tools.dotc.printing

import java.io.PrintWriter

import dotty.tools.io.JFile
import org.junit.Assert.fail
import org.junit.Test

import scala.io.Source

/** Runs all tests contained in `compiler/test-resources/printing/`
  * To check test cases, you can use "cat" or "less -r" from bash
  * To generate test files you can call the generateTestFile method*/
class SyntaxHighlightingTests {

  private def scripts(path: String): Array[JFile] = {
    val dir = new JFile(getClass.getResource(path).getPath)
    assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
    dir.listFiles
  }

  private def testFile(f: JFile): Unit = {
    val lines = Source.fromFile(f).getLines()
    val input = lines.takeWhile(_ != "result:").mkString("\n")
    val expectedOutput = lines.mkString("\n")
    val actualOutput = SyntaxHighlighting(input).mkString

    if (expectedOutput != actualOutput) {
      println("Expected output:")
      println(expectedOutput)
      println("Actual output:")
      println(actualOutput)

      // Call generateTestFile when you want to update a test file
      // or generate a test from a scala source file
      // if (f.getName == "nameOfFileToConvertToATest") generateTestFile()

      fail(s"Error in file $f, expected output did not match actual")
    }

    /** Writes `input` and `actualOutput` to the current test file*/
    def generateTestFile(): Unit = {
      val path = "compiler/test-resources/printing/" + f.getName
      new PrintWriter(path) {
        write(input + "\nresult:\n" + actualOutput)
        close()
      }
      println(s"Test file for ${f.getName} has been generated")
    }
  }

  @Test def syntaxHighlight = scripts("/printing").foreach(testFile)
}
{'fraction_non_alphanumeric': 0.11803165863730214, 'fraction_numerical': 0.0, 'mean_word_length': 3.563579277864992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5712652', 'n_tokens_mistral': 892, 'n_tokens_neox': 857, 'n_words': 245}