summaryrefslogtreecommitdiff
path: root/contrib/tut/test/src/mill/contrib/tut/TutTests.scala
blob: fd369eed15da794f53e053b9afd0ed702f9336fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package mill.contrib
package tut

import ammonite.ops._
import mill._
import mill.eval.Result._
import mill.scalalib._
import mill.util.{TestEvaluator, TestUtil}
import utest._
import utest.framework.TestPath

object TutTests extends TestSuite {

  trait TutTestModule extends TestUtil.BaseModule with TutModule {
    def millSourcePath = TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
    def scalaVersion = "2.12.4"
    def tutVersion = "0.6.7"
  }

  object TutTest extends TutTestModule

  object TutCustomTest extends TutTestModule {
    def tutTargetDirectory = millSourcePath
  }

  object TutLibrariesTest extends TutTestModule {
    def ivyDeps = Agg(ivy"org.typelevel::cats-core:1.4.0")
    def tutSourceDirectory = T.sources { resourcePathWithLibraries }
    def scalacPluginIvyDeps = Agg(ivy"org.spire-math::kind-projector:0.9.8")
  }

  val resourcePath = pwd / 'contrib / 'tut / 'test / 'tut
  val resourcePathWithLibraries = pwd / 'contrib / 'tut / 'test / "tut-with-libraries"

  def workspaceTest[T](m: TestUtil.BaseModule, resourcePath: Path = resourcePath)
                      (t: TestEvaluator => T)
                      (implicit tp: TestPath): T = {
    val eval = new TestEvaluator(m)
    rm(m.millSourcePath)
    rm(eval.outPath)
    mkdir(m.millSourcePath)
    cp(resourcePath, m.millSourcePath / 'tut)
    t(eval)
  }

  def tests: Tests = Tests {
    'tut - {
      'createOutputFile - workspaceTest(TutTest) { eval =>
        val expectedPath =
          eval.outPath / 'tutTargetDirectory / 'dest / "TutExample.md"

        val expected =
          """
          |```scala
          |scala> 1 + 1
          |res0: Int = 2
          |```
          |
          """.trim.stripMargin

        val Right((result, evalCount)) = eval.apply(TutTest.tut)

        assert(
          exists(expectedPath) &&
            read! expectedPath == expected
        )
      }

      'supportCustomSettings - workspaceTest(TutCustomTest) { eval =>
        val defaultPath =
          eval.outPath / 'tutTargetDirectory / 'dest / "TutExample.md"
        val expectedPath =
          TutCustomTest.millSourcePath / "TutExample.md"

        val expected =
          """
          |```scala
          |scala> 1 + 1
          |res0: Int = 2
          |```
          |
          """.trim.stripMargin

        val Right((result, evalCount)) = eval.apply(TutCustomTest.tut)

        assert(
          !exists(defaultPath) &&
            exists(expectedPath) &&
            read! expectedPath == expected
        )
      }

      'supportUsingLibraries - workspaceTest(TutLibrariesTest, resourcePath = resourcePathWithLibraries) { eval =>
        val expectedPath =
          eval.outPath / 'tutTargetDirectory / 'dest / "TutWithLibraries.md"

        val expected =
          """
            |```scala
            |import cats._
            |import cats.arrow.FunctionK
            |import cats.implicits._
            |```
            |
            |```scala
            |scala> List(1, 2, 3).combineAll
            |res0: Int = 6
            |
            |scala> λ[FunctionK[List, Option]](_.headOption)(List(1, 2 ,3))
            |res1: Option[Int] = Some(1)
            |```
            |
          """.trim.stripMargin

        val Right(_) = eval.apply(TutLibrariesTest.tut)

        assert(
          exists(expectedPath) &&
            read! expectedPath == expected
        )
      }
    }
  }
}