Skip to content

Commit 9a566ad

Browse files
authored
Merge pull request #2 from kenoir/fix-ci-failures
Fix ci failures
2 parents 91a3aac + 6ffd35f commit 9a566ad

File tree

6 files changed

+130
-8
lines changed

6 files changed

+130
-8
lines changed

.github/workflows/scala.yml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,44 @@ jobs:
44
test:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v2
7+
- uses: actions/checkout@v3
8+
- name: Set up JDK 11
9+
uses: actions/setup-java@v3
10+
with:
11+
java-version: '11'
12+
distribution: 'temurin'
13+
cache: 'sbt'
14+
- name: Install sbt
15+
run: |
16+
echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
17+
echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | sudo tee /etc/apt/sources.list.d/sbt_old.list
18+
curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add -
19+
sudo apt-get update
20+
sudo apt-get install -y sbt
821
- name: Run tests
922
run: sbt coverage test
1023
- name: Coverage Report
1124
run: sbt coverageReport
1225
- name: Upload coverage to Codecov
13-
uses: codecov/codecov-action@v1
26+
uses: codecov/codecov-action@v3
1427
with:
1528
fail_ci_if_error: true
1629
lint:
1730
runs-on: ubuntu-latest
1831
steps:
19-
- uses: actions/checkout@v2
32+
- uses: actions/checkout@v3
33+
- name: Set up JDK 11
34+
uses: actions/setup-java@v3
35+
with:
36+
java-version: '11'
37+
distribution: 'temurin'
38+
cache: 'sbt'
39+
- name: Install sbt
40+
run: |
41+
echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
42+
echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | sudo tee /etc/apt/sources.list.d/sbt_old.list
43+
curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add -
44+
sudo apt-get update
45+
sudo apt-get install -y sbt
2046
- name: Formatting
21-
run: sbt scalafmtSbtCheck scalafmtCheck test:scalafmtCheck
47+
run: sbt scalafmtSbtCheck scalafmtCheck test:scalafmtCheck

build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ libraryDependencies += "org.clapper" %% "grizzled-slf4j"
1111
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.8"
1212
libraryDependencies += "ch.qos.logback" % "logback-core" % "1.1.8"
1313
libraryDependencies += "ch.qos.logback" % "logback-access" % "1.1.8"
14+
15+
libraryDependencies ++= Seq(
16+
"io.circe" %% "circe-core" % "0.14.1",
17+
"io.circe" %% "circe-generic" % "0.14.1",
18+
"io.circe" %% "circe-parser" % "0.14.1"
19+
)

src/main/scala/JsonFormats.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import io.circe.{Encoder, Json}
2+
import io.circe.generic.semiauto._
3+
import java.util.UUID
4+
5+
// Assuming Participant and Society are in the default package (as seen from ls output)
6+
7+
object JsonFormats {
8+
9+
// Encoder for UUID
10+
implicit val uuidEncoder: Encoder[UUID] =
11+
Encoder.encodeString.contramap[UUID](_.toString)
12+
13+
// Encoder for Participant[Boolean]
14+
implicit val participantEncoder: Encoder[Participant[Boolean]] =
15+
deriveEncoder[Participant[Boolean]]
16+
17+
// Encoder for Society[Boolean]
18+
implicit val societyEncoder: Encoder[Society[Boolean]] =
19+
deriveEncoder[Society[Boolean]]
20+
}

src/main/scala/Society.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ case class Society[T](participants: List[Participant[T]]) extends Logging {
2424
.values
2525
.toList
2626

27-
info(updatedSocietyParticipants.map(_.opinion))
27+
// info(updatedSocietyParticipants.map(_.opinion))
2828
copy(participants = updatedSocietyParticipants)
2929
}
3030
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import scala.util.Random
2+
import JsonFormats._ // Import your encoders
3+
import io.circe.syntax._ // For .asJson
24

35
object UnitedWeStandApp extends App {
46
import Debatable._
57

68
def runCycles(cycleCount: Int = 20): Society[Boolean] = {
79
val society = Society.create(population = 7)(() => Random.nextBoolean())
8-
9-
(1 to cycleCount).foldLeft(society)((society, _) => society.update)
10+
(1 to cycleCount).foldLeft(society)((accSociety, _) => accSociety.update)
1011
}
1112

12-
runCycles()
13+
val finalSociety = runCycles()
14+
println(finalSociety.asJson.noSpaces) // Output JSON to stdout
1315
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
import io.circe.parser.parse
3+
import io.circe.syntax._ // For .asJson
4+
import io.circe.Json // For explicit Json type usage
5+
import JsonFormats._ // Your encoders
6+
7+
// Assuming UnitedWeStandApp is in the default package
8+
// Assuming Participant and Society case classes are in the default package
9+
10+
class UnitedWeStandAppTest extends AnyFunSuite {
11+
12+
test(
13+
"UnitedWeStandApp.runCycles output should be valid JSON and contain non-empty participants array"
14+
) {
15+
// Directly call runCycles and serialize, to avoid capturing stdout
16+
val society = UnitedWeStandApp.runCycles(cycleCount = 5) // Use a small cycle count for testing
17+
val jsonString = society.asJson.noSpaces
18+
19+
// 1. Check if the string is valid JSON
20+
val parsedJsonResult = parse(jsonString)
21+
assert(
22+
parsedJsonResult.isRight,
23+
s"Output should be valid JSON. Parsing error: ${parsedJsonResult.left.getOrElse("")}"
24+
)
25+
26+
// 2. Perform checks on the parsed JSON structure
27+
val json = parsedJsonResult.getOrElse(
28+
throw new RuntimeException(
29+
"JSON parsing failed in test, this should not happen due to the assert above."
30+
)
31+
)
32+
33+
// Check for the presence of the 'participants' array
34+
val participantsCursor = json.hcursor.downField("participants")
35+
assert(
36+
participantsCursor.succeeded,
37+
"JSON should contain a 'participants' field."
38+
)
39+
40+
// Check if 'participants' is an array and is non-empty
41+
val participantsArray = participantsCursor.as[List[Json]]
42+
assert(
43+
participantsArray.isRight,
44+
"'participants' field should be an array."
45+
)
46+
assert(
47+
participantsArray.getOrElse(Nil).nonEmpty,
48+
"Participants array should not be empty."
49+
)
50+
51+
// 3. Optionally, check structure of a participant (if needed, more detailed)
52+
val firstParticipantOpt = participantsArray.getOrElse(Nil).headOption
53+
assert(
54+
firstParticipantOpt.isDefined,
55+
"Participant array should have at least one participant."
56+
)
57+
58+
val firstParticipantJson = firstParticipantOpt.get
59+
assert(
60+
firstParticipantJson.hcursor.downField("uuid").as[String].isRight,
61+
"Participant should have a 'uuid' string field."
62+
)
63+
assert(
64+
firstParticipantJson.hcursor.downField("opinion").as[Boolean].isRight,
65+
"Participant should have an 'opinion' boolean field."
66+
)
67+
}
68+
}

0 commit comments

Comments
 (0)