Skip to content

Commit

Permalink
Added data gen acc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Sep 18, 2023
1 parent 454d229 commit 4d5e8e3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,15 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
def embedMarkdownTable: DataEmbed = new DataEmbed(
this,
delimiter = "\\|",
rowFilter = (row: String) =>
rowFilter = (row: String) => {
val rgx = """---[ ?]*\|""".r

row match {
case r if r.isEmpty => false
case r if r.contains("-|") => false
case _ => true
case r if r.isEmpty => false
case r if rgx.findFirstIn(r).isDefined => false
case _ => true
}
}
)

/** Used to embed data separated by some value other than commas, tabs, or pipes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object EmbedData {
// Strings delimited with single or double quotes preserve the delimited
// value, the quotes are dropped, but the other kind of quote within that
// string is kept.
// Cells cannot be empty.
def generate(
outDir: os.Path,
moduleName: String,
Expand Down Expand Up @@ -61,7 +62,12 @@ object EmbedData {
rows.map(r => extractRowData(r, delimiter))

def extractRowData(row: String, delimiter: String): List[DataType] =
parse(delimiter)(row).map(_._1)
parse(delimiter)(row).map(_._1).collect {
case d @ DataType.StringData(s) if s.nonEmpty => d
case d: DataType.BooleanData => d
case d: DataType.DoubleData => d
case d: DataType.IntData => d
}

// A parser of things,
// is a function from strings,
Expand Down Expand Up @@ -270,7 +276,7 @@ final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) {
object DataFrame {

private val standardMessage: String =
"Embedded data must have two rows (minimum) of the same length (two columns minimum). The first row is the headers / field names. The first column are the keys."
"Embedded data must have two rows (minimum) of the same length (two columns minimum). The first row is the headers / field names. The first column are the keys. Cells cannot be empty."

def fromRows(rows: List[List[DataType]]): DataFrame =
rows match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AcceptanceTests extends munit.FunSuite {
exclude = {
case p if p.startsWith(os.RelPath("ignored-folder")) => true
case p if p.startsWith(os.RelPath("mixed")) => true
case p if p.endsWith(os.RelPath("stats.md")) => true
case _ => false
},
None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package indigoplugin.generators

import indigoplugin.IndigoGenerators

class GeneratorAcceptanceTests extends munit.FunSuite {

val sourceCSV = os.pwd / "test-assets" / "data" / "stats.csv"
val sourceMD = os.pwd / "test-assets" / "data" / "stats.md"

val targetDir = os.pwd / "out" / "indigo-plugin-generator-acceptance-test-output"

override def beforeAll(): Unit = {
if (os.exists(targetDir)) {
os.remove.all(target = targetDir)
}

os.makeDir.all(targetDir)
}

test("Can generate an enum from a CSV file") {

val files =
IndigoGenerators
.default(targetDir, "com.example.test")
.embedCSV
.asEnum("StatsEnum", sourceCSV)
.toSources

files.headOption match {
case None =>
fail("No file was generated")

case Some(src) =>
assert(clue(src) == clue(targetDir / "indigo-compile-codegen-output" / "StatsEnum.scala"))

val actual = os.read(src)

val expected =
"""
|package com.example.test
|
|// DO NOT EDIT: Generated by Indigo.
|
|enum StatsEnum(val level: Int, val bonus: Int):
| case Intelligence extends StatsEnum(2, 4)
| case Strength extends StatsEnum(10, 0)
| case Fortitude extends StatsEnum(4, 1)
|""".stripMargin

assertEquals(actual.trim, expected.trim)
}
}

test("Can generate a map from a markdown table file") {

val files =
IndigoGenerators
.default(targetDir, "com.example.test")
.embedMarkdownTable
.asMap("StatsMap", sourceMD)
.toSources

files.headOption match {
case None =>
fail("No file was generated")

case Some(src) =>
assert(clue(src) == clue(targetDir / "indigo-compile-codegen-output" / "StatsMap.scala"))

val actual = os.read(src)

val expected =
"""
|package com.example.test
|
|// DO NOT EDIT: Generated by Indigo.
|
|final case class StatsMap(val level: Int, val bonus: Int)
|object StatsMap:
| val data: Map[String, StatsMap] =
| Map(
| "intelligence" -> StatsMap(2, 4),
| "strength" -> StatsMap(10, 0),
| "fortitude" -> StatsMap(4, 1)
| )
|""".stripMargin

assertEquals(actual.trim, expected.trim)
}
}

}
5 changes: 5 additions & 0 deletions indigo-plugin/test-assets/data/stats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| name | level | bonus |
| ------------ | ----- | ----- |
| intelligence | 2 | 4 |
| strength | 10 | 0 |
| fortitude | 4 | 1 |

0 comments on commit 4d5e8e3

Please sign in to comment.