add splitlanguages tests (to test if html css and js are seperated)
parent
466a917698
commit
50a381a1c3
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import junit.framework.TestCase.assertEquals
|
import junit.framework.TestCase.assertEquals
|
||||||
|
import junit.framework.TestCase.assertTrue
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
class ParserTest {
|
class ParserTest {
|
||||||
|
|
||||||
|
@ -14,16 +16,43 @@ class ParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testTakeOnlyJs() {
|
fun testTakeOnlyJs() {
|
||||||
val fileToTest = "src/test/kotlin/HTMLFileWithScript.html"
|
val fileToTest = "src/test/resources/FileWithScript.html"
|
||||||
val expectedJsCode = """
|
val expectedJsCode = File("src/test/resources/OnlyTheScript.txt").readText().trimIndent().replace("\\s+".toRegex(), " ")
|
||||||
function greet() {
|
|
||||||
return "Hello from JavaScript!";
|
|
||||||
}
|
|
||||||
console.log(greet());""".trimIndent().replace("\\s+".toRegex(), " ")
|
|
||||||
|
|
||||||
val output = takeOnlyJs(fileToTest).trimIndent().replace("\\s+".toRegex(), " ")
|
val output = takeOnlyJs(fileToTest).trimIndent().replace("\\s+".toRegex(), " ")
|
||||||
assertEquals(expectedJsCode, output)
|
assertEquals(expectedJsCode, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testJsWords() {
|
||||||
|
// Assume you have a sample code map
|
||||||
|
val codeMap = mapOf("word1" to 2, "word2" to 3, "javascript" to 5)
|
||||||
|
val output = jsWords(codeMap)
|
||||||
|
// Assert the expected output based on the filtering logic
|
||||||
|
assertEquals(mapOf("word1" to 2, "word2" to 3, "javascript" to 5), output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSplitLanguages() {
|
||||||
|
// Assume you have a sample HTML file with <script> and <style> tags
|
||||||
|
val fileWithHTMLCSSJS = "src/test/resources/FileWithStyleAndScript.html"
|
||||||
|
val expectedJsCode = "function greet() {\n return \"Hello from JavaScript!\";\n}\n\nconsole.log(greet());".trimIndent().replace("\\s+".toRegex(), " ")
|
||||||
|
val expectedCssCode = "body {\n background-color: #f0f0f0;\n}".trimIndent().replace("\\s+".toRegex(), " ")
|
||||||
|
|
||||||
|
// Call the parser function with the sample HTML file
|
||||||
|
splitLanguages(fileWithHTMLCSSJS)
|
||||||
|
|
||||||
|
// Verify the content of the generated files
|
||||||
|
val actualJsCode = File("javascript.js").readText().trimIndent().replace("\\s+".toRegex(), " ")
|
||||||
|
val actualHtmlCode = File("html.html").readText()
|
||||||
|
val actualCssCode = File("css.css").readText().trimIndent().replace("\\s+".toRegex(), " ")
|
||||||
|
|
||||||
|
// Assertions
|
||||||
|
assertEquals(expectedJsCode, actualJsCode)
|
||||||
|
assertEquals(expectedCssCode, actualCssCode)
|
||||||
|
|
||||||
|
// Ensure that HTML content is modified as expected
|
||||||
|
assertTrue(actualHtmlCode.contains("<script src=\"javascript.js\"></script>"))
|
||||||
|
assertTrue(actualHtmlCode.contains("<link href=\"css.css\" rel=\"stylesheet\" />"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Sample HTML with Script and Style</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Hello, World!</h1>
|
||||||
|
<script>
|
||||||
|
function greet() {
|
||||||
|
return "Hello from JavaScript!";
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(greet());
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
function greet() {
|
function greet() {
|
||||||
return "Hello from JavaScript!";
|
return "Hello from JavaScript!";
|
||||||
}
|
}
|
Loading…
Reference in New Issue