Skip to content

Commit

Permalink
Scala.js compatibility (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallisto authored Apr 21, 2024
1 parent aa5fec1 commit e5863a0
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 367 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ jobs:
java-version: '17'
distribution: 'temurin'
cache: 'sbt'
- name: Build and Test
run: sbt test
- name: Build and Test JVM
run: sbt tessellaJVM/test
- name: Build and Test JS
run: sbt tessellaJS/test
# Optional: This step uploads information to the GitHub dependency graph and unblocking Dependabot alerts for the repository
# - name: Upload dependency graph
# uses: scalacenter/sbt-dependency-submission@ab086b50c947c9774b70f39fc7f6e20ca2706c91
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.scala_tessella.tessella
package conversion

import Geometry.Box
import SharedML.addAttributes
import SVG.*

import org.scalatest.*
import org.scalatest.flatspec.*
import org.scalatest.matchers.*

import scala.xml.Elem

class ConverterSVGSpec extends AnyFlatSpec with should.Matchers {

val box: Box =
Box(-2.0, 10.0, -2.0, 10.0)

"An SVG element" can "be created" in {
val e: Elem =
svg(box)
prettyPrinter.format(e) shouldBe
"""<svg viewBox="-125 -125 650 650" xmlns="http://www.w3.org/2000/svg"></svg>"""
}

it can "have attributes added" in {
val e: Elem =
svg(box).addAttributes(rdfAttributes *)
prettyPrinter.format(e) shouldBe
"""<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" viewBox="-125 -125 650 650" xmlns="http://www.w3.org/2000/svg"></svg>"""
}

"A rect element" can "be created from a Box2D" in {
val e: Elem =
rect(box)
prettyPrinter.format(e) shouldBe
"""<rect width="600" height="600" x="-100" y="-100"></rect>"""
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.scala_tessella.tessella
package conversion

import SVG.*
import Outliers.sqr3x3Growth
import SVG.*

import org.scalatest.*
import org.scalatest.flatspec.*
Expand Down
30 changes: 20 additions & 10 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,33 @@ ThisBuild / semanticdbVersion := scalafixSemanticdb.revision
lazy val root: Project =
project
.in(file("."))
.aggregate(tessella.js, tessella.jvm)
.settings(
name := "tessella",
description := "Tilings by regular polygons",
licenses := Seq("APL2" -> url("https://www.apache.org/licenses/LICENSE-2.0.txt")),
sonatypeProjectHosting := Some(GitHubHosting("scala-tessella", "tessella", "[email protected]")),
sonatypeCredentialHost := "s01.oss.sonatype.org",
publishTo := sonatypePublishToBundle.value,
git.remoteRepo := sonatypeProjectHosting.value.get.scmUrl,
ghpagesNoJekyll := true,
libraryDependencies ++= Seq(
"io.github.iltotore" %% "iron" % "2.5.0",
"org.scala-lang.modules" %% "scala-xml" % "2.3.0",
"io.github.scala-tessella" %% "ring-seq" % "0.5.1",
"org.scalatest" %% "scalatest" % "3.2.18" % "test",
"org.scalacheck" %% "scalacheck" % "1.17.1" % "test"
),
SiteScaladoc / siteSubdirName := "api",
paradoxProperties += ("scaladoc.base_url" -> "api"),
scalacOptions += "-deprecation"
)

lazy val tessella =
crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.in(file("."))
.settings(
name := "tessella",
description := "Tilings by regular polygons",
licenses := Seq("APL2" -> url("https://www.apache.org/licenses/LICENSE-2.0.txt")),
libraryDependencies ++= Seq(
"io.github.scala-tessella" %%% "ring-seq" % "0.6.2",
"io.github.iltotore" %%% "iron" % "2.5.0",
"org.scala-lang.modules" %%% "scala-xml" % "2.3.0",
"org.scalatest" %%% "scalatest" % "3.2.18" % "test",
"org.scalacheck" %%% "scalacheck" % "1.17.1" % "test",
)
)
.jvmSettings()
.jsSettings()
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ addSbtPlugin("com.github.sbt" % "sbt-ghpages" % "0.8.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.10.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.2.1")
addSbtPlugin("com.github.sbt" % "sbt-dynver" % "5.0.1")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import org.scalatest.matchers.*
class GeometrySpec extends AnyFlatSpec with Helper with should.Matchers {

"A radian" can "be created as an opaque type" in {
Radian(0).toString shouldBe "0.0"
Radian(0).toDouble shouldBe
0.0
}

def foo(radian: Radian) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,152 +10,58 @@ import org.scalatest.matchers.*

class TilingGrowthEdgeSpec extends AnyFlatSpec with Helper with should.Matchers {

"A tiling" can "have some polygons added to an edge" in {
square.maybeGrowEdge(1--2, Polygon(4), BEFORE_PERIMETER).unsafe.allLabels shouldBe
"""<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" viewBox="-25.0 -75.0 100.0 150.0" xmlns="http://www.w3.org/2000/svg">
| <g>
| <title>Tiling</title>
| <desc>Finite tessellation of regular polygons</desc>
| <g style="stroke:black">
| <title>Edges</title>
| <desc>Sides of the regular polygons</desc>
| <line x1="0.0" y1="0.0" x2="50.0" y2="0.0"/>
| <line x1="50.0" y1="0.0" x2="50.0" y2="50.0"/>
| <line x1="50.0" y1="50.0" x2="0.0" y2="50.0"/>
| <line x1="0.0" y1="0.0" x2="0.0" y2="50.0"/>
| <line x1="50.0" y1="0.0" x2="50.0" y2="-50.0"/>
| <line x1="50.0" y1="-50.0" x2="0.0" y2="-50.0"/>
| <line x1="0.0" y1="0.0" x2="0.0" y2="-50.0"/>
| </g>
| <polygon style="fill:none;stroke:blue;stroke-width:2" points="0.0,0.0 0.0,50.0 50.0,50.0 50.0,0.0 50.0,-50.0 0.0,-50.0"/>
| <g style="fill:#4a4a4a;text-anchor:middle;font-family:Arial,Helvetica,sans-serif">
| <title>Node labels</title>
| <desc>Each node showing its value</desc>
| <text x="50.0" y="-50.0">5</text>
| <text x="0.0" y="0.0">1</text>
| <text x="0.0" y="-50.0">6</text>
| <text x="50.0" y="0.0">2</text>
| <text x="50.0" y="50.0">3</text>
| <text x="0.0" y="50.0">4</text>
| </g>
| </g>
| <metadata>
| <rdf:RDF>
| <cc:Work>
| <dc:source rdf:resource="https://github.com/scala-tessella/tessella">Tessella</dc:source>
| <cc:license rdf:resource="https://www.apache.org/licenses/LICENSE-2.0"/>
| </cc:Work>
| </rdf:RDF>
| </metadata>
|</svg>""".stripMargin
}
// "A tiling" can "have some polygons added to an edge" in {
// square.maybeGrowEdge(1--2, Polygon(4), BEFORE_PERIMETER).unsafe.allLabels shouldBe
// """<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" viewBox="-25.0 -75.0 100.0 150.0" xmlns="http://www.w3.org/2000/svg">
// | <g>
// | <title>Tiling</title>
// | <desc>Finite tessellation of regular polygons</desc>
// | <g style="stroke:black">
// | <title>Edges</title>
// | <desc>Sides of the regular polygons</desc>
// | <line x1="0.0" y1="0.0" x2="50.0" y2="0.0"/>
// | <line x1="50.0" y1="0.0" x2="50.0" y2="50.0"/>
// | <line x1="50.0" y1="50.0" x2="0.0" y2="50.0"/>
// | <line x1="0.0" y1="0.0" x2="0.0" y2="50.0"/>
// | <line x1="50.0" y1="0.0" x2="50.0" y2="-50.0"/>
// | <line x1="50.0" y1="-50.0" x2="0.0" y2="-50.0"/>
// | <line x1="0.0" y1="0.0" x2="0.0" y2="-50.0"/>
// | </g>
// | <polygon style="fill:none;stroke:blue;stroke-width:2" points="0.0,0.0 0.0,50.0 50.0,50.0 50.0,0.0 50.0,-50.0 0.0,-50.0"/>
// | <g style="fill:#4a4a4a;text-anchor:middle;font-family:Arial,Helvetica,sans-serif">
// | <title>Node labels</title>
// | <desc>Each node showing its value</desc>
// | <text x="50.0" y="-50.0">5</text>
// | <text x="0.0" y="0.0">1</text>
// | <text x="0.0" y="-50.0">6</text>
// | <text x="50.0" y="0.0">2</text>
// | <text x="50.0" y="50.0">3</text>
// | <text x="0.0" y="50.0">4</text>
// | </g>
// | </g>
// | <metadata>
// | <rdf:RDF>
// | <cc:Work>
// | <dc:source rdf:resource="https://github.com/scala-tessella/tessella">Tessella</dc:source>
// | <cc:license rdf:resource="https://www.apache.org/licenses/LICENSE-2.0"/>
// | </cc:Work>
// | </rdf:RDF>
// | </metadata>
// |</svg>""".stripMargin
// }

it can "NOT have a polygon added to a non existing edge" in {
Tiling.squareNet(2, 2).unsafe.maybeGrowEdge(1--5, Polygon(4), BEFORE_PERIMETER) shouldBe
Left(
"""Tiling can add polygons only to perimeter edges:
| found unknown edge 1--5.
|See SVG:
|<svg viewBox="-25.0 -125.0 150.0 150.0" xmlns="http://www.w3.org/2000/svg">
| <g>
| <title>Tiling with invalid addition</title>
| <desc>Adding to unknown edge 1--5</desc>
| <g style="stroke:red;stroke-width:1">
| <title>Highlighted</title>
| <desc>Edges</desc>
| <line x1="0.0" y1="0.0" x2="50.0" y2="-50.0"/>
| </g>
| <g>
| <title>Tiling</title>
| <desc>Finite tessellation of regular polygons</desc>
| <g style="stroke:black">
| <title>Edges</title>
| <desc>Sides of the regular polygons</desc>
| <line x1="0.0" y1="0.0" x2="50.0" y2="0.0"/>
| <line x1="0.0" y1="-50.0" x2="50.0" y2="-50.0"/>
| <line x1="0.0" y1="-100.0" x2="50.0" y2="-100.0"/>
| <line x1="50.0" y1="0.0" x2="100.0" y2="0.0"/>
| <line x1="50.0" y1="-50.0" x2="100.0" y2="-50.0"/>
| <line x1="50.0" y1="-100.0" x2="100.0" y2="-100.0"/>
| <line x1="0.0" y1="0.0" x2="0.0" y2="-50.0"/>
| <line x1="0.0" y1="-50.0" x2="0.0" y2="-100.0"/>
| <line x1="50.0" y1="0.0" x2="50.0" y2="-50.0"/>
| <line x1="50.0" y1="-50.0" x2="50.0" y2="-100.0"/>
| <line x1="100.0" y1="0.0" x2="100.0" y2="-50.0"/>
| <line x1="100.0" y1="-50.0" x2="100.0" y2="-100.0"/>
| </g>
| <polygon style="fill:none;stroke:blue;stroke-width:2" points="0.0,0.0 50.0,0.0 100.0,0.0 100.0,-50.0 100.0,-100.0 50.0,-100.0 0.0,-100.0 0.0,-50.0"/>
| <g style="fill:#4a4a4a;text-anchor:middle;font-family:Arial,Helvetica,sans-serif">
| <title>Node labels</title>
| <desc>Each node showing its value</desc>
| <text x="50.0" y="-50.0">5</text>
| <text x="0.0" y="0.0">1</text>
| <text x="100.0" y="-50.0">6</text>
| <text x="100.0" y="-100.0">9</text>
| <text x="50.0" y="0.0">2</text>
| <text x="0.0" y="-100.0">7</text>
| <text x="100.0" y="0.0">3</text>
| <text x="50.0" y="-100.0">8</text>
| <text x="0.0" y="-50.0">4</text>
| </g>
| </g>
| </g>
|</svg>""".stripMargin
)
"A tiling" can "NOT have a polygon added to a non existing edge" in {
Tiling.squareNet(2, 2).unsafe.maybeGrowEdge(1--5, Polygon(4), BEFORE_PERIMETER).left.getOrElse("").take(83) shouldBe
"""Tiling can add polygons only to perimeter edges:
| found unknown edge 1--5.
|See SVG:""".stripMargin
}

it can "NOT have a polygon added to a non perimeter edge" in {
Tiling.squareNet(2, 2).unsafe.maybeGrowEdge(2--5, Polygon(4), BEFORE_PERIMETER) shouldBe
Left(
"""Tiling can add polygons only to perimeter edges:
| found inner edge 2--5.
|See SVG:
|<svg viewBox="-25.0 -125.0 150.0 150.0" xmlns="http://www.w3.org/2000/svg">
| <g>
| <title>Tiling with invalid addition</title>
| <desc>Adding to inner edge 2--5</desc>
| <g style="stroke:red;stroke-width:3">
| <title>Highlighted</title>
| <desc>Edges</desc>
| <line x1="50.0" y1="0.0" x2="50.0" y2="-50.0"/>
| </g>
| <g>
| <title>Tiling</title>
| <desc>Finite tessellation of regular polygons</desc>
| <g style="stroke:black">
| <title>Edges</title>
| <desc>Sides of the regular polygons</desc>
| <line x1="0.0" y1="0.0" x2="50.0" y2="0.0"/>
| <line x1="0.0" y1="-50.0" x2="50.0" y2="-50.0"/>
| <line x1="0.0" y1="-100.0" x2="50.0" y2="-100.0"/>
| <line x1="50.0" y1="0.0" x2="100.0" y2="0.0"/>
| <line x1="50.0" y1="-50.0" x2="100.0" y2="-50.0"/>
| <line x1="50.0" y1="-100.0" x2="100.0" y2="-100.0"/>
| <line x1="0.0" y1="0.0" x2="0.0" y2="-50.0"/>
| <line x1="0.0" y1="-50.0" x2="0.0" y2="-100.0"/>
| <line x1="50.0" y1="0.0" x2="50.0" y2="-50.0"/>
| <line x1="50.0" y1="-50.0" x2="50.0" y2="-100.0"/>
| <line x1="100.0" y1="0.0" x2="100.0" y2="-50.0"/>
| <line x1="100.0" y1="-50.0" x2="100.0" y2="-100.0"/>
| </g>
| <polygon style="fill:none;stroke:blue;stroke-width:2" points="0.0,0.0 50.0,0.0 100.0,0.0 100.0,-50.0 100.0,-100.0 50.0,-100.0 0.0,-100.0 0.0,-50.0"/>
| <g style="fill:#4a4a4a;text-anchor:middle;font-family:Arial,Helvetica,sans-serif">
| <title>Node labels</title>
| <desc>Each node showing its value</desc>
| <text x="50.0" y="-50.0">5</text>
| <text x="0.0" y="0.0">1</text>
| <text x="100.0" y="-50.0">6</text>
| <text x="100.0" y="-100.0">9</text>
| <text x="50.0" y="0.0">2</text>
| <text x="0.0" y="-100.0">7</text>
| <text x="100.0" y="0.0">3</text>
| <text x="50.0" y="-100.0">8</text>
| <text x="0.0" y="-50.0">4</text>
| </g>
| </g>
| </g>
|</svg>""".stripMargin
)
Tiling.squareNet(2, 2).unsafe.maybeGrowEdge(2--5, Polygon(4), BEFORE_PERIMETER).left.getOrElse("").take(81) shouldBe
"""Tiling can add polygons only to perimeter edges:
| found inner edge 2--5.
|See SVG:""".stripMargin
}

val strange: Tiling =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,11 @@ class TilingGrowthSpec extends AnyFlatSpec with Helper with should.Matchers {
"A tiling grown by polygons" can "find a dead end" in {
val start: Tiling =
Tiling.maybe(square.edges ++ List(1--5, 2--5, 2--6, 3--6, 3--7, 4--7, 4--8, 1--8)).unsafe
start.growByPolygon(1, Polygon(42), List(NARROWEST_ANGLE, LOWEST_ORDINAL), List(HIGHER_ORDINAL)) shouldBe
Left(
"""Tiling cannot be grown after adding 0 * pgon-42,
| no more edges fillable
|See SVG:
|<svg viewBox="-68.30127 -68.30127 186.60254 186.60254" xmlns="http://www.w3.org/2000/svg">
| <g>
| <title>Tiling with invalid addition</title>
| <desc>No more space to add pgon-42 after 0 steps</desc>
| <g>
| <title>Tiling</title>
| <desc>Finite tessellation of regular polygons</desc>
| <g style="stroke:black">
| <title>Edges</title>
| <desc>Sides of the regular polygons</desc>
| <line x1="0.0" y1="0.0" x2="50.0" y2="0.0"/>
| <line x1="50.0" y1="0.0" x2="50.0" y2="50.0"/>
| <line x1="50.0" y1="50.0" x2="0.0" y2="50.0"/>
| <line x1="0.0" y1="0.0" x2="0.0" y2="50.0"/>
| <line x1="0.0" y1="0.0" x2="25.0" y2="-43.30127"/>
| <line x1="50.0" y1="0.0" x2="25.0" y2="-43.30127"/>
| <line x1="50.0" y1="0.0" x2="93.30127" y2="25.0"/>
| <line x1="50.0" y1="50.0" x2="93.30127" y2="25.0"/>
| <line x1="50.0" y1="50.0" x2="25.0" y2="93.30127"/>
| <line x1="0.0" y1="50.0" x2="25.0" y2="93.30127"/>
| <line x1="0.0" y1="50.0" x2="-43.30127" y2="25.0"/>
| <line x1="0.0" y1="0.0" x2="-43.30127" y2="25.0"/>
| </g>
| <polygon style="fill:none;stroke:blue;stroke-width:2" points="0.0,0.0 -43.30127,25.0 0.0,50.0 25.0,93.30127 50.0,50.0 93.30127,25.0 50.0,0.0 25.0,-43.30127"/>
| <g style="fill:#4a4a4a;text-anchor:middle;font-family:Arial,Helvetica,sans-serif">
| <title>Node labels</title>
| <desc>Each node showing its value</desc>
| <text x="25.0" y="-43.30127">5</text>
| <text x="0.0" y="0.0">1</text>
| <text x="93.30127" y="25.0">6</text>
| <text x="50.0" y="0.0">2</text>
| <text x="25.0" y="93.30127">7</text>
| <text x="50.0" y="50.0">3</text>
| <text x="-43.30127" y="25.0">8</text>
| <text x="0.0" y="50.0">4</text>
| </g>
| </g>
| </g>
|</svg>""".stripMargin
)
start.growByPolygon(1, Polygon(42), List(NARROWEST_ANGLE, LOWEST_ORDINAL), List(HIGHER_ORDINAL))
.left.getOrElse("").take(81) shouldBe
"""Tiling cannot be grown after adding 0 * pgon-42,
| no more edges fillable
|See SVG:""".stripMargin
}

"A tiling to be grown with a triangle plug" can "have a perimeter" in {
Expand Down
Loading

0 comments on commit e5863a0

Please sign in to comment.