Skip to content

Commit

Permalink
refactor: switch to using the toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
ckipp01 committed Aug 11, 2023
1 parent 6368da0 commit 9e4a8ab
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 155 deletions.
54 changes: 24 additions & 30 deletions src/reference/00-Getting-Started/02-sbt-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ sbt:Hello>

Note that the prompt has now changed to `sbt:Hello>`.

### Add ScalaTest to libraryDependencies
### Add toolkit-test to libraryDependencies

Using an editor, change `build.sbt` as follows:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/build.sbt) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/build.sbt) {}

Use the `reload` command to reflect the change in `build.sbt`.

Expand All @@ -226,36 +226,36 @@ sbt:Hello> ~testQuick

### Write a test

Leaving the previous command running, create a file named `src/test/scala/example/HelloSpec.scala`
Leaving the previous command running, create a file named `src/test/scala/example/HelloSuite.scala`
using an editor:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/src/test/scala/example/HelloSpec.scala) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/src/test/scala/example/HelloSuite.scala) {}

`~testQuick` should pick up the change:

```
[info] 2. Monitoring source files for hello/testQuick...
[info] Press <enter> to interrupt or '?' for more options.
[info] Build triggered by /private/tmp/foo-build/src/test/scala/HelloSpec.scala. Running 'testQuick'.
[info] compiling 1 Scala source to /private/tmp/foo-build/target/scala-2.13/test-classes ...
[info] HelloSpec:
[info] - Hello should start with H *** FAILED ***
[info] "hello" did not start with "H" (HelloSpec.scala:5)
[info] Run completed in 44 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[info] Build triggered by /tmp/foo-build/src/test/scala/example/HelloSuite.scala. Running 'testQuick'.
[info] compiling 1 Scala source to /tmp/foo-build/target/scala-2.13/test-classes ...
HelloSuite:
==> X HelloSuite.Hello should start with H 0.004s munit.FailException: /tmp/foo-build/src/test/scala/example/HelloSuite.scala:4 assertion failed
3: test("Hello should start with H") {
4: assert("hello".startsWith("H"))
5: }
at munit.FunSuite.assert(FunSuite.scala:11)
at HelloSuite.\$anonfun\$new\$1(HelloSuite.scala:4)
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error] HelloSpec
[error] (Test / testQuick) sbt.TestsFailedException: Tests unsuccessfull
[error] HelloSuite
[error] (Test / testQuick) sbt.TestsFailedException: Tests unsuccessful
```

### Make the test pass

Using an editor, change `src/test/scala/example/HelloSpec.scala` to:
Using an editor, change `src/test/scala/example/HelloSuite.scala` to:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/changes/HelloSpec.scala) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/changes/HelloSuite.scala) {}

Confirm that the test passes, then press `Enter` to exit the continuous test.

Expand Down Expand Up @@ -346,7 +346,7 @@ sbt:Hello> projects
sbt:Hello> helloCore/compile
```

### Add ScalaTest to the subproject
### Add toolkit-test to the subproject

Change `build.sbt` as follows:

Expand Down Expand Up @@ -374,7 +374,7 @@ Use `.dependsOn(...)` to add a dependency on other subprojects. Also let's move

### Parse JSON using uJson

Let's add uJson to `helloCore`.
Let's use uJson from the toolkit in `helloCore`.

@@snip [example-weather-build]($root$/src/sbt-test/ref/example-weather/build.sbt) {}

Expand All @@ -387,18 +387,15 @@ import sttp.client4.quick._
import sttp.client4.Response

object Weather {
def weather() = {
def temp() = {
val response: Response[String] = quickRequest
.get(
uri"https://api.open-meteo.com/v1/forecast?latitude=\$newYorkLatitude&longitude=\$newYorkLongitude&current_weather=true"
uri"https://api.open-meteo.com/v1/forecast?latitude=40.7143&longitude=-74.006&current_weather=true"
)
.send()
val json = ujson.read(response.body)
json.obj("current_weather")("temperature").num
}

private val newYorkLatitude: Double = 40.7143
private val newYorkLongitude: Double = -74.006
}
```

Expand All @@ -411,7 +408,7 @@ import example.core.Weather

object Hello {
def main(args: Array[String]): Unit = {
val temp = Weather.weather()
val temp = Weather.temp()
println(s"Hello! The current temperature in New York is \$temp C.")
}
}
Expand Down Expand Up @@ -449,9 +446,6 @@ sbt:Hello> dist
[info] Main Scala API documentation to /tmp/foo-build/core/target/scala-2.13/api...
[info] Wrote /tmp/foo-build/core/target/scala-2.13/hello-core_2.13-0.1.0-SNAPSHOT.pom
[info] Main Scala API documentation successful.
[warn] [1] The maintainer is empty
[warn] Add this to your build.sbt
[warn] maintainer := "your.name@company.org"
[success] All package validations passed
[info] Your package is ready in /tmp/foo-build/target/universal/hello-0.1.0-SNAPSHOT.zip
```
Expand Down Expand Up @@ -534,7 +528,7 @@ sbt:Hello> inspect tree dist
You can also run sbt in batch mode, passing sbt commands directly from the terminal.

```
\$ sbt clean "testOnly HelloSpec"
\$ sbt clean "testOnly HelloSuite"
```

**Note**: Running in batch mode requires JVM spinup and JIT each time,
Expand Down
56 changes: 25 additions & 31 deletions src/reference/es/00-Getting-Started/02-sbt-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ sbt:Hello>

Fíjate en cómo el prompt ha cambiado a `sbt:Hello>`.

### Añadir ScalaTest a `libraryDependencies`
### Añadir toolkit-test a `libraryDependencies`

Utilizando un editor, modifica `build.sbt` de la siguiente manera:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/build.sbt) {}
@@snip [scalatest]($root$/src/sbt-test/ref/example-test/build.sbt) {}

Usa el comando `reload` para reflejar los cambios de `build.sbt`.

Expand All @@ -233,35 +233,35 @@ sbt:Hello> ~testQuick
### Escribir un test

Con el comando anterior ejecutándose, crea un fichero llamado
`src/test/scala/example/HelloSpec.scala` utilizando un editor:
`src/test/scala/example/HelloSuite.scala` utilizando un editor:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/src/test/scala/example/HelloSpec.scala) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/src/test/scala/example/HelloSuite.scala) {}

`~testQuick` debería de coger el cambio:

```
[info] 2. Monitoring source files for hello/testQuick...
[info] Press <enter> to interrupt or '?' for more options.
[info] Build triggered by /private/tmp/foo-build/src/test/scala/HelloSpec.scala. Running 'testQuick'.
[info] compiling 1 Scala source to /private/tmp/foo-build/target/scala-2.13/test-classes ...
[info] HelloSpec:
[info] - Hello should start with H *** FAILED ***
[info] "hello" did not start with "H" (HelloSpec.scala:5)
[info] Run completed in 44 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[info] Build triggered by /tmp/foo-build/src/test/scala/example/HelloSuite.scala. Running 'testQuick'.
[info] compiling 1 Scala source to /tmp/foo-build/target/scala-2.13/test-classes ...
HelloSuite:
==> X HelloSuite.Hello should start with H 0.004s munit.FailException: /tmp/foo-build/src/test/scala/example/HelloSuite.scala:4 assertion failed
3: test("Hello should start with H") {
4: assert("hello".startsWith("H"))
5: }
at munit.FunSuite.assert(FunSuite.scala:11)
at HelloSuite.\$anonfun\$new\$1(HelloSuite.scala:4)
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error] HelloSpec
[error] (Test / testQuick) sbt.TestsFailedException: Tests unsuccessfull
[error] HelloSuite
[error] (Test / testQuick) sbt.TestsFailedException: Tests unsuccessful
```

### Hacer que el test pase

Utilizando un editor, cambia `src/test/scala/example/HelloSpec.scala` a:
Utilizando un editor, cambia `src/test/scala/example/HelloSuite.scala` a:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/changes/HelloSpec.scala) {}
@@snip [scalatest]($root$/src/sbt-test/ref/example-test/changes/HelloSuite.scala) {}

Confirma que el test pasa, luego pulsa `Intro` para salir del test continuo.

Expand Down Expand Up @@ -353,7 +353,7 @@ sbt:Hello> projects
sbt:Hello> helloCore/compile
```

### Añadir ScalaTest al subproyecto
### Añadir toolkit-test al subproyecto

Cambia `build.sbt` como sigue:

Expand Down Expand Up @@ -382,7 +382,7 @@ Además, movamos la dependencia de Gigahorse a `helloCore`.

### Parsear JSON con uJson

Vamos a añadir uJson a `helloCore`.
Vamos a añadir uJson de toolkit a `helloCore`.

@@snip [example-weather-build]($root$/src/sbt-test/ref/example-weather/build.sbt) {}

Expand All @@ -395,18 +395,15 @@ import sttp.client4.quick._
import sttp.client4.Response

object Weather {
def weather() = {
def temp() = {
val response: Response[String] = quickRequest
.get(
uri"https://api.open-meteo.com/v1/forecast?latitude=\$newYorkLatitude&longitude=\$newYorkLongitude&current_weather=true"
uri"https://api.open-meteo.com/v1/forecast?latitude=40.7143&longitude=-74.006&current_weather=true"
)
.send()
val json = ujson.read(response.body)
json.obj("current_weather")("temperature").num
}

private val newYorkLatitude: Double = 40.7143
private val newYorkLongitude: Double = -74.006
}
```

Expand All @@ -420,7 +417,7 @@ import example.core.Weather

object Hello {
def main(args: Array[String]): Unit = {
val temp = Weather.weather()
val temp = Weather.temp()
println(s"Hello! The current temperature in New York is \$temp C.")
}
}
Expand Down Expand Up @@ -459,9 +456,6 @@ sbt:Hello> dist
[info] Main Scala API documentation to /tmp/foo-build/core/target/scala-2.13/api...
[info] Wrote /tmp/foo-build/core/target/scala-2.13/hello-core_2.13-0.1.0-SNAPSHOT.pom
[info] Main Scala API documentation successful.
[warn] [1] The maintainer is empty
[warn] Add this to your build.sbt
[warn] maintainer := "your.name@company.org"
[success] All package validations passed
[info] Your package is ready in /tmp/foo-build/target/universal/hello-0.1.0-SNAPSHOT.zip
```
Expand Down Expand Up @@ -489,7 +483,7 @@ Así es cómo puedes ejecutar la app Dockerizada:
```
\$ docker run hello:0.1.0-SNAPSHOT
Hello! The current temperature in New York is 22.7 C.
``
```

### Establecer la versión

Expand Down Expand Up @@ -542,7 +536,7 @@ También puedes ejecutar sbt en por lotes, pasando comandos de sbt directamente
desde el terminal.

```
\$ sbt clean "testOnly HelloSpec"
\$ sbt clean "testOnly HelloSuite"
```

**Note**: El modo por lotes implica levantar una JVM y JIT cada vez, por lo que
Expand Down
52 changes: 23 additions & 29 deletions src/reference/ja/00-Getting-Started/02-sbt-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ sbt:Hello>

プロンプトが `sbt:Hello>` に変わったことに注目してほしい。

### libraryDependencies に ScalaTest を追加する
### libraryDependencies に toolkit-test を追加する

エディタを使って、`build.sbt` を以下のように変更する:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/build.sbt) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/build.sbt) {}

`reload` コマンドを使って、`build.sbt` の変更を反映させる。

Expand All @@ -221,35 +221,35 @@ sbt:Hello> ~testQuick

### テストを書く

上のコマンドを走らせたままで、エディタから `src/test/scala/example/HelloSpec.scala` という名前のファイルを作成する:
上のコマンドを走らせたままで、エディタから `src/test/scala/example/HelloSuite.scala` という名前のファイルを作成する:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/src/test/scala/example/HelloSpec.scala) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/src/test/scala/example/HelloSuite.scala) {}

`~testQuick` が検知したはずだ:

```
[info] 2. Monitoring source files for hello/testQuick...
[info] Press <enter> to interrupt or '?' for more options.
[info] Build triggered by /private/tmp/foo-build/src/test/scala/HelloSpec.scala. Running 'testQuick'.
[info] compiling 1 Scala source to /private/tmp/foo-build/target/scala-2.13/test-classes ...
[info] HelloSpec:
[info] - Hello should start with H *** FAILED ***
[info] "hello" did not start with "H" (HelloSpec.scala:5)
[info] Run completed in 44 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[info] Build triggered by /tmp/foo-build/src/test/scala/example/HelloSuite.scala. Running 'testQuick'.
[info] compiling 1 Scala source to /tmp/foo-build/target/scala-2.13/test-classes ...
HelloSuite:
==> X HelloSuite.Hello should start with H 0.004s munit.FailException: /tmp/foo-build/src/test/scala/example/HelloSuite.scala:4 assertion failed
3: test("Hello should start with H") {
4: assert("hello".startsWith("H"))
5: }
at munit.FunSuite.assert(FunSuite.scala:11)
at HelloSuite.\$anonfun\$new\$1(HelloSuite.scala:4)
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error] HelloSpec
[error] (Test / testQuick) sbt.TestsFailedException: Tests unsuccessfull
[error] HelloSuite
[error] (Test / testQuick) sbt.TestsFailedException: Tests unsuccessful
```

### テストが通るようにする

エディタを使って `src/test/scala/example/HelloSpec.scala` を以下のように変更する:
エディタを使って `src/test/scala/example/HelloSuite.scala` を以下のように変更する:

@@snip [scalatest]($root$/src/sbt-test/ref/example-scalatest/changes/HelloSpec.scala) {}
@@snip [example-test]($root$/src/sbt-test/ref/example-test/changes/HelloSuite.scala) {}

テストが通過したことを確認して、`Enter` を押して継続的テストを抜ける。

Expand Down Expand Up @@ -338,7 +338,7 @@ sbt:Hello> projects
sbt:Hello> helloCore/compile
```

### サブプロジェクトに ScalaTest を追加する
### サブプロジェクトに toolkit-test を追加する

`build.sbt` を以下のように変更する:

Expand Down Expand Up @@ -380,18 +380,15 @@ import sttp.client4.quick._
import sttp.client4.Response

object Weather {
def weather() = {
def temp() = {
val response: Response[String] = quickRequest
.get(
uri"https://api.open-meteo.com/v1/forecast?latitude=\$newYorkLatitude&longitude=\$newYorkLongitude&current_weather=true"
uri"https://api.open-meteo.com/v1/forecast?latitude=40.7143&longitude=-74.006&current_weather=true"
)
.send()
val json = ujson.read(response.body)
json.obj("current_weather")("temperature").num
}

private val newYorkLatitude: Double = 40.7143
private val newYorkLongitude: Double = -74.006
}
```

Expand All @@ -404,7 +401,7 @@ import example.core.Weather

object Hello {
def main(args: Array[String]): Unit = {
val temp = Weather.weather()
val temp = Weather.temp()
println(s"Hello! The current temperature in New York is \$temp C.")
}
}
Expand Down Expand Up @@ -443,9 +440,6 @@ sbt:Hello> dist
[info] Main Scala API documentation to /tmp/foo-build/core/target/scala-2.13/api...
[info] Wrote /tmp/foo-build/core/target/scala-2.13/hello-core_2.13-0.1.0-SNAPSHOT.pom
[info] Main Scala API documentation successful.
[warn] [1] The maintainer is empty
[warn] Add this to your build.sbt
[warn] maintainer := "your.name@company.org"
[success] All package validations passed
[info] Your package is ready in /tmp/foo-build/target/universal/hello-0.1.0-SNAPSHOT.zip
```
Expand Down Expand Up @@ -526,7 +520,7 @@ sbt:Hello> inspect tree dist
sbt のコマンドをターミナルから直接渡して sbt をバッチモードで実行することができる。

```
\$ sbt clean "testOnly HelloSpec"
\$ sbt clean "testOnly HelloSuite"
```

**Note**: バッチモードでの実行は JVM のスピンアップと JIT を毎回行うため、**ビルドかなり遅くなる。**
Expand Down
Loading

0 comments on commit 9e4a8ab

Please sign in to comment.