Skip to content

Commit

Permalink
Added some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Jul 27, 2022
1 parent 05d285e commit 7435b38
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class FileSystem private (imageIO: ImageIOWrapper) {
}

object FileSystem {
class NullArgs(val initialImages: Map[File, BufferedImage] = Map.empty,
val supportedImageFormats: Set[String] = Set("png", "jpg"))

def create(): FileSystem = new FileSystem(new RealImageIO)
def createNull(initialImages: Map[File, BufferedImage] = Map.empty,
supportedImageFormats: Set[String] = Set("png", "jpg")): FileSystem = {
val allSupportedFormats = supportedImageFormats | supportedImageFormats.map(_.toUpperCase)
val imageIO = new NullImageIO(initialImages, allSupportedFormats)
def createNull(args: NullArgs = new NullArgs()): FileSystem = {
val allSupportedFormats = args.supportedImageFormats | args.supportedImageFormats.map(_.toUpperCase)
val imageIO = new NullImageIO(args.initialImages, allSupportedFormats)
new FileSystem(imageIO)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ class TriPaintModel(val fileSystem: FileSystem) {

object TriPaintModel {
def create(): TriPaintModel = new TriPaintModel(FileSystem.create())
def createNull(): TriPaintModel = new TriPaintModel(FileSystem.createNull())

def createNull(fileSystemArgs: FileSystem.NullArgs = new FileSystem.NullArgs()): TriPaintModel =
new TriPaintModel(FileSystem.createNull(fileSystemArgs))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import com.martomate.tripaint.view.TriPaintView
import org.mockito.Mockito.when
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.mockito.MockitoSugar

class NewActionTest extends AnyFlatSpec with Matchers with MockitoSugar {
class NewActionTest extends AnyFlatSpec with Matchers {
"NewAction" should "add a new image to the grid" in {
val model = TriPaintModel.createNull()
model.imageGrid.setImageSizeIfEmpty(8)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.martomate.tripaint.control.action

import com.martomate.tripaint.infrastructure.FileSystem
import com.martomate.tripaint.model.{Color, TriPaintModel}
import com.martomate.tripaint.model.coords.{StorageCoords, TriImageCoords}
import com.martomate.tripaint.model.image.RegularImage
import com.martomate.tripaint.model.image.format.SimpleStorageFormat
import com.martomate.tripaint.view.FileOpenSettings
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.io.File

class OpenActionTest extends AnyFlatSpec with Matchers {
"OpenAction" should "open an image" in {
val file = new File("image.png")
val image = RegularImage.fill(8, 8, Color.Yellow)
image.setColor(5, 6, Color.Cyan)

val model = TriPaintModel.createNull(new FileSystem.NullArgs(initialImages = Map(file -> image.toBufferedImage)))
model.imageGrid.setImageSizeIfEmpty(8)

new OpenAction(
model,
() => Some(file),
(_, _, _) => Some(FileOpenSettings(StorageCoords(0, 0), new SimpleStorageFormat)),
() => Some((3, 4))
).perform()

val actualImage = model.imageGrid(TriImageCoords(3, 4))
.map(_.storage)
.map(_.toRegularImage(new SimpleStorageFormat))
.orNull

actualImage shouldBe image
}

it should "open an image at an offset" in {
val file = new File("image.png")
val image = RegularImage.fill(8, 8, Color.Yellow)
image.setColor(5, 6, Color.Cyan)

val storedImage = RegularImage.ofSize(9, 10)
val offset = StorageCoords(1, 2)
storedImage.pasteImage(offset, image)

val model = TriPaintModel.createNull(new FileSystem.NullArgs(initialImages = Map(file -> storedImage.toBufferedImage)))
model.imageGrid.setImageSizeIfEmpty(8)

new OpenAction(
model,
() => Some(file),
(_, _, _) => Some(FileOpenSettings(offset, new SimpleStorageFormat)),
() => Some((3, 4))
).perform()

val actualImage = model.imageGrid(TriImageCoords(3, 4))
.map(_.storage)
.map(_.toRegularImage(new SimpleStorageFormat))
.orNull

actualImage shouldBe image
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {
val f = new ImagePool()
f.move(image, location, info)

val fs = FileSystem.createNull(supportedImageFormats = Set())
val fs = FileSystem.createNull(new FileSystem.NullArgs(supportedImageFormats = Set()))
f.save(image, new ImageSaverToFile, fs) shouldBe false
}

Expand Down Expand Up @@ -90,7 +90,7 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {
val saver = new ImageSaverToFile

val existingImage = RegularImage.fill(2, 2, Color.Red).toBufferedImage
val fs = FileSystem.createNull(initialImages = Map(new File(path) -> existingImage))
val fs = FileSystem.createNull(new FileSystem.NullArgs(initialImages = Map(new File(path) -> existingImage)))

imagePool.save(image, saver, fs)

Expand All @@ -115,7 +115,7 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {
val saver = new ImageSaverToFile

val existingImage = RegularImage.fill(3, 5, Color.Red).toBufferedImage
val fs = FileSystem.createNull(initialImages = Map(new File(path) -> existingImage))
val fs = FileSystem.createNull(new FileSystem.NullArgs(initialImages = Map(new File(path) -> existingImage)))

imagePool.save(image, saver, fs)

Expand All @@ -141,7 +141,7 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {
val saver = new ImageSaverToFile

val existingImage = RegularImage.fill(3, 2, Color.Red).toBufferedImage
val fs = FileSystem.createNull(initialImages = Map(new File(path) -> existingImage))
val fs = FileSystem.createNull(new FileSystem.NullArgs(initialImages = Map(new File(path) -> existingImage)))

imagePool.save(image, saver, fs)

Expand Down Expand Up @@ -184,7 +184,7 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {
it should "return Failure if there is no image there and the loading failed" in {
val location = SaveLocation(null)
val imageSize = 16
val fs = FileSystem.createNull(initialImages = Map.empty)
val fs = FileSystem.createNull(new FileSystem.NullArgs(initialImages = Map.empty))

val pool = new ImagePool()

Expand All @@ -198,9 +198,9 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {

val image = ImageStorage.fromBGColor(FXColor.Orange, imageSize)
val regularImage = image.toRegularImage(storageFormat)
val fs = FileSystem.createNull(initialImages = Map(
val fs = FileSystem.createNull(new FileSystem.NullArgs(initialImages = Map(
file -> regularImage.toBufferedImage
))
)))

val pool = new ImagePool()

Expand All @@ -223,9 +223,9 @@ class ImagePoolTest extends AnyFlatSpec with Matchers with MockitoSugar {

val storedImage = RegularImage.ofSize(imageSize + offset.x, imageSize + offset.y)
storedImage.pasteImage(offset, regularImage)
val fs = FileSystem.createNull(initialImages = Map(
val fs = FileSystem.createNull(new FileSystem.NullArgs(initialImages = Map(
file -> storedImage.toBufferedImage
))
)))

val pool = new ImagePool()

Expand Down

0 comments on commit 7435b38

Please sign in to comment.